From f4c4804915cf416d723c626292b928329edb51e2 Mon Sep 17 00:00:00 2001 From: kittapath Date: Tue, 11 Mar 2025 18:38:52 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=E0=B9=80=E0=B8=9C?= =?UTF-8?q?=E0=B8=A2=E0=B9=81=E0=B8=9E=E0=B8=A3=E0=B9=88=E0=B9=82=E0=B8=84?= =?UTF-8?q?=E0=B8=A3=E0=B8=87=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 | 1411 +++++++++++---------- src/services/rabbitmq.ts | 902 ++++++++++++- 2 files changed, 1603 insertions(+), 710 deletions(-) diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 8d3fc507..ce43a347 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -35,6 +35,7 @@ import { PosMasterAct } from "../entities/PosMasterAct"; import { EmployeePosition } from "../entities/EmployeePosition"; import { EmployeePosMaster } from "../entities/EmployeePosMaster"; import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster"; +import { AuthRole } from "../entities/AuthRole"; @Route("api/v1/org") @Tags("Organization") @@ -226,7 +227,7 @@ export class OrganizationController extends Controller { //หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna const orgPosMaster = await this.posMasterRepository.find({ where: { orgRevisionId: requestBody.orgRevisionId }, - relations: ["positions"], + relations: ["positions", "posMasterAssigns"], }); let _orgPosMaster: PosMaster[]; @@ -246,51 +247,50 @@ export class OrganizationController extends Controller { await this.posMasterRepository.save(_orgPosMaster); } - //หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna - const orgemployeePosMaster = await this.employeePosMasterRepository.find({ - where: { orgRevisionId: requestBody.orgRevisionId }, - relations: ["positions"], - }); + // //หา 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); - } + // 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"], + // }); - //หา 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); - } + // 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) => { @@ -336,6 +336,7 @@ export class OrganizationController extends Controller { } else { posMaster.authRoleId = null; } + posMaster.current_holderId = null; posMaster.orgRevisionId = revision.id; posMaster.orgRootId = data.id; @@ -347,6 +348,12 @@ 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 }), + ); + posMaster.posMasterAssigns = item.posMasterAssigns; + //create position item.positions.map(async (pos: any) => { delete pos.id; @@ -368,126 +375,126 @@ export class OrganizationController extends Controller { }); }), ); - //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 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 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 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 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 @@ -549,6 +556,12 @@ 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 }), + ); + posMaster.posMasterAssigns = item.posMasterAssigns; + //create position item.positions.map(async (pos: any) => { delete pos.id; @@ -570,130 +583,132 @@ export class OrganizationController extends Controller { }); }), ); - //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.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 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.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.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 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 @@ -757,6 +772,15 @@ 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, + }), + ); + posMaster.posMasterAssigns = item.posMasterAssigns; + //create position item.positions.map(async (pos: any) => { delete pos.id; @@ -778,134 +802,138 @@ export class OrganizationController extends Controller { }); }), ); - //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.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 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.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.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 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 @@ -973,6 +1001,15 @@ 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, + }), + ); + posMaster.posMasterAssigns = item.posMasterAssigns; + //create position item.positions.map(async (pos: any) => { delete pos.id; @@ -994,135 +1031,141 @@ export class OrganizationController extends Controller { }); }), ); - //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.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 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.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.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 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 @@ -1190,6 +1233,15 @@ 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, + }), + ); + posMaster.posMasterAssigns = item.posMasterAssigns; + //create position item.positions.map(async (pos: any) => { delete pos.id; @@ -1211,134 +1263,142 @@ export class OrganizationController extends Controller { }); }), ); - //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.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 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.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.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); - }); - }), - ); + // //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); + // }); + // }), + // ); } }); }); @@ -1359,10 +1419,16 @@ export class OrganizationController extends Controller { 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({ @@ -1370,6 +1436,7 @@ export class OrganizationController extends Controller { }); 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)) }); @@ -3100,7 +3167,7 @@ export class OrganizationController extends Controller { }; const orgRevision = await this.orgRevisionRepository.findOne({ where: { id } }); - + if (!orgRevision) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } @@ -4352,14 +4419,14 @@ export class OrganizationController extends Controller { return new HttpSuccess(); // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่มีข้อมูลเผยแพร่"); } - if (orgRevisionPublish) { - orgRevisionPublish.orgRevisionIsDraft = false; - orgRevisionPublish.orgRevisionIsCurrent = false; - await this.orgRevisionRepository.save(orgRevisionPublish); - } - orgRevisionDraft.orgRevisionIsCurrent = true; - orgRevisionDraft.orgRevisionIsDraft = false; - await this.orgRevisionRepository.save(orgRevisionDraft); + // if (orgRevisionPublish) { + // orgRevisionPublish.orgRevisionIsDraft = false; + // orgRevisionPublish.orgRevisionIsCurrent = false; + // await this.orgRevisionRepository.save(orgRevisionPublish); + // } + // orgRevisionDraft.orgRevisionIsCurrent = true; + // orgRevisionDraft.orgRevisionIsDraft = false; + // await this.orgRevisionRepository.save(orgRevisionDraft); const msg = { data: { id: orgRevisionDraft.id, diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index 65bc4066..416a26bf 100644 --- a/src/services/rabbitmq.ts +++ b/src/services/rabbitmq.ts @@ -11,6 +11,14 @@ import { Profile } from "../entities/Profile"; import { EmployeePosMaster } from "../entities/EmployeePosMaster"; import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster"; import { ProfileEmployee } from "../entities/ProfileEmployee"; +import { OrgRevision } from "../entities/OrgRevision"; +import { request } from "http"; +import { EmployeePosition } from "../entities/EmployeePosition"; +import { OrgChild1 } from "../entities/OrgChild1"; +import { OrgChild2 } from "../entities/OrgChild2"; +import { OrgChild3 } from "../entities/OrgChild3"; +import { OrgChild4 } from "../entities/OrgChild4"; +import { OrgRoot } from "../entities/OrgRoot"; export let sendToQueue: (payload: any) => void; export let sendToQueueOrg: (payload: any) => void; @@ -132,6 +140,36 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { const repoEmployeeTempPosmaster = AppDataSource.getRepository(EmployeeTempPosMaster); const repoProfile = AppDataSource.getRepository(Profile); const repoProfileEmployee = AppDataSource.getRepository(ProfileEmployee); + const employeePositionRepository = AppDataSource.getRepository(EmployeePosition); + const repoOrgRevision = 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); + + const orgRevisionPublish = await repoOrgRevision + .createQueryBuilder("orgRevision") + .where("orgRevision.orgRevisionIsDraft = false") + .andWhere("orgRevision.orgRevisionIsCurrent = true") + .getOne(); + + const orgRevisionDraft = await repoOrgRevision + .createQueryBuilder("orgRevision") + .where("orgRevision.orgRevisionIsDraft = true") + .andWhere("orgRevision.orgRevisionIsCurrent = false") + .getOne(); + if (orgRevisionPublish) { + orgRevisionPublish.orgRevisionIsDraft = false; + orgRevisionPublish.orgRevisionIsCurrent = false; + await repoOrgRevision.save(orgRevisionPublish); + } + if (orgRevisionDraft) { + orgRevisionDraft.orgRevisionIsCurrent = true; + orgRevisionDraft.orgRevisionIsDraft = false; + await repoOrgRevision.save(orgRevisionDraft); + } + const { data, token, user } = JSON.parse(msg.content.toString()); const { id, status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt } = data; try { @@ -149,35 +187,825 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { "positions.posExecutive", ], }); - await Promise.all( - posMaster.map(async (item) => { - if (item.next_holderId != null && status == "NOW") { - const profile = await repoProfile.findOne({ - where: { id: item.next_holderId == null ? "" : item.next_holderId }, - }); - const position = await item.positions.find((x) => x.positionIsSelected == true); - const _null: any = null; - if (profile != null) { - profile.posLevelId = position?.posLevelId ?? _null; - profile.posTypeId = position?.posTypeId ?? _null; - profile.position = position?.positionName ?? _null; - await repoProfile.save(profile); - } + for (const item of posMaster) { + if (item.next_holderId != null && status == "NOW") { + const profile = await repoProfile.findOne({ + where: { id: item.next_holderId == null ? "" : item.next_holderId }, + }); + const position = await item.positions.find((x) => x.positionIsSelected == true); + const _null: any = null; + if (profile != null) { + profile.posLevelId = position?.posLevelId ?? _null; + profile.posTypeId = position?.posTypeId ?? _null; + profile.position = position?.positionName ?? _null; + await repoProfile.save(profile); } - item.current_holderId = item.next_holderId; - item.next_holderId = null; - item.lastUpdateUserId = lastUpdateUserId; - item.lastUpdateFullName = lastUpdateFullName; - item.lastUpdatedAt = lastUpdatedAt; - await repoPosmaster.save(item).catch((e) => console.log(e)); - }), - ); - const employeePosMaster = await repoEmployeePosmaster.find({ - where: { orgRevisionId: id }, - relations: ["positions", "positions.posLevel", "positions.posType"], - }); - await Promise.all( - employeePosMaster.map(async (item) => { + } + item.current_holderId = item.next_holderId; + item.next_holderId = null; + item.lastUpdateUserId = lastUpdateUserId; + item.lastUpdateFullName = lastUpdateFullName; + item.lastUpdatedAt = lastUpdatedAt; + await repoPosmaster.save(item).catch((e) => console.log(e)); + } + + if (orgRevisionPublish != null) { + //new main revision + const before = null; + + //cone tree + // if ( + // orgRevisionPublish.typeDraft.toUpperCase() == "ORG" || + // orgRevisionPublish.typeDraft.toUpperCase() == "ORG_POSITION" || + // orgRevisionPublish.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || + // orgRevisionPublish.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || + // orgRevisionPublish.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + // ) { + //หา dna tree + const orgRoot = await orgRootRepository.find({ + where: { orgRevisionId: orgRevisionPublish.id }, + }); + + const orgChild1 = await child1Repository.find({ + where: { orgRevisionId: orgRevisionPublish.id }, + }); + + const orgChild2 = await child2Repository.find({ + where: { orgRevisionId: orgRevisionPublish.id }, + }); + + const orgChild3 = await child3Repository.find({ + where: { orgRevisionId: orgRevisionPublish.id }, + }); + + const orgChild4 = await child4Repository.find({ + where: { orgRevisionId: orgRevisionPublish.id }, + }); + + //หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna + const orgemployeePosMaster = await repoEmployeePosmaster.find({ + where: { orgRevisionId: orgRevisionPublish.id }, + 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 repoEmployeePosmaster.save(_orgemployeePosMaster); + // } + //หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna + const orgemployeeTempPosMaster = await repoEmployeeTempPosmaster.find({ + where: { orgRevisionId: orgRevisionPublish.id }, + 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 repoEmployeeTempPosmaster.save(_orgemployeeTempPosMaster); + // } + + //create org + orgRoot.forEach(async (x: any) => { + var dataId = x.id; + // 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 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 = orgRevisionPublish.id; + employeePosMaster.orgRootId = dataId; + employeePosMaster.createdUserId = ""; + employeePosMaster.createdFullName = "System Administrator"; + employeePosMaster.createdAt = new Date(); + employeePosMaster.lastUpdateUserId = ""; + employeePosMaster.lastUpdateFullName = "System Administrator"; + employeePosMaster.lastUpdatedAt = new Date(); + await repoEmployeePosmaster.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 = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await 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 = orgRevisionPublish.id; + employeeTempPosMaster.orgRootId = dataId; + employeeTempPosMaster.createdUserId = ""; + employeeTempPosMaster.createdFullName = "System Administrator"; + employeeTempPosMaster.createdAt = new Date(); + employeeTempPosMaster.lastUpdateUserId = ""; + employeeTempPosMaster.lastUpdateFullName = "System Administrator"; + employeeTempPosMaster.lastUpdatedAt = new Date(); + await repoEmployeeTempPosmaster.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 = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await employeePositionRepository.save(employeePosition); + }); + }), + ); + // } + + //create org + orgChild1 + .filter((x: OrgChild1) => x.orgRootId == dataId) + .forEach(async (x: any) => { + var data1Id = x.id; + // 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 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 = orgRevisionPublish.id; + employeePosMaster.orgRootId = dataId; + employeePosMaster.orgChild1Id = data1Id; + employeePosMaster.createdUserId = ""; + employeePosMaster.createdFullName = "System Administrator"; + employeePosMaster.createdAt = new Date(); + employeePosMaster.lastUpdateUserId = ""; + employeePosMaster.lastUpdateFullName = "System Administrator"; + employeePosMaster.lastUpdatedAt = new Date(); + await repoEmployeePosmaster.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 = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await 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 = orgRevisionPublish.id; + employeeTempPosMaster.orgRootId = dataId; + employeeTempPosMaster.orgChild1Id = data1Id; + employeeTempPosMaster.createdUserId = ""; + employeeTempPosMaster.createdFullName = "System Administrator"; + employeeTempPosMaster.createdAt = new Date(); + employeeTempPosMaster.lastUpdateUserId = ""; + employeeTempPosMaster.lastUpdateFullName = "System Administrator"; + employeeTempPosMaster.lastUpdatedAt = new Date(); + await repoEmployeeTempPosmaster.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 = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await employeePositionRepository.save(employeePosition); + }); + }), + ); + // } + + //create org + orgChild2 + .filter((x: OrgChild2) => x.orgChild1Id == data1Id) + .forEach(async (x: any) => { + var data2Id = x.id; + // 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 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 = orgRevisionPublish.id; + employeePosMaster.orgRootId = dataId; + employeePosMaster.orgChild1Id = data1Id; + employeePosMaster.orgChild2Id = data2Id; + employeePosMaster.createdUserId = ""; + employeePosMaster.createdFullName = "System Administrator"; + employeePosMaster.createdAt = new Date(); + employeePosMaster.lastUpdateUserId = ""; + employeePosMaster.lastUpdateFullName = "System Administrator"; + employeePosMaster.lastUpdatedAt = new Date(); + await repoEmployeePosmaster.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 = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await 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 = orgRevisionPublish.id; + employeeTempPosMaster.orgRootId = dataId; + employeeTempPosMaster.orgChild1Id = data1Id; + employeeTempPosMaster.orgChild2Id = data2Id; + employeeTempPosMaster.createdUserId = ""; + employeeTempPosMaster.createdFullName = "System Administrator"; + employeeTempPosMaster.createdAt = new Date(); + employeeTempPosMaster.lastUpdateUserId = ""; + employeeTempPosMaster.lastUpdateFullName = "System Administrator"; + employeeTempPosMaster.lastUpdatedAt = new Date(); + await repoEmployeeTempPosmaster.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 = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await employeePositionRepository.save(employeePosition); + }); + }), + ); + // } + + //create org + orgChild3 + .filter((x: OrgChild3) => x.orgChild2Id == data2Id) + .forEach(async (x: any) => { + var data3Id = x.id; + // 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 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 = orgRevisionPublish.id; + employeePosMaster.orgRootId = dataId; + employeePosMaster.orgChild1Id = data1Id; + employeePosMaster.orgChild2Id = data2Id; + employeePosMaster.orgChild3Id = data3Id; + employeePosMaster.createdUserId = ""; + employeePosMaster.createdFullName = "System Administrator"; + employeePosMaster.createdAt = new Date(); + employeePosMaster.lastUpdateUserId = ""; + employeePosMaster.lastUpdateFullName = "System Administrator"; + employeePosMaster.lastUpdatedAt = new Date(); + await repoEmployeePosmaster.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 = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await 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 = orgRevisionPublish.id; + employeeTempPosMaster.orgRootId = dataId; + employeeTempPosMaster.orgChild1Id = data1Id; + employeeTempPosMaster.orgChild2Id = data2Id; + employeeTempPosMaster.orgChild3Id = data3Id; + employeeTempPosMaster.createdUserId = ""; + employeeTempPosMaster.createdFullName = "System Administrator"; + employeeTempPosMaster.createdAt = new Date(); + employeeTempPosMaster.lastUpdateUserId = ""; + employeeTempPosMaster.lastUpdateFullName = "System Administrator"; + employeeTempPosMaster.lastUpdatedAt = new Date(); + await repoEmployeeTempPosmaster.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 = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await employeePositionRepository.save(employeePosition); + }); + }), + ); + // } + + //create org + orgChild4 + .filter((x: OrgChild4) => x.orgChild3Id == data3Id) + .forEach(async (x: any) => { + var data4Id = x.id; + // 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 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 = orgRevisionPublish.id; + employeePosMaster.orgRootId = dataId; + employeePosMaster.orgChild1Id = data1Id; + employeePosMaster.orgChild2Id = data2Id; + employeePosMaster.orgChild3Id = data3Id; + employeePosMaster.orgChild4Id = data4Id; + employeePosMaster.createdUserId = ""; + employeePosMaster.createdFullName = "System Administrator"; + employeePosMaster.createdAt = new Date(); + employeePosMaster.lastUpdateUserId = ""; + employeePosMaster.lastUpdateFullName = "System Administrator"; + employeePosMaster.lastUpdatedAt = new Date(); + await repoEmployeePosmaster.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 = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await 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 = orgRevisionPublish.id; + employeeTempPosMaster.orgRootId = dataId; + employeeTempPosMaster.orgChild1Id = data1Id; + employeeTempPosMaster.orgChild2Id = data2Id; + employeeTempPosMaster.orgChild3Id = data3Id; + employeeTempPosMaster.orgChild4Id = data4Id; + employeeTempPosMaster.createdUserId = ""; + employeeTempPosMaster.createdFullName = "System Administrator"; + employeeTempPosMaster.createdAt = new Date(); + employeeTempPosMaster.lastUpdateUserId = ""; + employeeTempPosMaster.lastUpdateFullName = "System Administrator"; + employeeTempPosMaster.lastUpdatedAt = new Date(); + await repoEmployeeTempPosmaster.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 = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await employeePositionRepository.save(employeePosition); + }); + }), + ); + // } + }); + }); + }); + }); + }); + // } + + const employeePosMaster = await repoEmployeePosmaster.find({ + where: { orgRevisionId: orgRevisionPublish.id }, + relations: ["positions", "positions.posLevel", "positions.posType"], + }); + for (const item of employeePosMaster) { if (item.next_holderId != null && status == "NOW") { const profile = await repoProfileEmployee.findOne({ where: { id: item.next_holderId == null ? "" : item.next_holderId }, @@ -197,14 +1025,12 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { item.lastUpdateFullName = lastUpdateFullName; item.lastUpdatedAt = lastUpdatedAt; await repoEmployeePosmaster.save(item).catch((e) => console.log(e)); - }), - ); - const employeeTempPosMaster = await repoEmployeeTempPosmaster.find({ - where: { orgRevisionId: id }, - relations: ["positions", "positions.posLevel", "positions.posType"], - }); - await Promise.all( - employeeTempPosMaster.map(async (item) => { + } + const employeeTempPosMaster = await repoEmployeeTempPosmaster.find({ + where: { orgRevisionId: orgRevisionPublish.id }, + relations: ["positions", "positions.posLevel", "positions.posType"], + }); + for (const item of employeeTempPosMaster) { if (item.next_holderId != null && status == "NOW") { const profile = await repoProfileEmployee.findOne({ where: { id: item.next_holderId == null ? "" : item.next_holderId }, @@ -224,8 +1050,8 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { item.lastUpdateFullName = lastUpdateFullName; item.lastUpdatedAt = lastUpdatedAt; await repoEmployeeTempPosmaster.save(item).catch((e) => console.log(e)); - }), - ); + } + } console.log("[AMQ] Excecute Organization Success"); return true; } catch (error) {