From a03ce9d55a97643dc1c97dcf4727efbea74730f7 Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Mon, 17 Mar 2025 09:45:56 +0700 Subject: [PATCH 1/3] =?UTF-8?q?=E0=B9=80=E0=B8=9C=E0=B8=A2=E0=B9=81?= =?UTF-8?q?=E0=B8=9E=E0=B8=A3=E0=B9=88=E0=B9=82=E0=B8=84=E0=B8=A3=E0=B8=87?= =?UTF-8?q?=E0=B8=AA=E0=B8=A3=E0=B9=89=E0=B8=B2=E0=B8=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/OrganizationController.ts | 1458 ++++++--------------- 1 file changed, 391 insertions(+), 1067 deletions(-) diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index d9c896df..b64cbe91 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -166,6 +166,7 @@ export class OrganizationController extends Controller { //หา dna tree ถ้าไม่มีให้เอาตัวเองเป็น dna const orgRoot = await this.orgRootRepository.find({ where: { orgRevisionId: requestBody.orgRevisionId }, + order:{orgRootOrder:"ASC"} }); let _orgRoot: any = orgRoot.map((x) => ({ ...x, @@ -178,6 +179,7 @@ export class OrganizationController extends Controller { const orgChild1 = await this.child1Repository.find({ where: { orgRevisionId: requestBody.orgRevisionId }, + order:{orgChild1Order:"ASC"} }); let _orgChild1: any = orgChild1.map((x) => ({ ...x, @@ -190,6 +192,7 @@ export class OrganizationController extends Controller { const orgChild2 = await this.child2Repository.find({ where: { orgRevisionId: requestBody.orgRevisionId }, + order:{orgChild2Order:"ASC"} }); let _orgChild2: any = orgChild2.map((x) => ({ ...x, @@ -202,6 +205,7 @@ export class OrganizationController extends Controller { const orgChild3 = await this.child3Repository.find({ where: { orgRevisionId: requestBody.orgRevisionId }, + order:{orgChild3Order:"ASC"} }); let _orgChild3: any = orgChild3.map((x) => ({ ...x, @@ -214,6 +218,7 @@ export class OrganizationController extends Controller { const orgChild4 = await this.child4Repository.find({ where: { orgRevisionId: requestBody.orgRevisionId }, + order:{orgChild4Order:"ASC"} }); let _orgChild4: any = orgChild4.map((x) => ({ ...x, @@ -230,73 +235,33 @@ export class OrganizationController extends Controller { relations: ["positions", "posMasterAssigns"], }); - let _orgPosMaster: PosMaster[]; + let _orgPosMaster: PosMaster[] = []; if ( requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" ) { - _orgPosMaster = orgPosMaster.map((x) => ({ - ...x, - ancestorDNA: - x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" - ? x.id - : x.ancestorDNA, - })); + _orgPosMaster = orgPosMaster + .filter( + (x) => x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000", + ) + .map((x) => ({ + ...x, + ancestorDNA: + x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" + ? x.id + : x.ancestorDNA, + })); await this.posMasterRepository.save(_orgPosMaster); } - - // //หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna - // const orgemployeePosMaster = await this.employeePosMasterRepository.find({ - // where: { orgRevisionId: requestBody.orgRevisionId }, - // relations: ["positions"], - // }); - - // let _orgemployeePosMaster: EmployeePosMaster[]; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // _orgemployeePosMaster = orgemployeePosMaster.map((x) => ({ - // ...x, - // ancestorDNA: - // x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" - // ? x.id - // : x.ancestorDNA, - // })); - // await this.employeePosMasterRepository.save(_orgemployeePosMaster); - // } - // //หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna - // const orgemployeeTempPosMaster = await this.employeeTempPosMasterRepository.find({ - // where: { orgRevisionId: requestBody.orgRevisionId }, - // relations: ["positions"], - // }); - - // let _orgemployeeTempPosMaster: EmployeeTempPosMaster[]; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // _orgemployeeTempPosMaster = orgemployeeTempPosMaster.map((x) => ({ - // ...x, - // ancestorDNA: - // x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" - // ? x.id - // : x.ancestorDNA, - // })); - // await this.employeeTempPosMasterRepository.save(_orgemployeeTempPosMaster); - // } - - //create org - _orgRoot.forEach(async (x: any) => { - var dataId = x.id; - delete x.id; - const data = Object.assign(new OrgRoot(), x); + // Create org + for await (const x0 of _orgRoot) { + var dataId = x0.id; + console.log(`ch0 ${x0.orgRootOrder}`) + console.log(dataId) + delete x0.id; + const data = Object.assign(new OrgRoot(), x0); data.orgRevisionId = revision.id; data.createdUserId = request.user.sub; data.createdFullName = request.user.name; @@ -305,32 +270,220 @@ export class OrganizationController extends Controller { data.lastUpdateFullName = request.user.name; data.lastUpdatedAt = new Date(); await this.orgRootRepository.save(data); - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - ) { - //create posmaster - await Promise.all( - _orgPosMaster - .filter((x: PosMaster) => x.orgRootId == dataId && x.orgChild1Id == null) - .map(async (item: any) => { + + const orgTypes = new Set([ + "ORG_POSITION", + "ORG_POSITION_PERSON", + "ORG_POSITION_ROLE", + "ORG_POSITION_PERSON_ROLE", + ]); + + if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { + // Create posMaster + for await (const item of orgPosMaster.filter( + (x: PosMaster) => x.orgRootId == dataId && x.orgChild1Id == null, + ) as any) { + delete item.id; + const posMaster: any = Object.assign(new PosMaster(), item); + posMaster.positions = []; + + if ( + ["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + posMaster.next_holderId = item.current_holderId; + } else { + posMaster.next_holderId = null; + posMaster.isSit = false; + } + + if ( + ["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + posMaster.authRoleId = item.authRoleId; + } else { + posMaster.authRoleId = null; + } + + posMaster.current_holderId = null; + posMaster.orgRevisionId = revision.id; + posMaster.orgRootId = data.id; + posMaster.createdUserId = request.user.sub; + posMaster.createdFullName = request.user.name; + posMaster.createdAt = new Date(); + posMaster.lastUpdateUserId = request.user.sub; + posMaster.lastUpdateFullName = request.user.name; + posMaster.lastUpdatedAt = new Date(); + await this.posMasterRepository.save(posMaster); + + // Copy assignments + item.posMasterAssigns = item.posMasterAssigns.map( + ({ id, ...rest }: PosMasterAssign) => ({ + ...rest, + posMasterId: posMaster.id, + }), + ); + posMaster.posMasterAssigns = item.posMasterAssigns; + + // Create positions + for await (const pos of item.positions) { + delete pos.id; + const position = Object.assign(new Position(), pos); + position.posMasterId = posMaster.id; + + if ( + ["ORG_POSITION", "ORG_POSITION_ROLE"].includes(requestBody.typeDraft.toUpperCase()) + ) { + position.positionIsSelected = false; + } + + position.createdUserId = request.user.sub; + position.createdFullName = request.user.name; + position.createdAt = new Date(); + position.lastUpdateUserId = request.user.sub; + position.lastUpdateFullName = request.user.name; + position.lastUpdatedAt = new Date(); + await this.positionRepository.save(position); + } + } + } + + // Create orgChild1 + for await (const x1 of _orgChild1.filter((x: OrgChild1) => x.orgRootId == dataId)) { + var data1Id = x1.id; + console.log(`ch1 ${x1.orgChild1Order}`) + console.log(data1Id) + delete x1.id; + const data1 = Object.assign(new OrgChild1(), x1); + data1.orgRootId = data.id; + data1.orgRevisionId = revision.id; + data1.createdUserId = request.user.sub; + data1.createdFullName = request.user.name; + data1.createdAt = new Date(); + data1.lastUpdateUserId = request.user.sub; + data1.lastUpdateFullName = request.user.name; + data1.lastUpdatedAt = new Date(); + await this.child1Repository.save(data1); + + if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { + // Create posMaster + for await (const item of orgPosMaster.filter( + (x: PosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null, + ) as any) { + delete item.id; + const posMaster = Object.assign(new PosMaster(), item); + posMaster.positions = []; + + if ( + ["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + posMaster.next_holderId = item.current_holderId; + } else { + posMaster.next_holderId = null; + posMaster.isSit = false; + } + + if ( + ["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + posMaster.authRoleId = item.authRoleId; + } else { + posMaster.authRoleId = null; + } + + posMaster.current_holderId = null; + posMaster.orgRevisionId = revision.id; + posMaster.orgRootId = data.id; + posMaster.orgChild1Id = data1.id; + posMaster.createdUserId = request.user.sub; + posMaster.createdFullName = request.user.name; + posMaster.createdAt = new Date(); + posMaster.lastUpdateUserId = request.user.sub; + posMaster.lastUpdateFullName = request.user.name; + posMaster.lastUpdatedAt = new Date(); + await this.posMasterRepository.save(posMaster); + + // Copy assignments + item.posMasterAssigns = item.posMasterAssigns.map( + ({ id, ...rest }: PosMasterAssign) => ({ + ...rest, + posMasterId: posMaster.id, + }), + ); + posMaster.posMasterAssigns = item.posMasterAssigns; + + // Create positions + for await (const pos of item.positions) { + delete pos.id; + const position = Object.assign(new Position(), pos); + position.posMasterId = posMaster.id; + + if ( + ["ORG_POSITION", "ORG_POSITION_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + position.positionIsSelected = false; + } + + position.createdUserId = request.user.sub; + position.createdFullName = request.user.name; + position.createdAt = new Date(); + position.lastUpdateUserId = request.user.sub; + position.lastUpdateFullName = request.user.name; + position.lastUpdatedAt = new Date(); + await this.positionRepository.save(position); + } + } + } + for await (const x2 of _orgChild2.filter((x: OrgChild2) => x.orgChild1Id == data1Id)) { + var data2Id = x2.id; + console.log(`ch2 ${x2.orgChild2Order}`) + console.log(data2Id) + delete x2.id; + const data2 = Object.assign(new OrgChild2(), x2); + data2.orgChild1Id = data1.id; + data2.orgRootId = data.id; + data2.orgRevisionId = revision.id; + data2.createdUserId = request.user.sub; + data2.createdFullName = request.user.name; + data2.createdAt = new Date(); + data2.lastUpdateUserId = request.user.sub; + data2.lastUpdateFullName = request.user.name; + data2.lastUpdatedAt = new Date(); + await this.child2Repository.save(data2); + + if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { + // Create posMaster + for await (const item of orgPosMaster.filter( + (x: PosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null, + ) as any) { delete item.id; const posMaster = Object.assign(new PosMaster(), item); posMaster.positions = []; + if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + ["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) ) { posMaster.next_holderId = item.current_holderId; } else { posMaster.next_holderId = null; posMaster.isSit = false; } + if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + ["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) ) { posMaster.authRoleId = item.authRoleId; } else { @@ -340,6 +493,8 @@ export class OrganizationController extends Controller { posMaster.current_holderId = null; posMaster.orgRevisionId = revision.id; posMaster.orgRootId = data.id; + posMaster.orgChild1Id = data1.id; + posMaster.orgChild2Id = data2.id; posMaster.createdUserId = request.user.sub; posMaster.createdFullName = request.user.name; posMaster.createdAt = new Date(); @@ -348,20 +503,23 @@ export class OrganizationController extends Controller { posMaster.lastUpdatedAt = new Date(); await this.posMasterRepository.save(posMaster); - //copy มอบหมาย - item.posMasterAssigns = item.posMasterAssigns.map( - ({ id, ...rest }: PosMasterAssign) => ({ rest, posMasterId: posMaster.id }), + // Copy assignments + posMaster.posMasterAssigns = item.posMasterAssigns.map( + ({ id, ...rest }: PosMasterAssign) => ({ + ...rest, + posMasterId: posMaster.id, + }), ); - posMaster.posMasterAssigns = item.posMasterAssigns; - //create position - item.positions.map(async (pos: any) => { + // Create positions + for await (const pos of item.positions) { delete pos.id; const position = Object.assign(new Position(), pos); position.posMasterId = posMaster.id; if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" + ["ORG_POSITION", "ORG_POSITION_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) ) { position.positionIsSelected = false; } @@ -372,182 +530,162 @@ export class OrganizationController extends Controller { position.lastUpdateFullName = request.user.name; position.lastUpdatedAt = new Date(); await this.positionRepository.save(position); - }); - }), - ); - // //create employeePosmaster - // await Promise.all( - // _orgemployeePosMaster - // .filter((x: EmployeePosMaster) => x.orgRootId == dataId && x.orgChild1Id == null) - // .map(async (item: any) => { - // delete item.id; - // const employeePosMaster = Object.assign(new EmployeePosMaster(), item); - // employeePosMaster.positions = []; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeePosMaster.current_holderId = item.current_holderId; - // } else { - // // employeePosMaster.next_holderId = null; - // employeePosMaster.isSit = false; - // } - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeePosMaster.authRoleId = item.authRoleId; - // } else { - // employeePosMaster.authRoleId = null; - // } - // // employeePosMaster.current_holderId = null; - // employeePosMaster.orgRevisionId = revision.id; - // employeePosMaster.orgRootId = data.id; - // employeePosMaster.createdUserId = request.user.sub; - // employeePosMaster.createdFullName = request.user.name; - // employeePosMaster.createdAt = new Date(); - // employeePosMaster.lastUpdateUserId = request.user.sub; - // employeePosMaster.lastUpdateFullName = request.user.name; - // employeePosMaster.lastUpdatedAt = new Date(); - // await this.employeePosMasterRepository.save(employeePosMaster); + } + } + } - // //create employeePosition - // item.positions.map(async (pos: any) => { - // delete pos.id; - // const employeePosition: EmployeePosition = Object.assign( - // new EmployeePosition(), - // pos, - // ); - // employeePosition.posMasterId = employeePosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - // employeePosition.createdUserId = request.user.sub; - // employeePosition.createdFullName = request.user.name; - // employeePosition.createdAt = new Date(); - // employeePosition.lastUpdateUserId = request.user.sub; - // employeePosition.lastUpdateFullName = request.user.name; - // employeePosition.lastUpdatedAt = new Date(); - // await this.employeePositionRepository.save(employeePosition); - // }); - // }), - // ); - // //create employeeTempPosmaster - // await Promise.all( - // _orgemployeeTempPosMaster - // .filter((x: EmployeeTempPosMaster) => x.orgRootId == dataId && x.orgChild1Id == null) - // .map(async (item: any) => { - // delete item.id; - // const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item); - // employeeTempPosMaster.positions = []; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeeTempPosMaster.current_holderId = item.current_holderId; - // } else { - // // employeeTempPosMaster.next_holderId = null; - // employeeTempPosMaster.isSit = false; - // } - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeeTempPosMaster.authRoleId = item.authRoleId; - // } else { - // employeeTempPosMaster.authRoleId = null; - // } - // // employeeTempPosMaster.current_holderId = null; - // employeeTempPosMaster.orgRevisionId = revision.id; - // employeeTempPosMaster.orgRootId = data.id; - // employeeTempPosMaster.createdUserId = request.user.sub; - // employeeTempPosMaster.createdFullName = request.user.name; - // employeeTempPosMaster.createdAt = new Date(); - // employeeTempPosMaster.lastUpdateUserId = request.user.sub; - // employeeTempPosMaster.lastUpdateFullName = request.user.name; - // employeeTempPosMaster.lastUpdatedAt = new Date(); - // await this.employeeTempPosMasterRepository.save(employeeTempPosMaster); + // Create org + for await (const x3 of _orgChild3.filter((x: OrgChild3) => x.orgChild2Id == data2Id)) { + var data3Id = x3.id; + console.log(`ch3 ${x3.orgChild3Order}`) + console.log(data3Id) + delete x3.id; + const data3 = Object.assign(new OrgChild3(), x3); + data3.orgChild2Id = data2.id; + data3.orgChild1Id = data1.id; + data3.orgRootId = data.id; + data3.orgRevisionId = revision.id; + data3.createdUserId = request.user.sub; + data3.createdFullName = request.user.name; + data3.createdAt = new Date(); + data3.lastUpdateUserId = request.user.sub; + data3.lastUpdateFullName = request.user.name; + data3.lastUpdatedAt = new Date(); + await this.child3Repository.save(data3); - // //create employeePosition - // item.positions.map(async (pos: any) => { - // delete pos.id; - // const employeePosition: EmployeePosition = Object.assign( - // new EmployeePosition(), - // pos, - // ); - // employeePosition.posMasterTempId = employeeTempPosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - // employeePosition.createdUserId = request.user.sub; - // employeePosition.createdFullName = request.user.name; - // employeePosition.createdAt = new Date(); - // employeePosition.lastUpdateUserId = request.user.sub; - // employeePosition.lastUpdateFullName = request.user.name; - // employeePosition.lastUpdatedAt = new Date(); - // await this.employeePositionRepository.save(employeePosition); - // }); - // }), - // ); - } + if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { + // Create posMaster + for await (const item of orgPosMaster.filter( + (x: PosMaster) => x.orgChild3Id == data3Id && x.orgChild4Id == null, + ) as any) { + delete item.id; + const posMaster = Object.assign(new PosMaster(), item); + posMaster.positions = []; - //create org - _orgChild1 - .filter((x: OrgChild1) => x.orgRootId == dataId) - .forEach(async (x: any) => { - var data1Id = x.id; - delete x.id; - const data1 = Object.assign(new OrgChild1(), x); - data1.orgRootId = data.id; - data1.orgRevisionId = revision.id; - data1.createdUserId = request.user.sub; - data1.createdFullName = request.user.name; - data1.createdAt = new Date(); - data1.lastUpdateUserId = request.user.sub; - data1.lastUpdateFullName = request.user.name; - data1.lastUpdatedAt = new Date(); - await this.child1Repository.save(data1); - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - ) { - //create posmaster - await Promise.all( - _orgPosMaster - .filter((x: PosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null) - .map(async (item: any) => { + if ( + ["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + posMaster.next_holderId = item.current_holderId; + } else { + posMaster.next_holderId = null; + posMaster.isSit = false; + } + + if ( + ["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + posMaster.authRoleId = item.authRoleId; + } else { + posMaster.authRoleId = null; + } + + posMaster.current_holderId = null; + posMaster.orgRevisionId = revision.id; + posMaster.orgRootId = data.id; + posMaster.orgChild1Id = data1.id; + posMaster.orgChild2Id = data2.id; + posMaster.orgChild3Id = data3.id; + posMaster.createdUserId = request.user.sub; + posMaster.createdFullName = request.user.name; + posMaster.createdAt = new Date(); + posMaster.lastUpdateUserId = request.user.sub; + posMaster.lastUpdateFullName = request.user.name; + posMaster.lastUpdatedAt = new Date(); + await this.posMasterRepository.save(posMaster); + + // Copy assignments + posMaster.posMasterAssigns = item.posMasterAssigns.map( + ({ id, ...rest }: PosMasterAssign) => ({ + ...rest, + posMasterId: posMaster.id, + }), + ); + + // Create positions + for await (const pos of item.positions) { + delete pos.id; + const position = Object.assign(new Position(), pos); + position.posMasterId = posMaster.id; + if ( + ["ORG_POSITION", "ORG_POSITION_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + position.positionIsSelected = false; + } + position.createdUserId = request.user.sub; + position.createdFullName = request.user.name; + position.createdAt = new Date(); + position.lastUpdateUserId = request.user.sub; + position.lastUpdateFullName = request.user.name; + position.lastUpdatedAt = new Date(); + await this.positionRepository.save(position); + } + } + } + // Create org + for await (const x4 of _orgChild4.filter( + (x: OrgChild4) => x.orgChild3Id == data3Id, + )) { + var data4Id = x4.id; + console.log(`ch4 ${x4.orgChild4Order}`) + console.log(data4Id) + delete x4.id; + const data4 = Object.assign(new OrgChild4(), x4); + data4.orgChild3Id = data3.id; + data4.orgChild2Id = data2.id; + data4.orgChild1Id = data1.id; + data4.orgRootId = data.id; + data4.orgRevisionId = revision.id; + data4.createdUserId = request.user.sub; + data4.createdFullName = request.user.name; + data4.createdAt = new Date(); + data4.lastUpdateUserId = request.user.sub; + data4.lastUpdateFullName = request.user.name; + data4.lastUpdatedAt = new Date(); + await this.child4Repository.save(data4); + + if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { + // Create posMaster + for await (const item of orgPosMaster.filter( + (x: PosMaster) => x.orgChild4Id == data4Id, + ) as any) { delete item.id; const posMaster = Object.assign(new PosMaster(), item); posMaster.positions = []; + if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + ["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) ) { posMaster.next_holderId = item.current_holderId; } else { posMaster.next_holderId = null; posMaster.isSit = false; } + if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + ["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) ) { posMaster.authRoleId = item.authRoleId; } else { posMaster.authRoleId = null; } + posMaster.current_holderId = null; posMaster.orgRevisionId = revision.id; posMaster.orgRootId = data.id; posMaster.orgChild1Id = data1.id; + posMaster.orgChild2Id = data2.id; + posMaster.orgChild3Id = data3.id; + posMaster.orgChild4Id = data4.id; posMaster.createdUserId = request.user.sub; posMaster.createdFullName = request.user.name; posMaster.createdAt = new Date(); @@ -556,20 +694,23 @@ export class OrganizationController extends Controller { posMaster.lastUpdatedAt = new Date(); await this.posMasterRepository.save(posMaster); - //copy มอบหมาย - item.posMasterAssigns = item.posMasterAssigns.map( - ({ id, ...rest }: PosMasterAssign) => ({ rest, posMasterId: posMaster.id }), + // Copy assignments + posMaster.posMasterAssigns = item.posMasterAssigns.map( + ({ id, ...rest }: PosMasterAssign) => ({ + ...rest, + posMasterId: posMaster.id, + }), ); - posMaster.posMasterAssigns = item.posMasterAssigns; - //create position - item.positions.map(async (pos: any) => { + // Create positions + for await (const pos of item.positions) { delete pos.id; const position = Object.assign(new Position(), pos); position.posMasterId = posMaster.id; if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" + ["ORG_POSITION", "ORG_POSITION_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) ) { position.positionIsSelected = false; } @@ -580,831 +721,14 @@ export class OrganizationController extends Controller { position.lastUpdateFullName = request.user.name; position.lastUpdatedAt = new Date(); await this.positionRepository.save(position); - }); - }), - ); - // //create employeePosmaster - // await Promise.all( - // _orgemployeePosMaster - // .filter( - // (x: EmployeePosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null, - // ) - // .map(async (item: any) => { - // delete item.id; - // const employeePosMaster = Object.assign(new EmployeePosMaster(), item); - // employeePosMaster.positions = []; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeePosMaster.current_holderId = item.current_holderId; - // } else { - // // employeePosMaster.next_holderId = null; - // employeePosMaster.isSit = false; - // } - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeePosMaster.authRoleId = item.authRoleId; - // } else { - // employeePosMaster.authRoleId = null; - // } - // // employeePosMaster.current_holderId = null; - // employeePosMaster.orgRevisionId = revision.id; - // employeePosMaster.orgRootId = data.id; - // employeePosMaster.orgChild1Id = data1.id; - // employeePosMaster.createdUserId = request.user.sub; - // employeePosMaster.createdFullName = request.user.name; - // employeePosMaster.createdAt = new Date(); - // employeePosMaster.lastUpdateUserId = request.user.sub; - // employeePosMaster.lastUpdateFullName = request.user.name; - // employeePosMaster.lastUpdatedAt = new Date(); - // await this.employeePosMasterRepository.save(employeePosMaster); - - // //create employeePosition - // item.positions.map(async (pos: any) => { - // delete pos.id; - // const employeePosition: EmployeePosition = Object.assign( - // new EmployeePosition(), - // pos, - // ); - // employeePosition.posMasterId = employeePosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - // employeePosition.createdUserId = request.user.sub; - // employeePosition.createdFullName = request.user.name; - // employeePosition.createdAt = new Date(); - // employeePosition.lastUpdateUserId = request.user.sub; - // employeePosition.lastUpdateFullName = request.user.name; - // employeePosition.lastUpdatedAt = new Date(); - // await this.employeePositionRepository.save(employeePosition); - // }); - // }), - // ); - // // create employeeTempPosmaster - // await Promise.all( - // _orgemployeeTempPosMaster - // .filter( - // (x: EmployeeTempPosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null, - // ) - // .map(async (item: any) => { - // delete item.id; - // const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item); - // employeeTempPosMaster.positions = []; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeeTempPosMaster.current_holderId = item.current_holderId; - // } else { - // // employeeTempPosMaster.next_holderId = null; - // employeeTempPosMaster.isSit = false; - // } - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeeTempPosMaster.authRoleId = item.authRoleId; - // } else { - // employeeTempPosMaster.authRoleId = null; - // } - // // employeeTempPosMaster.current_holderId = null; - // employeeTempPosMaster.orgRevisionId = revision.id; - // employeeTempPosMaster.orgRootId = data.id; - // employeeTempPosMaster.orgChild1Id = data1.id; - // employeeTempPosMaster.createdUserId = request.user.sub; - // employeeTempPosMaster.createdFullName = request.user.name; - // employeeTempPosMaster.createdAt = new Date(); - // employeeTempPosMaster.lastUpdateUserId = request.user.sub; - // employeeTempPosMaster.lastUpdateFullName = request.user.name; - // employeeTempPosMaster.lastUpdatedAt = new Date(); - // await this.employeeTempPosMasterRepository.save(employeeTempPosMaster); - - // //create employeePosition - // item.positions.map(async (pos: any) => { - // delete pos.id; - // const employeePosition: EmployeePosition = Object.assign( - // new EmployeePosition(), - // pos, - // ); - // employeePosition.posMasterTempId = employeeTempPosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - // employeePosition.createdUserId = request.user.sub; - // employeePosition.createdFullName = request.user.name; - // employeePosition.createdAt = new Date(); - // employeePosition.lastUpdateUserId = request.user.sub; - // employeePosition.lastUpdateFullName = request.user.name; - // employeePosition.lastUpdatedAt = new Date(); - // await this.employeePositionRepository.save(employeePosition); - // }); - // }), - // ); - } - - //create org - _orgChild2 - .filter((x: OrgChild2) => x.orgChild1Id == data1Id) - .forEach(async (x: any) => { - var data2Id = x.id; - delete x.id; - const data2 = Object.assign(new OrgChild2(), x); - data2.orgChild1Id = data1.id; - data2.orgRootId = data.id; - data2.orgRevisionId = revision.id; - data2.createdUserId = request.user.sub; - data2.createdFullName = request.user.name; - data2.createdAt = new Date(); - data2.lastUpdateUserId = request.user.sub; - data2.lastUpdateFullName = request.user.name; - data2.lastUpdatedAt = new Date(); - await this.child2Repository.save(data2); - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - ) { - //create posmaster - await Promise.all( - _orgPosMaster - .filter((x: PosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null) - .map(async (item: any) => { - delete item.id; - const posMaster = Object.assign(new PosMaster(), item); - posMaster.positions = []; - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - ) { - posMaster.next_holderId = item.current_holderId; - } else { - posMaster.next_holderId = null; - posMaster.isSit = false; - } - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - ) { - posMaster.authRoleId = item.authRoleId; - } else { - posMaster.authRoleId = null; - } - posMaster.current_holderId = null; - posMaster.orgRevisionId = revision.id; - posMaster.orgRootId = data.id; - posMaster.orgChild1Id = data1.id; - posMaster.orgChild2Id = data2.id; - posMaster.createdUserId = request.user.sub; - posMaster.createdFullName = request.user.name; - posMaster.createdAt = new Date(); - posMaster.lastUpdateUserId = request.user.sub; - posMaster.lastUpdateFullName = request.user.name; - posMaster.lastUpdatedAt = new Date(); - await this.posMasterRepository.save(posMaster); - - //copy มอบหมาย - item.posMasterAssigns = item.posMasterAssigns.map( - ({ id, ...rest }: PosMasterAssign) => ({ - rest, - posMasterId: posMaster.id, - }), - ); - posMaster.posMasterAssigns = item.posMasterAssigns; - - //create position - item.positions.map(async (pos: any) => { - delete pos.id; - const position = Object.assign(new Position(), pos); - position.posMasterId = posMaster.id; - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - ) { - position.positionIsSelected = false; - } - position.createdUserId = request.user.sub; - position.createdFullName = request.user.name; - position.createdAt = new Date(); - position.lastUpdateUserId = request.user.sub; - position.lastUpdateFullName = request.user.name; - position.lastUpdatedAt = new Date(); - await this.positionRepository.save(position); - }); - }), - ); - // //create employeePosmaster - // await Promise.all( - // _orgemployeePosMaster - // .filter( - // (x: EmployeePosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null, - // ) - // .map(async (item: any) => { - // delete item.id; - // const employeePosMaster = Object.assign(new EmployeePosMaster(), item); - // employeePosMaster.positions = []; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeePosMaster.current_holderId = item.current_holderId; - // } else { - // // employeePosMaster.next_holderId = null; - // employeePosMaster.isSit = false; - // } - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeePosMaster.authRoleId = item.authRoleId; - // } else { - // employeePosMaster.authRoleId = null; - // } - // // employeePosMaster.current_holderId = null; - // employeePosMaster.orgRevisionId = revision.id; - // employeePosMaster.orgRootId = data.id; - // employeePosMaster.orgChild1Id = data1.id; - // employeePosMaster.orgChild2Id = data2.id; - // employeePosMaster.createdUserId = request.user.sub; - // employeePosMaster.createdFullName = request.user.name; - // employeePosMaster.createdAt = new Date(); - // employeePosMaster.lastUpdateUserId = request.user.sub; - // employeePosMaster.lastUpdateFullName = request.user.name; - // employeePosMaster.lastUpdatedAt = new Date(); - // await this.employeePosMasterRepository.save(employeePosMaster); - - // //create employeePosition - // item.positions.map(async (pos: any) => { - // delete pos.id; - // const employeePosition: EmployeePosition = Object.assign( - // new EmployeePosition(), - // pos, - // ); - // employeePosition.posMasterId = employeePosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - // employeePosition.createdUserId = request.user.sub; - // employeePosition.createdFullName = request.user.name; - // employeePosition.createdAt = new Date(); - // employeePosition.lastUpdateUserId = request.user.sub; - // employeePosition.lastUpdateFullName = request.user.name; - // employeePosition.lastUpdatedAt = new Date(); - // await this.employeePositionRepository.save(employeePosition); - // }); - // }), - // ); - // // create employeeTempPosmaster - // await Promise.all( - // _orgemployeeTempPosMaster - // .filter( - // (x: EmployeeTempPosMaster) => - // x.orgChild2Id == data2Id && x.orgChild3Id == null, - // ) - // .map(async (item: any) => { - // delete item.id; - // const employeeTempPosMaster = Object.assign( - // new EmployeeTempPosMaster(), - // item, - // ); - // employeeTempPosMaster.positions = []; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeeTempPosMaster.current_holderId = item.current_holderId; - // } else { - // // employeeTempPosMaster.next_holderId = null; - // employeeTempPosMaster.isSit = false; - // } - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeeTempPosMaster.authRoleId = item.authRoleId; - // } else { - // employeeTempPosMaster.authRoleId = null; - // } - // // employeeTempPosMaster.current_holderId = null; - // employeeTempPosMaster.orgRevisionId = revision.id; - // employeeTempPosMaster.orgRootId = data.id; - // employeeTempPosMaster.orgChild1Id = data1.id; - // employeeTempPosMaster.orgChild2Id = data2.id; - // employeeTempPosMaster.createdUserId = request.user.sub; - // employeeTempPosMaster.createdFullName = request.user.name; - // employeeTempPosMaster.createdAt = new Date(); - // employeeTempPosMaster.lastUpdateUserId = request.user.sub; - // employeeTempPosMaster.lastUpdateFullName = request.user.name; - // employeeTempPosMaster.lastUpdatedAt = new Date(); - // await this.employeeTempPosMasterRepository.save(employeeTempPosMaster); - - // //create employeePosition - // item.positions.map(async (pos: any) => { - // delete pos.id; - // const employeePosition: EmployeePosition = Object.assign( - // new EmployeePosition(), - // pos, - // ); - // employeePosition.posMasterTempId = employeeTempPosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - // employeePosition.createdUserId = request.user.sub; - // employeePosition.createdFullName = request.user.name; - // employeePosition.createdAt = new Date(); - // employeePosition.lastUpdateUserId = request.user.sub; - // employeePosition.lastUpdateFullName = request.user.name; - // employeePosition.lastUpdatedAt = new Date(); - // await this.employeePositionRepository.save(employeePosition); - // }); - // }), - // ); - } - - //create org - _orgChild3 - .filter((x: OrgChild3) => x.orgChild2Id == data2Id) - .forEach(async (x: any) => { - var data3Id = x.id; - delete x.id; - const data3 = Object.assign(new OrgChild3(), x); - data3.orgChild2Id = data2.id; - data3.orgChild1Id = data1.id; - data3.orgRootId = data.id; - data3.orgRevisionId = revision.id; - data3.createdUserId = request.user.sub; - data3.createdFullName = request.user.name; - data3.createdAt = new Date(); - data3.lastUpdateUserId = request.user.sub; - data3.lastUpdateFullName = request.user.name; - data3.lastUpdatedAt = new Date(); - await this.child3Repository.save(data3); - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - ) { - //create posmaster - await Promise.all( - _orgPosMaster - .filter( - (x: PosMaster) => x.orgChild3Id == data3Id && x.orgChild4Id == null, - ) - .map(async (item: any) => { - delete item.id; - const posMaster = Object.assign(new PosMaster(), item); - posMaster.positions = []; - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - ) { - posMaster.next_holderId = item.current_holderId; - } else { - posMaster.next_holderId = null; - posMaster.isSit = false; - } - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - ) { - posMaster.authRoleId = item.authRoleId; - } else { - posMaster.authRoleId = null; - } - posMaster.current_holderId = null; - posMaster.orgRevisionId = revision.id; - posMaster.orgRootId = data.id; - posMaster.orgChild1Id = data1.id; - posMaster.orgChild2Id = data2.id; - posMaster.orgChild3Id = data3.id; - posMaster.createdUserId = request.user.sub; - posMaster.createdFullName = request.user.name; - posMaster.createdAt = new Date(); - posMaster.lastUpdateUserId = request.user.sub; - posMaster.lastUpdateFullName = request.user.name; - posMaster.lastUpdatedAt = new Date(); - await this.posMasterRepository.save(posMaster); - - //copy มอบหมาย - item.posMasterAssigns = item.posMasterAssigns.map( - ({ id, ...rest }: PosMasterAssign) => ({ - rest, - posMasterId: posMaster.id, - }), - ); - posMaster.posMasterAssigns = item.posMasterAssigns; - - //create position - item.positions.map(async (pos: any) => { - delete pos.id; - const position = Object.assign(new Position(), pos); - position.posMasterId = posMaster.id; - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - ) { - position.positionIsSelected = false; - } - position.createdUserId = request.user.sub; - position.createdFullName = request.user.name; - position.createdAt = new Date(); - position.lastUpdateUserId = request.user.sub; - position.lastUpdateFullName = request.user.name; - position.lastUpdatedAt = new Date(); - await this.positionRepository.save(position); - }); - }), - ); - // //create employeePosmaster - // await Promise.all( - // _orgemployeePosMaster - // .filter( - // (x: EmployeePosMaster) => - // x.orgChild3Id == data3Id && x.orgChild4Id == null, - // ) - // .map(async (item: any) => { - // delete item.id; - // const employeePosMaster = Object.assign(new EmployeePosMaster(), item); - // employeePosMaster.positions = []; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeePosMaster.current_holderId = item.current_holderId; - // } else { - // // employeePosMaster.next_holderId = null; - // employeePosMaster.isSit = false; - // } - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeePosMaster.authRoleId = item.authRoleId; - // } else { - // employeePosMaster.authRoleId = null; - // } - // // employeePosMaster.current_holderId = null; - // employeePosMaster.orgRevisionId = revision.id; - // employeePosMaster.orgRootId = data.id; - // employeePosMaster.orgChild1Id = data1.id; - // employeePosMaster.orgChild2Id = data2.id; - // employeePosMaster.orgChild3Id = data3.id; - // employeePosMaster.createdUserId = request.user.sub; - // employeePosMaster.createdFullName = request.user.name; - // employeePosMaster.createdAt = new Date(); - // employeePosMaster.lastUpdateUserId = request.user.sub; - // employeePosMaster.lastUpdateFullName = request.user.name; - // employeePosMaster.lastUpdatedAt = new Date(); - // await this.employeePosMasterRepository.save(employeePosMaster); - - // //create employeePosition - // item.positions.map(async (pos: any) => { - // delete pos.id; - // const employeePosition: EmployeePosition = Object.assign( - // new EmployeePosition(), - // pos, - // ); - // employeePosition.posMasterId = employeePosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - // employeePosition.createdUserId = request.user.sub; - // employeePosition.createdFullName = request.user.name; - // employeePosition.createdAt = new Date(); - // employeePosition.lastUpdateUserId = request.user.sub; - // employeePosition.lastUpdateFullName = request.user.name; - // employeePosition.lastUpdatedAt = new Date(); - // await this.employeePositionRepository.save(employeePosition); - // }); - // }), - // ); - // // create employeeTempPosmaster - // await Promise.all( - // _orgemployeeTempPosMaster - // .filter( - // (x: EmployeeTempPosMaster) => - // x.orgChild3Id == data3Id && x.orgChild4Id == null, - // ) - // .map(async (item: any) => { - // delete item.id; - // const employeeTempPosMaster = Object.assign( - // new EmployeeTempPosMaster(), - // item, - // ); - // employeeTempPosMaster.positions = []; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeeTempPosMaster.current_holderId = item.current_holderId; - // } else { - // // employeeTempPosMaster.next_holderId = null; - // employeeTempPosMaster.isSit = false; - // } - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeeTempPosMaster.authRoleId = item.authRoleId; - // } else { - // employeeTempPosMaster.authRoleId = null; - // } - // // employeeTempPosMaster.current_holderId = null; - // employeeTempPosMaster.orgRevisionId = revision.id; - // employeeTempPosMaster.orgRootId = data.id; - // employeeTempPosMaster.orgChild1Id = data1.id; - // employeeTempPosMaster.orgChild2Id = data2.id; - // employeeTempPosMaster.orgChild3Id = data3.id; - // employeeTempPosMaster.createdUserId = request.user.sub; - // employeeTempPosMaster.createdFullName = request.user.name; - // employeeTempPosMaster.createdAt = new Date(); - // employeeTempPosMaster.lastUpdateUserId = request.user.sub; - // employeeTempPosMaster.lastUpdateFullName = request.user.name; - // employeeTempPosMaster.lastUpdatedAt = new Date(); - // await this.employeeTempPosMasterRepository.save(employeeTempPosMaster); - - // //create employeePosition - // item.positions.map(async (pos: any) => { - // delete pos.id; - // const employeePosition: EmployeePosition = Object.assign( - // new EmployeePosition(), - // pos, - // ); - // employeePosition.posMasterTempId = employeeTempPosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - // employeePosition.createdUserId = request.user.sub; - // employeePosition.createdFullName = request.user.name; - // employeePosition.createdAt = new Date(); - // employeePosition.lastUpdateUserId = request.user.sub; - // employeePosition.lastUpdateFullName = request.user.name; - // employeePosition.lastUpdatedAt = new Date(); - // await this.employeePositionRepository.save(employeePosition); - // }); - // }), - // ); } - - //create org - _orgChild4 - .filter((x: OrgChild4) => x.orgChild3Id == data3Id) - .forEach(async (x: any) => { - var data4Id = x.id; - delete x.id; - const data4 = Object.assign(new OrgChild4(), x); - data4.orgChild3Id = data3.id; - data4.orgChild2Id = data2.id; - data4.orgChild1Id = data1.id; - data4.orgRootId = data.id; - data4.orgRevisionId = revision.id; - data4.createdUserId = request.user.sub; - data4.createdFullName = request.user.name; - data4.createdAt = new Date(); - data4.lastUpdateUserId = request.user.sub; - data4.lastUpdateFullName = request.user.name; - data4.lastUpdatedAt = new Date(); - await this.child4Repository.save(data4); - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - ) { - //create posmaster - await Promise.all( - _orgPosMaster - .filter((x: PosMaster) => x.orgChild4Id == data4Id) - .map(async (item: any) => { - delete item.id; - const posMaster = Object.assign(new PosMaster(), item); - posMaster.positions = []; - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - ) { - posMaster.next_holderId = item.current_holderId; - } else { - posMaster.next_holderId = null; - posMaster.isSit = false; - } - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - ) { - posMaster.authRoleId = item.authRoleId; - } else { - posMaster.authRoleId = null; - } - posMaster.current_holderId = null; - posMaster.orgRevisionId = revision.id; - posMaster.orgRootId = data.id; - posMaster.orgChild1Id = data1.id; - posMaster.orgChild2Id = data2.id; - posMaster.orgChild3Id = data3.id; - posMaster.orgChild4Id = data4.id; - posMaster.createdUserId = request.user.sub; - posMaster.createdFullName = request.user.name; - posMaster.createdAt = new Date(); - posMaster.lastUpdateUserId = request.user.sub; - posMaster.lastUpdateFullName = request.user.name; - posMaster.lastUpdatedAt = new Date(); - await this.posMasterRepository.save(posMaster); - - //copy มอบหมาย - item.posMasterAssigns = item.posMasterAssigns.map( - ({ id, ...rest }: PosMasterAssign) => ({ - rest, - posMasterId: posMaster.id, - }), - ); - posMaster.posMasterAssigns = item.posMasterAssigns; - - //create position - item.positions.map(async (pos: any) => { - delete pos.id; - const position = Object.assign(new Position(), pos); - position.posMasterId = posMaster.id; - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - ) { - position.positionIsSelected = false; - } - position.createdUserId = request.user.sub; - position.createdFullName = request.user.name; - position.createdAt = new Date(); - position.lastUpdateUserId = request.user.sub; - position.lastUpdateFullName = request.user.name; - position.lastUpdatedAt = new Date(); - await this.positionRepository.save(position); - }); - }), - ); - // //create employeePosmaster - // await Promise.all( - // _orgemployeePosMaster - // .filter((x: EmployeePosMaster) => x.orgChild4Id == data4Id) - // .map(async (item: any) => { - // delete item.id; - // const employeePosMaster = Object.assign( - // new EmployeePosMaster(), - // item, - // ); - // employeePosMaster.positions = []; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeePosMaster.current_holderId = item.current_holderId; - // } else { - // // employeePosMaster.next_holderId = null; - // employeePosMaster.isSit = false; - // } - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeePosMaster.authRoleId = item.authRoleId; - // } else { - // employeePosMaster.authRoleId = null; - // } - // // employeePosMaster.current_holderId = null; - // employeePosMaster.orgRevisionId = revision.id; - // employeePosMaster.orgRootId = data.id; - // employeePosMaster.orgChild1Id = data1.id; - // employeePosMaster.orgChild2Id = data2.id; - // employeePosMaster.orgChild3Id = data3.id; - // employeePosMaster.orgChild4Id = data4.id; - // employeePosMaster.createdUserId = request.user.sub; - // employeePosMaster.createdFullName = request.user.name; - // employeePosMaster.createdAt = new Date(); - // employeePosMaster.lastUpdateUserId = request.user.sub; - // employeePosMaster.lastUpdateFullName = request.user.name; - // employeePosMaster.lastUpdatedAt = new Date(); - // await this.employeePosMasterRepository.save(employeePosMaster); - - // //create employeePosition - // item.positions.map(async (pos: any) => { - // delete pos.id; - // const employeePosition: EmployeePosition = Object.assign( - // new EmployeePosition(), - // pos, - // ); - // employeePosition.posMasterId = employeePosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - // employeePosition.createdUserId = request.user.sub; - // employeePosition.createdFullName = request.user.name; - // employeePosition.createdAt = new Date(); - // employeePosition.lastUpdateUserId = request.user.sub; - // employeePosition.lastUpdateFullName = request.user.name; - // employeePosition.lastUpdatedAt = new Date(); - // await this.employeePositionRepository.save(employeePosition); - // }); - // }), - // ); - // //create employeeTempPosmaster - // await Promise.all( - // _orgemployeeTempPosMaster - // .filter((x: EmployeeTempPosMaster) => x.orgChild4Id == data4Id) - // .map(async (item: any) => { - // delete item.id; - // const employeeTempPosMaster = Object.assign( - // new EmployeeTempPosMaster(), - // item, - // ); - // employeeTempPosMaster.positions = []; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeeTempPosMaster.current_holderId = item.current_holderId; - // } else { - // // employeeTempPosMaster.next_holderId = null; - // employeeTempPosMaster.isSit = false; - // } - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeeTempPosMaster.authRoleId = item.authRoleId; - // } else { - // employeeTempPosMaster.authRoleId = null; - // } - // // employeeTempPosMaster.current_holderId = null; - // employeeTempPosMaster.orgRevisionId = revision.id; - // employeeTempPosMaster.orgRootId = data.id; - // employeeTempPosMaster.orgChild1Id = data1.id; - // employeeTempPosMaster.orgChild2Id = data2.id; - // employeeTempPosMaster.orgChild3Id = data3.id; - // employeeTempPosMaster.orgChild4Id = data4.id; - // employeeTempPosMaster.createdUserId = request.user.sub; - // employeeTempPosMaster.createdFullName = request.user.name; - // employeeTempPosMaster.createdAt = new Date(); - // employeeTempPosMaster.lastUpdateUserId = request.user.sub; - // employeeTempPosMaster.lastUpdateFullName = request.user.name; - // employeeTempPosMaster.lastUpdatedAt = new Date(); - // await this.employeeTempPosMasterRepository.save( - // employeeTempPosMaster, - // ); - - // //create employeePosition - // item.positions.map(async (pos: any) => { - // delete pos.id; - // const employeePosition: EmployeePosition = Object.assign( - // new EmployeePosition(), - // pos, - // ); - // employeePosition.posMasterTempId = employeeTempPosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - // employeePosition.createdUserId = request.user.sub; - // employeePosition.createdFullName = request.user.name; - // employeePosition.createdAt = new Date(); - // employeePosition.lastUpdateUserId = request.user.sub; - // employeePosition.lastUpdateFullName = request.user.name; - // employeePosition.lastUpdatedAt = new Date(); - // await this.employeePositionRepository.save(employeePosition); - // }); - // }), - // ); - } - }); - }); - }); - }); - }); + } + } + } + } + } + } + } } const _orgRevisions = await this.orgRevisionRepository.find({ From b0358de9cb08140da52a9acb8c18118e9aefb45a Mon Sep 17 00:00:00 2001 From: Bright Date: Mon, 17 Mar 2025 13:43:54 +0700 Subject: [PATCH 2/3] =?UTF-8?q?issue=20=E0=B9=81=E0=B8=9B=E0=B8=A5?= =?UTF-8?q?=E0=B8=87=E0=B9=80=E0=B8=AD=E0=B8=B2=E0=B9=80=E0=B8=A5=E0=B8=82?= =?UTF-8?q?=E0=B9=84=E0=B8=97=E0=B8=A2=20#1249=20&&=20=E0=B9=80=E0=B8=AD?= =?UTF-8?q?=E0=B8=B2=E0=B9=80=E0=B8=94=E0=B8=B7=E0=B8=AD=E0=B8=99=E0=B8=AD?= =?UTF-8?q?=E0=B8=AD=E0=B8=81=20#1248?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ProfileController.ts | 24 +++++++++---------- src/controllers/ProfileEmployeeController.ts | 24 +++++++++---------- .../ProfileEmployeeTempController.ts | 24 +++++++++---------- src/interfaces/extension.ts | 7 ++++++ 4 files changed, 43 insertions(+), 36 deletions(-) diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index cb2c07d9..ccb5617d 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -269,17 +269,17 @@ export class ProfileController extends Controller { : null, position: Extension.ToThaiNumber( Extension.ToThaiNumber( - `${item.positionName != null ? item.positionName : "-"} ${item.positionType == null ? item.positionCee : (item.positionType == "อำนวยการ" || item.positionType == "บริหาร" ? item.positionType : "") + item.positionLevel}`, + `${item.positionName != null ? item.positionName : "-"} ${item.positionType == null ? (item.positionCee??"") : (item.positionType == "อำนวยการ" || item.positionType == "บริหาร" ? item.positionType : "") + item.positionLevel}`, ), ), - posNo: item.posNo != null ? Extension.ToThaiNumber(item.posNo) : null, - orgRoot: item.orgRoot, - orgChild1: item.orgChild1, - orgChild2: item.orgChild2, - orgChild3: item.orgChild3, - orgChild4: item.orgChild4, - positionCee: item.positionCee, - positionExecutive: item.positionExecutive, + posNo: item.posNo != null ? Extension.ToThaiNumber(item.posNo) : "", + orgRoot: item.orgRoot != null ? Extension.ToThaiNumber( item.orgRoot) : "", + orgChild1: item.orgChild1 != null ? Extension.ToThaiNumber( item.orgChild1) : "", + orgChild2: item.orgChild2 != null ? Extension.ToThaiNumber( item.orgChild2) : "", + orgChild3: item.orgChild3 != null ? Extension.ToThaiNumber( item.orgChild3) : "", + orgChild4: item.orgChild4 != null ? Extension.ToThaiNumber( item.orgChild4) : "", + positionCee: item.positionCee != null ? Extension.ToThaiNumber( item.positionCee) : "", + positionExecutive: item.positionExecutive != null ? Extension.ToThaiNumber( item.positionExecutive) : "", })) : [ { @@ -328,11 +328,11 @@ export class ProfileController extends Controller { citizenId: profile.citizenId != null ? Extension.ToThaiNumber(profile.citizenId) : "", dateOfBirth: profile.birthDate != null - ? Extension.ToThaiNumber(Extension.ToThaiShortDate_monthYear(profile.birthDate)) + ? Extension.ToThaiNumber(Extension.ToThaiShortDate_monthYear2(profile.birthDate)) : "", dateRetire: profile.dateRetire != null - ? Extension.ToThaiNumber(Extension.ToThaiShortDate_monthYear(profile.dateRetire)) + ? Extension.ToThaiNumber(Extension.ToThaiShortDate_monthYear2(profile.dateRetire)) : "", salaryAmount: profile.amount != null ? Extension.ToThaiNumber(profile.amount.toLocaleString()) : "", @@ -356,7 +356,7 @@ export class ProfileController extends Controller { salary_raw.length > 0 && salary_raw[0].positionName != null ? Extension.ToThaiNumber( Extension.ToThaiNumber( - `${salary_raw[0].positionName != null ? salary_raw[0].positionName : "-"} ${salary_raw[0].positionType == null ? salary_raw[0].positionCee : (salary_raw[0].positionType == "อำนวยการ" || salary_raw[0].positionType == "บริหาร" ? salary_raw[0].positionType : "") + salary_raw[0].positionLevel}`, + `${salary_raw[0].positionName != null ? salary_raw[0].positionName : "-"} ${salary_raw[0].positionType == null ? (salary_raw[0].positionCee??"") : (salary_raw[0].positionType == "อำนวยการ" || salary_raw[0].positionType == "บริหาร" ? salary_raw[0].positionType : "") + salary_raw[0].positionLevel}`, ), ) : "", diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index d2f5cf71..94214956 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -261,17 +261,17 @@ export class ProfileEmployeeController extends Controller { : null, position: Extension.ToThaiNumber( Extension.ToThaiNumber( - `${item.positionName != null ? item.positionName : "-"} ${item.positionType == null ? item.positionCee : (item.positionType == "อำนวยการ" || item.positionType == "บริหาร" ? item.positionType : "") + item.positionLevel}`, + `${item.positionName != null ? item.positionName : "-"} ${item.positionType == null ? (item.positionCee??"") : (item.positionType == "อำนวยการ" || item.positionType == "บริหาร" ? item.positionType : "") + item.positionLevel}`, ), ), - posNo: item.posNo != null ? Extension.ToThaiNumber(item.posNo) : null, - orgRoot: item.orgRoot, - orgChild1: item.orgChild1, - orgChild2: item.orgChild2, - orgChild3: item.orgChild3, - orgChild4: item.orgChild4, - positionCee: item.positionCee, - positionExecutive: item.positionExecutive, + posNo: item.posNo != null ? Extension.ToThaiNumber(item.posNo) : "", + orgRoot: item.orgRoot != null ? Extension.ToThaiNumber( item.orgRoot) : "", + orgChild1: item.orgChild1 != null ? Extension.ToThaiNumber( item.orgChild1) : "", + orgChild2: item.orgChild2 != null ? Extension.ToThaiNumber( item.orgChild2) : "", + orgChild3: item.orgChild3 != null ? Extension.ToThaiNumber( item.orgChild3) : "", + orgChild4: item.orgChild4 != null ? Extension.ToThaiNumber( item.orgChild4) : "", + positionCee: item.positionCee != null ? Extension.ToThaiNumber( item.positionCee) : "", + positionExecutive: item.positionExecutive != null ? Extension.ToThaiNumber( item.positionExecutive) : "", })) : [ { @@ -320,11 +320,11 @@ export class ProfileEmployeeController extends Controller { citizenId: profile.citizenId != null ? Extension.ToThaiNumber(profile.citizenId) : "", dateOfBirth: profile.birthDate != null - ? Extension.ToThaiNumber(Extension.ToThaiShortDate_monthYear(profile.birthDate)) + ? Extension.ToThaiNumber(Extension.ToThaiShortDate_monthYear2(profile.birthDate)) : "", dateRetire: profile.dateRetire != null - ? Extension.ToThaiNumber(Extension.ToThaiShortDate_monthYear(profile.dateRetire)) + ? Extension.ToThaiNumber(Extension.ToThaiShortDate_monthYear2(profile.dateRetire)) : "", salaryAmount: profile.amount != null ? Extension.ToThaiNumber(profile.amount.toLocaleString()) : "", @@ -348,7 +348,7 @@ export class ProfileEmployeeController extends Controller { salary_raw.length > 0 && salary_raw[0].positionName != null ? Extension.ToThaiNumber( Extension.ToThaiNumber( - `${salary_raw[0].positionName != null ? salary_raw[0].positionName : "-"} ${salary_raw[0].positionType == null ? salary_raw[0].positionCee : (salary_raw[0].positionType == "อำนวยการ" || salary_raw[0].positionType == "บริหาร" ? salary_raw[0].positionType : "") + salary_raw[0].positionLevel}`, + `${salary_raw[0].positionName != null ? salary_raw[0].positionName : "-"} ${salary_raw[0].positionType == null ? (salary_raw[0].positionCee??"") : (salary_raw[0].positionType == "อำนวยการ" || salary_raw[0].positionType == "บริหาร" ? salary_raw[0].positionType : "") + salary_raw[0].positionLevel}`, ), ) : "", diff --git a/src/controllers/ProfileEmployeeTempController.ts b/src/controllers/ProfileEmployeeTempController.ts index 3fc25988..ff6f4a2b 100644 --- a/src/controllers/ProfileEmployeeTempController.ts +++ b/src/controllers/ProfileEmployeeTempController.ts @@ -256,17 +256,17 @@ export class ProfileEmployeeTempController extends Controller { : null, position: Extension.ToThaiNumber( Extension.ToThaiNumber( - `${item.positionName != null ? item.positionName : "-"} ${item.positionType == null ? item.positionCee : (item.positionType == "อำนวยการ" || item.positionType == "บริหาร" ? item.positionType : "") + item.positionLevel}`, + `${item.positionName != null ? item.positionName : "-"} ${item.positionType == null ? (item.positionCee??"") : (item.positionType == "อำนวยการ" || item.positionType == "บริหาร" ? item.positionType : "") + item.positionLevel}`, ), ), - posNo: item.posNo != null ? Extension.ToThaiNumber(item.posNo) : null, - orgRoot: item.orgRoot, - orgChild1: item.orgChild1, - orgChild2: item.orgChild2, - orgChild3: item.orgChild3, - orgChild4: item.orgChild4, - positionCee: item.positionCee, - positionExecutive: item.positionExecutive, + posNo: item.posNo != null ? Extension.ToThaiNumber(item.posNo) : "", + orgRoot: item.orgRoot != null ? Extension.ToThaiNumber( item.orgRoot) : "", + orgChild1: item.orgChild1 != null ? Extension.ToThaiNumber( item.orgChild1) : "", + orgChild2: item.orgChild2 != null ? Extension.ToThaiNumber( item.orgChild2) : "", + orgChild3: item.orgChild3 != null ? Extension.ToThaiNumber( item.orgChild3) : "", + orgChild4: item.orgChild4 != null ? Extension.ToThaiNumber( item.orgChild4) : "", + positionCee: item.positionCee != null ? Extension.ToThaiNumber( item.positionCee) : "", + positionExecutive: item.positionExecutive != null ? Extension.ToThaiNumber( item.positionExecutive) : "", })) : [ { @@ -314,11 +314,11 @@ export class ProfileEmployeeTempController extends Controller { citizenId: profile.citizenId != null ? Extension.ToThaiNumber(profile.citizenId) : "", dateOfBirth: profile.birthDate != null - ? Extension.ToThaiNumber(Extension.ToThaiShortDate_monthYear(profile.birthDate)) + ? Extension.ToThaiNumber(Extension.ToThaiShortDate_monthYear2(profile.birthDate)) : "", dateRetire: profile.dateRetire != null - ? Extension.ToThaiNumber(Extension.ToThaiShortDate_monthYear(profile.dateRetire)) + ? Extension.ToThaiNumber(Extension.ToThaiShortDate_monthYear2(profile.dateRetire)) : "", salaryAmount: profile.amount != null ? Extension.ToThaiNumber(profile.amount.toLocaleString()) : "", @@ -342,7 +342,7 @@ export class ProfileEmployeeTempController extends Controller { salary_raw.length > 0 && salary_raw[0].positionName != null ? Extension.ToThaiNumber( Extension.ToThaiNumber( - `${salary_raw[0].positionName != null ? salary_raw[0].positionName : "-"} ${salary_raw[0].positionType == null ? salary_raw[0].positionCee : (salary_raw[0].positionType == "อำนวยการ" || salary_raw[0].positionType == "บริหาร" ? salary_raw[0].positionType : "") + salary_raw[0].positionLevel}`, + `${salary_raw[0].positionName != null ? salary_raw[0].positionName : "-"} ${salary_raw[0].positionType == null ? (salary_raw[0].positionCee??"") : (salary_raw[0].positionType == "อำนวยการ" || salary_raw[0].positionType == "บริหาร" ? salary_raw[0].positionType : "") + salary_raw[0].positionLevel}`, ), ) : "", diff --git a/src/interfaces/extension.ts b/src/interfaces/extension.ts index a2e766dc..15cbac88 100644 --- a/src/interfaces/extension.ts +++ b/src/interfaces/extension.ts @@ -228,6 +228,13 @@ class Extension { ); } + public static ToThaiShortDate_monthYear2(value: Date) { + let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear(); + return ( + value.getDate() + " " + Extension.ToThaiMonth(value.getMonth() + 1) + " พ.ศ. " + yy + ); + } + public static ToThaiShortYear(value: Date) { let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear(); return yy.toString(); From 283deb49f190ec0b954293f4bec72221f3b01a62 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Mon, 17 Mar 2025 14:08:31 +0700 Subject: [PATCH 3/3] rabbitMq draft test --- src/controllers/OrganizationController.ts | 638 +------------------- src/services/rabbitmq.ts | 674 +++++++++++++++++++++- 2 files changed, 682 insertions(+), 630 deletions(-) diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index b64cbe91..cf86d6e7 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -29,7 +29,7 @@ import { RequestWithUser } from "../middlewares/user"; import permission from "../interfaces/permission"; import { PermissionOrg } from "../entities/PermissionOrg"; import { setLogDataDiff } from "../interfaces/utils"; -import { sendToQueueOrg } from "../services/rabbitmq"; +import { sendToQueueOrg, sendToQueueOrgDraft } from "../services/rabbitmq"; import { PosMasterAssign } from "../entities/PosMasterAssign"; import { PosMasterAct } from "../entities/PosMasterAct"; import { EmployeePosition } from "../entities/EmployeePosition"; @@ -147,631 +147,19 @@ export class OrganizationController extends Controller { revision.lastUpdatedAt = new Date(); await this.orgRevisionRepository.save(revision, { data: request }); setLogDataDiff(request, { before, after: revision }); - //cone tree - if ( - requestBody.typeDraft.toUpperCase() == "ORG" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - ) { - //cone by revisionId - if (requestBody.orgRevisionId == null) - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); - const _revision = await this.orgRevisionRepository.findOne({ - where: { id: requestBody.orgRevisionId }, - }); - if (!_revision) throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); - - //หา dna tree ถ้าไม่มีให้เอาตัวเองเป็น dna - const orgRoot = await this.orgRootRepository.find({ - where: { orgRevisionId: requestBody.orgRevisionId }, - order:{orgRootOrder:"ASC"} - }); - let _orgRoot: any = orgRoot.map((x) => ({ - ...x, - ancestorDNA: - x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" - ? x.id - : x.ancestorDNA, - })); - await this.orgRootRepository.save(_orgRoot); - - const orgChild1 = await this.child1Repository.find({ - where: { orgRevisionId: requestBody.orgRevisionId }, - order:{orgChild1Order:"ASC"} - }); - let _orgChild1: any = orgChild1.map((x) => ({ - ...x, - ancestorDNA: - x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" - ? x.id - : x.ancestorDNA, - })); - await this.child1Repository.save(_orgChild1); - - const orgChild2 = await this.child2Repository.find({ - where: { orgRevisionId: requestBody.orgRevisionId }, - order:{orgChild2Order:"ASC"} - }); - let _orgChild2: any = orgChild2.map((x) => ({ - ...x, - ancestorDNA: - x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" - ? x.id - : x.ancestorDNA, - })); - await this.child2Repository.save(_orgChild2); - - const orgChild3 = await this.child3Repository.find({ - where: { orgRevisionId: requestBody.orgRevisionId }, - order:{orgChild3Order:"ASC"} - }); - let _orgChild3: any = orgChild3.map((x) => ({ - ...x, - ancestorDNA: - x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" - ? x.id - : x.ancestorDNA, - })); - await this.child3Repository.save(_orgChild3); - - const orgChild4 = await this.child4Repository.find({ - where: { orgRevisionId: requestBody.orgRevisionId }, - order:{orgChild4Order:"ASC"} - }); - let _orgChild4: any = orgChild4.map((x) => ({ - ...x, - ancestorDNA: - x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" - ? x.id - : x.ancestorDNA, - })); - await this.child4Repository.save(_orgChild4); - - //หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna - const orgPosMaster = await this.posMasterRepository.find({ - where: { orgRevisionId: requestBody.orgRevisionId }, - relations: ["positions", "posMasterAssigns"], - }); - - let _orgPosMaster: PosMaster[] = []; - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - ) { - _orgPosMaster = orgPosMaster - .filter( - (x) => x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000", - ) - .map((x) => ({ - ...x, - ancestorDNA: - x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" - ? x.id - : x.ancestorDNA, - })); - await this.posMasterRepository.save(_orgPosMaster); - } - // Create org - for await (const x0 of _orgRoot) { - var dataId = x0.id; - console.log(`ch0 ${x0.orgRootOrder}`) - console.log(dataId) - delete x0.id; - const data = Object.assign(new OrgRoot(), x0); - data.orgRevisionId = revision.id; - data.createdUserId = request.user.sub; - data.createdFullName = request.user.name; - data.createdAt = new Date(); - data.lastUpdateUserId = request.user.sub; - data.lastUpdateFullName = request.user.name; - data.lastUpdatedAt = new Date(); - await this.orgRootRepository.save(data); - - const orgTypes = new Set([ - "ORG_POSITION", - "ORG_POSITION_PERSON", - "ORG_POSITION_ROLE", - "ORG_POSITION_PERSON_ROLE", - ]); - - if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { - // Create posMaster - for await (const item of orgPosMaster.filter( - (x: PosMaster) => x.orgRootId == dataId && x.orgChild1Id == null, - ) as any) { - delete item.id; - const posMaster: any = Object.assign(new PosMaster(), item); - posMaster.positions = []; - - if ( - ["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes( - requestBody.typeDraft.toUpperCase(), - ) - ) { - posMaster.next_holderId = item.current_holderId; - } else { - posMaster.next_holderId = null; - posMaster.isSit = false; - } - - if ( - ["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes( - requestBody.typeDraft.toUpperCase(), - ) - ) { - posMaster.authRoleId = item.authRoleId; - } else { - posMaster.authRoleId = null; - } - - posMaster.current_holderId = null; - posMaster.orgRevisionId = revision.id; - posMaster.orgRootId = data.id; - posMaster.createdUserId = request.user.sub; - posMaster.createdFullName = request.user.name; - posMaster.createdAt = new Date(); - posMaster.lastUpdateUserId = request.user.sub; - posMaster.lastUpdateFullName = request.user.name; - posMaster.lastUpdatedAt = new Date(); - await this.posMasterRepository.save(posMaster); - - // Copy assignments - item.posMasterAssigns = item.posMasterAssigns.map( - ({ id, ...rest }: PosMasterAssign) => ({ - ...rest, - posMasterId: posMaster.id, - }), - ); - posMaster.posMasterAssigns = item.posMasterAssigns; - - // Create positions - for await (const pos of item.positions) { - delete pos.id; - const position = Object.assign(new Position(), pos); - position.posMasterId = posMaster.id; - - if ( - ["ORG_POSITION", "ORG_POSITION_ROLE"].includes(requestBody.typeDraft.toUpperCase()) - ) { - position.positionIsSelected = false; - } - - position.createdUserId = request.user.sub; - position.createdFullName = request.user.name; - position.createdAt = new Date(); - position.lastUpdateUserId = request.user.sub; - position.lastUpdateFullName = request.user.name; - position.lastUpdatedAt = new Date(); - await this.positionRepository.save(position); - } - } - } - - // Create orgChild1 - for await (const x1 of _orgChild1.filter((x: OrgChild1) => x.orgRootId == dataId)) { - var data1Id = x1.id; - console.log(`ch1 ${x1.orgChild1Order}`) - console.log(data1Id) - delete x1.id; - const data1 = Object.assign(new OrgChild1(), x1); - data1.orgRootId = data.id; - data1.orgRevisionId = revision.id; - data1.createdUserId = request.user.sub; - data1.createdFullName = request.user.name; - data1.createdAt = new Date(); - data1.lastUpdateUserId = request.user.sub; - data1.lastUpdateFullName = request.user.name; - data1.lastUpdatedAt = new Date(); - await this.child1Repository.save(data1); - - if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { - // Create posMaster - for await (const item of orgPosMaster.filter( - (x: PosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null, - ) as any) { - delete item.id; - const posMaster = Object.assign(new PosMaster(), item); - posMaster.positions = []; - - if ( - ["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes( - requestBody.typeDraft.toUpperCase(), - ) - ) { - posMaster.next_holderId = item.current_holderId; - } else { - posMaster.next_holderId = null; - posMaster.isSit = false; - } - - if ( - ["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes( - requestBody.typeDraft.toUpperCase(), - ) - ) { - posMaster.authRoleId = item.authRoleId; - } else { - posMaster.authRoleId = null; - } - - posMaster.current_holderId = null; - posMaster.orgRevisionId = revision.id; - posMaster.orgRootId = data.id; - posMaster.orgChild1Id = data1.id; - posMaster.createdUserId = request.user.sub; - posMaster.createdFullName = request.user.name; - posMaster.createdAt = new Date(); - posMaster.lastUpdateUserId = request.user.sub; - posMaster.lastUpdateFullName = request.user.name; - posMaster.lastUpdatedAt = new Date(); - await this.posMasterRepository.save(posMaster); - - // Copy assignments - item.posMasterAssigns = item.posMasterAssigns.map( - ({ id, ...rest }: PosMasterAssign) => ({ - ...rest, - posMasterId: posMaster.id, - }), - ); - posMaster.posMasterAssigns = item.posMasterAssigns; - - // Create positions - for await (const pos of item.positions) { - delete pos.id; - const position = Object.assign(new Position(), pos); - position.posMasterId = posMaster.id; - - if ( - ["ORG_POSITION", "ORG_POSITION_ROLE"].includes( - requestBody.typeDraft.toUpperCase(), - ) - ) { - position.positionIsSelected = false; - } - - position.createdUserId = request.user.sub; - position.createdFullName = request.user.name; - position.createdAt = new Date(); - position.lastUpdateUserId = request.user.sub; - position.lastUpdateFullName = request.user.name; - position.lastUpdatedAt = new Date(); - await this.positionRepository.save(position); - } - } - } - for await (const x2 of _orgChild2.filter((x: OrgChild2) => x.orgChild1Id == data1Id)) { - var data2Id = x2.id; - console.log(`ch2 ${x2.orgChild2Order}`) - console.log(data2Id) - delete x2.id; - const data2 = Object.assign(new OrgChild2(), x2); - data2.orgChild1Id = data1.id; - data2.orgRootId = data.id; - data2.orgRevisionId = revision.id; - data2.createdUserId = request.user.sub; - data2.createdFullName = request.user.name; - data2.createdAt = new Date(); - data2.lastUpdateUserId = request.user.sub; - data2.lastUpdateFullName = request.user.name; - data2.lastUpdatedAt = new Date(); - await this.child2Repository.save(data2); - - if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { - // Create posMaster - for await (const item of orgPosMaster.filter( - (x: PosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null, - ) as any) { - delete item.id; - const posMaster = Object.assign(new PosMaster(), item); - posMaster.positions = []; - - if ( - ["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes( - requestBody.typeDraft.toUpperCase(), - ) - ) { - posMaster.next_holderId = item.current_holderId; - } else { - posMaster.next_holderId = null; - posMaster.isSit = false; - } - - if ( - ["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes( - requestBody.typeDraft.toUpperCase(), - ) - ) { - posMaster.authRoleId = item.authRoleId; - } else { - posMaster.authRoleId = null; - } - - posMaster.current_holderId = null; - posMaster.orgRevisionId = revision.id; - posMaster.orgRootId = data.id; - posMaster.orgChild1Id = data1.id; - posMaster.orgChild2Id = data2.id; - posMaster.createdUserId = request.user.sub; - posMaster.createdFullName = request.user.name; - posMaster.createdAt = new Date(); - posMaster.lastUpdateUserId = request.user.sub; - posMaster.lastUpdateFullName = request.user.name; - posMaster.lastUpdatedAt = new Date(); - await this.posMasterRepository.save(posMaster); - - // Copy assignments - posMaster.posMasterAssigns = item.posMasterAssigns.map( - ({ id, ...rest }: PosMasterAssign) => ({ - ...rest, - posMasterId: posMaster.id, - }), - ); - - // Create positions - for await (const pos of item.positions) { - delete pos.id; - const position = Object.assign(new Position(), pos); - position.posMasterId = posMaster.id; - if ( - ["ORG_POSITION", "ORG_POSITION_ROLE"].includes( - requestBody.typeDraft.toUpperCase(), - ) - ) { - position.positionIsSelected = false; - } - position.createdUserId = request.user.sub; - position.createdFullName = request.user.name; - position.createdAt = new Date(); - position.lastUpdateUserId = request.user.sub; - position.lastUpdateFullName = request.user.name; - position.lastUpdatedAt = new Date(); - await this.positionRepository.save(position); - } - } - } - - // Create org - for await (const x3 of _orgChild3.filter((x: OrgChild3) => x.orgChild2Id == data2Id)) { - var data3Id = x3.id; - console.log(`ch3 ${x3.orgChild3Order}`) - console.log(data3Id) - delete x3.id; - const data3 = Object.assign(new OrgChild3(), x3); - data3.orgChild2Id = data2.id; - data3.orgChild1Id = data1.id; - data3.orgRootId = data.id; - data3.orgRevisionId = revision.id; - data3.createdUserId = request.user.sub; - data3.createdFullName = request.user.name; - data3.createdAt = new Date(); - data3.lastUpdateUserId = request.user.sub; - data3.lastUpdateFullName = request.user.name; - data3.lastUpdatedAt = new Date(); - await this.child3Repository.save(data3); - - if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { - // Create posMaster - for await (const item of orgPosMaster.filter( - (x: PosMaster) => x.orgChild3Id == data3Id && x.orgChild4Id == null, - ) as any) { - delete item.id; - const posMaster = Object.assign(new PosMaster(), item); - posMaster.positions = []; - - if ( - ["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes( - requestBody.typeDraft.toUpperCase(), - ) - ) { - posMaster.next_holderId = item.current_holderId; - } else { - posMaster.next_holderId = null; - posMaster.isSit = false; - } - - if ( - ["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes( - requestBody.typeDraft.toUpperCase(), - ) - ) { - posMaster.authRoleId = item.authRoleId; - } else { - posMaster.authRoleId = null; - } - - posMaster.current_holderId = null; - posMaster.orgRevisionId = revision.id; - posMaster.orgRootId = data.id; - posMaster.orgChild1Id = data1.id; - posMaster.orgChild2Id = data2.id; - posMaster.orgChild3Id = data3.id; - posMaster.createdUserId = request.user.sub; - posMaster.createdFullName = request.user.name; - posMaster.createdAt = new Date(); - posMaster.lastUpdateUserId = request.user.sub; - posMaster.lastUpdateFullName = request.user.name; - posMaster.lastUpdatedAt = new Date(); - await this.posMasterRepository.save(posMaster); - - // Copy assignments - posMaster.posMasterAssigns = item.posMasterAssigns.map( - ({ id, ...rest }: PosMasterAssign) => ({ - ...rest, - posMasterId: posMaster.id, - }), - ); - - // Create positions - for await (const pos of item.positions) { - delete pos.id; - const position = Object.assign(new Position(), pos); - position.posMasterId = posMaster.id; - if ( - ["ORG_POSITION", "ORG_POSITION_ROLE"].includes( - requestBody.typeDraft.toUpperCase(), - ) - ) { - position.positionIsSelected = false; - } - position.createdUserId = request.user.sub; - position.createdFullName = request.user.name; - position.createdAt = new Date(); - position.lastUpdateUserId = request.user.sub; - position.lastUpdateFullName = request.user.name; - position.lastUpdatedAt = new Date(); - await this.positionRepository.save(position); - } - } - } - // Create org - for await (const x4 of _orgChild4.filter( - (x: OrgChild4) => x.orgChild3Id == data3Id, - )) { - var data4Id = x4.id; - console.log(`ch4 ${x4.orgChild4Order}`) - console.log(data4Id) - delete x4.id; - const data4 = Object.assign(new OrgChild4(), x4); - data4.orgChild3Id = data3.id; - data4.orgChild2Id = data2.id; - data4.orgChild1Id = data1.id; - data4.orgRootId = data.id; - data4.orgRevisionId = revision.id; - data4.createdUserId = request.user.sub; - data4.createdFullName = request.user.name; - data4.createdAt = new Date(); - data4.lastUpdateUserId = request.user.sub; - data4.lastUpdateFullName = request.user.name; - data4.lastUpdatedAt = new Date(); - await this.child4Repository.save(data4); - - if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { - // Create posMaster - for await (const item of orgPosMaster.filter( - (x: PosMaster) => x.orgChild4Id == data4Id, - ) as any) { - delete item.id; - const posMaster = Object.assign(new PosMaster(), item); - posMaster.positions = []; - - if ( - ["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes( - requestBody.typeDraft.toUpperCase(), - ) - ) { - posMaster.next_holderId = item.current_holderId; - } else { - posMaster.next_holderId = null; - posMaster.isSit = false; - } - - if ( - ["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes( - requestBody.typeDraft.toUpperCase(), - ) - ) { - posMaster.authRoleId = item.authRoleId; - } else { - posMaster.authRoleId = null; - } - - posMaster.current_holderId = null; - posMaster.orgRevisionId = revision.id; - posMaster.orgRootId = data.id; - posMaster.orgChild1Id = data1.id; - posMaster.orgChild2Id = data2.id; - posMaster.orgChild3Id = data3.id; - posMaster.orgChild4Id = data4.id; - posMaster.createdUserId = request.user.sub; - posMaster.createdFullName = request.user.name; - posMaster.createdAt = new Date(); - posMaster.lastUpdateUserId = request.user.sub; - posMaster.lastUpdateFullName = request.user.name; - posMaster.lastUpdatedAt = new Date(); - await this.posMasterRepository.save(posMaster); - - // Copy assignments - posMaster.posMasterAssigns = item.posMasterAssigns.map( - ({ id, ...rest }: PosMasterAssign) => ({ - ...rest, - posMasterId: posMaster.id, - }), - ); - - // Create positions - for await (const pos of item.positions) { - delete pos.id; - const position = Object.assign(new Position(), pos); - position.posMasterId = posMaster.id; - if ( - ["ORG_POSITION", "ORG_POSITION_ROLE"].includes( - requestBody.typeDraft.toUpperCase(), - ) - ) { - position.positionIsSelected = false; - } - position.createdUserId = request.user.sub; - position.createdFullName = request.user.name; - position.createdAt = new Date(); - position.lastUpdateUserId = request.user.sub; - position.lastUpdateFullName = request.user.name; - position.lastUpdatedAt = new Date(); - await this.positionRepository.save(position); - } - } - } - } - } - } - } - } + const msg = { + data: { + requestBody: requestBody, + request: request.user, + revision: revision + }, + } + try { + await sendToQueueOrgDraft(msg); + return { message: 'Draft is being created... Processing in the background.' }; + } catch (error:any) { + return { message: 'Failed to process the draft. Please try again later.', error: error.message }; } - - const _orgRevisions = await this.orgRevisionRepository.find({ - where: [{ orgRevisionIsDraft: true, id: Not(revision.id) }], - }); - const _roots = await this.orgRootRepository.find({ - where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }], - }); - const _posMasters = await this.posMasterRepository.find({ - where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }], - }); - const _employeePosMasters = await this.employeePosMasterRepository.find({ - where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }], - }); - const _employeeTempPosMasters = await this.employeeTempPosMasterRepository.find({ - where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }], - }); - await this.positionRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) }); - await this.employeePositionRepository.delete({ - posMasterId: In(_employeePosMasters.map((x) => x.id)), - }); - await this.employeePositionRepository.delete({ - posMasterTempId: In(_employeeTempPosMasters.map((x) => x.id)), - }); - await this.posMasterAssignRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) }); - await this.posMasterActRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) }); - await this.posMasterActRepository.delete({ - posMasterChildId: In(_posMasters.map((x) => x.id)), - }); - await this.posMasterRepository.remove(_posMasters); - await this.employeePosMasterRepository.remove(_employeePosMasters); - await this.employeeTempPosMasterRepository.remove(_employeeTempPosMasters); - await this.child4Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); - await this.child3Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); - await this.child2Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); - await this.child1Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); - await this.permissionOrgRepository.delete({ - orgRootId: In(_roots.map((x) => x.id)), - }); - await this.orgRootRepository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); - await this.orgRevisionRepository.remove(_orgRevisions); - - return new HttpSuccess(revision); } /** diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index ba670b3a..56da11c7 100644 --- a/src/services/rabbitmq.ts +++ b/src/services/rabbitmq.ts @@ -19,14 +19,20 @@ import { OrgChild2 } from "../entities/OrgChild2"; import { OrgChild3 } from "../entities/OrgChild3"; import { OrgChild4 } from "../entities/OrgChild4"; import { OrgRoot } from "../entities/OrgRoot"; +import { PosMasterAssign } from "../entities/PosMasterAssign"; +import { Position } from "../entities/Position"; +import { In, Not } from "typeorm"; +import { PosMasterAct } from "../entities/PosMasterAct"; +import { PermissionOrg } from "../entities/PermissionOrg"; export let sendToQueue: (payload: any) => void; export let sendToQueueOrg: (payload: any) => void; +export let sendToQueueOrgDraft: (payload: any) => void; export async function init() { //----> (1) Producer - if (!process.env.AMQ_URL || !process.env.AMQ_QUEUE || !process.env.AMQ_QUEUE_ORG) return; + if (!process.env.AMQ_URL || !process.env.AMQ_QUEUE || !process.env.AMQ_QUEUE_ORG || !process.env.AMQ_QUEUE_ORG_DRAFT) return; - const { AMQ_URL: url, AMQ_QUEUE: queue, AMQ_QUEUE_ORG: queue_org } = process.env; //----> (1.2) get url and queue from .env + const { AMQ_URL: url, AMQ_QUEUE: queue, AMQ_QUEUE_ORG: queue_org, AMQ_QUEUE_ORG_DRAFT: queue_org_draft } = process.env; //----> (1.2) get url and queue from .env const connection = await amqp.connect(url); //----> (1.3) set up url with amqp protocol @@ -37,8 +43,9 @@ export async function init() { console.log(channel ? "[AMQ] Create channel success" : "[AMQ] Create channel failed"); channel.assertQueue(queue, { durable: true }), //----> (1.5) assert queue and set durable (if "true" save to disk on RabbitMQ) - channel.assertQueue(queue_org, { durable: true }), - channel.prefetch(1); + channel.assertQueue(queue_org, { durable: true }), + channel.assertQueue(queue_org_draft, { durable: true }), + channel.prefetch(1); sendToQueue = (payload: any, persistent = true) => { //----> (2) sendQueue To RabbitMQ and set persistent (if "true" redo the failed queue when server run again) @@ -51,9 +58,14 @@ export async function init() { channel.sendToQueue(queue_org, Buffer.from(JSON.stringify(payload)), { persistent }); }; + sendToQueueOrgDraft = (payload: any, persistent = true) => { + channel.sendToQueue(queue_org_draft, Buffer.from(JSON.stringify(payload)), { persistent }); + }; + console.log("[AMQ] Listening for message..."); createConsumer(queue, channel, handler), //----> (3) Process Consumer - createConsumer(queue_org, channel, handler_org); + createConsumer(queue_org, channel, handler_org); + createConsumer(queue_org_draft, channel, handler_org_draft); // createConsumer(queue2, channel, handler2); } @@ -1058,3 +1070,655 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { return false; } } + +async function handler_org_draft(msg: amqp.ConsumeMessage): Promise { + const { data, token, user } = JSON.parse(msg.content.toString()); + const { requestBody, request, revision } = data; + const posMasterRepository = AppDataSource.getRepository(PosMaster); + const positionRepository = AppDataSource.getRepository(Position); + const employeePosMasterRepository = AppDataSource.getRepository(EmployeePosMaster); + const employeeTempPosMasterRepository = AppDataSource.getRepository(EmployeeTempPosMaster); + const posMasterAssignRepository = AppDataSource.getRepository(PosMasterAssign); + const posMasterActRepository = AppDataSource.getRepository(PosMasterAct); + const permissionOrgRepository = AppDataSource.getRepository(PermissionOrg); + const employeePositionRepository = AppDataSource.getRepository(EmployeePosition); + const orgRevisionRepository = AppDataSource.getRepository(OrgRevision); + const orgRootRepository = AppDataSource.getRepository(OrgRoot); + const child1Repository = AppDataSource.getRepository(OrgChild1); + const child2Repository = AppDataSource.getRepository(OrgChild2); + const child3Repository = AppDataSource.getRepository(OrgChild3); + const child4Repository = AppDataSource.getRepository(OrgChild4); + + try { + //cone tree + if ( + requestBody.typeDraft.toUpperCase() == "ORG" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + ) { + //cone by revisionId + if (requestBody.orgRevisionId == null) + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); + const _revision = await orgRevisionRepository.findOne({ + where: { id: requestBody.orgRevisionId }, + }); + if (!_revision) throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); + + //หา dna tree ถ้าไม่มีให้เอาตัวเองเป็น dna + const orgRoot = await orgRootRepository.find({ + where: { orgRevisionId: requestBody.orgRevisionId }, + order:{orgRootOrder:"ASC"} + }); + let _orgRoot: any = orgRoot.map((x) => ({ + ...x, + ancestorDNA: + x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" + ? x.id + : x.ancestorDNA, + })); + await orgRootRepository.save(_orgRoot); + + const orgChild1 = await child1Repository.find({ + where: { orgRevisionId: requestBody.orgRevisionId }, + order:{orgChild1Order:"ASC"} + }); + let _orgChild1: any = orgChild1.map((x) => ({ + ...x, + ancestorDNA: + x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" + ? x.id + : x.ancestorDNA, + })); + await child1Repository.save(_orgChild1); + + const orgChild2 = await child2Repository.find({ + where: { orgRevisionId: requestBody.orgRevisionId }, + order:{orgChild2Order:"ASC"} + }); + let _orgChild2: any = orgChild2.map((x) => ({ + ...x, + ancestorDNA: + x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" + ? x.id + : x.ancestorDNA, + })); + await child2Repository.save(_orgChild2); + + const orgChild3 = await child3Repository.find({ + where: { orgRevisionId: requestBody.orgRevisionId }, + order:{orgChild3Order:"ASC"} + }); + let _orgChild3: any = orgChild3.map((x) => ({ + ...x, + ancestorDNA: + x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" + ? x.id + : x.ancestorDNA, + })); + await child3Repository.save(_orgChild3); + + const orgChild4 = await child4Repository.find({ + where: { orgRevisionId: requestBody.orgRevisionId }, + order:{orgChild4Order:"ASC"} + }); + let _orgChild4: any = orgChild4.map((x) => ({ + ...x, + ancestorDNA: + x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" + ? x.id + : x.ancestorDNA, + })); + await child4Repository.save(_orgChild4); + + //หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna + const orgPosMaster = await posMasterRepository.find({ + where: { orgRevisionId: requestBody.orgRevisionId }, + relations: ["positions", "posMasterAssigns"], + }); + + let _orgPosMaster: PosMaster[] = []; + if ( + requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + ) { + _orgPosMaster = orgPosMaster + .filter( + (x) => x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000", + ) + .map((x) => ({ + ...x, + ancestorDNA: + x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" + ? x.id + : x.ancestorDNA, + })); + await posMasterRepository.save(_orgPosMaster); + } + // Create org + for await (const x0 of _orgRoot) { + var dataId = x0.id; + console.log(`ch0 ${x0.orgRootOrder}`) + console.log(dataId) + delete x0.id; + const data = Object.assign(new OrgRoot(), x0); + data.orgRevisionId = revision.id; + data.createdUserId = request.sub; + data.createdFullName = request.name; + data.createdAt = new Date(); + data.lastUpdateUserId = request.sub; + data.lastUpdateFullName = request.name; + data.lastUpdatedAt = new Date(); + await orgRootRepository.save(data); + + const orgTypes = new Set([ + "ORG_POSITION", + "ORG_POSITION_PERSON", + "ORG_POSITION_ROLE", + "ORG_POSITION_PERSON_ROLE", + ]); + + if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { + // Create posMaster + for await (const item of orgPosMaster.filter( + (x: PosMaster) => x.orgRootId == dataId && x.orgChild1Id == null, + ) as any) { + delete item.id; + const posMaster: any = Object.assign(new PosMaster(), item); + posMaster.positions = []; + + if ( + ["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + posMaster.next_holderId = item.current_holderId; + } else { + posMaster.next_holderId = null; + posMaster.isSit = false; + } + + if ( + ["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + posMaster.authRoleId = item.authRoleId; + } else { + posMaster.authRoleId = null; + } + + posMaster.current_holderId = null; + posMaster.orgRevisionId = revision.id; + posMaster.orgRootId = data.id; + posMaster.createdUserId = request.sub; + posMaster.createdFullName = request.name; + posMaster.createdAt = new Date(); + posMaster.lastUpdateUserId = request.sub; + posMaster.lastUpdateFullName = request.name; + posMaster.lastUpdatedAt = new Date(); + await posMasterRepository.save(posMaster); + + // Copy assignments + item.posMasterAssigns = item.posMasterAssigns.map( + ({ id, ...rest }: PosMasterAssign) => ({ + ...rest, + posMasterId: posMaster.id, + }), + ); + posMaster.posMasterAssigns = item.posMasterAssigns; + + // Create positions + for await (const pos of item.positions) { + delete pos.id; + const position = Object.assign(new Position(), pos); + position.posMasterId = posMaster.id; + + if ( + ["ORG_POSITION", "ORG_POSITION_ROLE"].includes(requestBody.typeDraft.toUpperCase()) + ) { + position.positionIsSelected = false; + } + + position.createdUserId = request.sub; + position.createdFullName = request.name; + position.createdAt = new Date(); + position.lastUpdateUserId = request.sub; + position.lastUpdateFullName = request.name; + position.lastUpdatedAt = new Date(); + await positionRepository.save(position); + } + } + } + + // Create orgChild1 + for await (const x1 of _orgChild1.filter((x: OrgChild1) => x.orgRootId == dataId)) { + var data1Id = x1.id; + console.log(`ch1 ${x1.orgChild1Order}`) + console.log(data1Id) + delete x1.id; + const data1 = Object.assign(new OrgChild1(), x1); + data1.orgRootId = data.id; + data1.orgRevisionId = revision.id; + data1.createdUserId = request.sub; + data1.createdFullName = request.name; + data1.createdAt = new Date(); + data1.lastUpdateUserId = request.sub; + data1.lastUpdateFullName = request.name; + data1.lastUpdatedAt = new Date(); + await child1Repository.save(data1); + + if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { + // Create posMaster + for await (const item of orgPosMaster.filter( + (x: PosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null, + ) as any) { + delete item.id; + const posMaster = Object.assign(new PosMaster(), item); + posMaster.positions = []; + + if ( + ["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + posMaster.next_holderId = item.current_holderId; + } else { + posMaster.next_holderId = null; + posMaster.isSit = false; + } + + if ( + ["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + posMaster.authRoleId = item.authRoleId; + } else { + posMaster.authRoleId = null; + } + + posMaster.current_holderId = null; + posMaster.orgRevisionId = revision.id; + posMaster.orgRootId = data.id; + posMaster.orgChild1Id = data1.id; + posMaster.createdUserId = request.sub; + posMaster.createdFullName = request.name; + posMaster.createdAt = new Date(); + posMaster.lastUpdateUserId = request.sub; + posMaster.lastUpdateFullName = request.name; + posMaster.lastUpdatedAt = new Date(); + await posMasterRepository.save(posMaster); + + // Copy assignments + item.posMasterAssigns = item.posMasterAssigns.map( + ({ id, ...rest }: PosMasterAssign) => ({ + ...rest, + posMasterId: posMaster.id, + }), + ); + posMaster.posMasterAssigns = item.posMasterAssigns; + + // Create positions + for await (const pos of item.positions) { + delete pos.id; + const position = Object.assign(new Position(), pos); + position.posMasterId = posMaster.id; + + if ( + ["ORG_POSITION", "ORG_POSITION_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + position.positionIsSelected = false; + } + + position.createdUserId = request.sub; + position.createdFullName = request.name; + position.createdAt = new Date(); + position.lastUpdateUserId = request.sub; + position.lastUpdateFullName = request.name; + position.lastUpdatedAt = new Date(); + await positionRepository.save(position); + } + } + } + for await (const x2 of _orgChild2.filter((x: OrgChild2) => x.orgChild1Id == data1Id)) { + var data2Id = x2.id; + console.log(`ch2 ${x2.orgChild2Order}`) + console.log(data2Id) + delete x2.id; + const data2 = Object.assign(new OrgChild2(), x2); + data2.orgChild1Id = data1.id; + data2.orgRootId = data.id; + data2.orgRevisionId = revision.id; + data2.createdUserId = request.sub; + data2.createdFullName = request.name; + data2.createdAt = new Date(); + data2.lastUpdateUserId = request.sub; + data2.lastUpdateFullName = request.name; + data2.lastUpdatedAt = new Date(); + await child2Repository.save(data2); + + if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { + // Create posMaster + for await (const item of orgPosMaster.filter( + (x: PosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null, + ) as any) { + delete item.id; + const posMaster = Object.assign(new PosMaster(), item); + posMaster.positions = []; + + if ( + ["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + posMaster.next_holderId = item.current_holderId; + } else { + posMaster.next_holderId = null; + posMaster.isSit = false; + } + + if ( + ["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + posMaster.authRoleId = item.authRoleId; + } else { + posMaster.authRoleId = null; + } + + posMaster.current_holderId = null; + posMaster.orgRevisionId = revision.id; + posMaster.orgRootId = data.id; + posMaster.orgChild1Id = data1.id; + posMaster.orgChild2Id = data2.id; + posMaster.createdUserId = request.sub; + posMaster.createdFullName = request.name; + posMaster.createdAt = new Date(); + posMaster.lastUpdateUserId = request.sub; + posMaster.lastUpdateFullName = request.name; + posMaster.lastUpdatedAt = new Date(); + await posMasterRepository.save(posMaster); + + // Copy assignments + posMaster.posMasterAssigns = item.posMasterAssigns.map( + ({ id, ...rest }: PosMasterAssign) => ({ + ...rest, + posMasterId: posMaster.id, + }), + ); + + // Create positions + for await (const pos of item.positions) { + delete pos.id; + const position = Object.assign(new Position(), pos); + position.posMasterId = posMaster.id; + if ( + ["ORG_POSITION", "ORG_POSITION_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + position.positionIsSelected = false; + } + position.createdUserId = request.sub; + position.createdFullName = request.name; + position.createdAt = new Date(); + position.lastUpdateUserId = request.sub; + position.lastUpdateFullName = request.name; + position.lastUpdatedAt = new Date(); + await positionRepository.save(position); + } + } + } + + // Create org + for await (const x3 of _orgChild3.filter((x: OrgChild3) => x.orgChild2Id == data2Id)) { + var data3Id = x3.id; + console.log(`ch3 ${x3.orgChild3Order}`) + console.log(data3Id) + delete x3.id; + const data3 = Object.assign(new OrgChild3(), x3); + data3.orgChild2Id = data2.id; + data3.orgChild1Id = data1.id; + data3.orgRootId = data.id; + data3.orgRevisionId = revision.id; + data3.createdUserId = request.sub; + data3.createdFullName = request.name; + data3.createdAt = new Date(); + data3.lastUpdateUserId = request.sub; + data3.lastUpdateFullName = request.name; + data3.lastUpdatedAt = new Date(); + await child3Repository.save(data3); + + if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { + // Create posMaster + for await (const item of orgPosMaster.filter( + (x: PosMaster) => x.orgChild3Id == data3Id && x.orgChild4Id == null, + ) as any) { + delete item.id; + const posMaster = Object.assign(new PosMaster(), item); + posMaster.positions = []; + + if ( + ["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + posMaster.next_holderId = item.current_holderId; + } else { + posMaster.next_holderId = null; + posMaster.isSit = false; + } + + if ( + ["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + posMaster.authRoleId = item.authRoleId; + } else { + posMaster.authRoleId = null; + } + + posMaster.current_holderId = null; + posMaster.orgRevisionId = revision.id; + posMaster.orgRootId = data.id; + posMaster.orgChild1Id = data1.id; + posMaster.orgChild2Id = data2.id; + posMaster.orgChild3Id = data3.id; + posMaster.createdUserId = request.sub; + posMaster.createdFullName = request.name; + posMaster.createdAt = new Date(); + posMaster.lastUpdateUserId = request.sub; + posMaster.lastUpdateFullName = request.name; + posMaster.lastUpdatedAt = new Date(); + await posMasterRepository.save(posMaster); + + // Copy assignments + posMaster.posMasterAssigns = item.posMasterAssigns.map( + ({ id, ...rest }: PosMasterAssign) => ({ + ...rest, + posMasterId: posMaster.id, + }), + ); + + // Create positions + for await (const pos of item.positions) { + delete pos.id; + const position = Object.assign(new Position(), pos); + position.posMasterId = posMaster.id; + if ( + ["ORG_POSITION", "ORG_POSITION_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + position.positionIsSelected = false; + } + position.createdUserId = request.sub; + position.createdFullName = request.name; + position.createdAt = new Date(); + position.lastUpdateUserId = request.sub; + position.lastUpdateFullName = request.name; + position.lastUpdatedAt = new Date(); + await positionRepository.save(position); + } + } + } + // Create org + for await (const x4 of _orgChild4.filter( + (x: OrgChild4) => x.orgChild3Id == data3Id, + )) { + var data4Id = x4.id; + console.log(`ch4 ${x4.orgChild4Order}`) + console.log(data4Id) + delete x4.id; + const data4 = Object.assign(new OrgChild4(), x4); + data4.orgChild3Id = data3.id; + data4.orgChild2Id = data2.id; + data4.orgChild1Id = data1.id; + data4.orgRootId = data.id; + data4.orgRevisionId = revision.id; + data4.createdUserId = request.sub; + data4.createdFullName = request.name; + data4.createdAt = new Date(); + data4.lastUpdateUserId = request.sub; + data4.lastUpdateFullName = request.name; + data4.lastUpdatedAt = new Date(); + await child4Repository.save(data4); + + if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { + // Create posMaster + for await (const item of orgPosMaster.filter( + (x: PosMaster) => x.orgChild4Id == data4Id, + ) as any) { + delete item.id; + const posMaster = Object.assign(new PosMaster(), item); + posMaster.positions = []; + + if ( + ["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + posMaster.next_holderId = item.current_holderId; + } else { + posMaster.next_holderId = null; + posMaster.isSit = false; + } + + if ( + ["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + posMaster.authRoleId = item.authRoleId; + } else { + posMaster.authRoleId = null; + } + + posMaster.current_holderId = null; + posMaster.orgRevisionId = revision.id; + posMaster.orgRootId = data.id; + posMaster.orgChild1Id = data1.id; + posMaster.orgChild2Id = data2.id; + posMaster.orgChild3Id = data3.id; + posMaster.orgChild4Id = data4.id; + posMaster.createdUserId = request.sub; + posMaster.createdFullName = request.name; + posMaster.createdAt = new Date(); + posMaster.lastUpdateUserId = request.sub; + posMaster.lastUpdateFullName = request.name; + posMaster.lastUpdatedAt = new Date(); + await posMasterRepository.save(posMaster); + + // Copy assignments + posMaster.posMasterAssigns = item.posMasterAssigns.map( + ({ id, ...rest }: PosMasterAssign) => ({ + ...rest, + posMasterId: posMaster.id, + }), + ); + + // Create positions + for await (const pos of item.positions) { + delete pos.id; + const position = Object.assign(new Position(), pos); + position.posMasterId = posMaster.id; + if ( + ["ORG_POSITION", "ORG_POSITION_ROLE"].includes( + requestBody.typeDraft.toUpperCase(), + ) + ) { + position.positionIsSelected = false; + } + position.createdUserId = request.sub; + position.createdFullName = request.name; + position.createdAt = new Date(); + position.lastUpdateUserId = request.sub; + position.lastUpdateFullName = request.name; + position.lastUpdatedAt = new Date(); + await positionRepository.save(position); + } + } + } + } + } + } + } + } + } + + const _orgRevisions = await orgRevisionRepository.find({ + where: [{ orgRevisionIsDraft: true, id: Not(revision.id) }], + }); + const _roots = await orgRootRepository.find({ + where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }], + }); + const _posMasters = await posMasterRepository.find({ + where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }], + }); + const _employeePosMasters = await employeePosMasterRepository.find({ + where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }], + }); + const _employeeTempPosMasters = await employeeTempPosMasterRepository.find({ + where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }], + }); + await positionRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) }); + await employeePositionRepository.delete({ + posMasterId: In(_employeePosMasters.map((x) => x.id)), + }); + await employeePositionRepository.delete({ + posMasterTempId: In(_employeeTempPosMasters.map((x) => x.id)), + }); + await posMasterAssignRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) }); + await posMasterActRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) }); + await posMasterActRepository.delete({ + posMasterChildId: In(_posMasters.map((x) => x.id)), + }); + await posMasterRepository.remove(_posMasters); + await employeePosMasterRepository.remove(_employeePosMasters); + await employeeTempPosMasterRepository.remove(_employeeTempPosMasters); + await child4Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); + await child3Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); + await child2Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); + await child1Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); + await permissionOrgRepository.delete({ + orgRootId: In(_roots.map((x) => x.id)), + }); + await orgRootRepository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); + await orgRevisionRepository.remove(_orgRevisions); + + console.log("[AMQ] Create Draft Success"); + return true; + } catch (error) { + console.error(error); + return false; + } +} +