From a064753efda120e3f1106dae88b5f58f0f0dd3c2 Mon Sep 17 00:00:00 2001 From: kittapath Date: Wed, 26 Feb 2025 17:05:13 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=9A=E0=B8=9A=E0=B8=A3=E0=B9=88?= =?UTF-8?q?=E0=B8=B2=E0=B8=87=E0=B8=A5=E0=B8=B9=E0=B8=81=E0=B8=88=E0=B9=89?= =?UTF-8?q?=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 | 678 ++++++++++++++++++ ...40561148833-updateprofilesalaryaddrefid.ts | 18 + src/services/rabbitmq.ts | 63 +- 3 files changed, 757 insertions(+), 2 deletions(-) create mode 100644 src/migration/1740561148833-updateprofilesalaryaddrefid.ts diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 79ef4608..20438a6d 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -34,6 +34,7 @@ import { PosMasterAssign } from "../entities/PosMasterAssign"; import { PosMasterAct } from "../entities/PosMasterAct"; import { EmployeePosition } from "../entities/EmployeePosition"; import { EmployeePosMaster } from "../entities/EmployeePosMaster"; +import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster"; @Route("api/v1/org") @Tags("Organization") @@ -57,6 +58,7 @@ export class OrganizationController extends Controller { private profileRepo = AppDataSource.getRepository(Profile); private employeePosMasterRepository = AppDataSource.getRepository(EmployeePosMaster); private employeePositionRepository = AppDataSource.getRepository(EmployeePosition); + private employeeTempPosMasterRepository = AppDataSource.getRepository(EmployeeTempPosMaster); /** * API ล้างข้อมูล @@ -243,6 +245,53 @@ 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"], + }); + + let _orgemployeePosMaster: EmployeePosMaster[]; + if ( + requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + ) { + _orgemployeePosMaster = orgemployeePosMaster.map((x) => ({ + ...x, + ancestorDNA: + x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" + ? x.id + : x.ancestorDNA, + })); + await this.employeePosMasterRepository.save(_orgemployeePosMaster); + } + + //หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna + const orgemployeeTempPosMaster = await this.employeeTempPosMasterRepository.find({ + where: { orgRevisionId: requestBody.orgRevisionId }, + relations: ["positions"], + }); + + let _orgemployeeTempPosMaster: EmployeeTempPosMaster[]; + if ( + requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + ) { + _orgemployeeTempPosMaster = orgemployeeTempPosMaster.map((x) => ({ + ...x, + ancestorDNA: + x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" + ? x.id + : x.ancestorDNA, + })); + await this.employeeTempPosMasterRepository.save(_orgemployeeTempPosMaster); + } + //create org _orgRoot.forEach(async (x: any) => { var dataId = x.id; @@ -319,6 +368,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.next_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.next_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 org @@ -401,6 +570,130 @@ 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.next_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.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.next_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 org @@ -485,6 +778,134 @@ 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.next_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.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.next_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 org @@ -573,6 +994,135 @@ 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.next_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.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.next_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 org @@ -661,6 +1211,134 @@ 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.next_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.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.next_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); + }); + }), + ); } }); }); diff --git a/src/migration/1740561148833-updateprofilesalaryaddrefid.ts b/src/migration/1740561148833-updateprofilesalaryaddrefid.ts new file mode 100644 index 00000000..17b538e3 --- /dev/null +++ b/src/migration/1740561148833-updateprofilesalaryaddrefid.ts @@ -0,0 +1,18 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class Updateprofilesalaryaddrefid1740561148833 implements MigrationInterface { + name = 'Updateprofilesalaryaddrefid1740561148833' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TABLE \`ProfileSalaries\` (\`id\` varchar(36) NOT NULL, \`createdAt\` datetime(6) NOT NULL COMMENT 'สร้างข้อมูลเมื่อ' DEFAULT CURRENT_TIMESTAMP(6), \`createdUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่สร้างข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`lastUpdatedAt\` datetime(6) NOT NULL COMMENT 'แก้ไขข้อมูลล่าสุดเมื่อ' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`lastUpdateUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่แก้ไขข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`createdFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่สร้างข้อมูล' DEFAULT 'string', \`lastUpdateFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่แก้ไขข้อมูลล่าสุด' DEFAULT 'string', \`Order\` int NULL, \`Date\` datetime NULL, \`SalaryRef\` varchar(255) NULL, \`Amount\` double NULL, \`PositionSalaryAmount\` double NULL, \`PosNoName\` varchar(255) NULL, \`PositionTypeName\` varchar(255) NULL, \`PositionLevelName\` varchar(255) NULL, \`PositionName\` varchar(255) NULL, \`ProfileId\` varchar(255) NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`); + await queryRunner.query(`ALTER TABLE \`profileSalary\` ADD \`refId\` int NULL COMMENT 'refId'`); + await queryRunner.query(`ALTER TABLE \`profileSalary\` ADD \`isEntry\` tinyint NULL COMMENT 'ข้อมูลจาก Entry'`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileSalary\` DROP COLUMN \`isEntry\``); + await queryRunner.query(`ALTER TABLE \`profileSalary\` DROP COLUMN \`refId\``); + await queryRunner.query(`DROP TABLE \`ProfileSalaries\``); + } + +} diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index 7b1d35dd..65bc4066 100644 --- a/src/services/rabbitmq.ts +++ b/src/services/rabbitmq.ts @@ -8,6 +8,9 @@ import HttpStatusCode from "../interfaces/http-status"; import { RequestWithUser } from "../middlewares/user"; import { PosMaster } from "../entities/PosMaster"; import { Profile } from "../entities/Profile"; +import { EmployeePosMaster } from "../entities/EmployeePosMaster"; +import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster"; +import { ProfileEmployee } from "../entities/ProfileEmployee"; export let sendToQueue: (payload: any) => void; export let sendToQueueOrg: (payload: any) => void; @@ -42,7 +45,7 @@ export async function init() { console.log("[AMQ] Listening for message..."); createConsumer(queue, channel, handler), //----> (3) Process Consumer - createConsumer(queue_org, channel, handler_org); + createConsumer(queue_org, channel, handler_org); // createConsumer(queue2, channel, handler2); } @@ -125,7 +128,10 @@ async function handler(msg: amqp.ConsumeMessage): Promise { async function handler_org(msg: amqp.ConsumeMessage): Promise { //----> condition before process consume const repoPosmaster = AppDataSource.getRepository(PosMaster); + const repoEmployeePosmaster = AppDataSource.getRepository(EmployeePosMaster); + const repoEmployeeTempPosmaster = AppDataSource.getRepository(EmployeeTempPosMaster); const repoProfile = AppDataSource.getRepository(Profile); + const repoProfileEmployee = AppDataSource.getRepository(ProfileEmployee); const { data, token, user } = JSON.parse(msg.content.toString()); const { id, status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt } = data; try { @@ -166,6 +172,60 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { 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) => { + if (item.next_holderId != null && status == "NOW") { + const profile = await repoProfileEmployee.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 repoProfileEmployee.save(profile); + } + } + // item.current_holderId = item.next_holderId; + // item.next_holderId = null; + item.lastUpdateUserId = lastUpdateUserId; + 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) => { + if (item.next_holderId != null && status == "NOW") { + const profile = await repoProfileEmployee.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 repoProfileEmployee.save(profile); + } + } + // item.current_holderId = item.next_holderId; + // item.next_holderId = null; + item.lastUpdateUserId = lastUpdateUserId; + 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) { @@ -173,4 +233,3 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { return false; } } -