From c9a682f502b7a6963f030f4ba44dcc6b017c40ec Mon Sep 17 00:00:00 2001 From: harid Date: Mon, 29 Jun 2026 14:31:27 +0700 Subject: [PATCH] Linear Flow Salary #224 --- src/controllers/ReportController.ts | 995 +++++++++++++++++----------- 1 file changed, 626 insertions(+), 369 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 2e53835..7d6d44e 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -9074,77 +9074,131 @@ export class ReportController extends Controller { */ @Post("command33/officer/report/excecute") async SalaryReportExcecute33( - @Body() - body: { - refIds: { - refId: string; - // commandAffectDate: Date | null;//เก่า - commandDateAffect: Date | null; //ใหม่ (ปรับตาม rabbitMQ ORG) - commandNo: string | null; - commandYear: number | null; - commandId: string | null; - remark: string | null; - amount: Double | null; - amountSpecial?: Double | null; - positionSalaryAmount: Double | null; - mouthSalaryAmount: Double | null; - commandCode?: string | null; - commandName?: string | null; - commandDateSign: Date | null; - }[]; - }, - @Request() request: RequestWithUser, + @Body() + body: { + refIds: { + refId: string; + commandDateAffect: Date | null; + commandNo: string | null; + commandYear: number | null; + commandId: string | null; + remark: string | null; + amount: Double | null; + amountSpecial?: Double | null; + positionSalaryAmount: Double | null; + mouthSalaryAmount: Double | null; + commandCode?: string | null; + commandName?: string | null; + commandDateSign: Date | null; + }[]; + }, + @Request() request: RequestWithUser, ) { - await Promise.all( - body.refIds.map(async (v) => { - const salary = await this.salaryProfileRepository.findOne({ - where: { - id: v.refId, - }, - }); - if (salary != null) { - await new CallAPI() - .PostData(request, "/org/profile/salary/update", { - profileId: salary.profileId, - commandDateAffect: v.commandDateAffect, - commandDateSign: v.commandDateSign, - amount: v.amount, - amountSpecial: v.amountSpecial, - positionSalaryAmount: v.positionSalaryAmount, - mouthSalaryAmount: v.mouthSalaryAmount, - posNo: salary.posMasterNo.toString(), - posNoAbb: salary.orgShortName, - positionName: salary.position, - positionExecutive: salary.posExecutive, - positionExecutiveField: salary.positionExecutiveField, - positionArea: salary.positionArea, - positionType: salary.posType, - positionLevel: salary.posLevel, - commandId: v.commandId, - remark: v.remark, - orgRoot: salary.root, - orgChild1: salary.child1, - orgChild2: salary.child2, - orgChild3: salary.child3, - orgChild4: salary.child4, - commandCode: v.commandCode, - commandName: v.commandName, - commandNo: v.commandNo, - commandYear: v.commandYear, - }) - .then(async () => { - const before = null; - salary.status33 = "DONE"; - salary.lastUpdateUserId = request.user.sub; - salary.lastUpdateFullName = request.user.name; - salary.lastUpdatedAt = new Date(); - await this.salaryProfileRepository.save(salary, { data: request }); - setLogDataDiff(request, { before, after: salary }); - }); - } - }), - ); - return new HttpSuccess(); + // ═══════════════════════════════════════════════════════════════ + // old circle flow (เก็บไว้อ้างอิง — ลบการใช้งานจริงออกแล้ว) + // ═══════════════════════════════════════════════════════════════ + // await Promise.all( + // body.refIds.map(async (v) => { + // const salary = await this.salaryProfileRepository.findOne({ + // where: { id: v.refId }, + // }); + // if (salary != null) { + // // เดิม: ยิง callback เข้า org เพื่อให้ org สร้าง salary record + // await new CallAPI() + // .PostData(request, "/org/profile/salary/update", { + // profileId: salary.profileId, + // commandDateAffect: v.commandDateAffect, + // commandDateSign: v.commandDateSign, + // amount: v.amount, + // amountSpecial: v.amountSpecial, + // positionSalaryAmount: v.positionSalaryAmount, + // mouthSalaryAmount: v.mouthSalaryAmount, + // posNo: salary.posMasterNo.toString(), + // posNoAbb: salary.orgShortName, + // positionName: salary.position, + // positionExecutive: salary.posExecutive, + // positionExecutiveField: salary.positionExecutiveField, + // positionArea: salary.positionArea, + // positionType: salary.posType, + // positionLevel: salary.posLevel, + // commandId: v.commandId, + // remark: v.remark, + // orgRoot: salary.root, + // orgChild1: salary.child1, + // orgChild2: salary.child2, + // orgChild3: salary.child3, + // orgChild4: salary.child4, + // commandCode: v.commandCode, + // commandName: v.commandName, + // commandNo: v.commandNo, + // commandYear: v.commandYear, + // }) + // // เดิม: set status33 ใน .then() หลัง org ตอบกลับสำเร็จเท่านั้น + // .then(async () => { + // const before = null; + // salary.status33 = "DONE"; + // salary.lastUpdateUserId = request.user.sub; + // salary.lastUpdateFullName = request.user.name; + // salary.lastUpdatedAt = new Date(); + // await this.salaryProfileRepository.save(salary, { data: request }); + // setLogDataDiff(request, { before, after: salary }); + // }); + // } + // }), + // ); + // return new HttpSuccess(); + + // ═══════════════════════════════════════════════════════════════ + // new linear flow + // ═══════════════════════════════════════════════════════════════ + // เปลี่ยน: ไม่ยิง callback เข้า org แล้ว → ทำงานของตัวเอง (set status) แล้ว return data + // ให้ org ทำงานต่อ (org AMQ จะเรียก ExecuteSalaryReportService เองหลังได้รับ response) + const result = await Promise.all( + body.refIds.map(async (v) => { + const salary = await this.salaryProfileRepository.findOne({ where: { id: v.refId } }); + if (salary != null) { + // 1) set status33 = DONE (ย้ายขึ้นมาทำก่อนเลย ไม่ต้องรอ org ตอบกลับ) + salary.status33 = "DONE"; + salary.lastUpdateUserId = request.user.sub; + salary.lastUpdateFullName = request.user.name; + salary.lastUpdatedAt = new Date(); + await this.salaryProfileRepository.save(salary, { data: request }); + setLogDataDiff(request, { before: null, after: salary }); + + // 2) return ข้อมูลที่ org ต้องการสร้าง ProfileSalary (shape เดียวกับที่เคย PostData) + return { + profileId: salary.profileId, + commandDateAffect: v.commandDateAffect, + commandDateSign: v.commandDateSign, + amount: v.amount, + amountSpecial: v.amountSpecial, + positionSalaryAmount: v.positionSalaryAmount, + mouthSalaryAmount: v.mouthSalaryAmount, + posNo: salary.posMasterNo.toString(), + posNoAbb: salary.orgShortName, + positionName: salary.position, + positionExecutive: salary.posExecutive, + positionExecutiveField: salary.positionExecutiveField, + positionArea: salary.positionArea, + positionType: salary.posType, + positionLevel: salary.posLevel, + commandId: v.commandId, + remark: v.remark, + orgRoot: salary.root, + orgChild1: salary.child1, + orgChild2: salary.child2, + orgChild3: salary.child3, + orgChild4: salary.child4, + commandCode: v.commandCode, + commandName: v.commandName, + commandNo: v.commandNo, + commandYear: v.commandYear, + }; + } + return null; // refId ที่ไม่พบ salary → กรองออก (เหมือนเดิมที่ข้ามไป) + }), + ); + return result.filter(Boolean); // return array ของ salary data ให้ org ทำงานต่อ } /** @@ -9155,80 +9209,129 @@ export class ReportController extends Controller { * @param {string} id Guid, *Id ผังเงินเดือน */ @Post("command34/officer/report/excecute") - async SalaryReportExcecute34( - @Body() - body: { - refIds: { - refId: string; - // commandAffectDate: Date | null;//เก่า - commandDateAffect: Date | null; //ใหม่ (ปรับตาม rabbitMQ ORG) - commandNo: string | null; - commandYear: number | null; - commandId: string | null; - remark: string | null; - amount: Double | null; - amountSpecial?: Double | null; - positionSalaryAmount: Double | null; - mouthSalaryAmount: Double | null; - commandCode?: string | null; - commandName?: string | null; - commandDateSign: Date | null; - }[]; - }, - @Request() request: RequestWithUser, - ) { - await Promise.all( - body.refIds.map(async (v) => { - const salary = await this.salaryProfileRepository.findOne({ - where: { - id: v.refId, - }, - }); +async SalaryReportExcecute34( + @Body() + body: { + refIds: { + refId: string; + commandDateAffect: Date | null; + commandNo: string | null; + commandYear: number | null; + commandId: string | null; + remark: string | null; + amount: Double | null; + amountSpecial?: Double | null; + positionSalaryAmount: Double | null; + mouthSalaryAmount: Double | null; + commandCode?: string | null; + commandName?: string | null; + commandDateSign: Date | null; + }[]; + }, + @Request() request: RequestWithUser, +) { + // ═══════════════════════════════════════════════════════════════ + // old circle flow (เก็บไว้อ้างอิง — ลบการใช้งานจริงออกแล้ว) + // ═══════════════════════════════════════════════════════════════ + // await Promise.all( + // body.refIds.map(async (v) => { + // const salary = await this.salaryProfileRepository.findOne({ + // where: { id: v.refId }, + // }); + // if (salary != null) { + // await new CallAPI() + // .PostData(request, "/org/profile/salary/update", { + // profileId: salary.profileId, + // commandDateAffect: v.commandDateAffect, + // commandDateSign: v.commandDateSign, + // amount: v.amount, + // amountSpecial: v.amountSpecial, + // positionSalaryAmount: v.positionSalaryAmount, + // mouthSalaryAmount: v.mouthSalaryAmount, + // posNo: salary.posMasterNo.toString(), + // posNoAbb: salary.orgShortName, + // positionName: salary.position, + // positionExecutive: salary.posExecutive, + // positionExecutiveField: salary.positionExecutiveField, + // positionArea: salary.positionArea, + // positionType: salary.posType, + // positionLevel: salary.posLevel, + // commandId: v.commandId, + // remark: v.remark, + // orgRoot: salary.root, + // orgChild1: salary.child1, + // orgChild2: salary.child2, + // orgChild3: salary.child3, + // orgChild4: salary.child4, + // commandCode: v.commandCode, + // commandName: v.commandName, + // commandNo: v.commandNo, + // commandYear: v.commandYear, + // }) + // .then(async () => { + // const before = null; + // salary.status34 = "DONE"; + // salary.lastUpdateUserId = request.user.sub; + // salary.lastUpdateFullName = request.user.name; + // salary.lastUpdatedAt = new Date(); + // await this.salaryProfileRepository.save(salary, { data: request }); + // setLogDataDiff(request, { before, after: salary }); + // }); + // } + // }), + // ); + // return new HttpSuccess(); - if (salary != null) { - await new CallAPI() - .PostData(request, "/org/profile/salary/update", { - profileId: salary.profileId, - commandDateAffect: v.commandDateAffect, - commandDateSign: v.commandDateSign, - amount: v.amount, - amountSpecial: v.amountSpecial, - positionSalaryAmount: v.positionSalaryAmount, - mouthSalaryAmount: v.mouthSalaryAmount, - posNo: salary.posMasterNo.toString(), - posNoAbb: salary.orgShortName, - positionName: salary.position, - positionExecutive: salary.posExecutive, - positionExecutiveField: salary.positionExecutiveField, - positionArea: salary.positionArea, - positionType: salary.posType, - positionLevel: salary.posLevel, - commandId: v.commandId, - remark: v.remark, - orgRoot: salary.root, - orgChild1: salary.child1, - orgChild2: salary.child2, - orgChild3: salary.child3, - orgChild4: salary.child4, - commandCode: v.commandCode, - commandName: v.commandName, - commandNo: v.commandNo, - commandYear: v.commandYear, - }) - .then(async () => { - const before = null; - salary.status34 = "DONE"; - salary.lastUpdateUserId = request.user.sub; - salary.lastUpdateFullName = request.user.name; - salary.lastUpdatedAt = new Date(); - await this.salaryProfileRepository.save(salary, { data: request }); - setLogDataDiff(request, { before, after: salary }); - }); - } - }), - ); - return new HttpSuccess(); - } + // ═══════════════════════════════════════════════════════════════ + // new linear flow + // ═══════════════════════════════════════════════════════════════ + const result = await Promise.all( + body.refIds.map(async (v) => { + const salary = await this.salaryProfileRepository.findOne({ where: { id: v.refId } }); + if (salary != null) { + // 1) set status34 = DONE (ย้ายขึ้นมาทำก่อนเลย ไม่ต้องรอ org ตอบกลับ) + salary.status34 = "DONE"; + salary.lastUpdateUserId = request.user.sub; + salary.lastUpdateFullName = request.user.name; + salary.lastUpdatedAt = new Date(); + await this.salaryProfileRepository.save(salary, { data: request }); + setLogDataDiff(request, { before: null, after: salary }); + + // 2) return ข้อมูลที่ org ต้องการสร้าง ProfileSalary (shape เดียวกับที่เคย PostData) + return { + profileId: salary.profileId, + commandDateAffect: v.commandDateAffect, + commandDateSign: v.commandDateSign, + amount: v.amount, + amountSpecial: v.amountSpecial, + positionSalaryAmount: v.positionSalaryAmount, + mouthSalaryAmount: v.mouthSalaryAmount, + posNo: salary.posMasterNo.toString(), + posNoAbb: salary.orgShortName, + positionName: salary.position, + positionExecutive: salary.posExecutive, + positionExecutiveField: salary.positionExecutiveField, + positionArea: salary.positionArea, + positionType: salary.posType, + positionLevel: salary.posLevel, + commandId: v.commandId, + remark: v.remark, + orgRoot: salary.root, + orgChild1: salary.child1, + orgChild2: salary.child2, + orgChild3: salary.child3, + orgChild4: salary.child4, + commandCode: v.commandCode, + commandName: v.commandName, + commandNo: v.commandNo, + commandYear: v.commandYear, + }; + } + return null; + }), + ); + return result.filter(Boolean); +} /** * API ออกคำสั่ง C-PM-35 @@ -9239,78 +9342,128 @@ export class ReportController extends Controller { */ @Post("command35/officer/report/excecute") async SalaryReportExcecute35( - @Body() - body: { - refIds: { - refId: string; - // commandAffectDate: Date | null;//เก่า - commandDateAffect: Date | null; //ใหม่ (ปรับตาม rabbitMQ ORG) - commandNo: string | null; - commandYear: number | null; - commandId: string | null; - remark: string | null; - amount: Double | null; - amountSpecial?: Double | null; - positionSalaryAmount: Double | null; - mouthSalaryAmount: Double | null; - commandCode?: string | null; - commandName?: string | null; - commandDateSign: Date | null; - }[]; - }, - @Request() request: RequestWithUser, + @Body() + body: { + refIds: { + refId: string; + commandDateAffect: Date | null; + commandNo: string | null; + commandYear: number | null; + commandId: string | null; + remark: string | null; + amount: Double | null; + amountSpecial?: Double | null; + positionSalaryAmount: Double | null; + mouthSalaryAmount: Double | null; + commandCode?: string | null; + commandName?: string | null; + commandDateSign: Date | null; + }[]; + }, + @Request() request: RequestWithUser, ) { - await Promise.all( - body.refIds.map(async (v) => { - const salary = await this.salaryProfileRepository.findOne({ - where: { - id: v.refId, - }, - }); + // ═══════════════════════════════════════════════════════════════ + // old circle flow (เก็บไว้อ้างอิง — ลบการใช้งานจริงออกแล้ว) + // ═══════════════════════════════════════════════════════════════ + // await Promise.all( + // body.refIds.map(async (v) => { + // const salary = await this.salaryProfileRepository.findOne({ + // where: { id: v.refId }, + // }); + // if (salary != null) { + // await new CallAPI() + // .PostData(request, "/org/profile/salary/update", { + // profileId: salary.profileId, + // commandDateAffect: v.commandDateAffect, + // commandDateSign: v.commandDateSign, + // amount: v.amount, + // amountSpecial: v.amountSpecial, + // positionSalaryAmount: v.positionSalaryAmount, + // mouthSalaryAmount: v.mouthSalaryAmount, + // posNo: salary.posMasterNo.toString(), + // posNoAbb: salary.orgShortName, + // positionName: salary.position, + // positionExecutive: salary.posExecutive, + // positionExecutiveField: salary.positionExecutiveField, + // positionArea: salary.positionArea, + // positionType: salary.posType, + // positionLevel: salary.posLevel, + // commandId: v.commandId, + // remark: v.remark, + // orgRoot: salary.root, + // orgChild1: salary.child1, + // orgChild2: salary.child2, + // orgChild3: salary.child3, + // orgChild4: salary.child4, + // commandCode: v.commandCode, + // commandName: v.commandName, + // commandNo: v.commandNo, + // commandYear: v.commandYear, + // }) + // .then(async () => { + // const before = null; + // salary.status35 = "DONE"; + // salary.lastUpdateUserId = request.user.sub; + // salary.lastUpdateFullName = request.user.name; + // salary.lastUpdatedAt = new Date(); + // await this.salaryProfileRepository.save(salary, { data: request }); + // setLogDataDiff(request, { before, after: salary }); + // }); + // } + // }), + // ); + // return new HttpSuccess(); - if (salary != null) { - await new CallAPI() - .PostData(request, "/org/profile/salary/update", { - profileId: salary.profileId, - commandDateAffect: v.commandDateAffect, - commandDateSign: v.commandDateSign, - amount: v.amount, - amountSpecial: v.amountSpecial, - positionSalaryAmount: v.positionSalaryAmount, - mouthSalaryAmount: v.mouthSalaryAmount, - posNo: salary.posMasterNo.toString(), - posNoAbb: salary.orgShortName, - positionName: salary.position, - positionExecutive: salary.posExecutive, - positionExecutiveField: salary.positionExecutiveField, - positionArea: salary.positionArea, - positionType: salary.posType, - positionLevel: salary.posLevel, - commandId: v.commandId, - remark: v.remark, - orgRoot: salary.root, - orgChild1: salary.child1, - orgChild2: salary.child2, - orgChild3: salary.child3, - orgChild4: salary.child4, - commandCode: v.commandCode, - commandName: v.commandName, - commandNo: v.commandNo, - commandYear: v.commandYear, - }) - .then(async () => { - const before = null; - salary.status35 = "DONE"; - salary.lastUpdateUserId = request.user.sub; - salary.lastUpdateFullName = request.user.name; - salary.lastUpdatedAt = new Date(); - await this.salaryProfileRepository.save(salary, { data: request }); - setLogDataDiff(request, { before, after: salary }); - }); - } - }), - ); - return new HttpSuccess(); + // ═══════════════════════════════════════════════════════════════ + // new linear flow + // ═══════════════════════════════════════════════════════════════ + // ใช้ร่วมกับ C-PM-45 (path reuse) — ทั้ง C-PM-35/45 ยิงเข้าที่นี่ ตั้ง status35 เหมือนกัน + const result = await Promise.all( + body.refIds.map(async (v) => { + const salary = await this.salaryProfileRepository.findOne({ where: { id: v.refId } }); + if (salary != null) { + // 1) set status35 = DONE (ย้ายขึ้นมาทำก่อนเลย ไม่ต้องรอ org ตอบกลับ) + salary.status35 = "DONE"; + salary.lastUpdateUserId = request.user.sub; + salary.lastUpdateFullName = request.user.name; + salary.lastUpdatedAt = new Date(); + await this.salaryProfileRepository.save(salary, { data: request }); + setLogDataDiff(request, { before: null, after: salary }); + + // 2) return ข้อมูลที่ org ต้องการสร้าง ProfileSalary (shape เดียวกับที่เคย PostData) + return { + profileId: salary.profileId, + commandDateAffect: v.commandDateAffect, + commandDateSign: v.commandDateSign, + amount: v.amount, + amountSpecial: v.amountSpecial, + positionSalaryAmount: v.positionSalaryAmount, + mouthSalaryAmount: v.mouthSalaryAmount, + posNo: salary.posMasterNo.toString(), + posNoAbb: salary.orgShortName, + positionName: salary.position, + positionExecutive: salary.posExecutive, + positionExecutiveField: salary.positionExecutiveField, + positionArea: salary.positionArea, + positionType: salary.posType, + positionLevel: salary.posLevel, + commandId: v.commandId, + remark: v.remark, + orgRoot: salary.root, + orgChild1: salary.child1, + orgChild2: salary.child2, + orgChild3: salary.child3, + orgChild4: salary.child4, + commandCode: v.commandCode, + commandName: v.commandName, + commandNo: v.commandNo, + commandYear: v.commandYear, + }; + } + return null; + }), + ); + return result.filter(Boolean); } /** @@ -9322,86 +9475,139 @@ export class ReportController extends Controller { */ @Post("command36/employee/report/excecute") async SalaryEmployeeReportExcecute36( - @Body() - body: { - refIds: { - refId: string; - // commandAffectDate: Date | null;//เก่า - commandDateAffect: Date | null; //ใหม่ (ปรับตาม rabbitMQ ORG) - commandDateSign?: Date | null; - commandNo: string | null; - commandYear: number | null; - commandId: string | null; - commandCode?: string | null; - commandName?: string | null; - templateDoc?: string | null; - amount: Double | null; - amountSpecial?: Double | null; - positionSalaryAmount: Double | null; - mouthSalaryAmount: Double | null; - mpCee?: string | null; - refCommandCode?: string | null; - refCommandName?: string | null; - remark?: string | null; - }[]; - }, - @Request() request: RequestWithUser, + @Body() + body: { + refIds: { + refId: string; + commandDateAffect: Date | null; + commandDateSign?: Date | null; + commandNo: string | null; + commandYear: number | null; + commandId: string | null; + commandCode?: string | null; + commandName?: string | null; + templateDoc?: string | null; + amount: Double | null; + amountSpecial?: Double | null; + positionSalaryAmount: Double | null; + mouthSalaryAmount: Double | null; + mpCee?: string | null; + refCommandCode?: string | null; + refCommandName?: string | null; + remark?: string | null; + }[]; + }, + @Request() request: RequestWithUser, ) { - await Promise.all( - body.refIds.map(async (v) => { - const salary = await this.salaryProfileEmployeeRepository.findOne({ - where: { - id: v.refId, - }, - }); + // ═══════════════════════════════════════════════════════════════ + // old circle flow (เก็บไว้อ้างอิง — ลบการใช้งานจริงออกแล้ว) + // ═══════════════════════════════════════════════════════════════ + // await Promise.all( + // body.refIds.map(async (v) => { + // const salary = await this.salaryProfileEmployeeRepository.findOne({ + // where: { id: v.refId }, + // }); + // if (salary != null) { + // await new CallAPI() + // .PostData(request, "/org/profile-employee/salary/update", { + // profileEmployeeId: salary.profileId, + // commandDateAffect: v.commandDateAffect, + // commandDateSign: v.commandDateSign, + // amount: v.amount, + // amountSpecial: v.amountSpecial, + // positionSalaryAmount: v.positionSalaryAmount, + // mouthSalaryAmount: v.mouthSalaryAmount, + // posNo: salary.posMasterNo != null ? salary.posMasterNo.toString() : null, + // posNoAbb: salary.orgShortName, + // positionName: salary.position, + // positionType: salary.posType, + // positionLevel: + // (salary.posTypeShort != null ? salary.posTypeShort + " " : "") + + // (salary.posLevel != null ? salary.posLevel.toString() : ""), + // remark: v.remark, + // orgRoot: salary.root, + // orgChild1: salary.child1, + // orgChild2: salary.child2, + // orgChild3: salary.child3, + // orgChild4: salary.child4, + // commandCode: v.commandCode, + // commandName: v.commandName, + // commandNo: v.commandNo, + // commandYear: v.commandYear, + // commandId: v.commandId, + // salaryLevel: + // salary.salaryLevel && salary.salaryLevelNew + // ? salary.salaryLevelNew + // : salary.salaryLevel, + // group: salary.groupNew ?? salary.group, + // }) + // .then(async () => { + // const before = null; + // salary.status36 = "DONE"; + // salary.lastUpdateUserId = request.user.sub; + // salary.lastUpdateFullName = request.user.name; + // salary.lastUpdatedAt = new Date(); + // await this.salaryProfileEmployeeRepository.save(salary, { data: request }); + // setLogDataDiff(request, { before, after: salary }); + // }); + // } + // }), + // ); + // return new HttpSuccess(); - if (salary != null) { - await new CallAPI() - .PostData(request, "/org/profile-employee/salary/update", { - profileEmployeeId: salary.profileId, - commandDateAffect: v.commandDateAffect, - commandDateSign: v.commandDateSign, - amount: v.amount, - amountSpecial: v.amountSpecial, - positionSalaryAmount: v.positionSalaryAmount, - mouthSalaryAmount: v.mouthSalaryAmount, - posNo: salary.posMasterNo != null ? salary.posMasterNo.toString() : null, - posNoAbb: salary.orgShortName, - positionName: salary.position, - positionType: salary.posType, - positionLevel: - (salary.posTypeShort != null ? salary.posTypeShort + " " : "") + - (salary.posLevel != null ? salary.posLevel.toString() : ""), - remark: v.remark, - orgRoot: salary.root, - orgChild1: salary.child1, - orgChild2: salary.child2, - orgChild3: salary.child3, - orgChild4: salary.child4, - commandCode: v.commandCode, - commandName: v.commandName, - commandNo: v.commandNo, - commandYear: v.commandYear, - commandId: v.commandId, - salaryLevel: - salary.salaryLevel && salary.salaryLevelNew - ? salary.salaryLevelNew - : salary.salaryLevel, - group: salary.groupNew ?? salary.group, - }) - .then(async () => { - const before = null; - salary.status36 = "DONE"; - salary.lastUpdateUserId = request.user.sub; - salary.lastUpdateFullName = request.user.name; - salary.lastUpdatedAt = new Date(); - await this.salaryProfileEmployeeRepository.save(salary, { data: request }); - setLogDataDiff(request, { before, after: salary }); - }); - } - }), - ); - return new HttpSuccess(); + // ═══════════════════════════════════════════════════════════════ + // new linear flow + // ═══════════════════════════════════════════════════════════════ + // ใช้ร่วมกับ C-PM-46 (path reuse) — ทั้ง C-PM-36/46 ยิงเข้าที่นี่ ตั้ง status36 เหมือนกัน + const result = await Promise.all( + body.refIds.map(async (v) => { + const salary = await this.salaryProfileEmployeeRepository.findOne({ where: { id: v.refId } }); + if (salary != null) { + // 1) set status36 = DONE (ย้ายขึ้นมาทำก่อนเลย ไม่ต้องรอ org ตอบกลับ) + salary.status36 = "DONE"; + salary.lastUpdateUserId = request.user.sub; + salary.lastUpdateFullName = request.user.name; + salary.lastUpdatedAt = new Date(); + await this.salaryProfileEmployeeRepository.save(salary, { data: request }); + setLogDataDiff(request, { before: null, after: salary }); + + // 2) return ข้อมูลที่ org ต้องการสร้าง ProfileSalary (shape เดียวกับที่เคย PostData) + // สังเกต: profileEmployeeId (ไม่ใช่ profileId) + salaryLevel/group เพิ่ม + return { + profileEmployeeId: salary.profileId, + commandDateAffect: v.commandDateAffect, + commandDateSign: v.commandDateSign, + amount: v.amount, + amountSpecial: v.amountSpecial, + positionSalaryAmount: v.positionSalaryAmount, + mouthSalaryAmount: v.mouthSalaryAmount, + posNo: salary.posMasterNo != null ? salary.posMasterNo.toString() : null, + posNoAbb: salary.orgShortName, + positionName: salary.position, + positionType: salary.posType, + positionLevel: + (salary.posTypeShort != null ? salary.posTypeShort + " " : "") + + (salary.posLevel != null ? salary.posLevel.toString() : ""), + remark: v.remark, + orgRoot: salary.root, + orgChild1: salary.child1, + orgChild2: salary.child2, + orgChild3: salary.child3, + orgChild4: salary.child4, + commandCode: v.commandCode, + commandName: v.commandName, + commandNo: v.commandNo, + commandYear: v.commandYear, + commandId: v.commandId, + salaryLevel: + salary.salaryLevel && salary.salaryLevelNew ? salary.salaryLevelNew : salary.salaryLevel, + group: salary.groupNew ?? salary.group, + }; + } + return null; + }), + ); + return result.filter(Boolean); } /** @@ -9413,86 +9619,137 @@ export class ReportController extends Controller { */ @Post("command37/employee/report/excecute") async SalaryEmployeeReportExcecute37( - @Body() - body: { - refIds: { - refId: string; - // commandAffectDate: Date | null;//เก่า - commandDateAffect: Date | null; //ใหม่ (ปรับตาม rabbitMQ ORG) - commandDateSign?: Date | null; - commandNo: string | null; - commandYear: number | null; - commandId: string | null; - commandCode?: string | null; - commandName?: string | null; - templateDoc?: string | null; - amount: Double | null; - amountSpecial?: Double | null; - positionSalaryAmount: Double | null; - mouthSalaryAmount: Double | null; - mpCee?: string | null; - refCommandCode?: string | null; - refCommandName?: string | null; - remark?: string | null; - }[]; - }, - @Request() request: RequestWithUser, + @Body() + body: { + refIds: { + refId: string; + commandDateAffect: Date | null; + commandDateSign?: Date | null; + commandNo: string | null; + commandYear: number | null; + commandId: string | null; + commandCode?: string | null; + commandName?: string | null; + templateDoc?: string | null; + amount: Double | null; + amountSpecial?: Double | null; + positionSalaryAmount: Double | null; + mouthSalaryAmount: Double | null; + mpCee?: string | null; + refCommandCode?: string | null; + refCommandName?: string | null; + remark?: string | null; + }[]; + }, + @Request() request: RequestWithUser, ) { - await Promise.all( - body.refIds.map(async (v) => { - const salary = await this.salaryProfileEmployeeRepository.findOne({ - where: { - id: v.refId, - }, - }); + // ═══════════════════════════════════════════════════════════════ + // old circle flow (เก็บไว้อ้างอิง — ลบการใช้งานจริงออกแล้ว) + // ═══════════════════════════════════════════════════════════════ + // await Promise.all( + // body.refIds.map(async (v) => { + // const salary = await this.salaryProfileEmployeeRepository.findOne({ + // where: { id: v.refId }, + // }); + // if (salary != null) { + // await new CallAPI() + // .PostData(request, "/org/profile-employee/salary/update", { + // profileEmployeeId: salary.profileId, + // commandDateAffect: v.commandDateAffect, + // commandDateSign: v.commandDateSign, + // amount: v.amount, + // amountSpecial: v.amountSpecial, + // positionSalaryAmount: v.positionSalaryAmount, + // mouthSalaryAmount: v.mouthSalaryAmount, + // posNo: salary.posMasterNo != null ? salary.posMasterNo.toString() : null, + // posNoAbb: salary.orgShortName, + // positionName: salary.position, + // positionType: salary.posType, + // positionLevel: + // (salary.posTypeShort != null ? salary.posTypeShort + " " : "") + + // (salary.posLevel != null ? salary.posLevel.toString() : ""), + // remark: v.remark, + // orgRoot: salary.root, + // orgChild1: salary.child1, + // orgChild2: salary.child2, + // orgChild3: salary.child3, + // orgChild4: salary.child4, + // commandCode: v.commandCode, + // commandName: v.commandName, + // commandNo: v.commandNo, + // commandYear: v.commandYear, + // commandId: v.commandId, + // salaryLevel: + // salary.salaryLevel && salary.salaryLevelNew + // ? salary.salaryLevelNew + // : salary.salaryLevel, + // group: salary.groupNew ?? salary.group, + // }) + // .then(async () => { + // const before = null; + // salary.status37 = "DONE"; + // salary.lastUpdateUserId = request.user.sub; + // salary.lastUpdateFullName = request.user.name; + // salary.lastUpdatedAt = new Date(); + // await this.salaryProfileEmployeeRepository.save(salary, { data: request }); + // setLogDataDiff(request, { before, after: salary }); + // }); + // } + // }), + // ); + // return new HttpSuccess(); - if (salary != null) { - await new CallAPI() - .PostData(request, "/org/profile-employee/salary/update", { - profileEmployeeId: salary.profileId, - commandDateAffect: v.commandDateAffect, - commandDateSign: v.commandDateSign, - amount: v.amount, - amountSpecial: v.amountSpecial, - positionSalaryAmount: v.positionSalaryAmount, - mouthSalaryAmount: v.mouthSalaryAmount, - posNo: salary.posMasterNo != null ? salary.posMasterNo.toString() : null, - posNoAbb: salary.orgShortName, - positionName: salary.position, - positionType: salary.posType, - positionLevel: - (salary.posTypeShort != null ? salary.posTypeShort + " " : "") + - (salary.posLevel != null ? salary.posLevel.toString() : ""), - remark: v.remark, - orgRoot: salary.root, - orgChild1: salary.child1, - orgChild2: salary.child2, - orgChild3: salary.child3, - orgChild4: salary.child4, - commandCode: v.commandCode, - commandName: v.commandName, - commandNo: v.commandNo, - commandYear: v.commandYear, - commandId: v.commandId, - salaryLevel: - salary.salaryLevel && salary.salaryLevelNew - ? salary.salaryLevelNew - : salary.salaryLevel, - group: salary.groupNew ?? salary.group, - }) - .then(async () => { - const before = null; - salary.status37 = "DONE"; - salary.lastUpdateUserId = request.user.sub; - salary.lastUpdateFullName = request.user.name; - salary.lastUpdatedAt = new Date(); - await this.salaryProfileEmployeeRepository.save(salary, { data: request }); - setLogDataDiff(request, { before, after: salary }); - }); - } - }), - ); - return new HttpSuccess(); + // ═══════════════════════════════════════════════════════════════ + // new linear flow + // ═══════════════════════════════════════════════════════════════ + const result = await Promise.all( + body.refIds.map(async (v) => { + const salary = await this.salaryProfileEmployeeRepository.findOne({ where: { id: v.refId } }); + if (salary != null) { + // 1) set status37 = DONE (ย้ายขึ้นมาทำก่อนเลย ไม่ต้องรอ org ตอบกลับ) + salary.status37 = "DONE"; + salary.lastUpdateUserId = request.user.sub; + salary.lastUpdateFullName = request.user.name; + salary.lastUpdatedAt = new Date(); + await this.salaryProfileEmployeeRepository.save(salary, { data: request }); + setLogDataDiff(request, { before: null, after: salary }); + + // 2) return ข้อมูลที่ org ต้องการสร้าง ProfileSalary (shape เดียวกับที่เคย PostData) + return { + profileEmployeeId: salary.profileId, + commandDateAffect: v.commandDateAffect, + commandDateSign: v.commandDateSign, + amount: v.amount, + amountSpecial: v.amountSpecial, + positionSalaryAmount: v.positionSalaryAmount, + mouthSalaryAmount: v.mouthSalaryAmount, + posNo: salary.posMasterNo != null ? salary.posMasterNo.toString() : null, + posNoAbb: salary.orgShortName, + positionName: salary.position, + positionType: salary.posType, + positionLevel: + (salary.posTypeShort != null ? salary.posTypeShort + " " : "") + + (salary.posLevel != null ? salary.posLevel.toString() : ""), + remark: v.remark, + orgRoot: salary.root, + orgChild1: salary.child1, + orgChild2: salary.child2, + orgChild3: salary.child3, + orgChild4: salary.child4, + commandCode: v.commandCode, + commandName: v.commandName, + commandNo: v.commandNo, + commandYear: v.commandYear, + commandId: v.commandId, + salaryLevel: + salary.salaryLevel && salary.salaryLevelNew ? salary.salaryLevelNew : salary.salaryLevel, + group: salary.groupNew ?? salary.group, + }; + } + return null; + }), + ); + return result.filter(Boolean); } /**