diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 0a542aaa..a1e30a1f 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -685,18 +685,29 @@ export class PositionController extends Controller { select: ["posMasterId"], }); masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); - // let keywordAsInt: any; - // keywordAsInt = body.keyword == null ? null : parseInt(body.keyword, 10); - // if (isNaN(keywordAsInt)) { - // keywordAsInt = ""; - // } + let keywordAsInt: any; + keywordAsInt = body.keyword == null ? null : parseInt(body.keyword, 10); + if (isNaN(keywordAsInt)) { + keywordAsInt = ""; + } + masterId = [...new Set(masterId)]; const posMaster = await this.posMasterRepository.find({ - where: { - ...typeCondition, - ...checkChildConditions, - // posMasterNo: Like(`%${keywordAsInt}%`), - id: In(masterId), - }, + where: [ + { + ...checkChildConditions, + ...typeCondition, + }, + { + ...checkChildConditions, + ...typeCondition, + id: In(masterId), + }, + { + ...checkChildConditions, + ...typeCondition, + posMasterNo: Like(`%${keywordAsInt}%`), + }, + ], order: { posMasterOrder: "ASC" }, relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"], skip: (body.page - 1) * body.pageSize, @@ -715,6 +726,11 @@ export class PositionController extends Controller { return { id: posMaster.id, + orgRootId: posMaster.orgRootId, + orgChild1Id: posMaster.orgChild1Id, + orgChild2Id: posMaster.orgChild2Id, + orgChild3Id: posMaster.orgChild3Id, + orgChild4Id: posMaster.orgChild4Id, posMasterNoPrefix: posMaster.posMasterNoPrefix, posMasterNo: posMaster.posMasterNo, posMasterNoSuffix: posMaster.posMasterNoSuffix, @@ -1052,11 +1068,8 @@ export class PositionController extends Controller { * */ @Post("summary") - async PositionSummary( - @Body() requestBody: { id: string; type: number }, - ) { + async PositionSummary(@Body() requestBody: { id: string; type: number }) { try { - let summary: any; let totalPosition: any; let totalPositionCurrentUse: any; @@ -1064,34 +1077,39 @@ export class PositionController extends Controller { let totalPositionNextUse: any; let totalPositionNextVacant: any; - switch(requestBody.type){ + switch (requestBody.type) { case 0: { - const NodeId = await this.posMasterRepository.findOne({where: {orgRootId: requestBody.id}}); - if(!NodeId){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id); - } - totalPosition = await this.posMasterRepository.count({ + const NodeId = await this.posMasterRepository.findOne({ where: { orgRootId: requestBody.id }, }); - totalPositionCurrentUse = await this.posMasterRepository.count({ + if (!NodeId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, + ); + } + totalPosition = await this.posMasterRepository.count({ + where: { orgRootId: requestBody.id }, + }); + totalPositionCurrentUse = await this.posMasterRepository.count({ where: { orgRootId: requestBody.id, profileIdCurrentHolder: Not(IsNull()) && Not(""), }, }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ + totalPositionCurrentVacant = await this.posMasterRepository.count({ where: { orgRootId: requestBody.id, profileIdCurrentHolder: IsNull() && "", }, }); - totalPositionNextUse = await this.posMasterRepository.count({ + totalPositionNextUse = await this.posMasterRepository.count({ where: { orgRootId: requestBody.id, profileIdNextHolder: Not(IsNull()) && Not(""), }, }); - totalPositionNextVacant = await this.posMasterRepository.count({ + totalPositionNextVacant = await this.posMasterRepository.count({ where: { orgRootId: requestBody.id, profileIdNextHolder: IsNull() && "", @@ -1100,32 +1118,37 @@ export class PositionController extends Controller { break; } case 1: { - const NodeId = await this.posMasterRepository.findOne({where: {orgChild1Id: requestBody.id}}); - if(!NodeId){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id); + const NodeId = await this.posMasterRepository.findOne({ + where: { orgChild1Id: requestBody.id }, + }); + if (!NodeId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, + ); } totalPosition = await this.posMasterRepository.count({ where: { orgChild1Id: requestBody.id }, }); - totalPositionCurrentUse = await this.posMasterRepository.count({ + totalPositionCurrentUse = await this.posMasterRepository.count({ where: { orgChild1Id: requestBody.id, profileIdCurrentHolder: Not(IsNull()) && Not(""), }, }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ + totalPositionCurrentVacant = await this.posMasterRepository.count({ where: { orgChild1Id: requestBody.id, profileIdCurrentHolder: IsNull() && "", }, }); - totalPositionNextUse = await this.posMasterRepository.count({ + totalPositionNextUse = await this.posMasterRepository.count({ where: { orgChild1Id: requestBody.id, profileIdNextHolder: Not(IsNull()) && Not(""), }, }); - totalPositionNextVacant = await this.posMasterRepository.count({ + totalPositionNextVacant = await this.posMasterRepository.count({ where: { orgChild1Id: requestBody.id, profileIdNextHolder: IsNull() && "", @@ -1134,32 +1157,37 @@ export class PositionController extends Controller { break; } case 2: { - const NodeId = await this.posMasterRepository.findOne({where: {orgChild2Id: requestBody.id}}); - if(!NodeId){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id); + const NodeId = await this.posMasterRepository.findOne({ + where: { orgChild2Id: requestBody.id }, + }); + if (!NodeId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, + ); } totalPosition = await this.posMasterRepository.count({ where: { orgChild2Id: requestBody.id }, }); - totalPositionCurrentUse = await this.posMasterRepository.count({ + totalPositionCurrentUse = await this.posMasterRepository.count({ where: { orgChild2Id: requestBody.id, profileIdCurrentHolder: Not(IsNull()) && Not(""), }, }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ + totalPositionCurrentVacant = await this.posMasterRepository.count({ where: { orgChild2Id: requestBody.id, profileIdCurrentHolder: IsNull() && "", }, }); - totalPositionNextUse = await this.posMasterRepository.count({ + totalPositionNextUse = await this.posMasterRepository.count({ where: { orgChild2Id: requestBody.id, profileIdNextHolder: Not(IsNull()) && Not(""), }, }); - totalPositionNextVacant = await this.posMasterRepository.count({ + totalPositionNextVacant = await this.posMasterRepository.count({ where: { orgChild2Id: requestBody.id, profileIdNextHolder: IsNull() && "", @@ -1168,32 +1196,37 @@ export class PositionController extends Controller { break; } case 3: { - const NodeId = await this.posMasterRepository.findOne({where: {orgChild3Id: requestBody.id}}); - if(!NodeId){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id); + const NodeId = await this.posMasterRepository.findOne({ + where: { orgChild3Id: requestBody.id }, + }); + if (!NodeId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, + ); } totalPosition = await this.posMasterRepository.count({ where: { orgChild3Id: requestBody.id }, }); - totalPositionCurrentUse = await this.posMasterRepository.count({ + totalPositionCurrentUse = await this.posMasterRepository.count({ where: { orgChild3Id: requestBody.id, profileIdCurrentHolder: Not(IsNull()) && Not(""), }, }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ + totalPositionCurrentVacant = await this.posMasterRepository.count({ where: { orgChild3Id: requestBody.id, profileIdCurrentHolder: IsNull() && "", }, }); - totalPositionNextUse = await this.posMasterRepository.count({ + totalPositionNextUse = await this.posMasterRepository.count({ where: { orgChild3Id: requestBody.id, profileIdNextHolder: Not(IsNull()) && Not(""), }, }); - totalPositionNextVacant = await this.posMasterRepository.count({ + totalPositionNextVacant = await this.posMasterRepository.count({ where: { orgChild3Id: requestBody.id, profileIdNextHolder: IsNull() && "", @@ -1202,32 +1235,37 @@ export class PositionController extends Controller { break; } case 4: { - const NodeId = await this.posMasterRepository.findOne({where: {orgChild4Id: requestBody.id}}); - if(!NodeId){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id); + const NodeId = await this.posMasterRepository.findOne({ + where: { orgChild4Id: requestBody.id }, + }); + if (!NodeId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, + ); } totalPosition = await this.posMasterRepository.count({ where: { orgChild4Id: requestBody.id }, }); - totalPositionCurrentUse = await this.posMasterRepository.count({ + totalPositionCurrentUse = await this.posMasterRepository.count({ where: { orgChild4Id: requestBody.id, profileIdCurrentHolder: Not(IsNull()) && Not(""), }, }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ + totalPositionCurrentVacant = await this.posMasterRepository.count({ where: { orgChild4Id: requestBody.id, profileIdCurrentHolder: IsNull() && "", }, }); - totalPositionNextUse = await this.posMasterRepository.count({ + totalPositionNextUse = await this.posMasterRepository.count({ where: { orgChild4Id: requestBody.id, profileIdNextHolder: Not(IsNull()) && Not(""), }, }); - totalPositionNextVacant = await this.posMasterRepository.count({ + totalPositionNextVacant = await this.posMasterRepository.count({ where: { orgChild4Id: requestBody.id, profileIdNextHolder: IsNull() && "", @@ -1239,15 +1277,14 @@ export class PositionController extends Controller { break; } - summary = { + summary = { totalPosition: totalPosition, totalPositionCurrentUse: totalPositionCurrentUse, totalPositionCurrentVacant: totalPositionCurrentVacant, totalPositionNextUse: totalPositionNextUse, - totalPositionNextVacant: totalPositionNextVacant - } + totalPositionNextVacant: totalPositionNextVacant, + }; return new HttpSuccess(summary); - } catch (error) { return error; } diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index cb7030e9..2a133eba 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -35,8 +35,8 @@ import { OrgChild4 } from "../entities/OrgChild4"; import { Position } from "../entities/Position"; import { Brackets } from "typeorm/browser"; @Route("api/v1/org/report") -@Tags("Position") -@Security("bearerAuth") +// @Tags("Position") +// @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", @@ -66,28 +66,328 @@ export class ReportController extends Controller { @Get("position") async findPosition() { try { - // const getPosType = await this.posTypeRepository.findOne({ - // select: ["id", "posTypeName", "posTypeRank"], - // relations: ["posLevels"], - // where: { id: id }, - // }); - // if (!getPosType) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); - // } + const orgRevision = await this.orgRevisionRepository.findOne({ + where: { orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, + relations: ["orgRoots"], + }); + if (!orgRevision) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } - // const mapGetPosType = { - // id: getPosType.id, - // posTypeName: getPosType.posTypeName, - // posTypeRank: getPosType.posTypeRank, - // posLevels: getPosType.posLevels.map((posLevel) => ({ - // id: posLevel.id, - // posLevelName: posLevel.posLevelName, - // posLevelRank: posLevel.posLevelRank, - // posLevelAuthority: posLevel.posLevelAuthority, - // })), - // }; + const orgRootData = await AppDataSource.getRepository(OrgRoot) + .createQueryBuilder("orgRoot") + .where("orgRoot.orgRevisionId IN (:...ids)", { + ids: [orgRevision.id], + }) + .select([ + "orgRoot.id", + "orgRoot.orgRootName", + "orgRoot.orgRootShortName", + "orgRoot.orgRootOrder", + ]) + .orderBy("orgRoot.orgRootOrder", "ASC") + .getMany(); - return new HttpSuccess(); + const orgRootIds = orgRootData.map((orgRoot) => orgRoot.id) || null; + const orgChild1Data = + orgRootIds && orgRootIds.length > 0 + ? await AppDataSource.getRepository(OrgChild1) + .createQueryBuilder("orgChild1") + .where("orgChild1.orgRootId IN (:...ids)", { ids: orgRootIds }) + .select([ + "orgChild1.id", + "orgChild1.orgChild1Name", + "orgChild1.orgChild1ShortName", + "orgChild1.orgRootId", + "orgChild1.orgChild1Order", + ]) + .orderBy("orgChild1.orgChild1Order", "ASC") + .getMany() + : []; + + const orgChild1Ids = orgChild1Data.map((orgChild1) => orgChild1.id) || null; + const orgChild2Data = + orgChild1Ids && orgChild1Ids.length > 0 + ? await AppDataSource.getRepository(OrgChild2) + .createQueryBuilder("orgChild2") + .where("orgChild2.orgChild1Id IN (:...ids)", { ids: orgChild1Ids }) + .select([ + "orgChild2.id", + "orgChild2.orgChild2Name", + "orgChild2.orgChild2ShortName", + "orgChild2.orgChild1Id", + "orgChild2.orgChild2Order", + ]) + .orderBy("orgChild2.orgChild2Order", "ASC") + .getMany() + : []; + + const orgChild2Ids = orgChild2Data.map((orgChild2) => orgChild2.id) || null; + const orgChild3Data = + orgChild2Ids && orgChild2Ids.length > 0 + ? await AppDataSource.getRepository(OrgChild3) + .createQueryBuilder("orgChild3") + .where("orgChild3.orgChild2Id IN (:...ids)", { ids: orgChild2Ids }) + .select([ + "orgChild3.id", + "orgChild3.orgChild3Name", + "orgChild3.orgChild3ShortName", + "orgChild3.orgChild2Id", + "orgChild3.orgChild3Order", + ]) + .orderBy("orgChild3.orgChild3Order", "ASC") + .getMany() + : []; + + const orgChild3Ids = orgChild3Data.map((orgChild3) => orgChild3.id) || null; + const orgChild4Data = + orgChild3Ids && orgChild3Ids.length > 0 + ? await AppDataSource.getRepository(OrgChild4) + .createQueryBuilder("orgChild4") + .where("orgChild4.orgChild3Id IN (:...ids)", { ids: orgChild3Ids }) + .select([ + "orgChild4.id", + "orgChild4.orgChild4Name", + "orgChild4.orgChild4ShortName", + "orgChild4.orgChild3Id", + "orgChild4.orgChild4Order", + ]) + .orderBy("orgChild4.orgChild4Order", "ASC") + .getMany() + : []; + const posMasters = await AppDataSource.getRepository(PosMaster) + .createQueryBuilder("posMaster") + .where("posMaster.orgRevisionId IN (:...ids)", { + ids: [orgRevision.id], + }) + .select([ + "posMaster.id", + "posMaster.posMasterNoPrefix", + "posMaster.posMasterNo", + "posMaster.posMasterNoSuffix", + "posMaster.orgRootId", + "posMaster.orgChild1Id", + "posMaster.orgChild2Id", + "posMaster.orgChild3Id", + "posMaster.orgChild4Id", + ]) + .orderBy("posMaster.posMasterOrder", "ASC") + .getMany(); + const positions = await this.positionRepository.find({ + where: { + posMasterId: In(posMasters.map((posMaster: any) => posMaster.id)), + }, + relations: ["posLevel", "posType", "posExecutive"], + }); + + let data = new Array(); + await Promise.all( + orgRootData.map(async (orgRoot) => { + await Promise.all( + posMasters + .filter((posMaster) => posMaster.orgRootId === orgRoot.id) + .map(async (posMaster) => { + await Promise.all( + positions + .filter((position) => position.posMasterId === posMaster.id) + .map(async (position) => { + const node = { + orgRootName: orgRoot.orgRootName, + orgTreeName: orgRoot.orgRootName, + orgTreeShortName: orgRoot.orgRootShortName, + posMasterNoPrefix: posMaster.posMasterNoPrefix, + posMasterNo: posMaster.posMasterNo, + posMasterNoSuffix: posMaster.posMasterNoSuffix, + positionName: position.positionName, + posType: position.posType == null ? null : position.posType.posTypeName, + posLevel: position.posLevel == null ? null : position.posLevel.posLevelName, + posExecutive: + position.posExecutive == null + ? null + : position.posExecutive.posExecutiveName, + }; + data.push(node); + }), + ); + }), + ), + await Promise.all( + orgChild1Data + .filter((orgChild1) => orgChild1.orgRootId === orgRoot.id) + .map(async (orgChild1) => { + const node = { + positions: await Promise.all( + posMasters + .filter((posMaster) => posMaster.orgChild1Id === orgChild1.id) + .map(async (posMaster) => { + await Promise.all( + positions + .filter((position) => position.posMasterId === posMaster.id) + .map(async (position) => { + const node = { + orgRootName: orgRoot.orgRootName, + orgTreeName: orgChild1.orgChild1Name, + orgTreeShortName: orgChild1.orgChild1ShortName, + posMasterNoPrefix: posMaster.posMasterNoPrefix, + posMasterNo: posMaster.posMasterNo, + posMasterNoSuffix: posMaster.posMasterNoSuffix, + positionName: position.positionName, + posType: + position.posType == null ? null : position.posType.posTypeName, + posLevel: + position.posLevel == null + ? null + : position.posLevel.posLevelName, + posExecutive: + position.posExecutive == null + ? null + : position.posExecutive.posExecutiveName, + }; + data.push(node); + }), + ); + }), + ), + }; + await Promise.all( + orgChild2Data + .filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id) + .map(async (orgChild2) => { + const node = { + positions: await Promise.all( + posMasters + .filter((posMaster) => posMaster.orgChild2Id === orgChild2.id) + .map(async (posMaster) => { + await Promise.all( + positions + .filter((position) => position.posMasterId === posMaster.id) + .map(async (position) => { + const node = { + orgRootName: orgRoot.orgRootName, + orgTreeName: orgChild2.orgChild2Name, + orgTreeShortName: orgChild2.orgChild2ShortName, + posMasterNoPrefix: posMaster.posMasterNoPrefix, + posMasterNo: posMaster.posMasterNo, + posMasterNoSuffix: posMaster.posMasterNoSuffix, + positionName: position.positionName, + posType: + position.posType == null + ? null + : position.posType.posTypeName, + posLevel: + position.posLevel == null + ? null + : position.posLevel.posLevelName, + posExecutive: + position.posExecutive == null + ? null + : position.posExecutive.posExecutiveName, + }; + data.push(node); + }), + ); + }), + ), + }; + await Promise.all( + orgChild3Data + .filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id) + .map(async (orgChild3) => { + const node = { + positions: await Promise.all( + posMasters + .filter((posMaster) => posMaster.orgChild3Id === orgChild3.id) + .map(async (posMaster) => { + await Promise.all( + positions + .filter( + (position) => position.posMasterId === posMaster.id, + ) + .map(async (position) => { + const node = { + orgRootName: orgRoot.orgRootName, + orgTreeName: orgChild3.orgChild3Name, + orgTreeShortName: orgChild3.orgChild3ShortName, + posMasterNoPrefix: posMaster.posMasterNoPrefix, + posMasterNo: posMaster.posMasterNo, + posMasterNoSuffix: posMaster.posMasterNoSuffix, + positionName: position.positionName, + posType: + position.posType == null + ? null + : position.posType.posTypeName, + posLevel: + position.posLevel == null + ? null + : position.posLevel.posLevelName, + posExecutive: + position.posExecutive == null + ? null + : position.posExecutive.posExecutiveName, + }; + data.push(node); + }), + ); + }), + ), + }; + await Promise.all( + orgChild4Data + .filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id) + .map(async (orgChild4) => { + const node = { + positions: await Promise.all( + posMasters + .filter( + (posMaster) => posMaster.orgChild4Id === orgChild4.id, + ) + .map(async (posMaster) => { + await Promise.all( + positions + .filter( + (position) => + position.posMasterId === posMaster.id, + ) + .map(async (position) => { + const node = { + orgRootName: orgRoot.orgRootName, + orgTreeName: orgChild4.orgChild4Name, + orgTreeShortName: orgChild4.orgChild4ShortName, + posMasterNoPrefix: posMaster.posMasterNoPrefix, + posMasterNo: posMaster.posMasterNo, + posMasterNoSuffix: posMaster.posMasterNoSuffix, + positionName: position.positionName, + posType: + position.posType == null + ? null + : position.posType.posTypeName, + posLevel: + position.posLevel == null + ? null + : position.posLevel.posLevelName, + posExecutive: + position.posExecutive == null + ? null + : position.posExecutive.posExecutiveName, + }; + data.push(node); + }), + ); + }), + ), + }; + }), + ); + }), + ); + }), + ); + }), + ); + }), + ); + + return new HttpSuccess(data); } catch (error) { return error; }