From 910e56897359fc7b46c5cf4529c7a7048a0ec522 Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Tue, 26 Aug 2025 13:47:43 +0700 Subject: [PATCH] history update position --- src/controllers/CommandController.ts | 30 ++- src/controllers/EmployeePositionController.ts | 28 ++- .../EmployeeTempPositionController.ts | 25 +- src/controllers/PositionController.ts | 31 ++- src/database/data-source.ts | 4 +- src/entities/PosMasterEmployeeHistory.ts | 101 ++++++++ src/entities/PosMasterEmployeeTempHistory.ts | 101 ++++++++ src/entities/PosMasterHistory.ts | 101 ++++++++ src/services/PositionService.ts | 216 ++++++++++++++++++ src/services/rabbitmq.ts | 4 +- 10 files changed, 625 insertions(+), 16 deletions(-) create mode 100644 src/entities/PosMasterEmployeeHistory.ts create mode 100644 src/entities/PosMasterEmployeeTempHistory.ts create mode 100644 src/entities/PosMasterHistory.ts create mode 100644 src/services/PositionService.ts diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 5dfcf45d..78bb9be3 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -59,7 +59,6 @@ import { createUser, getRoles, deleteUser, - enableStatus, getUserByUsername, getRoleMappings, removeUserRoles, @@ -92,6 +91,10 @@ import { ProfileInsignia, CreateProfileInsignia } from "../entities/ProfileInsig import { ProfileInsigniaHistory } from "../entities/ProfileInsigniaHistory"; import { Gender } from "../entities/Gender"; import { ProfileAvatar } from "../entities/ProfileAvatar"; +import { + CreatePosMasterHistoryEmployee, + CreatePosMasterHistoryOfficer, +} from "../services/PositionService"; @Route("api/v1/org/command") @Tags("Command") @Security("bearerAuth") @@ -3154,8 +3157,12 @@ export class CommandController extends Controller { posMaster.lastUpdatedAt = new Date(); posMaster.conditionReason = _null; posMaster.isCondition = false; - if (posMasterOld != null) await this.posMasterRepository.save(posMasterOld); + if (posMasterOld != null) { + await this.posMasterRepository.save(posMasterOld); + await CreatePosMasterHistoryOfficer(posMasterOld.id, req); + } await this.posMasterRepository.save(posMaster); + await CreatePosMasterHistoryOfficer(posMaster.id, req); const positionNew = await this.positionRepository.findOne({ where: { @@ -3342,8 +3349,12 @@ export class CommandController extends Controller { posMaster.current_holderId = item.profileId; posMaster.lastUpdatedAt = new Date(); posMaster.next_holderId = null; - if (posMasterOld != null) await this.employeePosMasterRepository.save(posMasterOld); + if (posMasterOld != null) { + await this.employeePosMasterRepository.save(posMasterOld); + await CreatePosMasterHistoryEmployee(posMasterOld.id, req); + } await this.employeePosMasterRepository.save(posMaster); + await CreatePosMasterHistoryEmployee(posMaster.id, req); const positionNew = await this.employeePositionRepository.findOne({ where: { @@ -3573,6 +3584,7 @@ export class CommandController extends Controller { posMaster.conditionReason = _null; posMaster.isCondition = false; await this.posMasterRepository.save(posMaster); + await CreatePosMasterHistoryOfficer(posMaster.id, req); const positionNew = await this.positionRepository.findOne({ where: { posMasterId: posMaster.id, @@ -6030,8 +6042,12 @@ export class CommandController extends Controller { posMaster.lastUpdatedAt = new Date(); posMaster.conditionReason = _null; posMaster.isCondition = false; - if (posMasterOld != null) await this.posMasterRepository.save(posMasterOld); + if (posMasterOld != null) { + await this.posMasterRepository.save(posMasterOld); + await CreatePosMasterHistoryOfficer(posMasterOld.id, req); + } await this.posMasterRepository.save(posMaster); + await CreatePosMasterHistoryOfficer(posMaster.id, req); const positionNew = await this.positionRepository.findOne({ where: { @@ -6411,8 +6427,12 @@ export class CommandController extends Controller { posMaster.current_holderId = profile.id; posMaster.lastUpdatedAt = new Date(); posMaster.next_holderId = null; - if (posMasterOld != null) await this.employeePosMasterRepository.save(posMasterOld); + if (posMasterOld != null) { + await this.employeePosMasterRepository.save(posMasterOld); + await CreatePosMasterHistoryEmployee(posMasterOld.id, req); + } await this.employeePosMasterRepository.save(posMaster); + await CreatePosMasterHistoryEmployee(posMaster.id, req); const clsTempPosmaster = await this.employeeTempPosMasterRepository.find({ where: { diff --git a/src/controllers/EmployeePositionController.ts b/src/controllers/EmployeePositionController.ts index 6ef4ffaa..6622f78f 100644 --- a/src/controllers/EmployeePositionController.ts +++ b/src/controllers/EmployeePositionController.ts @@ -38,6 +38,11 @@ import { AuthRole } from "../entities/AuthRole"; import { RequestWithUser } from "../middlewares/user"; import permission from "../interfaces/permission"; import { setLogDataDiff } from "../interfaces/utils"; +import { + CreatePosMasterHistoryOfficer, + CreatePosMasterHistoryEmployee, +} from "../services/PositionService"; +import { PosMasterEmployeeHistory } from "../entities/PosMasterEmployeeHistory"; @Route("api/v1/org/employee/pos") @Tags("Employee") @Security("bearerAuth") @@ -50,6 +55,7 @@ export class EmployeePositionController extends Controller { private employeePosTypeRepository = AppDataSource.getRepository(EmployeePosType); private employeePosLevelRepository = AppDataSource.getRepository(EmployeePosLevel); private employeePosMasterRepository = AppDataSource.getRepository(EmployeePosMaster); + private posMasterHistoryRepository = AppDataSource.getRepository(PosMasterEmployeeHistory); private employeePositionRepository = AppDataSource.getRepository(EmployeePosition); private profileRepository = AppDataSource.getRepository(ProfileEmployee); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); @@ -2265,6 +2271,7 @@ export class EmployeePositionController extends Controller { dataMaster.lastUpdatedAt = new Date(); // dataMaster.next_holderId = requestBody.profileId; await this.employeePosMasterRepository.save(dataMaster); + await CreatePosMasterHistoryEmployee(dataMaster.id, request); return new HttpSuccess(); } @@ -2439,8 +2446,12 @@ export class EmployeePositionController extends Controller { posMaster.current_holderId = body.profileId; posMaster.lastUpdatedAt = new Date(); // posMaster.next_holderId = body.profileId; - if (posMasterOld != null) await this.employeePosMasterRepository.save(posMasterOld); + if (posMasterOld != null) { + await this.employeePosMasterRepository.save(posMasterOld); + await CreatePosMasterHistoryEmployee(posMasterOld.id, request); + } await this.employeePosMasterRepository.save(posMaster); + await CreatePosMasterHistoryEmployee(posMaster.id, request); const positionNew = await this.employeePositionRepository.findOne({ where: { @@ -2463,4 +2474,19 @@ export class EmployeePositionController extends Controller { } return new HttpSuccess(); } + + /** + * API ประวัติแก้ไขตำแหน่ง + * + * @summary ประวัติแก้ไขตำแหน่ง (ADMIN) + * + */ + @Get("history-update/{id}") + async listPosMasterHistory(@Path() id: string, @Request() request: RequestWithUser) { + const posMasterHistory = await this.posMasterHistoryRepository.find({ + where: { ancestorDNA: id }, + }); + + return new HttpSuccess(posMasterHistory); + } } diff --git a/src/controllers/EmployeeTempPositionController.ts b/src/controllers/EmployeeTempPositionController.ts index 7ad6b9c7..d4316e3c 100644 --- a/src/controllers/EmployeeTempPositionController.ts +++ b/src/controllers/EmployeeTempPositionController.ts @@ -41,6 +41,8 @@ import { AuthRole } from "../entities/AuthRole"; import { RequestWithUser } from "../middlewares/user"; import permission from "../interfaces/permission"; import { setLogDataDiff } from "../interfaces/utils"; +import { CreatePosMasterHistoryEmployeeTemp } from "../services/PositionService"; +import { PosMasterEmployeeTempHistory } from "../entities/PosMasterEmployeeTempHistory"; @Route("api/v1/org/employee-temp/pos") @Tags("Employee") @Security("bearerAuth") @@ -53,6 +55,7 @@ export class EmployeeTempPositionController extends Controller { private employeePosTypeRepository = AppDataSource.getRepository(EmployeePosType); private employeePosLevelRepository = AppDataSource.getRepository(EmployeePosLevel); private employeeTempPosMasterRepository = AppDataSource.getRepository(EmployeeTempPosMaster); + private posMasterHistoryRepository = AppDataSource.getRepository(PosMasterEmployeeTempHistory); private employeePositionRepository = AppDataSource.getRepository(EmployeePosition); private profileRepository = AppDataSource.getRepository(ProfileEmployee); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); @@ -2004,6 +2007,7 @@ export class EmployeeTempPositionController extends Controller { dataMaster.lastUpdatedAt = new Date(); // dataMaster.next_holderId = requestBody.profileId; await this.employeeTempPosMasterRepository.save(dataMaster); + await CreatePosMasterHistoryEmployeeTemp(dataMaster.id, request); return new HttpSuccess(); } @@ -2178,8 +2182,12 @@ export class EmployeeTempPositionController extends Controller { posMaster.current_holderId = body.profileId; posMaster.lastUpdatedAt = new Date(); // posMaster.next_holderId = body.profileId; - if (posMasterOld != null) await this.employeeTempPosMasterRepository.save(posMasterOld); + if (posMasterOld != null) { + await this.employeeTempPosMasterRepository.save(posMasterOld); + await CreatePosMasterHistoryEmployeeTemp(posMasterOld.id, request); + } await this.employeeTempPosMasterRepository.save(posMaster); + await CreatePosMasterHistoryEmployeeTemp(posMaster.id, request); const positionNew = await this.employeePositionRepository.findOne({ where: { @@ -2202,4 +2210,19 @@ export class EmployeeTempPositionController extends Controller { } return new HttpSuccess(); } + + /** + * API ประวัติแก้ไขตำแหน่ง + * + * @summary ประวัติแก้ไขตำแหน่ง (ADMIN) + * + */ + @Get("history-update/{id}") + async listPosMasterHistory(@Path() id: string, @Request() request: RequestWithUser) { + const posMasterHistory = await this.posMasterHistoryRepository.find({ + where: { ancestorDNA: id }, + }); + + return new HttpSuccess(posMasterHistory); + } } diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index b233b631..997f5627 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -42,6 +42,8 @@ import { setLogDataDiff } from "../interfaces/utils"; import { PosMasterAssign } from "../entities/PosMasterAssign"; import { Assign } from "../entities/Assign"; import { ProfileEmployee } from "../entities/ProfileEmployee"; +import { PosMasterHistory } from "../entities/PosMasterHistory"; +import { CreatePosMasterHistoryOfficer } from "../services/PositionService"; @Route("api/v1/org/pos") @Tags("Position") @Security("bearerAuth") @@ -57,6 +59,7 @@ export class PositionController extends Controller { private posLevelEmployeeRepository = AppDataSource.getRepository(EmployeePosLevel); private posDictRepository = AppDataSource.getRepository(PosDict); private posMasterRepository = AppDataSource.getRepository(PosMaster); + private posMasterHistoryRepository = AppDataSource.getRepository(PosMasterHistory); private employeePosMasterRepository = AppDataSource.getRepository(EmployeePosMaster); private positionRepository = AppDataSource.getRepository(Position); private profileRepository = AppDataSource.getRepository(Profile); @@ -1541,7 +1544,7 @@ export class PositionController extends Controller { // posLevelRank: "ASC", // }, orderNo: "ASC", - createdAt: "ASC" + createdAt: "ASC", }, }); const formattedData = { @@ -2381,7 +2384,7 @@ export class PositionController extends Controller { // posType: { posTypeRank: "ASC" }, // posLevel: { posLevelRank: "ASC" }, orderNo: "ASC", - createdAt: "ASC" + createdAt: "ASC", }, }); @@ -3654,6 +3657,7 @@ export class PositionController extends Controller { dataMaster.current_holderId = _null; } await this.posMasterRepository.save(dataMaster, { data: request }); + await CreatePosMasterHistoryOfficer(dataMaster.id, request); setLogDataDiff(request, { before, after: dataMaster }); return new HttpSuccess(); @@ -4826,6 +4830,7 @@ export class PositionController extends Controller { positionId: string; profileId: string; }, + @Request() request: RequestWithUser, ) { const posMaster = await this.posMasterRepository.findOne({ where: { id: body.posmasterId }, @@ -4879,8 +4884,12 @@ export class PositionController extends Controller { const _null: any = null; posMaster.conditionReason = _null; posMaster.isCondition = false; - if (posMasterOld != null) await this.posMasterRepository.save(posMasterOld); + if (posMasterOld != null) { + await this.posMasterRepository.save(posMasterOld); + await CreatePosMasterHistoryOfficer(posMasterOld.id, request); + } await this.posMasterRepository.save(posMaster); + await CreatePosMasterHistoryOfficer(posMaster.id, request); const positionNew = await this.positionRepository.findOne({ where: { @@ -5432,7 +5441,6 @@ export class PositionController extends Controller { posMaster.lastUpdatedAt = new Date(); posMaster.lastUpdateUserId = request.user.sub; posMaster.lastUpdateFullName = request.user.name; - posMaster.lastUpdatedAt = new Date(); await this.posMasterRepository.save(posMaster); return new HttpSuccess(); } @@ -5617,4 +5625,19 @@ export class PositionController extends Controller { } return new HttpSuccess(); } + + /** + * API ประวัติแก้ไขตำแหน่ง + * + * @summary ประวัติแก้ไขตำแหน่ง (ADMIN) + * + */ + @Get("history-update/{id}") + async listPosMasterHistory(@Path() id: string, @Request() request: RequestWithUser) { + const posMasterHistory = await this.posMasterHistoryRepository.find({ + where: { ancestorDNA: id }, + }); + + return new HttpSuccess(posMasterHistory); + } } diff --git a/src/database/data-source.ts b/src/database/data-source.ts index 655d6386..9de306d7 100644 --- a/src/database/data-source.ts +++ b/src/database/data-source.ts @@ -47,9 +47,7 @@ export const AppDataSource = new DataSource({ logging: true, // timezone: "Z", entities: - process.env.NODE_ENV !== "production" - ? ["src/entities/**/*.ts"] - : ["dist/entities/**/*{.ts,.js}"], + process.env.NODE_ENV !== "production" ? ["src/entities/*.ts"] : ["dist/entities/*{.ts,.js}"], migrations: process.env.NODE_ENV !== "production" ? ["src/migration/**/*.ts"] diff --git a/src/entities/PosMasterEmployeeHistory.ts b/src/entities/PosMasterEmployeeHistory.ts new file mode 100644 index 00000000..7dfa3b0c --- /dev/null +++ b/src/entities/PosMasterEmployeeHistory.ts @@ -0,0 +1,101 @@ +import { Entity, Column } from "typeorm"; +import { EntityBase } from "./base/Base"; + +@Entity("posMasterEmployeeHistory") +export class PosMasterEmployeeHistory extends EntityBase { + @Column({ + nullable: true, + comment: "คำนำหน้า", + length: 255, + default: null, + }) + prefix: string; + + @Column({ + nullable: true, + comment: "ชื่อ", + length: 255, + default: null, + }) + firstName: string; + + @Column({ + nullable: true, + comment: "สกุล", + length: 255, + default: null, + }) + lastName: string; + + @Column({ + nullable: true, + comment: "อักษรย่อ", + length: 16, + default: null, + }) + shortName: string; + + @Column({ + nullable: true, + comment: "Prefix นำหน้าเลขที่ตำแหน่ง เป็น Optional (ไม่ใช่อักษรย่อของหน่วยงาน/ส่วนราชการ)", + length: 16, + default: null, + }) + posMasterNoPrefix: string; + + @Column({ + nullable: true, + comment: "เลขที่ตำแหน่ง เป็นตัวเลข", + default: null, + }) + posMasterNo: number; + + @Column({ + nullable: true, + comment: "Suffix หลังเลขที่ตำแหน่ง เช่น ช.", + length: 16, + default: null, + }) + posMasterNoSuffix: string; + + @Column({ + nullable: true, + comment: "ชื่อตำแหน่ง", + length: 255, + default: null, + }) + position: string; + + @Column({ + nullable: true, + comment: "ชื่อประเภทตำแหน่ง", + length: 255, + default: null, + }) + posType: string; + + @Column({ + nullable: true, + comment: "ชื่อระดับตำแหน่ง", + length: 255, + default: null, + }) + posLevel: string; + + @Column({ + nullable: true, + comment: "ชื่อตำแหน่งทางการบริหาร", + length: 255, + default: null, + }) + posExecutive: string; + + @Column({ + nullable: true, + comment: + "รหัส DNA ใช้ในกรณีที่มีการทำสำเนาโครงสร้างและตำแหน่ง ตำแหน่งที่ทำสำเนามากับตำแหน่งเก่าจะต้องมี DNA เดียวกัน เพื่อให้ track ประวัติการแก้ไขตำแหน่งย้อนหลังได้", + length: 40, + default: null, + }) + ancestorDNA: string; +} diff --git a/src/entities/PosMasterEmployeeTempHistory.ts b/src/entities/PosMasterEmployeeTempHistory.ts new file mode 100644 index 00000000..c664959d --- /dev/null +++ b/src/entities/PosMasterEmployeeTempHistory.ts @@ -0,0 +1,101 @@ +import { Entity, Column } from "typeorm"; +import { EntityBase } from "./base/Base"; + +@Entity("posMasterEmployeeTempHistory") +export class PosMasterEmployeeTempHistory extends EntityBase { + @Column({ + nullable: true, + comment: "คำนำหน้า", + length: 255, + default: null, + }) + prefix: string; + + @Column({ + nullable: true, + comment: "ชื่อ", + length: 255, + default: null, + }) + firstName: string; + + @Column({ + nullable: true, + comment: "สกุล", + length: 255, + default: null, + }) + lastName: string; + + @Column({ + nullable: true, + comment: "อักษรย่อ", + length: 16, + default: null, + }) + shortName: string; + + @Column({ + nullable: true, + comment: "Prefix นำหน้าเลขที่ตำแหน่ง เป็น Optional (ไม่ใช่อักษรย่อของหน่วยงาน/ส่วนราชการ)", + length: 16, + default: null, + }) + posMasterNoPrefix: string; + + @Column({ + nullable: true, + comment: "เลขที่ตำแหน่ง เป็นตัวเลข", + default: null, + }) + posMasterNo: number; + + @Column({ + nullable: true, + comment: "Suffix หลังเลขที่ตำแหน่ง เช่น ช.", + length: 16, + default: null, + }) + posMasterNoSuffix: string; + + @Column({ + nullable: true, + comment: "ชื่อตำแหน่ง", + length: 255, + default: null, + }) + position: string; + + @Column({ + nullable: true, + comment: "ชื่อประเภทตำแหน่ง", + length: 255, + default: null, + }) + posType: string; + + @Column({ + nullable: true, + comment: "ชื่อระดับตำแหน่ง", + length: 255, + default: null, + }) + posLevel: string; + + @Column({ + nullable: true, + comment: "ชื่อตำแหน่งทางการบริหาร", + length: 255, + default: null, + }) + posExecutive: string; + + @Column({ + nullable: true, + comment: + "รหัส DNA ใช้ในกรณีที่มีการทำสำเนาโครงสร้างและตำแหน่ง ตำแหน่งที่ทำสำเนามากับตำแหน่งเก่าจะต้องมี DNA เดียวกัน เพื่อให้ track ประวัติการแก้ไขตำแหน่งย้อนหลังได้", + length: 40, + default: null, + }) + ancestorDNA: string; +} diff --git a/src/entities/PosMasterHistory.ts b/src/entities/PosMasterHistory.ts new file mode 100644 index 00000000..4360cf1e --- /dev/null +++ b/src/entities/PosMasterHistory.ts @@ -0,0 +1,101 @@ +import { Entity, Column } from "typeorm"; +import { EntityBase } from "./base/Base"; + +@Entity("posMasterHistory") +export class PosMasterHistory extends EntityBase { + @Column({ + nullable: true, + comment: "คำนำหน้า", + length: 255, + default: null, + }) + prefix: string; + + @Column({ + nullable: true, + comment: "ชื่อ", + length: 255, + default: null, + }) + firstName: string; + + @Column({ + nullable: true, + comment: "สกุล", + length: 255, + default: null, + }) + lastName: string; + + @Column({ + nullable: true, + comment: "อักษรย่อ", + length: 16, + default: null, + }) + shortName: string; + + @Column({ + nullable: true, + comment: "Prefix นำหน้าเลขที่ตำแหน่ง เป็น Optional (ไม่ใช่อักษรย่อของหน่วยงาน/ส่วนราชการ)", + length: 16, + default: null, + }) + posMasterNoPrefix: string; + + @Column({ + nullable: true, + comment: "เลขที่ตำแหน่ง เป็นตัวเลข", + default: null, + }) + posMasterNo: number; + + @Column({ + nullable: true, + comment: "Suffix หลังเลขที่ตำแหน่ง เช่น ช.", + length: 16, + default: null, + }) + posMasterNoSuffix: string; + + @Column({ + nullable: true, + comment: "ชื่อตำแหน่ง", + length: 255, + default: null, + }) + position: string; + + @Column({ + nullable: true, + comment: "ชื่อประเภทตำแหน่ง", + length: 255, + default: null, + }) + posType: string; + + @Column({ + nullable: true, + comment: "ชื่อระดับตำแหน่ง", + length: 255, + default: null, + }) + posLevel: string; + + @Column({ + nullable: true, + comment: "ชื่อตำแหน่งทางการบริหาร", + length: 255, + default: null, + }) + posExecutive: string; + + @Column({ + nullable: true, + comment: + "รหัส DNA ใช้ในกรณีที่มีการทำสำเนาโครงสร้างและตำแหน่ง ตำแหน่งที่ทำสำเนามากับตำแหน่งเก่าจะต้องมี DNA เดียวกัน เพื่อให้ track ประวัติการแก้ไขตำแหน่งย้อนหลังได้", + length: 40, + default: null, + }) + ancestorDNA: string; +} diff --git a/src/services/PositionService.ts b/src/services/PositionService.ts new file mode 100644 index 00000000..cbf842bb --- /dev/null +++ b/src/services/PositionService.ts @@ -0,0 +1,216 @@ +import { AppDataSource } from "../database/data-source"; +import { EmployeePosMaster } from "../entities/EmployeePosMaster"; +import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster"; +import { PosMaster } from "../entities/PosMaster"; +import { PosMasterEmployeeHistory } from "../entities/PosMasterEmployeeHistory"; +import { PosMasterEmployeeTempHistory } from "../entities/PosMasterEmployeeTempHistory"; +import { PosMasterHistory } from "../entities/PosMasterHistory"; +import { RequestWithUser } from "../middlewares/user"; + +export async function CreatePosMasterHistoryOfficer( + posMasterId: string, + request: RequestWithUser | null, +): Promise { + try { + await AppDataSource.transaction(async (manager) => { + const repoPosmaster = manager.getRepository(PosMaster); + const repoHistory = manager.getRepository(PosMasterHistory); + + const pm = await repoPosmaster.findOne({ + where: { id: posMasterId }, + relations: [ + "positions", + "positions.posLevel", + "positions.posType", + "positions.posExecutive", + "orgRoot", + "orgChild1", + "orgChild2", + "orgChild3", + "orgChild4", + "current_holder", + ], + }); + + if (!pm) return false; + if (!pm.ancestorDNA) return false; + const _null: any = null; + const h = new PosMasterHistory(); + const selectedPosition = + pm.positions.length > 0 + ? pm.positions.find((p) => p.positionIsSelected === true) ?? null + : null; + h.ancestorDNA = pm.ancestorDNA; + h.prefix = pm.current_holder?.prefix || _null; + h.firstName = pm.current_holder?.firstName || _null; + h.lastName = pm.current_holder?.lastName || _null; + h.posMasterNoPrefix = pm.posMasterNoPrefix ?? _null; + h.posMasterNo = pm.posMasterNo ?? _null; + h.posMasterNoSuffix = pm.posMasterNoSuffix ?? _null; + h.position = selectedPosition?.positionName ?? _null; + h.posType = selectedPosition?.posType?.posTypeName ?? _null; + h.posLevel = selectedPosition?.posLevel?.posLevelName ?? _null; + h.posExecutive = selectedPosition?.posExecutive?.posExecutiveName ?? _null; + h.shortName = + [ + pm.orgChild4?.orgChild4ShortName, + pm.orgChild3?.orgChild3ShortName, + pm.orgChild2?.orgChild2ShortName, + pm.orgChild1?.orgChild1ShortName, + pm.orgRoot?.orgRootShortName, + ].find((s) => typeof s === "string" && s.trim().length > 0) ?? _null; + const userId = request?.user?.sub ?? ""; + const userName = request?.user?.name ?? "system"; + h.createdUserId = userId; + h.createdFullName = userName; + h.lastUpdateUserId = userId; + h.lastUpdateFullName = userName; + h.createdAt = new Date(); + h.lastUpdatedAt = new Date(); + await repoHistory.save(h); + }); + + return true; + } catch (err) { + console.error("CreatePosMasterHistoryOfficer transaction error:", err); + return false; + } +} + +export async function CreatePosMasterHistoryEmployee( + posMasterId: string, + request: RequestWithUser | null, +): Promise { + try { + await AppDataSource.transaction(async (manager) => { + const repoPosmaster = manager.getRepository(EmployeePosMaster); + const repoHistory = manager.getRepository(PosMasterEmployeeHistory); + + const pm = await repoPosmaster.findOne({ + where: { id: posMasterId }, + relations: [ + "positions", + "positions.posLevel", + "positions.posType", + "positions.posExecutive", + "orgRoot", + "orgChild1", + "orgChild2", + "orgChild3", + "orgChild4", + "current_holder", + ], + }); + + if (!pm) return false; + if (!pm.ancestorDNA) return false; + const _null: any = null; + const h = new PosMasterEmployeeHistory(); + const selectedPosition = + pm.positions.length > 0 + ? pm.positions.find((p) => p.positionIsSelected === true) ?? null + : null; + h.ancestorDNA = pm.ancestorDNA; + h.prefix = pm.current_holder?.prefix || _null; + h.firstName = pm.current_holder?.firstName || _null; + h.lastName = pm.current_holder?.lastName || _null; + h.posMasterNoPrefix = pm.posMasterNoPrefix ?? _null; + h.posMasterNo = pm.posMasterNo ?? _null; + h.posMasterNoSuffix = pm.posMasterNoSuffix ?? _null; + h.position = selectedPosition?.positionName ?? _null; + h.posType = selectedPosition?.posType?.posTypeName ?? _null; + h.posLevel = selectedPosition?.posLevel?.posLevelName ?? _null; + h.shortName = + [ + pm.orgChild4?.orgChild4ShortName, + pm.orgChild3?.orgChild3ShortName, + pm.orgChild2?.orgChild2ShortName, + pm.orgChild1?.orgChild1ShortName, + pm.orgRoot?.orgRootShortName, + ].find((s) => typeof s === "string" && s.trim().length > 0) ?? _null; + const userId = request?.user?.sub ?? ""; + const userName = request?.user?.name ?? "system"; + h.createdUserId = userId; + h.createdFullName = userName; + h.lastUpdateUserId = userId; + h.lastUpdateFullName = userName; + h.createdAt = new Date(); + h.lastUpdatedAt = new Date(); + await repoHistory.save(h); + }); + + return true; + } catch (err) { + console.error("CreatePosMasterHistoryEmployee transaction error:", err); + return false; + } +} + +export async function CreatePosMasterHistoryEmployeeTemp( + posMasterId: string, + request: RequestWithUser | null, +): Promise { + try { + await AppDataSource.transaction(async (manager) => { + const repoPosmaster = manager.getRepository(EmployeeTempPosMaster); + const repoHistory = manager.getRepository(PosMasterEmployeeTempHistory); + + const pm = await repoPosmaster.findOne({ + where: { id: posMasterId }, + relations: [ + "positions", + "positions.posLevel", + "positions.posType", + "positions.posExecutive", + "orgRoot", + "orgChild1", + "orgChild2", + "orgChild3", + "orgChild4", + "current_holder", + ], + }); + + if (!pm) return false; + if (!pm.ancestorDNA) return false; + const _null: any = null; + const h = new PosMasterEmployeeTempHistory(); + const selectedPosition = + pm.positions.length > 0 + ? pm.positions.find((p) => p.positionIsSelected === true) ?? null + : null; + h.ancestorDNA = pm.ancestorDNA; + h.prefix = pm.current_holder?.prefix || _null; + h.firstName = pm.current_holder?.firstName || _null; + h.lastName = pm.current_holder?.lastName || _null; + h.posMasterNoPrefix = pm.posMasterNoPrefix ?? _null; + h.posMasterNo = pm.posMasterNo ?? _null; + h.posMasterNoSuffix = pm.posMasterNoSuffix ?? _null; + h.position = selectedPosition?.positionName ?? _null; + h.posType = selectedPosition?.posType?.posTypeName ?? _null; + h.posLevel = selectedPosition?.posLevel?.posLevelName ?? _null; + h.shortName = + [ + pm.orgChild4?.orgChild4ShortName, + pm.orgChild3?.orgChild3ShortName, + pm.orgChild2?.orgChild2ShortName, + pm.orgChild1?.orgChild1ShortName, + pm.orgRoot?.orgRootShortName, + ].find((s) => typeof s === "string" && s.trim().length > 0) ?? _null; + const userId = request?.user?.sub ?? ""; + const userName = request?.user?.name ?? "system"; + h.createdUserId = userId; + h.createdFullName = userName; + h.lastUpdateUserId = userId; + h.lastUpdateFullName = userName; + h.createdAt = new Date(); + h.lastUpdatedAt = new Date(); + await repoHistory.save(h); + }); + + return true; + } catch (err) { + console.error("CreatePosMasterHistoryEmployeeTemp transaction error:", err); + return false; + } +} diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index 183a0e94..8c19f8f7 100644 --- a/src/services/rabbitmq.ts +++ b/src/services/rabbitmq.ts @@ -5,14 +5,12 @@ import { chunkArray, commandTypePath } from "../interfaces/utils"; import CallAPI from "../interfaces/call-api"; import HttpError from "../interfaces/http-error"; 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"; import { OrgRevision } from "../entities/OrgRevision"; -import { request } from "http"; import { EmployeePosition } from "../entities/EmployeePosition"; import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild2 } from "../entities/OrgChild2"; @@ -25,6 +23,7 @@ import { In, Not } from "typeorm"; import { PosMasterAct } from "../entities/PosMasterAct"; import { PermissionOrg } from "../entities/PermissionOrg"; import { sendWebSocket } from "./webSocket"; +import { CreatePosMasterHistoryOfficer } from "./PositionService"; export let sendToQueue: (payload: any) => void; export let sendToQueueOrg: (payload: any) => void; @@ -580,6 +579,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { item.lastUpdateFullName = lastUpdateFullName; item.lastUpdatedAt = lastUpdatedAt; await repoPosmaster.save(item).catch((e) => console.log(e)); + await CreatePosMasterHistoryOfficer(item.id, null); } if (orgRevisionPublish != null && orgRevisionDraft != null) { //new main revision