From e9ed8ff3f445a84231ff505b80a6ba0d80659242 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Wed, 28 Feb 2024 11:31:01 +0700 Subject: [PATCH] remove try catch --- src/controllers/OrgChild1Controller.ts | 114 +- src/controllers/OrgChild2Controller.ts | 116 +- src/controllers/OrgChild3Controller.ts | 118 +- src/controllers/OrgChild4Controller.ts | 120 +- src/controllers/OrgRootController.ts | 101 +- src/controllers/PosExecutiveController.ts | 98 +- src/controllers/PosLevelController.ts | 236 +- src/controllers/PositionController.ts | 2416 ++++++++++----------- 8 files changed, 1575 insertions(+), 1744 deletions(-) diff --git a/src/controllers/OrgChild1Controller.ts b/src/controllers/OrgChild1Controller.ts index a453bbaa..598f6dfc 100644 --- a/src/controllers/OrgChild1Controller.ts +++ b/src/controllers/OrgChild1Controller.ts @@ -60,26 +60,22 @@ export class OrgChild1Controller { if (!orgChild1) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 1"); } - try { - const getOrgChild1 = { - orgChild1Id: orgChild1.id, - orgRootName: orgChild1.orgRoot.orgRootName, - orgName: `${orgChild1.orgChild1Name}/${orgChild1.orgRoot.orgRootName}`, - orgChild1Name: orgChild1.orgChild1Name, - orgChild1ShortName: orgChild1.orgChild1ShortName, - orgChild1Code: orgChild1.orgChild1Code, - orgChild1Rank: orgChild1.orgChild1Rank, - orgChild1Order: orgChild1.orgChild1Order, - orgChild1PhoneEx: orgChild1.orgChild1PhoneEx, - orgChild1PhoneIn: orgChild1.orgChild1PhoneIn, - orgChild1Fax: orgChild1.orgChild1Fax, - orgRevisionId: orgChild1.orgRevisionId, - orgCode: orgChild1.orgRoot.orgRootCode + orgChild1.orgChild1Code, - }; - return new HttpSuccess(getOrgChild1); - } catch (error) { - return error; - } + const getOrgChild1 = { + orgChild1Id: orgChild1.id, + orgRootName: orgChild1.orgRoot.orgRootName, + orgName: `${orgChild1.orgChild1Name}/${orgChild1.orgRoot.orgRootName}`, + orgChild1Name: orgChild1.orgChild1Name, + orgChild1ShortName: orgChild1.orgChild1ShortName, + orgChild1Code: orgChild1.orgChild1Code, + orgChild1Rank: orgChild1.orgChild1Rank, + orgChild1Order: orgChild1.orgChild1Order, + orgChild1PhoneEx: orgChild1.orgChild1PhoneEx, + orgChild1PhoneIn: orgChild1.orgChild1PhoneIn, + orgChild1Fax: orgChild1.orgChild1Fax, + orgRevisionId: orgChild1.orgRevisionId, + orgCode: orgChild1.orgRoot.orgRootCode + orgChild1.orgChild1Code, + }; + return new HttpSuccess(getOrgChild1); } /** @@ -136,22 +132,18 @@ export class OrgChild1Controller { }, order: { orgChild1Order: "DESC" }, }); - try { - const child1 = Object.assign(new OrgChild1(), requestBody) as OrgChild1; - child1.orgChild1Name = requestBody.orgChild1Name; - child1.createdUserId = request.user.sub; - child1.createdFullName = request.user.name; - child1.lastUpdateUserId = request.user.sub; - child1.lastUpdateFullName = request.user.name; - child1.orgRevisionId = String(rootIdExits?.orgRevisionId); - child1.orgRootId = String(rootIdExits?.id); - child1.orgChild1Order = - order == null || order.orgChild1Order == null ? 1 : order.orgChild1Order + 1; - await this.child1Repository.save(child1); - return new HttpSuccess(); - } catch (error) { - return error; - } + const child1 = Object.assign(new OrgChild1(), requestBody) as OrgChild1; + child1.orgChild1Name = requestBody.orgChild1Name; + child1.createdUserId = request.user.sub; + child1.createdFullName = request.user.name; + child1.lastUpdateUserId = request.user.sub; + child1.lastUpdateFullName = request.user.name; + child1.orgRevisionId = String(rootIdExits?.orgRevisionId); + child1.orgRootId = String(rootIdExits?.id); + child1.orgChild1Order = + order == null || order.orgChild1Order == null ? 1 : order.orgChild1Order + 1; + await this.child1Repository.save(child1); + return new HttpSuccess(); } /** @@ -212,18 +204,14 @@ export class OrgChild1Controller { if (chkCode != null) { throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); } - try { - child1.lastUpdateUserId = request.user.sub; - child1.lastUpdateFullName = request.user.name; - child1.lastUpdatedAt = new Date(); - child1.orgRevisionId = String(rootIdExits?.orgRevisionId); - child1.orgRootId = String(rootIdExits?.id); - this.child1Repository.merge(child1, requestBody); - await this.child1Repository.save(child1); - return new HttpSuccess(); - } catch (error) { - return error; - } + child1.lastUpdateUserId = request.user.sub; + child1.lastUpdateFullName = request.user.name; + child1.lastUpdatedAt = new Date(); + child1.orgRevisionId = String(rootIdExits?.orgRevisionId); + child1.orgRootId = String(rootIdExits?.id); + this.child1Repository.merge(child1, requestBody); + await this.child1Repository.save(child1); + return new HttpSuccess(); } /** @@ -261,23 +249,19 @@ export class OrgChild1Controller { // "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ2", // ); // } - try { - const posMasters = await this.posMasterRepository.find({ - where: { orgChild1Id: id }, - }); - const positions = await this.positionRepository.find({ - where: [{ posMasterId: In(posMasters.map((x) => x.id)) }], - }); + const posMasters = await this.posMasterRepository.find({ + where: { orgChild1Id: id }, + }); + const positions = await this.positionRepository.find({ + where: [{ posMasterId: In(posMasters.map((x) => x.id)) }], + }); - await this.positionRepository.remove(positions); - await this.posMasterRepository.remove(posMasters); - await this.child4Repository.delete({ orgChild1Id: id }); - await this.child3Repository.delete({ orgChild1Id: id }); - await this.child2Repository.delete({ orgChild1Id: id }); - await this.child1Repository.delete({ id }); - return new HttpSuccess(); - } catch (error) { - return error; - } + await this.positionRepository.remove(positions); + await this.posMasterRepository.remove(posMasters); + await this.child4Repository.delete({ orgChild1Id: id }); + await this.child3Repository.delete({ orgChild1Id: id }); + await this.child2Repository.delete({ orgChild1Id: id }); + await this.child1Repository.delete({ id }); + return new HttpSuccess(); } } diff --git a/src/controllers/OrgChild2Controller.ts b/src/controllers/OrgChild2Controller.ts index 3c75a933..8dc58922 100644 --- a/src/controllers/OrgChild2Controller.ts +++ b/src/controllers/OrgChild2Controller.ts @@ -62,26 +62,22 @@ export class OrgChild2Controller extends Controller { if (!orgChild2) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 2"); } - try { - const getOrgChild2 = { - orgChild2Id: orgChild2.id, - orgRootName: orgChild2.orgRoot.orgRootName, - orgName: `${orgChild2.orgChild2Name}/${orgChild2.orgChild1.orgChild1Name}/${orgChild2.orgRoot.orgRootName}`, - orgChild2Name: orgChild2.orgChild2Name, - orgChild2ShortName: orgChild2.orgChild2ShortName, - orgChild2Code: orgChild2.orgChild2Code, - orgChild2Rank: orgChild2.orgChild2Rank, - orgChild2Order: orgChild2.orgChild2Order, - orgChild2PhoneEx: orgChild2.orgChild2PhoneEx, - orgChild2PhoneIn: orgChild2.orgChild2PhoneIn, - orgChild2Fax: orgChild2.orgChild2Fax, - orgRevisionId: orgChild2.orgRevisionId, - orgCode: orgChild2.orgRoot.orgRootCode + orgChild2.orgChild2Code, - }; - return new HttpSuccess(getOrgChild2); - } catch (error) { - return error; - } + const getOrgChild2 = { + orgChild2Id: orgChild2.id, + orgRootName: orgChild2.orgRoot.orgRootName, + orgName: `${orgChild2.orgChild2Name}/${orgChild2.orgChild1.orgChild1Name}/${orgChild2.orgRoot.orgRootName}`, + orgChild2Name: orgChild2.orgChild2Name, + orgChild2ShortName: orgChild2.orgChild2ShortName, + orgChild2Code: orgChild2.orgChild2Code, + orgChild2Rank: orgChild2.orgChild2Rank, + orgChild2Order: orgChild2.orgChild2Order, + orgChild2PhoneEx: orgChild2.orgChild2PhoneEx, + orgChild2PhoneIn: orgChild2.orgChild2PhoneIn, + orgChild2Fax: orgChild2.orgChild2Fax, + orgRevisionId: orgChild2.orgRevisionId, + orgCode: orgChild2.orgRoot.orgRootCode + orgChild2.orgChild2Code, + }; + return new HttpSuccess(getOrgChild2); } /** @@ -142,23 +138,19 @@ export class OrgChild2Controller extends Controller { }, order: { orgChild2Order: "DESC" }, }); - try { - const child2 = Object.assign(new OrgChild2(), requestBody) as OrgChild2; - child2.orgChild2Name = requestBody.orgChild2Name; - child2.createdUserId = request.user.sub; - child2.createdFullName = request.user.name; - child2.lastUpdateUserId = request.user.sub; - child2.lastUpdateFullName = request.user.name; - child2.orgRootId = String(child1?.orgRootId); - child2.orgRevisionId = String(child1?.orgRevisionId); - child2.orgChild1Id = String(child1?.id); - child2.orgChild2Order = - order == null || order.orgChild2Order == null ? 1 : order.orgChild2Order + 1; - await this.child2Repository.save(child2); - return new HttpSuccess(); - } catch (error) { - return error; - } + const child2 = Object.assign(new OrgChild2(), requestBody) as OrgChild2; + child2.orgChild2Name = requestBody.orgChild2Name; + child2.createdUserId = request.user.sub; + child2.createdFullName = request.user.name; + child2.lastUpdateUserId = request.user.sub; + child2.lastUpdateFullName = request.user.name; + child2.orgRootId = String(child1?.orgRootId); + child2.orgRevisionId = String(child1?.orgRevisionId); + child2.orgChild1Id = String(child1?.id); + child2.orgChild2Order = + order == null || order.orgChild2Order == null ? 1 : order.orgChild2Order + 1; + await this.child2Repository.save(child2); + return new HttpSuccess(); } /** @@ -223,19 +215,15 @@ export class OrgChild2Controller extends Controller { if (!child2) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } - try { - child2.lastUpdateUserId = request.user.sub; - child2.lastUpdateFullName = request.user.name; - child2.lastUpdatedAt = new Date(); - child2.orgRootId = String(child1IdExits?.orgRootId); - child2.orgRevisionId = String(child1IdExits?.orgRevisionId); - child2.orgChild1Id = String(child1IdExits?.id); - this.child2Repository.merge(child2, requestBody); - await this.child2Repository.save(child2); - return new HttpSuccess(); - } catch (error) { - return error; - } + child2.lastUpdateUserId = request.user.sub; + child2.lastUpdateFullName = request.user.name; + child2.lastUpdatedAt = new Date(); + child2.orgRootId = String(child1IdExits?.orgRootId); + child2.orgRevisionId = String(child1IdExits?.orgRevisionId); + child2.orgChild1Id = String(child1IdExits?.id); + this.child2Repository.merge(child2, requestBody); + await this.child2Repository.save(child2); + return new HttpSuccess(); } /** @@ -275,22 +263,18 @@ export class OrgChild2Controller extends Controller { // "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4", // ); // } - try { - const posMasters = await this.posMasterRepository.find({ - where: { orgChild2Id: id }, - }); - const positions = await this.positionRepository.find({ - where: [{ posMasterId: In(posMasters.map((x) => x.id)) }], - }); + const posMasters = await this.posMasterRepository.find({ + where: { orgChild2Id: id }, + }); + const positions = await this.positionRepository.find({ + where: [{ posMasterId: In(posMasters.map((x) => x.id)) }], + }); - await this.positionRepository.remove(positions); - await this.posMasterRepository.remove(posMasters); - await this.child4Repository.delete({ orgChild2Id: id }); - await this.child3Repository.delete({ orgChild2Id: id }); - await this.child2Repository.delete({ id }); - return new HttpSuccess(); - } catch (error) { - return error; - } + await this.positionRepository.remove(positions); + await this.posMasterRepository.remove(posMasters); + await this.child4Repository.delete({ orgChild2Id: id }); + await this.child3Repository.delete({ orgChild2Id: id }); + await this.child2Repository.delete({ id }); + return new HttpSuccess(); } } diff --git a/src/controllers/OrgChild3Controller.ts b/src/controllers/OrgChild3Controller.ts index b656f646..87937fab 100644 --- a/src/controllers/OrgChild3Controller.ts +++ b/src/controllers/OrgChild3Controller.ts @@ -58,26 +58,22 @@ export class OrgChild3Controller { if (!orgChild3) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 3"); } - try { - const getOrgChild3 = { - orgChild3Id: orgChild3.id, - orgRootName: orgChild3.orgRoot.orgRootName, - orgName: `${orgChild3.orgChild3Name}/${orgChild3.orgChild2.orgChild2Name}/${orgChild3.orgChild1.orgChild1Name}/${orgChild3.orgRoot.orgRootName}`, - orgChild3Name: orgChild3.orgChild3Name, - orgChild3ShortName: orgChild3.orgChild3ShortName, - orgChild3Code: orgChild3.orgChild3Code, - orgChild3Rank: orgChild3.orgChild3Rank, - orgChild3Order: orgChild3.orgChild3Order, - orgChild3PhoneEx: orgChild3.orgChild3PhoneEx, - orgChild3PhoneIn: orgChild3.orgChild3PhoneIn, - orgChild3Fax: orgChild3.orgChild3Fax, - orgRevisionId: orgChild3.orgRevisionId, - orgCode: orgChild3.orgRoot.orgRootCode + orgChild3.orgChild3Code, - }; - return new HttpSuccess(getOrgChild3); - } catch (error) { - return error; - } + const getOrgChild3 = { + orgChild3Id: orgChild3.id, + orgRootName: orgChild3.orgRoot.orgRootName, + orgName: `${orgChild3.orgChild3Name}/${orgChild3.orgChild2.orgChild2Name}/${orgChild3.orgChild1.orgChild1Name}/${orgChild3.orgRoot.orgRootName}`, + orgChild3Name: orgChild3.orgChild3Name, + orgChild3ShortName: orgChild3.orgChild3ShortName, + orgChild3Code: orgChild3.orgChild3Code, + orgChild3Rank: orgChild3.orgChild3Rank, + orgChild3Order: orgChild3.orgChild3Order, + orgChild3PhoneEx: orgChild3.orgChild3PhoneEx, + orgChild3PhoneIn: orgChild3.orgChild3PhoneIn, + orgChild3Fax: orgChild3.orgChild3Fax, + orgRevisionId: orgChild3.orgRevisionId, + orgCode: orgChild3.orgRoot.orgRootCode + orgChild3.orgChild3Code, + }; + return new HttpSuccess(getOrgChild3); } /** @@ -124,24 +120,20 @@ export class OrgChild3Controller { }, order: { orgChild3Order: "DESC" }, }); - try { - const child3 = Object.assign(new OrgChild3(), requestBody) as OrgChild3; - child3.orgChild3Name = requestBody.orgChild3Name; - child3.createdUserId = request.user.sub; - child3.createdFullName = request.user.name; - child3.lastUpdateUserId = request.user.sub; - child3.lastUpdateFullName = request.user.name; - child3.orgRootId = String(child2?.orgRootId); - child3.orgChild1Id = String(child2?.orgChild1Id); - child3.orgRevisionId = String(child2?.orgRevisionId); - child3.orgChild2Id = String(child2?.id); - child3.orgChild3Order = - order == null || order.orgChild3Order == null ? 1 : order.orgChild3Order + 1; - await this.child3Repository.save(child3); - return new HttpSuccess(); - } catch (error) { - return error; - } + const child3 = Object.assign(new OrgChild3(), requestBody) as OrgChild3; + child3.orgChild3Name = requestBody.orgChild3Name; + child3.createdUserId = request.user.sub; + child3.createdFullName = request.user.name; + child3.lastUpdateUserId = request.user.sub; + child3.lastUpdateFullName = request.user.name; + child3.orgRootId = String(child2?.orgRootId); + child3.orgChild1Id = String(child2?.orgChild1Id); + child3.orgRevisionId = String(child2?.orgRevisionId); + child3.orgChild2Id = String(child2?.id); + child3.orgChild3Order = + order == null || order.orgChild3Order == null ? 1 : order.orgChild3Order + 1; + await this.child3Repository.save(child3); + return new HttpSuccess(); } /** @@ -192,20 +184,16 @@ export class OrgChild3Controller { if (!child3) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } - try { - child3.lastUpdateUserId = request.user.sub; - child3.lastUpdateFullName = request.user.name; - child3.lastUpdatedAt = new Date(); - child3.orgRootId = String(child2IdExits?.orgRootId); - child3.orgChild1Id = String(child2IdExits?.orgChild1Id); - child3.orgRevisionId = String(child2IdExits?.orgRevisionId); - child3.orgChild2Id = String(child2IdExits?.id); - this.child3Repository.merge(child3, requestBody); - await this.child3Repository.save(child3); - return new HttpSuccess(); - } catch (error) { - return error; - } + child3.lastUpdateUserId = request.user.sub; + child3.lastUpdateFullName = request.user.name; + child3.lastUpdatedAt = new Date(); + child3.orgRootId = String(child2IdExits?.orgRootId); + child3.orgChild1Id = String(child2IdExits?.orgChild1Id); + child3.orgRevisionId = String(child2IdExits?.orgRevisionId); + child3.orgChild2Id = String(child2IdExits?.id); + this.child3Repository.merge(child3, requestBody); + await this.child3Repository.save(child3); + return new HttpSuccess(); } /** @@ -245,21 +233,17 @@ export class OrgChild3Controller { // "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4", // ); // } - try { - const posMasters = await this.posMasterRepository.find({ - where: { orgChild3Id: id }, - }); - const positions = await this.positionRepository.find({ - where: [{ posMasterId: In(posMasters.map((x) => x.id)) }], - }); + const posMasters = await this.posMasterRepository.find({ + where: { orgChild3Id: id }, + }); + const positions = await this.positionRepository.find({ + where: [{ posMasterId: In(posMasters.map((x) => x.id)) }], + }); - await this.positionRepository.remove(positions); - await this.posMasterRepository.remove(posMasters); - await this.child4Repository.delete({ orgChild3Id: id }); - await this.child3Repository.delete(id); - return new HttpSuccess(); - } catch (error) { - return error; - } + await this.positionRepository.remove(positions); + await this.posMasterRepository.remove(posMasters); + await this.child4Repository.delete({ orgChild3Id: id }); + await this.child3Repository.delete(id); + return new HttpSuccess(); } } diff --git a/src/controllers/OrgChild4Controller.ts b/src/controllers/OrgChild4Controller.ts index ca426372..e6ac25bd 100644 --- a/src/controllers/OrgChild4Controller.ts +++ b/src/controllers/OrgChild4Controller.ts @@ -60,26 +60,22 @@ export class OrgChild4Controller extends Controller { if (!orgChild4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 4"); } - try { - const getOrgChild4 = { - orgChild4Id: orgChild4.id, - orgRootName: orgChild4.orgRoot.orgRootName, - orgName: `${orgChild4.orgChild4Name}/${orgChild4.orgChild3.orgChild3Name}/${orgChild4.orgChild2.orgChild2Name}/${orgChild4.orgChild1.orgChild1Name}/${orgChild4.orgRoot.orgRootName}`, - orgChild4Name: orgChild4.orgChild4Name, - orgChild4ShortName: orgChild4.orgChild4ShortName, - orgChild4Code: orgChild4.orgChild4Code, - orgChild4Rank: orgChild4.orgChild4Rank, - orgChild4Order: orgChild4.orgChild4Order, - orgChild4PhoneEx: orgChild4.orgChild4PhoneEx, - orgChild4PhoneIn: orgChild4.orgChild4PhoneIn, - orgChild4Fax: orgChild4.orgChild4Fax, - orgRevisionId: orgChild4.orgRevisionId, - orgCode: orgChild4.orgRoot.orgRootCode + orgChild4.orgChild4Code, - }; - return new HttpSuccess(getOrgChild4); - } catch (error) { - return error; - } + const getOrgChild4 = { + orgChild4Id: orgChild4.id, + orgRootName: orgChild4.orgRoot.orgRootName, + orgName: `${orgChild4.orgChild4Name}/${orgChild4.orgChild3.orgChild3Name}/${orgChild4.orgChild2.orgChild2Name}/${orgChild4.orgChild1.orgChild1Name}/${orgChild4.orgRoot.orgRootName}`, + orgChild4Name: orgChild4.orgChild4Name, + orgChild4ShortName: orgChild4.orgChild4ShortName, + orgChild4Code: orgChild4.orgChild4Code, + orgChild4Rank: orgChild4.orgChild4Rank, + orgChild4Order: orgChild4.orgChild4Order, + orgChild4PhoneEx: orgChild4.orgChild4PhoneEx, + orgChild4PhoneIn: orgChild4.orgChild4PhoneIn, + orgChild4Fax: orgChild4.orgChild4Fax, + orgRevisionId: orgChild4.orgRevisionId, + orgCode: orgChild4.orgRoot.orgRootCode + orgChild4.orgChild4Code, + }; + return new HttpSuccess(getOrgChild4); } /** @@ -140,26 +136,22 @@ export class OrgChild4Controller extends Controller { }, order: { orgChild4Order: "DESC" }, }); - try { - const child4 = Object.assign(new OrgChild4(), requestBody) as OrgChild4; - child4.orgChild4Name = requestBody.orgChild4Name; - child4.createdUserId = request.user.sub; - child4.createdFullName = request.user.name; - child4.lastUpdateUserId = request.user.sub; - child4.lastUpdateFullName = request.user.name; - child4.orgRootId = String(child3?.orgRootId); - child4.orgChild1Id = String(child3?.orgChild1Id); - child4.orgChild2Id = String(child3?.orgChild2Id); - child4.orgRevisionId = String(child3?.orgRevisionId); - child4.orgChild3Id = String(child3?.id); - child4.orgChild4Order = - order == null || order.orgChild4Order == null ? 1 : order.orgChild4Order + 1; - await this.child4Repository.save(child4); + const child4 = Object.assign(new OrgChild4(), requestBody) as OrgChild4; + child4.orgChild4Name = requestBody.orgChild4Name; + child4.createdUserId = request.user.sub; + child4.createdFullName = request.user.name; + child4.lastUpdateUserId = request.user.sub; + child4.lastUpdateFullName = request.user.name; + child4.orgRootId = String(child3?.orgRootId); + child4.orgChild1Id = String(child3?.orgChild1Id); + child4.orgChild2Id = String(child3?.orgChild2Id); + child4.orgRevisionId = String(child3?.orgRevisionId); + child4.orgChild3Id = String(child3?.id); + child4.orgChild4Order = + order == null || order.orgChild4Order == null ? 1 : order.orgChild4Order + 1; + await this.child4Repository.save(child4); - return new HttpSuccess(); - } catch (error) { - return error; - } + return new HttpSuccess(); } /** @@ -224,21 +216,17 @@ export class OrgChild4Controller extends Controller { if (!child4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } - try { - child4.lastUpdateUserId = request.user.sub; - child4.lastUpdateFullName = request.user.name; - child4.lastUpdatedAt = new Date(); - child4.orgRootId = String(child3IdExits?.orgRootId); - child4.orgChild1Id = String(child3IdExits?.orgChild1Id); - child4.orgChild2Id = String(child3IdExits?.orgChild2Id); - child4.orgRevisionId = String(child3IdExits?.orgRevisionId); - child4.orgChild3Id = String(child3IdExits?.id); - this.child4Repository.merge(child4, requestBody); - await this.child4Repository.save(child4); - return new HttpSuccess(); - } catch (error) { - return error; - } + child4.lastUpdateUserId = request.user.sub; + child4.lastUpdateFullName = request.user.name; + child4.lastUpdatedAt = new Date(); + child4.orgRootId = String(child3IdExits?.orgRootId); + child4.orgChild1Id = String(child3IdExits?.orgChild1Id); + child4.orgChild2Id = String(child3IdExits?.orgChild2Id); + child4.orgRevisionId = String(child3IdExits?.orgRevisionId); + child4.orgChild3Id = String(child3IdExits?.id); + this.child4Repository.merge(child4, requestBody); + await this.child4Repository.save(child4); + return new HttpSuccess(); } /** @@ -270,20 +258,16 @@ export class OrgChild4Controller extends Controller { "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false", ); } - try { - const posMasters = await this.posMasterRepository.find({ - where: { orgChild4Id: id }, - }); - const positions = await this.positionRepository.find({ - where: [{ posMasterId: In(posMasters.map((x) => x.id)) }], - }); + const posMasters = await this.posMasterRepository.find({ + where: { orgChild4Id: id }, + }); + const positions = await this.positionRepository.find({ + where: [{ posMasterId: In(posMasters.map((x) => x.id)) }], + }); - await this.positionRepository.remove(positions); - await this.posMasterRepository.remove(posMasters); - await this.child4Repository.delete(id); - return new HttpSuccess(); - } catch (error) { - return error; - } + await this.positionRepository.remove(positions); + await this.posMasterRepository.remove(posMasters); + await this.child4Repository.delete(id); + return new HttpSuccess(); } } diff --git a/src/controllers/OrgRootController.ts b/src/controllers/OrgRootController.ts index 857c9ff9..3983ef45 100644 --- a/src/controllers/OrgRootController.ts +++ b/src/controllers/OrgRootController.ts @@ -53,25 +53,21 @@ export class OrgRootController extends Controller { if (!orgRoot) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root"); } - try { - const getOrgRoot = { - orgRootId: orgRoot.id, - orgRootName: orgRoot.orgRootName, - orgName: "-", - orgRootShortName: orgRoot.orgRootShortName, - orgRootCode: orgRoot.orgRootCode, - orgRootRank: orgRoot.orgRootRank, - orgRootOrder: orgRoot.orgRootOrder, - orgRootPhoneEx: orgRoot.orgRootPhoneEx, - orgRootPhoneIn: orgRoot.orgRootPhoneIn, - orgRootFax: orgRoot.orgRootFax, - orgRevisionId: orgRoot.orgRevisionId, - orgCode: orgRoot.orgRootCode + "00", - }; - return new HttpSuccess(getOrgRoot); - } catch (error) { - return error; - } + const getOrgRoot = { + orgRootId: orgRoot.id, + orgRootName: orgRoot.orgRootName, + orgName: "-", + orgRootShortName: orgRoot.orgRootShortName, + orgRootCode: orgRoot.orgRootCode, + orgRootRank: orgRoot.orgRootRank, + orgRootOrder: orgRoot.orgRootOrder, + orgRootPhoneEx: orgRoot.orgRootPhoneEx, + orgRootPhoneIn: orgRoot.orgRootPhoneIn, + orgRootFax: orgRoot.orgRootFax, + orgRevisionId: orgRoot.orgRevisionId, + orgCode: orgRoot.orgRootCode + "00", + }; + return new HttpSuccess(getOrgRoot); } /** @@ -134,19 +130,14 @@ export class OrgRootController extends Controller { }, order: { orgRootOrder: "DESC" }, }); - try { - orgRoot.createdUserId = request.user.sub; - orgRoot.createdFullName = request.user.name; - orgRoot.lastUpdateUserId = request.user.sub; - orgRoot.lastUpdateFullName = request.user.name; - orgRoot.orgRootOrder = - order == null || order.orgRootOrder == null ? 1 : order.orgRootOrder + 1; - await this.orgRootRepository.save(orgRoot); + orgRoot.createdUserId = request.user.sub; + orgRoot.createdFullName = request.user.name; + orgRoot.lastUpdateUserId = request.user.sub; + orgRoot.lastUpdateFullName = request.user.name; + orgRoot.orgRootOrder = order == null || order.orgRootOrder == null ? 1 : order.orgRootOrder + 1; + await this.orgRootRepository.save(orgRoot); - return new HttpSuccess(); - } catch (error) { - return error; - } + return new HttpSuccess(); } /** @@ -209,17 +200,13 @@ export class OrgRootController extends Controller { "ไม่พบข้อมูลโครงสร้างระดับ Root ตามไอดีนี้ :" + id, ); } - try { - orgRoot.lastUpdateUserId = request.user.sub; - orgRoot.lastUpdateFullName = request.user.name; - orgRoot.lastUpdatedAt = new Date(); - this.orgRootRepository.merge(orgRoot, requestBody); - await this.orgRootRepository.save(orgRoot); + orgRoot.lastUpdateUserId = request.user.sub; + orgRoot.lastUpdateFullName = request.user.name; + orgRoot.lastUpdatedAt = new Date(); + this.orgRootRepository.merge(orgRoot, requestBody); + await this.orgRootRepository.save(orgRoot); - return new HttpSuccess(); - } catch (error) { - return error; - } + return new HttpSuccess(); } /** @@ -262,25 +249,21 @@ export class OrgRootController extends Controller { "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false", ); } - try { - const posMasters = await this.posMasterRepository.find({ - where: { orgRootId: id }, - }); - const positions = await this.positionRepository.find({ - where: [{ posMasterId: In(posMasters.map((x) => x.id)) }], - }); + const posMasters = await this.posMasterRepository.find({ + where: { orgRootId: id }, + }); + const positions = await this.positionRepository.find({ + where: [{ posMasterId: In(posMasters.map((x) => x.id)) }], + }); - await this.positionRepository.remove(positions); - await this.posMasterRepository.remove(posMasters); - await this.orgChild4Repository.delete({ orgRootId: id }); - await this.orgChild3Repository.delete({ orgRootId: id }); - await this.orgChild2Repository.delete({ orgRootId: id }); - await this.orgChild1Repository.delete({ orgRootId: id }); - await this.orgRootRepository.delete({ id }); + await this.positionRepository.remove(positions); + await this.posMasterRepository.remove(posMasters); + await this.orgChild4Repository.delete({ orgRootId: id }); + await this.orgChild3Repository.delete({ orgRootId: id }); + await this.orgChild2Repository.delete({ orgRootId: id }); + await this.orgChild1Repository.delete({ orgRootId: id }); + await this.orgRootRepository.delete({ id }); - return new HttpSuccess(); - } catch (error) { - return error; - } + return new HttpSuccess(); } } diff --git a/src/controllers/PosExecutiveController.ts b/src/controllers/PosExecutiveController.ts index b4d7539c..2910d1e0 100644 --- a/src/controllers/PosExecutiveController.ts +++ b/src/controllers/PosExecutiveController.ts @@ -12,7 +12,7 @@ import { SuccessResponse, Response, Get, - Example + Example, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; @@ -44,7 +44,6 @@ export class PosExecutiveController extends Controller { requestBody: CreatePosExecutive, @Request() request: { user: Record }, ) { - const checkName = await this.posExecutiveRepository.findOne({ where: { posExecutiveName: requestBody.posExecutiveName }, }); @@ -53,17 +52,13 @@ export class PosExecutiveController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - try { - const posExecutive = Object.assign(new PosExecutive(), requestBody); - posExecutive.createdUserId = request.user.sub; - posExecutive.createdFullName = request.user.name; - posExecutive.lastUpdateUserId = request.user.sub; - posExecutive.lastUpdateFullName = request.user.name; - await this.posExecutiveRepository.save(posExecutive); - return new HttpSuccess(); - } catch (error) { - return error; - } + const posExecutive = Object.assign(new PosExecutive(), requestBody); + posExecutive.createdUserId = request.user.sub; + posExecutive.createdFullName = request.user.name; + posExecutive.lastUpdateUserId = request.user.sub; + posExecutive.lastUpdateFullName = request.user.name; + await this.posExecutiveRepository.save(posExecutive); + return new HttpSuccess(); } /** @@ -80,11 +75,11 @@ export class PosExecutiveController extends Controller { requestBody: CreatePosExecutive, @Request() request: { user: Record }, ) { - const posExecutive = await this.posExecutiveRepository.findOne({where: {id: id}}); + const posExecutive = await this.posExecutiveRepository.findOne({ where: { id: id } }); if (!posExecutive) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); } - + const checkName = await this.posExecutiveRepository.findOne({ where: { posExecutiveName: requestBody.posExecutiveName }, }); @@ -93,15 +88,11 @@ export class PosExecutiveController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - try { - posExecutive.lastUpdateUserId = request.user.sub; - posExecutive.lastUpdateFullName = request.user.name; - this.posExecutiveRepository.merge(posExecutive, requestBody); - await this.posExecutiveRepository.save(posExecutive); - return new HttpSuccess(); - } catch (error) { - return error; - } + posExecutive.lastUpdateUserId = request.user.sub; + posExecutive.lastUpdateFullName = request.user.name; + this.posExecutiveRepository.merge(posExecutive, requestBody); + await this.posExecutiveRepository.save(posExecutive); + return new HttpSuccess(); } /** @@ -119,44 +110,31 @@ export class PosExecutiveController extends Controller { if (!delPosExecutive) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id); } - try { - await this.positionRepository.delete({ posExecutiveId: id }); - await this.posExecutiveRepository.delete({ id }); - return new HttpSuccess(); - } catch (error) { - return error; - } + await this.positionRepository.delete({ posExecutiveId: id }); + await this.posExecutiveRepository.delete({ id }); + return new HttpSuccess(); } - /** + /** * API รายละเอียดตำแหน่งทางการบริหาร * * @summary ORG_044 - รายละเอียดตำแหน่งทางการบริหาร (ADMIN) #47 - * + * * @param {string} id Id ตำแหน่งทางการบริหาร */ - @Get("{id}") - async detailPosExecutive(@Path() id: string) { - + @Get("{id}") + async detailPosExecutive(@Path() id: string) { const posExecutive = await this.posExecutiveRepository.findOne({ where: { id }, - select:[ - "id", - "posExecutiveName", - "posExecutivePriority" - ] - }); - if (!posExecutive) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - } - try { - return new HttpSuccess(posExecutive); - } catch (error) { - return error; - } - } + select: ["id", "posExecutiveName", "posExecutivePriority"], + }); + if (!posExecutive) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + return new HttpSuccess(posExecutive); + } - /** + /** * API รายการตำแหน่งทางการบริหาร * * @summary ORG_026 - รายการตำแหน่งทางการบริหาร (ADMIN) #28 @@ -171,16 +149,12 @@ export class PosExecutiveController extends Controller { }, ]) async GetPosExecutive() { - try { - const posExecutive = await this.posExecutiveRepository.find({ - select: ["id", "posExecutiveName", "posExecutivePriority"], - }); - if (!posExecutive) { - return new HttpSuccess([]); - } - return new HttpSuccess(posExecutive); - } catch (error) { - return error; + const posExecutive = await this.posExecutiveRepository.find({ + select: ["id", "posExecutiveName", "posExecutivePriority"], + }); + if (!posExecutive) { + return new HttpSuccess([]); } + return new HttpSuccess(posExecutive); } } diff --git a/src/controllers/PosLevelController.ts b/src/controllers/PosLevelController.ts index 8f3a6806..9b1396df 100644 --- a/src/controllers/PosLevelController.ts +++ b/src/controllers/PosLevelController.ts @@ -32,9 +32,7 @@ import { Not } from "typeorm"; "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") - export class PosLevelController extends Controller { - private posTypeRepository = AppDataSource.getRepository(PosType); private posLevelRepository = AppDataSource.getRepository(PosLevel); @@ -45,14 +43,12 @@ export class PosLevelController extends Controller { * */ @Post() - @Example( - { - posLevelName: "นักบริหาร", - posLevelRank: 1, - posLevelAuthority: "HEAD", - posTypeId: "08db9e81-fc46-4e95-8b33-be4ca0016abf" - }, - ) + @Example({ + posLevelName: "นักบริหาร", + posLevelRank: 1, + posLevelAuthority: "HEAD", + posTypeId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", + }) async createLevel( @Body() requestBody: CreatePosLevel, @@ -62,19 +58,29 @@ export class PosLevelController extends Controller { if (!posLevel) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } - const chkPosTypeId = await this.posTypeRepository.findOne({ where: { - id: requestBody.posTypeId } - }) - if(!chkPosTypeId){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล posTypeId ไอดีนี้ : " + requestBody.posTypeId); + const chkPosTypeId = await this.posTypeRepository.findOne({ + where: { + id: requestBody.posTypeId, + }, + }); + if (!chkPosTypeId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูล posTypeId ไอดีนี้ : " + requestBody.posTypeId, + ); } - const chkPosLevelName = await this.posLevelRepository.findOne({ where: { - posTypeId: requestBody.posTypeId, //ระดับประเภทเดียวกัน ชื่อระดับตำแหน่ง ห้ามซ้ำ - posLevelName: requestBody.posLevelName } - }) - if(chkPosLevelName){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อระดับตำแหน่ง: " + requestBody.posLevelName + " มีอยู่ในระบบแล้ว"); + const chkPosLevelName = await this.posLevelRepository.findOne({ + where: { + posTypeId: requestBody.posTypeId, //ระดับประเภทเดียวกัน ชื่อระดับตำแหน่ง ห้ามซ้ำ + posLevelName: requestBody.posLevelName, + }, + }); + if (chkPosLevelName) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ชื่อระดับตำแหน่ง: " + requestBody.posLevelName + " มีอยู่ในระบบแล้ว", + ); } const validPosLevelAuthority = ["HEAD", "DEPUTY", "GOVERNOR"]; @@ -85,16 +91,12 @@ export class PosLevelController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. posLevelAuthority"); } - try { - posLevel.createdUserId = request.user.sub; - posLevel.createdFullName = request.user.name; - posLevel.lastUpdateUserId = request.user.sub; - posLevel.lastUpdateFullName = request.user.name; - await this.posLevelRepository.save(posLevel); - return new HttpSuccess(posLevel); - } catch (error) { - return error; - } + posLevel.createdUserId = request.user.sub; + posLevel.createdFullName = request.user.name; + posLevel.lastUpdateUserId = request.user.sub; + posLevel.lastUpdateFullName = request.user.name; + await this.posLevelRepository.save(posLevel); + return new HttpSuccess(posLevel); } /** @@ -105,12 +107,10 @@ export class PosLevelController extends Controller { * @param {string} id Id ระดับตำแหน่ง */ @Put("{id}") - @Example( - { - positionName: "นักบริหาร", - posTypeRank: 1, - }, - ) + @Example({ + positionName: "นักบริหาร", + posTypeRank: 1, + }) async editLevel( @Path() id: string, @Body() requestBody: UpdatePosLevel, @@ -121,20 +121,30 @@ export class PosLevelController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); } - const chkPosTypeId = await this.posTypeRepository.findOne({ where: { - id: requestBody.posTypeId } - }) - if(!chkPosTypeId){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล posTypeId ไอดีนี้ : " + requestBody.posTypeId); + const chkPosTypeId = await this.posTypeRepository.findOne({ + where: { + id: requestBody.posTypeId, + }, + }); + if (!chkPosTypeId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูล posTypeId ไอดีนี้ : " + requestBody.posTypeId, + ); } - const chkPosLevelName = await this.posLevelRepository.findOne({ where: { - id: Not(id), - posTypeId: requestBody.posTypeId, - posLevelName: requestBody.posLevelName } - }) - if(chkPosLevelName){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อระดับตำแหน่ง: " + requestBody.posLevelName + " มีอยู่ในระบบแล้ว"); + const chkPosLevelName = await this.posLevelRepository.findOne({ + where: { + id: Not(id), + posTypeId: requestBody.posTypeId, + posLevelName: requestBody.posLevelName, + }, + }); + if (chkPosLevelName) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ชื่อระดับตำแหน่ง: " + requestBody.posLevelName + " มีอยู่ในระบบแล้ว", + ); } const validPosLevelAuthority = ["HEAD", "DEPUTY", "GOVERNOR"]; if ( @@ -144,15 +154,11 @@ export class PosLevelController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. posLevelAuthority"); } - try { - posLevel.lastUpdateUserId = request.user.sub; - posLevel.lastUpdateFullName = request.user.name; - this.posLevelRepository.merge(posLevel, requestBody); - await this.posLevelRepository.save(posLevel); - return new HttpSuccess(posLevel.id); - } catch (error) { - return error; - } + posLevel.lastUpdateUserId = request.user.sub; + posLevel.lastUpdateFullName = request.user.name; + this.posLevelRepository.merge(posLevel, requestBody); + await this.posLevelRepository.save(posLevel); + return new HttpSuccess(posLevel.id); } /** @@ -168,12 +174,8 @@ export class PosLevelController extends Controller { if (!delPosLevel) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); } - try { - await this.posLevelRepository.remove(delPosLevel); - return new HttpSuccess(); - } catch (error) { - return error; - } + await this.posLevelRepository.remove(delPosLevel); + return new HttpSuccess(); } /** @@ -184,46 +186,40 @@ export class PosLevelController extends Controller { * @param {string} id Id ระดับตำแหน่ง */ @Get("{id}") - @Example( - { + @Example({ + id: "00000000-0000-0000-0000-000000000000", + posLevelName: "นักบริหาร", + posLevelRank: 1, + posLevelAuthority: "HEAD", + posTypes: { id: "00000000-0000-0000-0000-000000000000", - posLevelName: "นักบริหาร", - posLevelRank: 1, - posLevelAuthority: "HEAD", - posTypes: { - id: "00000000-0000-0000-0000-000000000000", - posTypeName: "นักบริหาร", - posTypeRank: 1, - }, + posTypeName: "นักบริหาร", + posTypeRank: 1, }, - ) + }) async GetLevelDetail(@Path() id: string) { - try { - const getPosType = await this.posLevelRepository.findOne({ - select: ["id", "posLevelName", "posLevelRank", "posLevelAuthority"], - relations: ["posType"], - where: { id: id } - }); - if (!getPosType) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); - } - - const mapPosLevel = { - id: getPosType.id, - posLevelName: getPosType.posLevelName, - posLevelRank: getPosType.posLevelRank, - posLevelAuthority: getPosType.posLevelAuthority, - posTypes:{ - id: getPosType.posType.id, - posTypeName: getPosType.posType.posTypeName, - posTypeRank: getPosType.posType.posTypeRank - } - } - - return new HttpSuccess(mapPosLevel); - } catch (error) { - return error; + const getPosType = await this.posLevelRepository.findOne({ + select: ["id", "posLevelName", "posLevelRank", "posLevelAuthority"], + relations: ["posType"], + where: { id: id }, + }); + if (!getPosType) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); } + + const mapPosLevel = { + id: getPosType.id, + posLevelName: getPosType.posLevelName, + posLevelRank: getPosType.posLevelRank, + posLevelAuthority: getPosType.posLevelAuthority, + posTypes: { + id: getPosType.posType.id, + posTypeName: getPosType.posType.posTypeName, + posTypeRank: getPosType.posType.posTypeRank, + }, + }; + + return new HttpSuccess(mapPosLevel); } /** @@ -247,28 +243,24 @@ export class PosLevelController extends Controller { }, ]) async GetPosLevel() { - try { - const posLevel = await this.posLevelRepository.find({ - select: ["id", "posLevelName", "posLevelRank", "posLevelAuthority", "posTypeId"], - relations: ["posType"], - }); - if (!posLevel) { - return new HttpSuccess([]); - } - const mapPosLevel = posLevel.map((item) => ({ - id: item.id, - posLevelName: item.posLevelName, - posLevelRank: item.posLevelRank, - posLevelAuthority: item.posLevelAuthority, - posTypes: { - id: item.posType.id, - posTypeName: item.posType.posTypeName, - posTypeRank: item.posType.posTypeRank, - } - })); - return new HttpSuccess(mapPosLevel); - } catch (error) { - return error; + const posLevel = await this.posLevelRepository.find({ + select: ["id", "posLevelName", "posLevelRank", "posLevelAuthority", "posTypeId"], + relations: ["posType"], + }); + if (!posLevel) { + return new HttpSuccess([]); } + const mapPosLevel = posLevel.map((item) => ({ + id: item.id, + posLevelName: item.posLevelName, + posLevelRank: item.posLevelRank, + posLevelAuthority: item.posLevelAuthority, + posTypes: { + id: item.posType.id, + posTypeName: item.posType.posTypeName, + posTypeRank: item.posType.posTypeRank, + }, + })); + return new HttpSuccess(mapPosLevel); } } diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index d3e85db0..10ac2429 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -128,16 +128,12 @@ export class PositionController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว"); } - try { - posDict.createdUserId = request.user.sub; - posDict.createdFullName = request.user.name; - posDict.lastUpdateUserId = request.user.sub; - posDict.lastUpdateFullName = request.user.name; - await this.posDictRepository.save(posDict); - return new HttpSuccess(posDict.id); - } catch (error) { - return error; - } + posDict.createdUserId = request.user.sub; + posDict.createdFullName = request.user.name; + posDict.lastUpdateUserId = request.user.sub; + posDict.lastUpdateFullName = request.user.name; + await this.posDictRepository.save(posDict); + return new HttpSuccess(posDict.id); } /** @@ -216,15 +212,11 @@ export class PositionController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว"); } - try { - posDict.lastUpdateUserId = request.user.sub; - posDict.lastUpdateFullName = request.user.name; - this.posDictRepository.merge(posDict, requestBody); - await this.posDictRepository.save(posDict); - return new HttpSuccess(); - } catch (error) { - return error; - } + posDict.lastUpdateUserId = request.user.sub; + posDict.lastUpdateFullName = request.user.name; + this.posDictRepository.merge(posDict, requestBody); + await this.posDictRepository.save(posDict); + return new HttpSuccess(); } /** @@ -240,12 +232,8 @@ export class PositionController extends Controller { if (!delPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id); } - try { - await this.posDictRepository.remove(delPosDict); - return new HttpSuccess(); - } catch (error) { - return error; - } + await this.posDictRepository.remove(delPosDict); + return new HttpSuccess(); } /** @@ -256,116 +244,112 @@ export class PositionController extends Controller { */ @Get("position") async findPosition(@Query("keyword") keyword?: string, @Query("type") type?: string) { - try { - let findPosDict: any; - switch (type) { - case "positionName": - findPosDict = await this.posDictRepository.find({ - where: { posDictName: Like(`%${keyword}%`) }, - relations: ["posType", "posLevel", "posExecutive"], - }); - // if (!findPosDict) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); - // } - break; + let findPosDict: any; + switch (type) { + case "positionName": + findPosDict = await this.posDictRepository.find({ + where: { posDictName: Like(`%${keyword}%`) }, + relations: ["posType", "posLevel", "posExecutive"], + }); + // if (!findPosDict) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); + // } + break; - case "positionField": - findPosDict = await this.posDictRepository.find({ - where: { posDictField: Like(`%${keyword}%`) }, - relations: ["posType", "posLevel", "posExecutive"], - }); - // if (!findPosDict) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); - // } - break; + case "positionField": + findPosDict = await this.posDictRepository.find({ + where: { posDictField: Like(`%${keyword}%`) }, + relations: ["posType", "posLevel", "posExecutive"], + }); + // if (!findPosDict) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); + // } + break; - case "positionType": - const findTypes: PosType[] = await this.posTypeRepository.find({ - where: { posTypeName: Like(`%${keyword}%`) }, - select: ["id"], - }); - findPosDict = await this.posDictRepository.find({ - where: { posTypeId: In(findTypes.map((x) => x.id)) }, - relations: ["posType", "posLevel", "posExecutive"], - }); - break; + case "positionType": + const findTypes: PosType[] = await this.posTypeRepository.find({ + where: { posTypeName: Like(`%${keyword}%`) }, + select: ["id"], + }); + findPosDict = await this.posDictRepository.find({ + where: { posTypeId: In(findTypes.map((x) => x.id)) }, + relations: ["posType", "posLevel", "posExecutive"], + }); + break; - case "positionLevel": - const findLevel: PosLevel[] = await this.posLevelRepository.find({ - where: { posLevelName: Like(`%${keyword}%`) }, - select: ["id"], - }); - findPosDict = await this.posDictRepository.find({ - where: { posLevelId: In(findLevel.map((x) => x.id)) }, - relations: ["posType", "posLevel", "posExecutive"], - }); - break; + case "positionLevel": + const findLevel: PosLevel[] = await this.posLevelRepository.find({ + where: { posLevelName: Like(`%${keyword}%`) }, + select: ["id"], + }); + findPosDict = await this.posDictRepository.find({ + where: { posLevelId: In(findLevel.map((x) => x.id)) }, + relations: ["posType", "posLevel", "posExecutive"], + }); + break; - case "positionExecutive": - const findExecutive: PosExecutive[] = await this.posExecutiveRepository.find({ - where: { posExecutiveName: Like(`%${keyword}%`) }, - select: ["id"], - }); - findPosDict = await this.posDictRepository.find({ - where: { posExecutiveId: In(findExecutive.map((x) => x.id)) }, - relations: ["posType", "posLevel", "posExecutive"], - }); - break; + case "positionExecutive": + const findExecutive: PosExecutive[] = await this.posExecutiveRepository.find({ + where: { posExecutiveName: Like(`%${keyword}%`) }, + select: ["id"], + }); + findPosDict = await this.posDictRepository.find({ + where: { posExecutiveId: In(findExecutive.map((x) => x.id)) }, + relations: ["posType", "posLevel", "posExecutive"], + }); + break; - case "positionExecutiveField": - findPosDict = await this.posDictRepository.find({ - where: { posDictExecutiveField: Like(`%${keyword}%`) }, - relations: ["posType", "posLevel", "posExecutive"], - }); - // if (!findPosDict) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); - // } - break; + case "positionExecutiveField": + findPosDict = await this.posDictRepository.find({ + where: { posDictExecutiveField: Like(`%${keyword}%`) }, + relations: ["posType", "posLevel", "posExecutive"], + }); + // if (!findPosDict) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); + // } + break; - case "positionArea": - findPosDict = await this.posDictRepository.find({ - where: { posDictArea: Like(`%${keyword}%`) }, - relations: ["posType", "posLevel", "posExecutive"], - }); - // if (!findPosDict) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); - // } - break; + case "positionArea": + findPosDict = await this.posDictRepository.find({ + where: { posDictArea: Like(`%${keyword}%`) }, + relations: ["posType", "posLevel", "posExecutive"], + }); + // if (!findPosDict) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); + // } + break; - default: - findPosDict = await this.posDictRepository.find({ - relations: ["posType", "posLevel", "posExecutive"], - }); - // if (!findPosDict) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - // } - break; - } - - const mapDataPosDict = await Promise.all( - findPosDict.map(async (item: any) => { - return { - id: item.id, - positionName: item.posDictName, - positionField: item.posDictField, - posTypeId: item.posTypeId, - posTypeName: item.posType == null ? null : item.posType.posTypeName, - posLevelId: item.posLevelId, - posLevelName: item.posLevel == null ? null : item.posLevel.posLevelName, - posExecutiveId: item.posExecutiveId, - posExecutiveName: item.posExecutive == null ? null : item.posExecutive.posExecutiveName, - positionExecutiveField: item.posDictExecutiveField, - positionArea: item.posDictArea, - isSpecial: item.isSpecial, - positionIsSelected: false, - }; - }), - ); - - return new HttpSuccess(mapDataPosDict); - } catch (error) { - return error; + default: + findPosDict = await this.posDictRepository.find({ + relations: ["posType", "posLevel", "posExecutive"], + }); + // if (!findPosDict) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + // } + break; } + + const mapDataPosDict = await Promise.all( + findPosDict.map(async (item: any) => { + return { + id: item.id, + positionName: item.posDictName, + positionField: item.posDictField, + posTypeId: item.posTypeId, + posTypeName: item.posType == null ? null : item.posType.posTypeName, + posLevelId: item.posLevelId, + posLevelName: item.posLevel == null ? null : item.posLevel.posLevelName, + posExecutiveId: item.posExecutiveId, + posExecutiveName: item.posExecutive == null ? null : item.posExecutive.posExecutiveName, + positionExecutiveField: item.posDictExecutiveField, + positionArea: item.posDictArea, + isSpecial: item.isSpecial, + positionIsSelected: false, + }; + }), + ); + + return new HttpSuccess(mapDataPosDict); } /** @@ -880,44 +864,40 @@ export class PositionController extends Controller { */ @Get("position/{id}") async detailPosition(@Path() id: string) { - try { - const posMaster = await this.posMasterRepository.findOne({ - where: { id }, - }); - if (!posMaster) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - } - const positions = await this.positionRepository.find({ - where: { posMasterId: posMaster.id }, - relations: ["posType", "posLevel", "posExecutive"], - order: { lastUpdatedAt: "ASC" }, - }); - const formattedData = { - id: posMaster.id, - posMasterNoPrefix: posMaster.posMasterNoPrefix, - posMasterNo: posMaster.posMasterNo, - posMasterNoSuffix: posMaster.posMasterNoSuffix, - positions: positions.map((position) => ({ - id: position.id, - positionName: position.positionName, - positionField: position.positionField, - posTypeId: position.posTypeId, - posTypeName: position.posType == null ? null : position.posType.posTypeName, - posLevelId: position.posLevelId, - posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName, - posExecutiveId: position.posExecutiveId, - posExecutiveName: - position.posExecutive == null ? null : position.posExecutive.posExecutiveName, - positionExecutiveField: position.positionExecutiveField, - positionArea: position.positionArea, - positionIsSelected: position.positionIsSelected, - isSpecial: position.isSpecial, - })), - }; - return new HttpSuccess(formattedData); - } catch (error) { - return error; + const posMaster = await this.posMasterRepository.findOne({ + where: { id }, + }); + if (!posMaster) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } + const positions = await this.positionRepository.find({ + where: { posMasterId: posMaster.id }, + relations: ["posType", "posLevel", "posExecutive"], + order: { lastUpdatedAt: "ASC" }, + }); + const formattedData = { + id: posMaster.id, + posMasterNoPrefix: posMaster.posMasterNoPrefix, + posMasterNo: posMaster.posMasterNo, + posMasterNoSuffix: posMaster.posMasterNoSuffix, + positions: positions.map((position) => ({ + id: position.id, + positionName: position.positionName, + positionField: position.positionField, + posTypeId: position.posTypeId, + posTypeName: position.posType == null ? null : position.posType.posTypeName, + posLevelId: position.posLevelId, + posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName, + posExecutiveId: position.posExecutiveId, + posExecutiveName: + position.posExecutive == null ? null : position.posExecutive.posExecutiveName, + positionExecutiveField: position.positionExecutiveField, + positionArea: position.positionArea, + positionIsSelected: position.positionIsSelected, + isSpecial: position.isSpecial, + })), + }; + return new HttpSuccess(formattedData); } /** @@ -936,13 +916,9 @@ export class PositionController extends Controller { if (!delPosMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id); } - try { - await this.positionRepository.delete({ posMasterId: id }); - await this.posMasterRepository.delete({ id }); - return new HttpSuccess(); - } catch (error) { - return error; - } + await this.positionRepository.delete({ posMasterId: id }); + await this.posMasterRepository.delete({ id }); + return new HttpSuccess(); } /** @@ -964,289 +940,283 @@ export class PositionController extends Controller { keyword?: string; }, ) { - try { - let typeCondition: any = {}; - let checkChildConditions: any = {}; - let keywordAsInt: any; - let searchShortName = ""; - let searchShortName0 = `CONCAT(orgRoot.orgRootShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; - let searchShortName1 = `CONCAT(orgChild1.orgChild1ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; - let searchShortName2 = `CONCAT(orgChild2.orgChild2ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; - let searchShortName3 = `CONCAT(orgChild3.orgChild3ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; - let searchShortName4 = `CONCAT(orgChild4.orgChild4ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; + let typeCondition: any = {}; + let checkChildConditions: any = {}; + let keywordAsInt: any; + let searchShortName = ""; + let searchShortName0 = `CONCAT(orgRoot.orgRootShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; + let searchShortName1 = `CONCAT(orgChild1.orgChild1ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; + let searchShortName2 = `CONCAT(orgChild2.orgChild2ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; + let searchShortName3 = `CONCAT(orgChild3.orgChild3ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; + let searchShortName4 = `CONCAT(orgChild4.orgChild4ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; - if (body.type === 0) { - typeCondition = { - orgRootId: body.id, - }; - if (!body.isAll) { - checkChildConditions = { - orgChild1Id: IsNull(), - }; - searchShortName = `CONCAT(orgRoot.orgRootShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; - } else { - } - } else if (body.type === 1) { - typeCondition = { - orgChild1Id: body.id, - }; - if (!body.isAll) { - checkChildConditions = { - orgChild2Id: IsNull(), - }; - searchShortName = `CONCAT(orgChild1.orgChild1ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; - } else { - } - } else if (body.type === 2) { - typeCondition = { - orgChild2Id: body.id, - }; - if (!body.isAll) { - checkChildConditions = { - orgChild3Id: IsNull(), - }; - searchShortName = `CONCAT(orgChild2.orgChild2ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; - } else { - } - } else if (body.type === 3) { - typeCondition = { - orgChild3Id: body.id, - }; - if (!body.isAll) { - checkChildConditions = { - orgChild4Id: IsNull(), - }; - searchShortName = `CONCAT(orgChild3.orgChild3ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; - } else { - } - } else if (body.type === 4) { - typeCondition = { - orgChild4Id: body.id, - }; - searchShortName = `CONCAT(orgChild4.orgChild4ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; - } - let findPosition: any; - let masterId = new Array(); - if (body.keyword != null && body.keyword != "") { - const findTypes: PosType[] = await this.posTypeRepository.find({ - where: { posTypeName: Like(`%${body.keyword}%`) }, - select: ["id"], - }); - findPosition = await this.positionRepository.find({ - where: { posTypeId: In(findTypes.map((x) => x.id)) }, - select: ["posMasterId"], - }); - masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); - const findLevel: PosLevel[] = await this.posLevelRepository.find({ - where: { posLevelName: Like(`%${body.keyword}%`) }, - select: ["id"], - }); - findPosition = await this.positionRepository.find({ - where: { posLevelId: In(findLevel.map((x) => x.id)) }, - select: ["posMasterId"], - }); - masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); - const findExecutive: PosExecutive[] = await this.posExecutiveRepository.find({ - where: { posExecutiveName: Like(`%${body.keyword}%`) }, - select: ["id"], - }); - findPosition = await this.positionRepository.find({ - where: { posExecutiveId: In(findExecutive.map((x) => x.id)) }, - select: ["posMasterId"], - }); - masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); - findPosition = await this.positionRepository.find({ - where: { positionName: Like(`%${body.keyword}%`) }, - select: ["posMasterId"], - }); - masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); - keywordAsInt = body.keyword == null ? null : parseInt(body.keyword, 10); - if (isNaN(keywordAsInt)) { - keywordAsInt = "P@ssw0rd!z"; - } - masterId = [...new Set(masterId)]; - } - - const revisionCondition = { - orgRevisionId: body.revisionId, + if (body.type === 0) { + typeCondition = { + orgRootId: body.id, }; - const conditions = [ - { - ...checkChildConditions, - ...typeCondition, - ...revisionCondition, - ...(body.keyword && - (masterId.length > 0 - ? { id: In(masterId) } - : { posMasterNo: Like(`%${body.keyword}%`) })), - }, - ]; - - - const [posMaster, total] = await AppDataSource.getRepository(PosMaster) - .createQueryBuilder("posMaster") - .leftJoinAndSelect("posMaster.orgRoot", "orgRoot") - .leftJoinAndSelect("posMaster.orgChild1", "orgChild1") - .leftJoinAndSelect("posMaster.orgChild2", "orgChild2") - .leftJoinAndSelect("posMaster.orgChild3", "orgChild3") - .leftJoinAndSelect("posMaster.orgChild4", "orgChild4") - .leftJoinAndSelect("posMaster.current_holder", "current_holder") - .leftJoinAndSelect("posMaster.next_holder", "next_holder") - .where(conditions) - .orWhere( - new Brackets((qb) => { - qb.andWhere( - body.keyword != null && body.keyword != "" - ? body.isAll == false - ? searchShortName - : `CASE WHEN posMaster.orgChild1 is null THEN ${searchShortName0} WHEN posMaster.orgChild2 is null THEN ${searchShortName1} WHEN posMaster.orgChild3 is null THEN ${searchShortName2} WHEN posMaster.orgChild4 is null THEN ${searchShortName3} ELSE ${searchShortName4} END LIKE '%${body.keyword}%'` - : "1=1", - ) - .andWhere(checkChildConditions) - .andWhere(typeCondition) - .andWhere(revisionCondition); - }), - ) - .orWhere( - new Brackets((qb) => { - qb.andWhere( - body.keyword != null && body.keyword != "" - ? `CONCAT(current_holder.prefix, current_holder.firstName, current_holder.lastName) like '%${body.keyword}%'` - : "1=1", - { - keyword: `%${body.keyword}%`, - }, - ) - .andWhere(checkChildConditions) - .andWhere(typeCondition) - .andWhere(revisionCondition); - }), - ) - .orderBy("posMaster.posMasterOrder", "ASC") - .skip((body.page - 1) * body.pageSize) - .take(body.pageSize) - .getManyAndCount(); - - const formattedData = await Promise.all( - posMaster.map(async (posMaster) => { - const positions = await this.positionRepository.find({ - where: { - posMasterId: posMaster.id, - }, - relations: ["posLevel", "posType", "posExecutive"], - }); - - let profile: any; - const chkRevision = await this.orgRevisionRepository.findOne({ - where: { id: posMaster.orgRevisionId }, - }); - if (chkRevision?.orgRevisionIsCurrent && !chkRevision?.orgRevisionIsDraft) { - profile = await this.profileRepository.findOne({ - where: { id: String(posMaster.current_holderId) }, - }); - } else if (!chkRevision?.orgRevisionIsCurrent && chkRevision?.orgRevisionIsDraft) { - profile = await this.profileRepository.findOne({ - where: { id: String(posMaster.next_holderId) }, - }); - } - const type = await this.posTypeRepository.findOne({ - where: { id: String(profile?.posTypeId) }, - }); - const level = await this.posLevelRepository.findOne({ - where: { id: String(profile?.posLevelId) }, - }); - - let shortName = ""; - - if ( - posMaster.orgRootId !== null && - posMaster.orgChild1Id == null && - posMaster.orgChild2Id == null && - posMaster.orgChild3Id == null - ) { - body.type = 0; - shortName = posMaster.orgRoot.orgRootShortName; - } else if ( - posMaster.orgRootId !== null && - posMaster.orgChild1Id !== null && - posMaster.orgChild2Id == null && - posMaster.orgChild3Id == null - ) { - body.type = 1; - shortName = posMaster.orgChild1.orgChild1ShortName; - } else if ( - posMaster.orgRootId !== null && - posMaster.orgChild1Id !== null && - posMaster.orgChild2Id !== null && - posMaster.orgChild3Id == null - ) { - body.type = 2; - shortName = posMaster.orgChild2.orgChild2ShortName; - } else if ( - posMaster.orgRootId !== null && - posMaster.orgChild1Id !== null && - posMaster.orgChild2Id !== null && - posMaster.orgChild3Id !== null - ) { - body.type = 3; - shortName = posMaster.orgChild3.orgChild3ShortName; - } else if ( - posMaster.orgRootId !== null && - posMaster.orgChild1Id !== null && - posMaster.orgChild2Id !== null && - posMaster.orgChild3Id !== null - ) { - body.type = 4; - shortName = posMaster.orgChild4.orgChild4ShortName; - } - - 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, - fullNameCurrentHolder: - posMaster.current_holder == null - ? null - : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}`, - fullNameNextHolder: - posMaster.next_holder == null - ? null - : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, - orgShortname: shortName, - isSit: posMaster.isSit, - profilePosition: profile == null || profile.position == null ? null : profile.position, - profilePostype: type == null || type.posTypeName == null ? null : type.posTypeName, - profilePoslevel: - level == null || level.posLevelName == null ? null : level.posLevelName, - positions: positions.map((position) => ({ - id: position.id, - positionName: position.positionName, - positionField: position.positionField, - posTypeId: position.posTypeId, - posTypeName: position.posType == null ? null : position.posType.posTypeName, - posLevelId: position.posLevelId, - posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName, - posExecutiveId: position.posExecutiveId, - posExecutiveName: - position.posExecutive == null ? null : position.posExecutive.posExecutiveName, - positionExecutiveField: position.positionExecutiveField, - positionArea: position.positionArea, - positionIsSelected: position.positionIsSelected, - isSpecial: position.isSpecial, - })), - }; - }), - ); - return new HttpSuccess({ data: formattedData, total }); - } catch (error) { - return error; + if (!body.isAll) { + checkChildConditions = { + orgChild1Id: IsNull(), + }; + searchShortName = `CONCAT(orgRoot.orgRootShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; + } else { + } + } else if (body.type === 1) { + typeCondition = { + orgChild1Id: body.id, + }; + if (!body.isAll) { + checkChildConditions = { + orgChild2Id: IsNull(), + }; + searchShortName = `CONCAT(orgChild1.orgChild1ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; + } else { + } + } else if (body.type === 2) { + typeCondition = { + orgChild2Id: body.id, + }; + if (!body.isAll) { + checkChildConditions = { + orgChild3Id: IsNull(), + }; + searchShortName = `CONCAT(orgChild2.orgChild2ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; + } else { + } + } else if (body.type === 3) { + typeCondition = { + orgChild3Id: body.id, + }; + if (!body.isAll) { + checkChildConditions = { + orgChild4Id: IsNull(), + }; + searchShortName = `CONCAT(orgChild3.orgChild3ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; + } else { + } + } else if (body.type === 4) { + typeCondition = { + orgChild4Id: body.id, + }; + searchShortName = `CONCAT(orgChild4.orgChild4ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; } + let findPosition: any; + let masterId = new Array(); + if (body.keyword != null && body.keyword != "") { + const findTypes: PosType[] = await this.posTypeRepository.find({ + where: { posTypeName: Like(`%${body.keyword}%`) }, + select: ["id"], + }); + findPosition = await this.positionRepository.find({ + where: { posTypeId: In(findTypes.map((x) => x.id)) }, + select: ["posMasterId"], + }); + masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); + const findLevel: PosLevel[] = await this.posLevelRepository.find({ + where: { posLevelName: Like(`%${body.keyword}%`) }, + select: ["id"], + }); + findPosition = await this.positionRepository.find({ + where: { posLevelId: In(findLevel.map((x) => x.id)) }, + select: ["posMasterId"], + }); + masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); + const findExecutive: PosExecutive[] = await this.posExecutiveRepository.find({ + where: { posExecutiveName: Like(`%${body.keyword}%`) }, + select: ["id"], + }); + findPosition = await this.positionRepository.find({ + where: { posExecutiveId: In(findExecutive.map((x) => x.id)) }, + select: ["posMasterId"], + }); + masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); + findPosition = await this.positionRepository.find({ + where: { positionName: Like(`%${body.keyword}%`) }, + select: ["posMasterId"], + }); + masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); + keywordAsInt = body.keyword == null ? null : parseInt(body.keyword, 10); + if (isNaN(keywordAsInt)) { + keywordAsInt = "P@ssw0rd!z"; + } + masterId = [...new Set(masterId)]; + } + + const revisionCondition = { + orgRevisionId: body.revisionId, + }; + const conditions = [ + { + ...checkChildConditions, + ...typeCondition, + ...revisionCondition, + ...(body.keyword && + (masterId.length > 0 + ? { id: In(masterId) } + : { posMasterNo: Like(`%${body.keyword}%`) })), + }, + ]; + + const [posMaster, total] = await AppDataSource.getRepository(PosMaster) + .createQueryBuilder("posMaster") + .leftJoinAndSelect("posMaster.orgRoot", "orgRoot") + .leftJoinAndSelect("posMaster.orgChild1", "orgChild1") + .leftJoinAndSelect("posMaster.orgChild2", "orgChild2") + .leftJoinAndSelect("posMaster.orgChild3", "orgChild3") + .leftJoinAndSelect("posMaster.orgChild4", "orgChild4") + .leftJoinAndSelect("posMaster.current_holder", "current_holder") + .leftJoinAndSelect("posMaster.next_holder", "next_holder") + .where(conditions) + .orWhere( + new Brackets((qb) => { + qb.andWhere( + body.keyword != null && body.keyword != "" + ? body.isAll == false + ? searchShortName + : `CASE WHEN posMaster.orgChild1 is null THEN ${searchShortName0} WHEN posMaster.orgChild2 is null THEN ${searchShortName1} WHEN posMaster.orgChild3 is null THEN ${searchShortName2} WHEN posMaster.orgChild4 is null THEN ${searchShortName3} ELSE ${searchShortName4} END LIKE '%${body.keyword}%'` + : "1=1", + ) + .andWhere(checkChildConditions) + .andWhere(typeCondition) + .andWhere(revisionCondition); + }), + ) + .orWhere( + new Brackets((qb) => { + qb.andWhere( + body.keyword != null && body.keyword != "" + ? `CONCAT(current_holder.prefix, current_holder.firstName, current_holder.lastName) like '%${body.keyword}%'` + : "1=1", + { + keyword: `%${body.keyword}%`, + }, + ) + .andWhere(checkChildConditions) + .andWhere(typeCondition) + .andWhere(revisionCondition); + }), + ) + .orderBy("posMaster.posMasterOrder", "ASC") + .skip((body.page - 1) * body.pageSize) + .take(body.pageSize) + .getManyAndCount(); + + const formattedData = await Promise.all( + posMaster.map(async (posMaster) => { + const positions = await this.positionRepository.find({ + where: { + posMasterId: posMaster.id, + }, + relations: ["posLevel", "posType", "posExecutive"], + }); + + let profile: any; + const chkRevision = await this.orgRevisionRepository.findOne({ + where: { id: posMaster.orgRevisionId }, + }); + if (chkRevision?.orgRevisionIsCurrent && !chkRevision?.orgRevisionIsDraft) { + profile = await this.profileRepository.findOne({ + where: { id: String(posMaster.current_holderId) }, + }); + } else if (!chkRevision?.orgRevisionIsCurrent && chkRevision?.orgRevisionIsDraft) { + profile = await this.profileRepository.findOne({ + where: { id: String(posMaster.next_holderId) }, + }); + } + const type = await this.posTypeRepository.findOne({ + where: { id: String(profile?.posTypeId) }, + }); + const level = await this.posLevelRepository.findOne({ + where: { id: String(profile?.posLevelId) }, + }); + + let shortName = ""; + + if ( + posMaster.orgRootId !== null && + posMaster.orgChild1Id == null && + posMaster.orgChild2Id == null && + posMaster.orgChild3Id == null + ) { + body.type = 0; + shortName = posMaster.orgRoot.orgRootShortName; + } else if ( + posMaster.orgRootId !== null && + posMaster.orgChild1Id !== null && + posMaster.orgChild2Id == null && + posMaster.orgChild3Id == null + ) { + body.type = 1; + shortName = posMaster.orgChild1.orgChild1ShortName; + } else if ( + posMaster.orgRootId !== null && + posMaster.orgChild1Id !== null && + posMaster.orgChild2Id !== null && + posMaster.orgChild3Id == null + ) { + body.type = 2; + shortName = posMaster.orgChild2.orgChild2ShortName; + } else if ( + posMaster.orgRootId !== null && + posMaster.orgChild1Id !== null && + posMaster.orgChild2Id !== null && + posMaster.orgChild3Id !== null + ) { + body.type = 3; + shortName = posMaster.orgChild3.orgChild3ShortName; + } else if ( + posMaster.orgRootId !== null && + posMaster.orgChild1Id !== null && + posMaster.orgChild2Id !== null && + posMaster.orgChild3Id !== null + ) { + body.type = 4; + shortName = posMaster.orgChild4.orgChild4ShortName; + } + + 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, + fullNameCurrentHolder: + posMaster.current_holder == null + ? null + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}`, + fullNameNextHolder: + posMaster.next_holder == null + ? null + : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + orgShortname: shortName, + isSit: posMaster.isSit, + profilePosition: profile == null || profile.position == null ? null : profile.position, + profilePostype: type == null || type.posTypeName == null ? null : type.posTypeName, + profilePoslevel: level == null || level.posLevelName == null ? null : level.posLevelName, + positions: positions.map((position) => ({ + id: position.id, + positionName: position.positionName, + positionField: position.positionField, + posTypeId: position.posTypeId, + posTypeName: position.posType == null ? null : position.posType.posTypeName, + posLevelId: position.posLevelId, + posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName, + posExecutiveId: position.posExecutiveId, + posExecutiveName: + position.posExecutive == null ? null : position.posExecutive.posExecutiveName, + positionExecutiveField: position.positionExecutiveField, + positionArea: position.positionArea, + positionIsSelected: position.positionIsSelected, + isSpecial: position.isSpecial, + })), + }; + }), + ); + return new HttpSuccess({ data: formattedData, total }); } /** @@ -1257,155 +1227,151 @@ export class PositionController extends Controller { */ @Post("sort") async Sort(@Body() requestBody: { id: string; type: number; sortId: string[] }) { - try { - switch (requestBody.type) { - case 0: { - const rootId = await this.posMasterRepository.findOne({ - where: { orgRootId: requestBody.id }, - }); - if (!rootId?.id) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId: " + requestBody.id); - } - const listPosMasterId_0 = await this.posMasterRepository.find({ - where: { - orgRootId: requestBody.id, - orgChild1Id: IsNull(), - orgChild2Id: IsNull(), - orgChild3Id: IsNull(), - orgChild4Id: IsNull(), - }, - select: ["id", "posMasterOrder"], - }); - if (!listPosMasterId_0) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 0."); - } - const sortData_0 = listPosMasterId_0.map((data) => ({ - id: data.id, - posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, - })); - await this.posMasterRepository.save(sortData_0); - break; + switch (requestBody.type) { + case 0: { + const rootId = await this.posMasterRepository.findOne({ + where: { orgRootId: requestBody.id }, + }); + if (!rootId?.id) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId: " + requestBody.id); } - - case 1: { - const child1Id = await this.posMasterRepository.findOne({ - where: { orgChild1Id: requestBody.id }, - }); - if (!child1Id?.id) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id: " + requestBody.id); - } - const listPosMasterId_1 = await this.posMasterRepository.find({ - where: { - orgRootId: Not(IsNull()), - orgChild1Id: requestBody.id, - orgChild2Id: IsNull(), - orgChild3Id: IsNull(), - orgChild4Id: IsNull(), - }, - select: ["id", "posMasterOrder"], - }); - if (!listPosMasterId_1) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 1."); - } - const sortData_1 = listPosMasterId_1.map((data) => ({ - id: data.id, - posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, - })); - await this.posMasterRepository.save(sortData_1); - break; + const listPosMasterId_0 = await this.posMasterRepository.find({ + where: { + orgRootId: requestBody.id, + orgChild1Id: IsNull(), + orgChild2Id: IsNull(), + orgChild3Id: IsNull(), + orgChild4Id: IsNull(), + }, + select: ["id", "posMasterOrder"], + }); + if (!listPosMasterId_0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 0."); } - - case 2: { - const child2Id = await this.posMasterRepository.findOne({ - where: { orgChild2Id: requestBody.id }, - }); - if (!child2Id?.id) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2Id: " + requestBody.id); - } - const listPosMasterId_2 = await this.posMasterRepository.find({ - where: { - orgRootId: Not(IsNull()), - orgChild1Id: Not(IsNull()), - orgChild2Id: requestBody.id, - orgChild3Id: IsNull(), - orgChild4Id: IsNull(), - }, - select: ["id", "posMasterOrder"], - }); - if (!listPosMasterId_2) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 2."); - } - const sortData_2 = listPosMasterId_2.map((data) => ({ - id: data.id, - posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, - })); - await this.posMasterRepository.save(sortData_2); - break; - } - - case 3: { - const child3Id = await this.posMasterRepository.findOne({ - where: { orgChild3Id: requestBody.id }, - }); - if (!child3Id?.id) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found chil3Id: " + requestBody.id); - } - const listPosMasterId_3 = await this.posMasterRepository.find({ - where: { - orgRootId: Not(IsNull()), - orgChild1Id: Not(IsNull()), - orgChild2Id: Not(IsNull()), - orgChild3Id: requestBody.id, - orgChild4Id: IsNull(), - }, - select: ["id", "posMasterOrder"], - }); - if (!listPosMasterId_3) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 3."); - } - const sortData_3 = listPosMasterId_3.map((data) => ({ - id: data.id, - posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, - })); - await this.posMasterRepository.save(sortData_3); - break; - } - - case 4: { - const child4Id = await this.posMasterRepository.findOne({ - where: { orgChild4Id: requestBody.id }, - }); - if (!child4Id?.id) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child4Id: " + requestBody.id); - } - const listPosMasterId_4 = await this.posMasterRepository.find({ - where: { - orgRootId: Not(IsNull()), - orgChild1Id: Not(IsNull()), - orgChild2Id: Not(IsNull()), - orgChild3Id: Not(IsNull()), - orgChild4Id: requestBody.id, - }, - select: ["id", "posMasterOrder"], - }); - if (!listPosMasterId_4) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 4."); - } - const sortData_4 = listPosMasterId_4.map((data) => ({ - id: data.id, - posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, - })); - await this.posMasterRepository.save(sortData_4); - break; - } - - default: - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found type: " + requestBody.type); + const sortData_0 = listPosMasterId_0.map((data) => ({ + id: data.id, + posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, + })); + await this.posMasterRepository.save(sortData_0); + break; } - return new HttpSuccess(); - } catch (error) { - return error; + + case 1: { + const child1Id = await this.posMasterRepository.findOne({ + where: { orgChild1Id: requestBody.id }, + }); + if (!child1Id?.id) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id: " + requestBody.id); + } + const listPosMasterId_1 = await this.posMasterRepository.find({ + where: { + orgRootId: Not(IsNull()), + orgChild1Id: requestBody.id, + orgChild2Id: IsNull(), + orgChild3Id: IsNull(), + orgChild4Id: IsNull(), + }, + select: ["id", "posMasterOrder"], + }); + if (!listPosMasterId_1) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 1."); + } + const sortData_1 = listPosMasterId_1.map((data) => ({ + id: data.id, + posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, + })); + await this.posMasterRepository.save(sortData_1); + break; + } + + case 2: { + const child2Id = await this.posMasterRepository.findOne({ + where: { orgChild2Id: requestBody.id }, + }); + if (!child2Id?.id) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2Id: " + requestBody.id); + } + const listPosMasterId_2 = await this.posMasterRepository.find({ + where: { + orgRootId: Not(IsNull()), + orgChild1Id: Not(IsNull()), + orgChild2Id: requestBody.id, + orgChild3Id: IsNull(), + orgChild4Id: IsNull(), + }, + select: ["id", "posMasterOrder"], + }); + if (!listPosMasterId_2) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 2."); + } + const sortData_2 = listPosMasterId_2.map((data) => ({ + id: data.id, + posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, + })); + await this.posMasterRepository.save(sortData_2); + break; + } + + case 3: { + const child3Id = await this.posMasterRepository.findOne({ + where: { orgChild3Id: requestBody.id }, + }); + if (!child3Id?.id) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found chil3Id: " + requestBody.id); + } + const listPosMasterId_3 = await this.posMasterRepository.find({ + where: { + orgRootId: Not(IsNull()), + orgChild1Id: Not(IsNull()), + orgChild2Id: Not(IsNull()), + orgChild3Id: requestBody.id, + orgChild4Id: IsNull(), + }, + select: ["id", "posMasterOrder"], + }); + if (!listPosMasterId_3) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 3."); + } + const sortData_3 = listPosMasterId_3.map((data) => ({ + id: data.id, + posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, + })); + await this.posMasterRepository.save(sortData_3); + break; + } + + case 4: { + const child4Id = await this.posMasterRepository.findOne({ + where: { orgChild4Id: requestBody.id }, + }); + if (!child4Id?.id) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child4Id: " + requestBody.id); + } + const listPosMasterId_4 = await this.posMasterRepository.find({ + where: { + orgRootId: Not(IsNull()), + orgChild1Id: Not(IsNull()), + orgChild2Id: Not(IsNull()), + orgChild3Id: Not(IsNull()), + orgChild4Id: requestBody.id, + }, + select: ["id", "posMasterOrder"], + }); + if (!listPosMasterId_4) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 4."); + } + const sortData_4 = listPosMasterId_4.map((data) => ({ + id: data.id, + posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, + })); + await this.posMasterRepository.save(sortData_4); + break; + } + + default: + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found type: " + requestBody.type); } + return new HttpSuccess(); } /** @@ -1466,83 +1432,79 @@ export class PositionController extends Controller { @Body() requestBody: { id: string; type: number; positionMaster: string[] }, @Request() request: { user: Record }, ) { - try { - const posMasters = await this.posMasterRepository.find({ - where: { id: In(requestBody.positionMaster) }, - }); + const posMasters = await this.posMasterRepository.find({ + where: { id: In(requestBody.positionMaster) }, + }); - posMasters.forEach(async (posMaster: any) => { - posMaster.orgRootId = null; - posMaster.orgChild1Id = null; - posMaster.orgChild2Id = null; - posMaster.orgChild3Id = null; - posMaster.orgChild4Id = null; + posMasters.forEach(async (posMaster: any) => { + posMaster.orgRootId = null; + posMaster.orgChild1Id = null; + posMaster.orgChild2Id = null; + posMaster.orgChild3Id = null; + posMaster.orgChild4Id = null; - if (requestBody.type == 0) { - const org = await this.orgRootRepository.findOne({ - where: { id: requestBody.id }, - }); - if (org != null) { - posMaster.orgRootId = org.id; - posMaster.orgRevisionId = org.orgRevisionId; - } + if (requestBody.type == 0) { + const org = await this.orgRootRepository.findOne({ + where: { id: requestBody.id }, + }); + if (org != null) { + posMaster.orgRootId = org.id; + posMaster.orgRevisionId = org.orgRevisionId; } - if (requestBody.type == 1) { - const org = await this.child1Repository.findOne({ - where: { id: requestBody.id }, - }); - if (org != null) { - posMaster.orgRootId = org.orgRootId; - posMaster.orgChild1Id = org.id; - posMaster.orgRevisionId = org.orgRevisionId; - } + } + if (requestBody.type == 1) { + const org = await this.child1Repository.findOne({ + where: { id: requestBody.id }, + }); + if (org != null) { + posMaster.orgRootId = org.orgRootId; + posMaster.orgChild1Id = org.id; + posMaster.orgRevisionId = org.orgRevisionId; } - if (requestBody.type == 2) { - const org = await this.child2Repository.findOne({ - where: { id: requestBody.id }, - }); - if (org != null) { - posMaster.orgRootId = org.orgRootId; - posMaster.orgChild1Id = org.orgChild1Id; - posMaster.orgChild2Id = org.id; - posMaster.orgRevisionId = org.orgRevisionId; - } + } + if (requestBody.type == 2) { + const org = await this.child2Repository.findOne({ + where: { id: requestBody.id }, + }); + if (org != null) { + posMaster.orgRootId = org.orgRootId; + posMaster.orgChild1Id = org.orgChild1Id; + posMaster.orgChild2Id = org.id; + posMaster.orgRevisionId = org.orgRevisionId; } - if (requestBody.type == 3) { - const org = await this.child3Repository.findOne({ - where: { id: requestBody.id }, - }); - if (org != null) { - posMaster.orgRootId = org.orgRootId; - posMaster.orgChild1Id = org.orgChild1Id; - posMaster.orgChild2Id = org.orgChild2Id; - posMaster.orgChild3Id = org.id; - posMaster.orgRevisionId = org.orgRevisionId; - } + } + if (requestBody.type == 3) { + const org = await this.child3Repository.findOne({ + where: { id: requestBody.id }, + }); + if (org != null) { + posMaster.orgRootId = org.orgRootId; + posMaster.orgChild1Id = org.orgChild1Id; + posMaster.orgChild2Id = org.orgChild2Id; + posMaster.orgChild3Id = org.id; + posMaster.orgRevisionId = org.orgRevisionId; } - if (requestBody.type == 4) { - const org = await this.child4Repository.findOne({ - where: { id: requestBody.id }, - }); - if (org != null) { - posMaster.orgRootId = org.orgRootId; - posMaster.orgChild1Id = org.orgChild1Id; - posMaster.orgChild2Id = org.orgChild2Id; - posMaster.orgChild3Id = org.orgChild3Id; - posMaster.orgChild4Id = org.id; - posMaster.orgRevisionId = org.orgRevisionId; - } + } + if (requestBody.type == 4) { + const org = await this.child4Repository.findOne({ + where: { id: requestBody.id }, + }); + if (org != null) { + posMaster.orgRootId = org.orgRootId; + posMaster.orgChild1Id = org.orgChild1Id; + posMaster.orgChild2Id = org.orgChild2Id; + posMaster.orgChild3Id = org.orgChild3Id; + posMaster.orgChild4Id = org.id; + posMaster.orgRevisionId = org.orgRevisionId; } - posMaster.createdUserId = request.user.sub; - posMaster.createdFullName = request.user.name; - posMaster.lastUpdateUserId = request.user.sub; - posMaster.lastUpdateFullName = request.user.name; - await this.posMasterRepository.save(posMaster); - }); - return new HttpSuccess(); - } catch (error) { - return error; - } + } + posMaster.createdUserId = request.user.sub; + posMaster.createdFullName = request.user.name; + posMaster.lastUpdateUserId = request.user.sub; + posMaster.lastUpdateFullName = request.user.name; + await this.posMasterRepository.save(posMaster); + }); + return new HttpSuccess(); } /** @@ -1553,537 +1515,533 @@ export class PositionController extends Controller { */ @Post("summary") async PositionSummary(@Body() requestBody: { id: string; type: number; isNode: boolean }) { - try { - let summary: any; - let totalPosition: any; - let totalPositionCurrentUse: any; - let totalPositionCurrentVacant: any; - let totalPositionNextUse: any; - let totalPositionNextVacant: any; + let summary: any; + let totalPosition: any; + let totalPositionCurrentUse: any; + let totalPositionCurrentVacant: any; + let totalPositionNextUse: any; + let totalPositionNextVacant: any; - if (requestBody.isNode === true) { - 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({ - where: { orgRootId: requestBody.id }, - }); - totalPositionCurrentUse = await this.posMasterRepository.count({ - where: { - orgRootId: requestBody.id, - current_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ - where: { - orgRootId: requestBody.id, - current_holderId: IsNull() || "", - }, - }); - totalPositionNextUse = await this.posMasterRepository.count({ - where: { - orgRootId: requestBody.id, - next_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionNextVacant = await this.posMasterRepository.count({ - where: { - orgRootId: requestBody.id, - next_holderId: IsNull() || "", - }, - }); - break; - } - case 1: { - // 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({ - where: { - orgChild1Id: requestBody.id, - current_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ - where: { - orgChild1Id: requestBody.id, - current_holderId: IsNull() || "", - }, - }); - totalPositionNextUse = await this.posMasterRepository.count({ - where: { - orgChild1Id: requestBody.id, - next_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionNextVacant = await this.posMasterRepository.count({ - where: { - orgChild1Id: requestBody.id, - next_holderId: IsNull() || "", - }, - }); - break; - } - case 2: { - // 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({ - where: { - orgChild2Id: requestBody.id, - current_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ - where: { - orgChild2Id: requestBody.id, - current_holderId: IsNull() || "", - }, - }); - totalPositionNextUse = await this.posMasterRepository.count({ - where: { - orgChild2Id: requestBody.id, - next_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionNextVacant = await this.posMasterRepository.count({ - where: { - orgChild2Id: requestBody.id, - next_holderId: IsNull() || "", - }, - }); - break; - } - case 3: { - // 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({ - where: { - orgChild3Id: requestBody.id, - current_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ - where: { - orgChild3Id: requestBody.id, - current_holderId: IsNull() || "", - }, - }); - totalPositionNextUse = await this.posMasterRepository.count({ - where: { - orgChild3Id: requestBody.id, - next_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionNextVacant = await this.posMasterRepository.count({ - where: { - orgChild3Id: requestBody.id, - next_holderId: IsNull() || "", - }, - }); - break; - } - case 4: { - // 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({ - where: { - orgChild4Id: requestBody.id, - current_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ - where: { - orgChild4Id: requestBody.id, - current_holderId: IsNull() || "", - }, - }); - totalPositionNextUse = await this.posMasterRepository.count({ - where: { - orgChild4Id: requestBody.id, - next_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionNextVacant = await this.posMasterRepository.count({ - where: { - orgChild4Id: requestBody.id, - next_holderId: IsNull() || "", - }, - }); - break; - } - default: - break; + if (requestBody.isNode === true) { + 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({ + where: { orgRootId: requestBody.id }, + }); + totalPositionCurrentUse = await this.posMasterRepository.count({ + where: { + orgRootId: requestBody.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgRootId: requestBody.id, + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgRootId: requestBody.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgRootId: requestBody.id, + next_holderId: IsNull() || "", + }, + }); + break; } - } else { - 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({ - where: { - orgRootId: requestBody.id, - orgChild1Id: IsNull() || "", - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - }, - }); - totalPositionCurrentUse = await this.posMasterRepository.count({ - where: { - orgRootId: requestBody.id, - orgChild1Id: IsNull() || "", - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ - where: { - orgRootId: requestBody.id, - orgChild1Id: IsNull() || "", - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: IsNull() || "", - }, - }); - totalPositionNextUse = await this.posMasterRepository.count({ - where: { - orgRootId: requestBody.id, - orgChild1Id: IsNull() || "", - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionNextVacant = await this.posMasterRepository.count({ - where: { - orgRootId: requestBody.id, - orgChild1Id: IsNull() || "", - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: IsNull() || "", - }, - }); - break; - } - case 1: { - // 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: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: requestBody.id, - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - }, - }); - totalPositionCurrentUse = await this.posMasterRepository.count({ - where: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: requestBody.id, - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ - where: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: requestBody.id, - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: IsNull() || "", - }, - }); - totalPositionNextUse = await this.posMasterRepository.count({ - where: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: requestBody.id, - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionNextVacant = await this.posMasterRepository.count({ - where: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: requestBody.id, - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: IsNull() || "", - }, - }); - break; - } - case 2: { - // 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: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: Not(IsNull()) || Not(""), - orgChild2Id: requestBody.id, - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - }, - }); - totalPositionCurrentUse = await this.posMasterRepository.count({ - where: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: Not(IsNull()) || Not(""), - orgChild2Id: requestBody.id, - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ - where: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: Not(IsNull()) || Not(""), - orgChild2Id: requestBody.id, - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: IsNull() || "", - }, - }); - totalPositionNextUse = await this.posMasterRepository.count({ - where: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: Not(IsNull()) || Not(""), - orgChild2Id: requestBody.id, - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionNextVacant = await this.posMasterRepository.count({ - where: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: Not(IsNull()) || Not(""), - orgChild2Id: requestBody.id, - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: IsNull() || "", - }, - }); - break; - } - case 3: { - // 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: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: Not(IsNull()) || Not(""), - orgChild2Id: Not(IsNull()) || Not(""), - orgChild3Id: requestBody.id, - orgChild4Id: IsNull() || "", - }, - }); - totalPositionCurrentUse = await this.posMasterRepository.count({ - where: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: Not(IsNull()) || Not(""), - orgChild2Id: Not(IsNull()) || Not(""), - orgChild3Id: requestBody.id, - orgChild4Id: IsNull() || "", - current_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ - where: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: Not(IsNull()) || Not(""), - orgChild2Id: Not(IsNull()) || Not(""), - orgChild3Id: requestBody.id, - orgChild4Id: IsNull() || "", - current_holderId: IsNull() || "", - }, - }); - totalPositionNextUse = await this.posMasterRepository.count({ - where: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: Not(IsNull()) || Not(""), - orgChild2Id: Not(IsNull()) || Not(""), - orgChild3Id: requestBody.id, - orgChild4Id: IsNull() || "", - next_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionNextVacant = await this.posMasterRepository.count({ - where: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: Not(IsNull()) || Not(""), - orgChild2Id: Not(IsNull()) || Not(""), - orgChild3Id: requestBody.id, - orgChild4Id: IsNull() || "", - next_holderId: IsNull() || "", - }, - }); - break; - } - case 4: { - // 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: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: Not(IsNull()) || Not(""), - orgChild2Id: Not(IsNull()) || Not(""), - orgChild3Id: Not(IsNull()) || Not(""), - orgChild4Id: requestBody.id, - }, - }); - totalPositionCurrentUse = await this.posMasterRepository.count({ - where: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: Not(IsNull()) || Not(""), - orgChild2Id: Not(IsNull()) || Not(""), - orgChild3Id: Not(IsNull()) || Not(""), - orgChild4Id: requestBody.id, - current_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ - where: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: Not(IsNull()) || Not(""), - orgChild2Id: Not(IsNull()) || Not(""), - orgChild3Id: Not(IsNull()) || Not(""), - orgChild4Id: requestBody.id, - current_holderId: IsNull() || "", - }, - }); - totalPositionNextUse = await this.posMasterRepository.count({ - where: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: Not(IsNull()) || Not(""), - orgChild2Id: Not(IsNull()) || Not(""), - orgChild3Id: Not(IsNull()) || Not(""), - orgChild4Id: requestBody.id, - next_holderId: Not(IsNull()) || Not(""), - }, - }); - totalPositionNextVacant = await this.posMasterRepository.count({ - where: { - orgRootId: Not(IsNull()) || Not(""), - orgChild1Id: Not(IsNull()) || Not(""), - orgChild2Id: Not(IsNull()) || Not(""), - orgChild3Id: Not(IsNull()) || Not(""), - orgChild4Id: requestBody.id, - next_holderId: IsNull() || "", - }, - }); - break; - } - default: - break; + case 1: { + // 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({ + where: { + orgChild1Id: requestBody.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgChild1Id: requestBody.id, + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgChild1Id: requestBody.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgChild1Id: requestBody.id, + next_holderId: IsNull() || "", + }, + }); + break; } + case 2: { + // 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({ + where: { + orgChild2Id: requestBody.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgChild2Id: requestBody.id, + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgChild2Id: requestBody.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgChild2Id: requestBody.id, + next_holderId: IsNull() || "", + }, + }); + break; + } + case 3: { + // 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({ + where: { + orgChild3Id: requestBody.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgChild3Id: requestBody.id, + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgChild3Id: requestBody.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgChild3Id: requestBody.id, + next_holderId: IsNull() || "", + }, + }); + break; + } + case 4: { + // 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({ + where: { + orgChild4Id: requestBody.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgChild4Id: requestBody.id, + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgChild4Id: requestBody.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgChild4Id: requestBody.id, + next_holderId: IsNull() || "", + }, + }); + break; + } + default: + break; + } + } else { + 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({ + where: { + orgRootId: requestBody.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + }, + }); + totalPositionCurrentUse = await this.posMasterRepository.count({ + where: { + orgRootId: requestBody.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgRootId: requestBody.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgRootId: requestBody.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgRootId: requestBody.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: IsNull() || "", + }, + }); + break; + } + case 1: { + // 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: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: requestBody.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + }, + }); + totalPositionCurrentUse = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: requestBody.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: requestBody.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: requestBody.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: requestBody.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: IsNull() || "", + }, + }); + break; + } + case 2: { + // 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: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: requestBody.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + }, + }); + totalPositionCurrentUse = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: requestBody.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: requestBody.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: requestBody.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: requestBody.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: IsNull() || "", + }, + }); + break; + } + case 3: { + // 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: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: requestBody.id, + orgChild4Id: IsNull() || "", + }, + }); + totalPositionCurrentUse = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: requestBody.id, + orgChild4Id: IsNull() || "", + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: requestBody.id, + orgChild4Id: IsNull() || "", + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: requestBody.id, + orgChild4Id: IsNull() || "", + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: requestBody.id, + orgChild4Id: IsNull() || "", + next_holderId: IsNull() || "", + }, + }); + break; + } + case 4: { + // 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: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: Not(IsNull()) || Not(""), + orgChild4Id: requestBody.id, + }, + }); + totalPositionCurrentUse = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: Not(IsNull()) || Not(""), + orgChild4Id: requestBody.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: Not(IsNull()) || Not(""), + orgChild4Id: requestBody.id, + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: Not(IsNull()) || Not(""), + orgChild4Id: requestBody.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: Not(IsNull()) || Not(""), + orgChild4Id: requestBody.id, + next_holderId: IsNull() || "", + }, + }); + break; + } + default: + break; } - - summary = { - totalPosition: totalPosition, - totalPositionCurrentUse: totalPositionCurrentUse, - totalPositionCurrentVacant: totalPositionCurrentVacant, - totalPositionNextUse: totalPositionNextUse, - totalPositionNextVacant: totalPositionNextVacant, - }; - return new HttpSuccess(summary); - } catch (error) { - return error; } + + summary = { + totalPosition: totalPosition, + totalPositionCurrentUse: totalPositionCurrentUse, + totalPositionCurrentVacant: totalPositionCurrentVacant, + totalPositionNextUse: totalPositionNextUse, + totalPositionNextVacant: totalPositionNextVacant, + }; + return new HttpSuccess(summary); } /** * API สร้างคนครองตำแหน่ง @@ -2105,24 +2063,20 @@ export class PositionController extends Controller { "ไม่พบข้อมูลไอดีนี้ : " + requestBody.posMaster, ); } - try { - dataMaster.positions.forEach(async (position) => { - if (position.id === requestBody.position) { - position.positionIsSelected = true; - } else { - position.positionIsSelected = false; - } - await this.positionRepository.save(position); - }); + dataMaster.positions.forEach(async (position) => { + if (position.id === requestBody.position) { + position.positionIsSelected = true; + } else { + position.positionIsSelected = false; + } + await this.positionRepository.save(position); + }); - dataMaster.isSit = requestBody.isSit; - dataMaster.next_holderId = requestBody.profileId; - await this.posMasterRepository.save(dataMaster); + dataMaster.isSit = requestBody.isSit; + dataMaster.next_holderId = requestBody.profileId; + await this.posMasterRepository.save(dataMaster); - return new HttpSuccess(); - } catch (error) { - return error; - } + return new HttpSuccess(); } /** @@ -2141,22 +2095,18 @@ export class PositionController extends Controller { if (!dataMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); } - try { - await this.posMasterRepository.update(id, { - isSit: false, - next_holderId: null, - }); + await this.posMasterRepository.update(id, { + isSit: false, + next_holderId: null, + }); - dataMaster.positions.forEach(async (position) => { - await this.positionRepository.update(position.id, { - positionIsSelected: false, - }); + dataMaster.positions.forEach(async (position) => { + await this.positionRepository.update(position.id, { + positionIsSelected: false, }); + }); - return new HttpSuccess(); - } catch (error) { - return error; - } + return new HttpSuccess(); } /** @@ -2200,21 +2150,17 @@ export class PositionController extends Controller { ); } - try { - await this.posMasterRepository.update( - { orgRevisionId: findDraft.id, ancestorDNA: dataPublish.ancestorDNA }, - { ancestorDNA: "" }, - ); - if (dataPublish.ancestorDNA == null || dataPublish.ancestorDNA == "") - dataPublish.ancestorDNA = dataPublish.id; - dataDraft.ancestorDNA = dataPublish.ancestorDNA; - await this.posMasterRepository.save(dataDraft); - await this.posMasterRepository.save(dataPublish); + await this.posMasterRepository.update( + { orgRevisionId: findDraft.id, ancestorDNA: dataPublish.ancestorDNA }, + { ancestorDNA: "" }, + ); + if (dataPublish.ancestorDNA == null || dataPublish.ancestorDNA == "") + dataPublish.ancestorDNA = dataPublish.id; + dataDraft.ancestorDNA = dataPublish.ancestorDNA; + await this.posMasterRepository.save(dataDraft); + await this.posMasterRepository.save(dataPublish); - return new HttpSuccess(); - } catch (error) { - return error; - } + return new HttpSuccess(); } /**