Compare commits

...

45 commits

Author SHA1 Message Date
harid
8807ee3226 revert add password redis
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m5s
2026-06-30 11:04:19 +07:00
harid
2f7ec40e78 add pass redis test
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m6s
2026-06-29 16:01:16 +07:00
harid
7b37cc37db Linear Flow Probation+Salary #224 2026-06-29 14:28:50 +07:00
harid
3d2fc5128a Linear Flow discipline + organization #224
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m11s
2026-06-26 18:09:45 +07:00
harid
832c5d2cb3 add transaction #224
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m10s
2026-06-24 18:05:54 +07:00
harid
ecd0388eb0 เพิ่ม log
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m4s
2026-06-23 18:33:27 +07:00
harid
5acd485368 fix #62
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m6s
2026-06-23 16:35:30 +07:00
harid
64be68d0a3 [ExecuteSalaryCurrentService] ครอบ transaction + respone success/fail count
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m3s
2026-06-23 13:34:06 +07:00
harid
00c35e8974 fix ส่งรายชื่อไปยังรายการอื่น ๆ #2571
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m10s
2026-06-22 13:32:26 +07:00
harid
bbc6a5e6a9 fix การค้นหา labelName #2573
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m6s
2026-06-22 10:06:59 +07:00
harid
41862b8dca fix api web services #62
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m9s
2026-06-19 18:13:03 +07:00
harid
d33e818ce8 Linear Flow (RetirementService) #224
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m9s
2026-06-19 09:25:18 +07:00
harid
616ccf9e64 Linear Flow (PlacementService) #224
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m10s
2026-06-18 18:29:03 +07:00
harid
2f17c10050 fix error #224
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m4s
2026-06-18 16:26:27 +07:00
harid
9dddaf40db Linear Flow (C-PM-01, C-PM-02, C-PM-14) #224
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m11s
2026-06-18 15:48:52 +07:00
harid
c26fb19c1c Linear Flow (ทดสอบเฉพาะคำสั่ง C-PM-01) #224
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m7s
2026-06-18 11:48:46 +07:00
harid
9f7803cc74 fix #2563
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m8s
2026-06-18 10:21:04 +07:00
harid
bdd31ce72c fix #2563
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m5s
2026-06-17 17:57:37 +07:00
harid
51d447109e Merge branch 'develop-Bright' into develop
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m14s
2026-06-17 17:15:01 +07:00
harid
e7eeb3e34d fix #2570 2026-06-17 17:14:52 +07:00
harid
e410f83683 Merge branch 'develop-Bright' into develop
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m7s
2026-06-17 16:53:34 +07:00
harid
12da167794 fix #2570 2026-06-17 16:53:26 +07:00
harid
e6bdea2b20 อัตรากำลังลูกจ้างประจำ กรณีนั่งทับที่ต้องเป็นตำแหน่งจริง #2563
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m7s
2026-06-17 15:42:48 +07:00
harid
db36b250e3 face เลือกหัวหน้าทั้งหน่วยงาน #1597
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m11s
2026-06-16 15:50:15 +07:00
ca5b11e36b fix #1584
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m8s
2026-06-16 15:24:35 +07:00
967120c4ce Merge branch 'dev' into adiDev
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m14s
2026-06-16 13:58:35 +07:00
61e4bcdf03 test clear redis เผยแพร่ย่อย 2026-06-16 13:58:16 +07:00
harid
e84f93f6db fix #252
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m9s
2026-06-16 10:25:45 +07:00
harid
7c77745d46 migrate add field all tables #252
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m12s
2026-06-16 09:53:23 +07:00
6319f7206a Merge branch 'dev' into adiDev
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m7s
2026-06-15 18:27:20 +07:00
84fd3fb9e5 comment เคลียร์ redis 2026-06-15 18:26:34 +07:00
harid
b722068758 migrate เพิ่มฟิลด์บันทึกที่โครงสร้าง orgRoot, orgChild1, ..., orgChild4 #252
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m5s
2026-06-15 17:17:36 +07:00
c398354208 #1596 and revert cronjob
All checks were successful
Build & Deploy on Dev / build (push) Successful in 59s
2026-06-15 15:21:15 +07:00
e2aeabf155 Merge branch 'dev' into adiDev
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m5s
2026-06-15 14:21:31 +07:00
5b17073eeb test เผยแพร่ (3) 2026-06-15 14:21:10 +07:00
harid
8236caf458 fix #1594
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m17s
2026-06-15 11:52:20 +07:00
ed6ab06b52 fix
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m8s
2026-06-15 11:32:43 +07:00
82c94073ff test เผยแพร่ (2) 2026-06-15 11:32:01 +07:00
bc8fbf4cf8 test เผยแพร่ (1)
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m3s
2026-06-15 11:04:22 +07:00
cf4c8a6388 Merge branch 'dev' into adiDev
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m9s
2026-06-15 10:59:28 +07:00
5c5fc08269 test เผยแพร่ 2026-06-15 10:58:56 +07:00
harid
7920832c5c revert
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m2s
2026-06-12 18:02:54 +07:00
harid
8232c6696e no message
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m6s
2026-06-12 17:53:33 +07:00
harid
aeae391034 no message 2026-06-12 17:53:23 +07:00
harid
d84ec4f55d test
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m7s
2026-06-12 15:58:31 +07:00
41 changed files with 8157 additions and 4941 deletions

View file

@ -66,8 +66,7 @@ async function main() {
}); });
// Cron job for updating org revision - every day at 01:00:00 // Cron job for updating org revision - every day at 01:00:00
// const cronTime = "0 0 1 * * *"; const cronTime = "0 0 1 * * *";
const cronTime = "0 45 15 * * *"; // test by dev
cron.schedule(cronTime, async () => { cron.schedule(cronTime, async () => {
try { try {
const orgController = new OrganizationController(); const orgController = new OrganizationController();

View file

@ -161,39 +161,47 @@ export class ApiWebServiceController extends Controller {
); );
} else if (dnaIds.dnaChild3Id) { } else if (dnaIds.dnaChild3Id) {
conditions.push( conditions.push(
`${tableAlias}.orgChild3Id IN (SELECT id FROM orgChild3 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA = "${dnaIds.dnaChild3Id}")`, accessType === "NORMAL"
? `(${tableAlias}.orgChild3Id IN (SELECT id FROM orgChild3 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA = "${dnaIds.dnaChild3Id}") AND ${tableAlias}.orgChild4Id IS NULL)`
: `${tableAlias}.orgChild3Id IN (SELECT id FROM orgChild3 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA = "${dnaIds.dnaChild3Id}")`,
); );
// For CHILD type, include all descendants // For CHILD type, include all descendants
if (accessType === "CHILD") { if (accessType === "CHILD") {
conditions.push( conditions.push(
`(${tableAlias}.orgChild3Id IN (SELECT id FROM orgChild3 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaChild3Id}%") OR ${tableAlias}.orgChild4Id IS NOT NULL)`, `(${tableAlias}.orgChild3Id IN (SELECT id FROM orgChild3 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaChild3Id}%") OR ${tableAlias}.orgChild4Id IN (SELECT id FROM orgChild4 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaChild3Id}%"))`,
); );
} }
} else if (dnaIds.dnaChild2Id) { } else if (dnaIds.dnaChild2Id) {
conditions.push( conditions.push(
`${tableAlias}.orgChild2Id IN (SELECT id FROM orgChild2 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA = "${dnaIds.dnaChild2Id}")`, accessType === "NORMAL"
? `(${tableAlias}.orgChild2Id IN (SELECT id FROM orgChild2 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA = "${dnaIds.dnaChild2Id}") AND ${tableAlias}.orgChild3Id IS NULL AND ${tableAlias}.orgChild4Id IS NULL)`
: `${tableAlias}.orgChild2Id IN (SELECT id FROM orgChild2 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA = "${dnaIds.dnaChild2Id}")`,
); );
if (accessType === "CHILD") { if (accessType === "CHILD") {
conditions.push( conditions.push(
`(${tableAlias}.orgChild2Id IN (SELECT id FROM orgChild2 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaChild2Id}%") OR ${tableAlias}.orgChild3Id IS NOT NULL)`, `(${tableAlias}.orgChild2Id IN (SELECT id FROM orgChild2 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaChild2Id}%") OR ${tableAlias}.orgChild3Id IN (SELECT id FROM orgChild3 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaChild2Id}%") OR ${tableAlias}.orgChild4Id IN (SELECT id FROM orgChild4 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaChild2Id}%"))`,
); );
} }
} else if (dnaIds.dnaChild1Id) { } else if (dnaIds.dnaChild1Id) {
conditions.push( conditions.push(
`${tableAlias}.orgChild1Id IN (SELECT id FROM orgChild1 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA = "${dnaIds.dnaChild1Id}")`, accessType === "NORMAL"
? `(${tableAlias}.orgChild1Id IN (SELECT id FROM orgChild1 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA = "${dnaIds.dnaChild1Id}") AND ${tableAlias}.orgChild2Id IS NULL AND ${tableAlias}.orgChild3Id IS NULL AND ${tableAlias}.orgChild4Id IS NULL)`
: `${tableAlias}.orgChild1Id IN (SELECT id FROM orgChild1 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA = "${dnaIds.dnaChild1Id}")`,
); );
if (accessType === "CHILD") { if (accessType === "CHILD") {
conditions.push( conditions.push(
`(${tableAlias}.orgChild1Id IN (SELECT id FROM orgChild1 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaChild1Id}%") OR ${tableAlias}.orgChild2Id IS NOT NULL)`, `(${tableAlias}.orgChild1Id IN (SELECT id FROM orgChild1 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaChild1Id}%") OR ${tableAlias}.orgChild2Id IN (SELECT id FROM orgChild2 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaChild1Id}%") OR ${tableAlias}.orgChild3Id IN (SELECT id FROM orgChild3 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaChild1Id}%") OR ${tableAlias}.orgChild4Id IN (SELECT id FROM orgChild4 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaChild1Id}%"))`,
); );
} }
} else if (dnaIds.dnaRootId) { } else if (dnaIds.dnaRootId) {
conditions.push( conditions.push(
`${tableAlias}.orgRootId IN (SELECT id FROM orgRoot WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA = "${dnaIds.dnaRootId}")`, accessType === "NORMAL"
? `(${tableAlias}.orgRootId IN (SELECT id FROM orgRoot WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA = "${dnaIds.dnaRootId}") AND ${tableAlias}.orgChild1Id IS NULL AND ${tableAlias}.orgChild2Id IS NULL AND ${tableAlias}.orgChild3Id IS NULL AND ${tableAlias}.orgChild4Id IS NULL)`
: `${tableAlias}.orgRootId IN (SELECT id FROM orgRoot WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA = "${dnaIds.dnaRootId}")`,
); );
if (accessType === "CHILD") { if (accessType === "CHILD") {
conditions.push( conditions.push(
`(${tableAlias}.orgRootId IN (SELECT id FROM orgRoot WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaRootId}%") OR ${tableAlias}.orgChild1Id IS NOT NULL)`, `(${tableAlias}.orgRootId IN (SELECT id FROM orgRoot WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaRootId}%") OR ${tableAlias}.orgChild1Id IN (SELECT id FROM orgChild1 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaRootId}%") OR ${tableAlias}.orgChild2Id IN (SELECT id FROM orgChild2 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaRootId}%") OR ${tableAlias}.orgChild3Id IN (SELECT id FROM orgChild3 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaRootId}%") OR ${tableAlias}.orgChild4Id IN (SELECT id FROM orgChild4 WHERE orgRevisionId = "${this.currentRevisionId}" AND ancestorDNA LIKE "${dnaIds.dnaRootId}%"))`,
); );
} }
} }

File diff suppressed because it is too large Load diff

View file

@ -39,8 +39,7 @@ import { RequestWithUser } from "../middlewares/user";
import permission from "../interfaces/permission"; import permission from "../interfaces/permission";
import { setLogDataDiff } from "../interfaces/utils"; import { setLogDataDiff } from "../interfaces/utils";
import { import {
CreatePosMasterHistoryEmployee, CreatePosMasterHistoryEmployee
CreatePosMasterHistoryOfficer,
} from "../services/PositionService"; } from "../services/PositionService";
import { PosMasterEmployeeHistory } from "../entities/PosMasterEmployeeHistory"; import { PosMasterEmployeeHistory } from "../entities/PosMasterEmployeeHistory";
import { KeycloakAttributeService } from "../services/KeycloakAttributeService"; import { KeycloakAttributeService } from "../services/KeycloakAttributeService";
@ -2377,26 +2376,47 @@ export class EmployeePositionController extends Controller {
dataMaster.positions.forEach(async (position) => { dataMaster.positions.forEach(async (position) => {
if (position.id === requestBody.position) { if (position.id === requestBody.position) {
position.positionIsSelected = true; position.positionIsSelected = true;
const profile = await this.profileRepository.findOne({
where: { id: requestBody.profileId },
});
if (profile != null) {
const _null: any = null;
profile.posLevelId = position?.posLevelId ?? _null;
profile.posTypeId = position?.posTypeId ?? _null;
profile.position = position?.positionName ?? _null;
await this.profileRepository.save(profile);
}
} else { } else {
position.positionIsSelected = false; position.positionIsSelected = false;
} }
await this.employeePositionRepository.save(position); await this.employeePositionRepository.save(position);
}); });
const _null: any = null;
const before = null;
dataMaster.isSit = requestBody.isSit; dataMaster.isSit = requestBody.isSit;
dataMaster.current_holderId = requestBody.profileId;
dataMaster.lastUpdatedAt = new Date(); dataMaster.lastUpdatedAt = new Date();
// dataMaster.next_holderId = requestBody.profileId;
//เช็คถ้า revision ปัจจุบันให้ปั๊มที่ profile
const chkRevision = await this.orgRevisionRepository.findOne({
where: { id: dataMaster.orgRevisionId },
});
if (chkRevision?.orgRevisionIsCurrent) {
const _profile = await this.profileRepository.findOne({
where: { id: requestBody.profileId },
});
if (_profile) {
let _position = await this.employeePositionRepository.findOne({
where: { id: requestBody.position, posMasterId: requestBody.posMaster },
});
if (_position) {
// ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ
if (!dataMaster.isSit) {
_profile.position = _position.positionName;
_profile.posTypeId = _position.posTypeId;
_profile.posLevelId = _position.posLevelId;
}
await this.profileRepository.save(_profile);
setLogDataDiff(request, { before, after: _profile });
}
}
dataMaster.current_holderId = requestBody.profileId;
dataMaster.next_holderId = _null;
} else {
dataMaster.next_holderId = requestBody.profileId;
dataMaster.current_holderId = _null;
}
await this.employeePosMasterRepository.save(dataMaster); await this.employeePosMasterRepository.save(dataMaster);
await CreatePosMasterHistoryEmployee(dataMaster.id, request); await CreatePosMasterHistoryEmployee(dataMaster.id, request);

View file

@ -74,6 +74,11 @@ export class OrgChild1Controller {
DIVISION_CODE: orgChild1.DIVISION_CODE, DIVISION_CODE: orgChild1.DIVISION_CODE,
SECTION_CODE: orgChild1.SECTION_CODE, SECTION_CODE: orgChild1.SECTION_CODE,
JOB_CODE: orgChild1.JOB_CODE, JOB_CODE: orgChild1.JOB_CODE,
ROOT_CODE: orgChild1.ROOT_CODE,
CHILD1_CODE: orgChild1.CHILD1_CODE,
CHILD2_CODE: orgChild1.CHILD2_CODE,
CHILD3_CODE: orgChild1.CHILD3_CODE,
CHILD4_CODE: orgChild1.CHILD4_CODE,
orgCode: orgChild1.orgRoot.orgRootCode + orgChild1.orgChild1Code, orgCode: orgChild1.orgRoot.orgRootCode + orgChild1.orgChild1Code,
}; };
return new HttpSuccess(getOrgChild1); return new HttpSuccess(getOrgChild1);
@ -346,6 +351,11 @@ export class OrgChild1Controller {
DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null, DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null,
SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null, SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null,
JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null, JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null,
ROOT_CODE: requestBody.ROOT_CODE != null ? requestBody.ROOT_CODE : _null,
CHILD1_CODE: requestBody.CHILD1_CODE != null ? requestBody.CHILD1_CODE : _null,
CHILD2_CODE: requestBody.CHILD2_CODE != null ? requestBody.CHILD2_CODE : _null,
CHILD3_CODE: requestBody.CHILD3_CODE != null ? requestBody.CHILD3_CODE : _null,
CHILD4_CODE: requestBody.CHILD4_CODE != null ? requestBody.CHILD4_CODE : _null,
isOfficer: requestBody.isOfficer, isOfficer: requestBody.isOfficer,
isInformation: requestBody.isInformation, isInformation: requestBody.isInformation,
orgChild1PhoneEx: requestBody.orgChild1PhoneEx, orgChild1PhoneEx: requestBody.orgChild1PhoneEx,

View file

@ -85,6 +85,11 @@ export class OrgChild2Controller extends Controller {
DIVISION_CODE: orgChild2.DIVISION_CODE, DIVISION_CODE: orgChild2.DIVISION_CODE,
SECTION_CODE: orgChild2.SECTION_CODE, SECTION_CODE: orgChild2.SECTION_CODE,
JOB_CODE: orgChild2.JOB_CODE, JOB_CODE: orgChild2.JOB_CODE,
ROOT_CODE: orgChild2.ROOT_CODE,
CHILD1_CODE: orgChild2.CHILD1_CODE,
CHILD2_CODE: orgChild2.CHILD2_CODE,
CHILD3_CODE: orgChild2.CHILD3_CODE,
CHILD4_CODE: orgChild2.CHILD4_CODE,
orgCode: orgChild2.orgRoot.orgRootCode + orgChild2.orgChild2Code, orgCode: orgChild2.orgRoot.orgRootCode + orgChild2.orgChild2Code,
}; };
return new HttpSuccess(getOrgChild2); return new HttpSuccess(getOrgChild2);
@ -252,6 +257,11 @@ export class OrgChild2Controller extends Controller {
DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null, DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null,
SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null, SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null,
JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null, JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null,
ROOT_CODE: requestBody.ROOT_CODE != null ? requestBody.ROOT_CODE : _null,
CHILD1_CODE: requestBody.CHILD1_CODE != null ? requestBody.CHILD1_CODE : _null,
CHILD2_CODE: requestBody.CHILD2_CODE != null ? requestBody.CHILD2_CODE : _null,
CHILD3_CODE: requestBody.CHILD3_CODE != null ? requestBody.CHILD3_CODE : _null,
CHILD4_CODE: requestBody.CHILD4_CODE != null ? requestBody.CHILD4_CODE : _null,
orgChild2PhoneEx: requestBody.orgChild2PhoneEx, orgChild2PhoneEx: requestBody.orgChild2PhoneEx,
orgChild2PhoneIn: requestBody.orgChild2PhoneIn, orgChild2PhoneIn: requestBody.orgChild2PhoneIn,
orgChild2Fax: requestBody.orgChild2Fax, orgChild2Fax: requestBody.orgChild2Fax,

View file

@ -69,6 +69,11 @@ export class OrgChild3Controller {
DIVISION_CODE: orgChild3.DIVISION_CODE, DIVISION_CODE: orgChild3.DIVISION_CODE,
SECTION_CODE: orgChild3.SECTION_CODE, SECTION_CODE: orgChild3.SECTION_CODE,
JOB_CODE: orgChild3.JOB_CODE, JOB_CODE: orgChild3.JOB_CODE,
ROOT_CODE: orgChild3.ROOT_CODE,
CHILD1_CODE: orgChild3.CHILD1_CODE,
CHILD2_CODE: orgChild3.CHILD2_CODE,
CHILD3_CODE: orgChild3.CHILD3_CODE,
CHILD4_CODE: orgChild3.CHILD4_CODE,
orgCode: orgChild3.orgRoot.orgRootCode + orgChild3.orgChild3Code, orgCode: orgChild3.orgRoot.orgRootCode + orgChild3.orgChild3Code,
}; };
return new HttpSuccess(getOrgChild3); return new HttpSuccess(getOrgChild3);
@ -207,6 +212,11 @@ export class OrgChild3Controller {
DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null, DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null,
SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null, SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null,
JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null, JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null,
ROOT_CODE: requestBody.ROOT_CODE != null ? requestBody.ROOT_CODE : _null,
CHILD1_CODE: requestBody.CHILD1_CODE != null ? requestBody.CHILD1_CODE : _null,
CHILD2_CODE: requestBody.CHILD2_CODE != null ? requestBody.CHILD2_CODE : _null,
CHILD3_CODE: requestBody.CHILD3_CODE != null ? requestBody.CHILD3_CODE : _null,
CHILD4_CODE: requestBody.CHILD4_CODE != null ? requestBody.CHILD4_CODE : _null,
orgChild3PhoneEx: requestBody.orgChild3PhoneEx, orgChild3PhoneEx: requestBody.orgChild3PhoneEx,
orgChild3PhoneIn: requestBody.orgChild3PhoneIn, orgChild3PhoneIn: requestBody.orgChild3PhoneIn,
orgChild3Fax: requestBody.orgChild3Fax, orgChild3Fax: requestBody.orgChild3Fax,

View file

@ -82,6 +82,11 @@ export class OrgChild4Controller extends Controller {
DIVISION_CODE: orgChild4.DIVISION_CODE, DIVISION_CODE: orgChild4.DIVISION_CODE,
SECTION_CODE: orgChild4.SECTION_CODE, SECTION_CODE: orgChild4.SECTION_CODE,
JOB_CODE: orgChild4.JOB_CODE, JOB_CODE: orgChild4.JOB_CODE,
ROOT_CODE: orgChild4.ROOT_CODE,
CHILD1_CODE: orgChild4.CHILD1_CODE,
CHILD2_CODE: orgChild4.CHILD2_CODE,
CHILD3_CODE: orgChild4.CHILD3_CODE,
CHILD4_CODE: orgChild4.CHILD4_CODE,
orgCode: orgChild4.orgRoot.orgRootCode + orgChild4.orgChild4Code, orgCode: orgChild4.orgRoot.orgRootCode + orgChild4.orgChild4Code,
}; };
return new HttpSuccess(getOrgChild4); return new HttpSuccess(getOrgChild4);
@ -254,6 +259,11 @@ export class OrgChild4Controller extends Controller {
DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null, DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null,
SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null, SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null,
JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null, JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null,
ROOT_CODE: requestBody.ROOT_CODE != null ? requestBody.ROOT_CODE : _null,
CHILD1_CODE: requestBody.CHILD1_CODE != null ? requestBody.CHILD1_CODE : _null,
CHILD2_CODE: requestBody.CHILD2_CODE != null ? requestBody.CHILD2_CODE : _null,
CHILD3_CODE: requestBody.CHILD3_CODE != null ? requestBody.CHILD3_CODE : _null,
CHILD4_CODE: requestBody.CHILD4_CODE != null ? requestBody.CHILD4_CODE : _null,
orgChild4PhoneEx: requestBody.orgChild4PhoneEx, orgChild4PhoneEx: requestBody.orgChild4PhoneEx,
orgChild4PhoneIn: requestBody.orgChild4PhoneIn, orgChild4PhoneIn: requestBody.orgChild4PhoneIn,
orgChild4Fax: requestBody.orgChild4Fax, orgChild4Fax: requestBody.orgChild4Fax,

View file

@ -83,6 +83,11 @@ export class OrgRootController extends Controller {
DIVISION_CODE: orgRoot.DIVISION_CODE, DIVISION_CODE: orgRoot.DIVISION_CODE,
SECTION_CODE: orgRoot.SECTION_CODE, SECTION_CODE: orgRoot.SECTION_CODE,
JOB_CODE: orgRoot.JOB_CODE, JOB_CODE: orgRoot.JOB_CODE,
ROOT_CODE: orgRoot.ROOT_CODE,
CHILD1_CODE: orgRoot.CHILD1_CODE,
CHILD2_CODE: orgRoot.CHILD2_CODE,
CHILD3_CODE: orgRoot.CHILD3_CODE,
CHILD4_CODE: orgRoot.CHILD4_CODE,
orgCode: orgRoot.orgRootCode + "00", orgCode: orgRoot.orgRootCode + "00",
}; };
return new HttpSuccess(getOrgRoot); return new HttpSuccess(getOrgRoot);
@ -350,6 +355,11 @@ export class OrgRootController extends Controller {
DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null, DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null,
SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null, SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null,
JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null, JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null,
ROOT_CODE: requestBody.ROOT_CODE != null ? requestBody.ROOT_CODE : _null,
CHILD1_CODE: requestBody.CHILD1_CODE != null ? requestBody.CHILD1_CODE : _null,
CHILD2_CODE: requestBody.CHILD2_CODE != null ? requestBody.CHILD2_CODE : _null,
CHILD3_CODE: requestBody.CHILD3_CODE != null ? requestBody.CHILD3_CODE : _null,
CHILD4_CODE: requestBody.CHILD4_CODE != null ? requestBody.CHILD4_CODE : _null,
}); });
await this.orgRootRepository.save(orgRoot, { data: request }); await this.orgRootRepository.save(orgRoot, { data: request });
setLogDataDiff(request, { before, after: orgRoot }); setLogDataDiff(request, { before, after: orgRoot });

File diff suppressed because it is too large Load diff

View file

@ -65,8 +65,7 @@ export class PermissionProfileController extends Controller {
if (!request.user.role.includes("SUPER_ADMIN")) { if (!request.user.role.includes("SUPER_ADMIN")) {
rootId = rootId =
orgRevisionActive?.posMasters?.filter((x) => x.next_holderId == profile.id)[0] orgRevisionActive?.posMasters?.filter((x) => x.current_holderId == profile.id)[0]
// orgRevisionActive?.posMasters?.filter((x) => x.current_holderId == profile.id)[0]
?.orgRootId || null; ?.orgRootId || null;
if (!rootId) return new HttpSuccess([]); if (!rootId) return new HttpSuccess([]);
} }

View file

@ -111,9 +111,12 @@ export class ProfileChangeNameController extends Controller {
setLogDataDiff(req, { before, after: history }); setLogDataDiff(req, { before, after: history });
profile.firstName = body.firstName ?? profile.firstName; profile.firstName = body.firstName ?? profile.firstName;
profile.lastName = body.lastName ?? profile.lastName; profile.lastName = body.lastName ?? profile.lastName;
profile.prefix = body.prefix ?? profile.prefix; // profile.prefix = body.prefix ?? profile.prefix; //old
profile.rank = body.rank ?? profile.rank; profile.rank = body.rank ?? profile.rank;
profile.prefixMain = profile.rank ?? profile.prefix; // profile.prefixMain = profile.rank ?? profile.prefix; // old
profile.prefixMain = body.prefix ?? profile.prefix;
profile.prefix = body.rank && body.rank.length > 0 ? body.rank : body.prefix ?? profile.prefix;
await this.profileRepository.save(profile, { data: req }); await this.profileRepository.save(profile, { data: req });
setLogDataDiff(req, { before, after: profile }); setLogDataDiff(req, { before, after: profile });
@ -184,9 +187,11 @@ export class ProfileChangeNameController extends Controller {
if (profile && chkLastRecord.id === record.id) { if (profile && chkLastRecord.id === record.id) {
profile.firstName = body.firstName ?? profile.firstName; profile.firstName = body.firstName ?? profile.firstName;
profile.lastName = body.lastName ?? profile.lastName; profile.lastName = body.lastName ?? profile.lastName;
profile.prefix = body.prefix ?? profile.prefix; // profile.prefix = body.prefix ?? profile.prefix; //old
profile.rank = body.rank ?? profile.rank; profile.rank = body.rank ?? profile.rank;
profile.prefixMain = profile.rank ?? profile.prefix; // profile.prefixMain = profile.rank ?? profile.prefix; // old
profile.prefixMain = body.prefix ?? profile.prefix;
profile.prefix = body.rank && body.rank.length > 0 ? body.rank : body.prefix ?? profile.prefix;
await this.profileRepository.save(profile, { data: req }); await this.profileRepository.save(profile, { data: req });
setLogDataDiff(req, { before: before_profile, after: profile }); setLogDataDiff(req, { before: before_profile, after: profile });
} }

View file

@ -117,9 +117,11 @@ export class ProfileChangeNameEmployeeController extends Controller {
profile.firstName = body.firstName ?? profile.firstName; profile.firstName = body.firstName ?? profile.firstName;
profile.lastName = body.lastName ?? profile.lastName; profile.lastName = body.lastName ?? profile.lastName;
profile.prefix = body.prefix ?? profile.prefix; // profile.prefix = body.prefix ?? profile.prefix; //old
profile.rank = body.rank ?? profile.rank; profile.rank = body.rank ?? profile.rank;
profile.prefixMain = profile.rank ?? profile.prefix; // profile.prefixMain = profile.rank ?? profile.prefix; // old
profile.prefixMain = body.prefix ?? profile.prefix;
profile.prefix = body.rank && body.rank.length > 0 ? body.rank : body.prefix ?? profile.prefix;
await this.profileEmployeeRepo.save(profile, { data: req }); await this.profileEmployeeRepo.save(profile, { data: req });
setLogDataDiff(req, { before, after: profile }); setLogDataDiff(req, { before, after: profile });
@ -191,9 +193,11 @@ export class ProfileChangeNameEmployeeController extends Controller {
if (profile && chkLastRecord.id === record.id) { if (profile && chkLastRecord.id === record.id) {
profile.firstName = body.firstName ?? profile.firstName; profile.firstName = body.firstName ?? profile.firstName;
profile.lastName = body.lastName ?? profile.lastName; profile.lastName = body.lastName ?? profile.lastName;
profile.prefix = body.prefix ?? profile.prefix; // profile.prefix = body.prefix ?? profile.prefix; //old
profile.rank = body.rank ?? profile.rank; profile.rank = body.rank ?? profile.rank;
profile.prefixMain = profile.rank ?? profile.prefix; // profile.prefixMain = profile.rank ?? profile.prefix; // old
profile.prefixMain = body.prefix ?? profile.prefix;
profile.prefix = body.rank && body.rank.length > 0 ? body.rank : body.prefix ?? profile.prefix;
await this.profileEmployeeRepo.save(profile); await this.profileEmployeeRepo.save(profile);
} }

View file

@ -108,9 +108,11 @@ export class ProfileChangeNameEmployeeTempController extends Controller {
profile.firstName = body.firstName ?? profile.firstName; profile.firstName = body.firstName ?? profile.firstName;
profile.lastName = body.lastName ?? profile.lastName; profile.lastName = body.lastName ?? profile.lastName;
profile.prefix = body.prefix ?? profile.prefix; // profile.prefix = body.prefix ?? profile.prefix; //old
profile.rank = body.rank ?? profile.rank; profile.rank = body.rank ?? profile.rank;
profile.prefixMain = profile.rank ?? profile.prefix; // profile.prefixMain = profile.rank ?? profile.prefix; // old
profile.prefixMain = body.prefix ?? profile.prefix;
profile.prefix = body.rank && body.rank.length > 0 ? body.rank : body.prefix ?? profile.prefix;
await this.profileEmployeeRepo.save(profile, { data: req }); await this.profileEmployeeRepo.save(profile, { data: req });
setLogDataDiff(req, { before, after: profile }); setLogDataDiff(req, { before, after: profile });
@ -179,9 +181,11 @@ export class ProfileChangeNameEmployeeTempController extends Controller {
if (profile && chkLastRecord.id === record.id) { if (profile && chkLastRecord.id === record.id) {
profile.firstName = body.firstName ?? profile.firstName; profile.firstName = body.firstName ?? profile.firstName;
profile.lastName = body.lastName ?? profile.lastName; profile.lastName = body.lastName ?? profile.lastName;
profile.prefix = body.prefix ?? profile.prefix; // profile.prefix = body.prefix ?? profile.prefix; //old
profile.rank = body.rank ?? profile.rank; profile.rank = body.rank ?? profile.rank;
profile.prefixMain = profile.rank ?? profile.prefix; // profile.prefixMain = profile.rank ?? profile.prefix; // old
profile.prefixMain = body.prefix ?? profile.prefix;
profile.prefix = body.rank && body.rank.length > 0 ? body.rank : body.prefix ?? profile.prefix;
await this.profileEmployeeRepo.save(profile); await this.profileEmployeeRepo.save(profile);
} }

View file

@ -5801,7 +5801,8 @@ export class ProfileController extends Controller {
Object.assign(record, body); Object.assign(record, body);
record.dateRetireLaw = calculateRetireLaw(record.birthDate); record.dateRetireLaw = calculateRetireLaw(record.birthDate);
record.prefixMain = record.prefix; record.prefixMain = record.prefix;
record.prefix = record.rank && record.rank.length > 0 ? record.rank : record.prefixMain; // record.prefix = record.rank && record.rank.length > 0 ? record.rank : record.prefixMain;
record.prefix = record.rank && record.rank.length > 0 ? record.rank : record.prefix;
record.createdUserId = request.user.sub; record.createdUserId = request.user.sub;
record.createdFullName = request.user.name; record.createdFullName = request.user.name;
record.createdAt = new Date(); record.createdAt = new Date();
@ -7161,6 +7162,7 @@ export class ProfileController extends Controller {
: `profile.isLeave IS TRUE` : `profile.isLeave IS TRUE`
: "1=1", : "1=1",
) )
.andWhere("profile.isActive IS TRUE AND profile.isDelete IS FALSE")
.andWhere(nodeCondition, { nodeId: nodeId }) .andWhere(nodeCondition, { nodeId: nodeId })
.andWhere( .andWhere(
new Brackets((qb) => { new Brackets((qb) => {
@ -8724,14 +8726,10 @@ export class ProfileController extends Controller {
"current_holders.orgChild2", "current_holders.orgChild2",
"current_holders.orgChild3", "current_holders.orgChild3",
"current_holders.orgChild4", "current_holders.orgChild4",
// "profileSalary",
"profileEducations", "profileEducations",
"profileActpositions", "profileActpositions",
], ],
order: { order: {
// profileSalary: {
// order: "DESC",
// },
profileEducations: { profileEducations: {
level: "ASC", level: "ASC",
}, },
@ -8796,72 +8794,6 @@ export class ProfileController extends Controller {
}); });
const holder = profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id); const holder = profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id);
const numPart = holder ? [holder.posMasterNoPrefix, holder.posMasterNo, holder.posMasterNoSuffix].filter((p) => p !== null && p !== undefined && p !== '').join(' ') : ''; const numPart = holder ? [holder.posMasterNoPrefix, holder.posMasterNo, holder.posMasterNoSuffix].filter((p) => p !== null && p !== undefined && p !== '').join(' ') : '';
const shortName =
holder == null
? null
: holder.orgChild4 != null
? `${holder.orgChild4.orgChild4ShortName} ${numPart}`
: holder.orgChild3 != null
? `${holder.orgChild3.orgChild3ShortName} ${numPart}`
: holder.orgChild2 != null
? `${holder.orgChild2.orgChild2ShortName} ${numPart}`
: holder.orgChild1 != null
? `${holder.orgChild1.orgChild1ShortName} ${numPart}`
: holder.orgRoot != null
? `${holder.orgRoot.orgRootShortName} ${numPart}`
: null;
// const posMasterActs = await this.posMasterActRepository.find({
// relations: [
// "posMaster",
// "posMaster.orgRoot",
// "posMaster.orgChild1",
// "posMaster.orgChild2",
// "posMaster.orgChild3",
// "posMaster.orgChild4",
// "posMaster.current_holder",
// "posMaster.current_holder.posLevel",
// "posMaster.current_holder.posType",
// ],
// where: {
// posMaster: {
// orgRevisionId: orgRevisionPublish.id,
// },
// posMasterChild: {
// current_holderId: profile.id,
// },
// },
// });
// const data = await Promise.all(
// posMasterActs
// .sort((a, b) => a.posMaster.posMasterOrder - b.posMaster.posMasterOrder)
// .map((item) => {
// const shortName =
// item.posMaster != null && item.posMaster.orgChild4 != null
// ? `${item.posMaster.orgChild4.orgChild4ShortName} ${item.posMaster.posMasterNo}`
// : item.posMaster != null && item.posMaster?.orgChild3 != null
// ? `${item.posMaster.orgChild3.orgChild3ShortName} ${item.posMaster.posMasterNo}`
// : item.posMaster != null && item.posMaster?.orgChild2 != null
// ? `${item.posMaster.orgChild2.orgChild2ShortName} ${item.posMaster.posMasterNo}`
// : item.posMaster != null && item.posMaster?.orgChild1 != null
// ? `${item.posMaster.orgChild1.orgChild1ShortName} ${item.posMaster.posMasterNo}`
// : item.posMaster != null && item.posMaster?.orgRoot != null
// ? `${item.posMaster.orgRoot.orgRootShortName} ${item.posMaster.posMasterNo}`
// : null;
// return {
// id: item.id,
// posMasterOrder: item.posMasterOrder,
// profileId: item.posMaster?.current_holder?.id ?? null,
// citizenId: item.posMaster?.current_holder?.citizenId ?? null,
// prefix: item.posMaster?.current_holder?.prefix ?? null,
// firstName: item.posMaster?.current_holder?.firstName ?? null,
// lastName: item.posMaster?.current_holder?.lastName ?? null,
// posLevel: item.posMaster?.current_holder?.posLevel?.posLevelName ?? null,
// posType: item.posMaster?.current_holder?.posType?.posTypeName ?? null,
// position: item.posMaster?.current_holder?.position ?? null,
// posNo: shortName,
// };
// }),
// );
const data = await Promise.all( const data = await Promise.all(
profile.profileActpositions profile.profileActpositions
.filter((x) => x.status) .filter((x) => x.status)
@ -8908,6 +8840,96 @@ export class ProfileController extends Controller {
}, },
}); });
let _Org = null;
let _PosNo = null;
if (!profile.org && !profile.posMasterNo) {
if (profile.isLeave) {
const profileWithSalary = await this.profileRepo.findOne({
where: {
id: id,
profileSalary: {
commandCode: In([
"0",
"9",
"1",
"2",
"3",
"4",
"8",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"20",
]),
},
},
relations: { profileSalary: true },
order: {
profileSalary: {
order: "DESC",
createdAt: "DESC",
},
},
});
const profileSalaryList = profileWithSalary?.profileSalary || [];
if (profileSalaryList.length > 0) {
const _profileSalary =
profile.leaveType == "RETIRE"
? profileSalaryList.length > 1
? profileSalaryList[1]
: profileSalaryList[0]
: profileSalaryList[0];
if (_profileSalary) {
const orgLeaveParts = [
_profileSalary.orgChild4 ?? null,
_profileSalary.orgChild3 ?? null,
_profileSalary.orgChild2 ?? null,
_profileSalary.orgChild1 ?? null,
_profileSalary.orgRoot ?? null,
];
_Org = orgLeaveParts
.filter((x: any) => x !== undefined && x !== null)
.join("\n");
_PosNo = `${_profileSalary.posNoAbb} ${_profileSalary.posNo}`;
}
}
} else {
_Org = [
child4?.orgChild4Name,
child3?.orgChild3Name,
child2?.orgChild2Name,
child1?.orgChild1Name,
root?.orgRootName,
]
.filter((x) => x != null && x !== "")
.join("\n");
_PosNo =
holder == null
? null
: holder.orgChild4 != null
? `${holder.orgChild4.orgChild4ShortName} ${numPart}`
: holder.orgChild3 != null
? `${holder.orgChild3.orgChild3ShortName} ${numPart}`
: holder.orgChild2 != null
? `${holder.orgChild2.orgChild2ShortName} ${numPart}`
: holder.orgChild1 != null
? `${holder.orgChild1.orgChild1ShortName} ${numPart}`
: holder.orgRoot != null
? `${holder.orgRoot.orgRootShortName} ${numPart}`
: null;
}
} else {
_Org = profile.org;
_PosNo = profile.posMasterNo;
}
const _profile: any = { const _profile: any = {
profileId: profile.id, profileId: profile.id,
prefix: profile.prefix, prefix: profile.prefix,
@ -8959,7 +8981,8 @@ export class ProfileController extends Controller {
node: null, node: null,
nodeId: null, nodeId: null,
nodeDnaId: null, nodeDnaId: null,
posNo: shortName, posNo: _PosNo,
org: _Org,
isPosmasterAct: data.length > 0, isPosmasterAct: data.length > 0,
posmasterAct: data, posmasterAct: data,
salary: profile ? profile.amount : null, salary: profile ? profile.amount : null,

View file

@ -2415,7 +2415,7 @@ export class ProfileEmployeeController extends Controller {
Object.assign(record, body); Object.assign(record, body);
record.dateRetireLaw = calculateRetireLaw(record.birthDate); record.dateRetireLaw = calculateRetireLaw(record.birthDate);
record.prefixMain = record.prefix; record.prefixMain = record.prefix;
record.prefix = record.rank && record.rank.length > 0 ? record.rank : record.prefixMain; record.prefix = record.rank && record.rank.length > 0 ? record.rank : record.prefix;
record.createdUserId = request.user.sub; record.createdUserId = request.user.sub;
record.createdFullName = request.user.name; record.createdFullName = request.user.name;
record.createdAt = new Date(); record.createdAt = new Date();
@ -3254,8 +3254,8 @@ export class ProfileEmployeeController extends Controller {
.leftJoinAndSelect("current_holders.orgChild2", "orgChild2") .leftJoinAndSelect("current_holders.orgChild2", "orgChild2")
.leftJoinAndSelect("current_holders.orgChild3", "orgChild3") .leftJoinAndSelect("current_holders.orgChild3", "orgChild3")
.leftJoinAndSelect("current_holders.orgChild4", "orgChild4") .leftJoinAndSelect("current_holders.orgChild4", "orgChild4")
.where(node && nodeId ? "current_holders.orgRevisionId = :orgRevisionId" : "1=1", { .where("current_holders.orgRevisionId = :orgRevisionId", {
orgRevisionId: node && nodeId ? findRevision.id : undefined, orgRevisionId: findRevision.id,
}) })
.andWhere( .andWhere(
_data.root != undefined && _data.root != null _data.root != undefined && _data.root != null
@ -3338,6 +3338,7 @@ export class ProfileEmployeeController extends Controller {
: `profileEmployee.isLeave IS TRUE` : `profileEmployee.isLeave IS TRUE`
: "1=1", : "1=1",
) )
.andWhere("profileEmployee.isActive IS TRUE AND profileEmployee.isDelete IS FALSE")
.andWhere("profileEmployee.employeeClass LIKE :type", { .andWhere("profileEmployee.employeeClass LIKE :type", {
type: "PERM", type: "PERM",
}) })
@ -6461,6 +6462,92 @@ export class ProfileEmployeeController extends Controller {
? null ? null
: profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild4; : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild4;
const _numPart = posMaster ? [posMaster.posMasterNoPrefix, posMaster.posMasterNo, posMaster.posMasterNoSuffix].filter((p) => p !== null && p !== undefined && p !== '').join(' ') : ''; const _numPart = posMaster ? [posMaster.posMasterNoPrefix, posMaster.posMasterNo, posMaster.posMasterNoSuffix].filter((p) => p !== null && p !== undefined && p !== '').join(' ') : '';
// org / posNo — ล้อ fallback ของ officer (ProfileController.getProfileByProfileid)
// employee ไม่มี profile.org / profile.posMasterNo จึงแยกตาม isLeave โดยตรง
let _Org: string | null = null;
let _PosNo: string | null = null;
if (profile.isLeave) {
const profileWithSalary = await this.profileRepo.findOne({
where: {
id: id,
profileSalary: {
commandCode: In([
"0",
"9",
"1",
"2",
"3",
"4",
"8",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"20",
]),
},
},
relations: { profileSalary: true },
order: {
profileSalary: {
order: "DESC",
createdAt: "DESC",
},
},
});
const profileSalaryList = profileWithSalary?.profileSalary || [];
if (profileSalaryList.length > 0) {
const _profileSalary =
profile.leaveType == "RETIRE"
? profileSalaryList.length > 1
? profileSalaryList[1]
: profileSalaryList[0]
: profileSalaryList[0];
if (_profileSalary) {
_Org = [
_profileSalary.orgChild4 ?? null,
_profileSalary.orgChild3 ?? null,
_profileSalary.orgChild2 ?? null,
_profileSalary.orgChild1 ?? null,
_profileSalary.orgRoot ?? null,
]
.filter((x: any) => x !== undefined && x !== null)
.join("\n");
_PosNo = `${_profileSalary.posNoAbb} ${_profileSalary.posNo}`;
}
}
} else {
_Org = [
child4?.orgChild4Name,
child3?.orgChild3Name,
child2?.orgChild2Name,
child1?.orgChild1Name,
root?.orgRootName,
]
.filter((x) => x != null && x !== "")
.join("\n");
_PosNo =
posMaster == null
? null
: child4 != null
? `${child4.orgChild4ShortName} ${_numPart}`
: child3 != null
? `${child3.orgChild3ShortName} ${_numPart}`
: child2 != null
? `${child2.orgChild2ShortName} ${_numPart}`
: child1 != null
? `${child1.orgChild1ShortName} ${_numPart}`
: root != null
? `${root.orgRootShortName} ${_numPart}`
: null;
}
const _profile: any = { const _profile: any = {
profileId: profile.id, profileId: profile.id,
prefix: profile.prefix, prefix: profile.prefix,
@ -6473,7 +6560,7 @@ export class ProfileEmployeeController extends Controller {
position: profile.position, position: profile.position,
leaveDate: profile.dateLeave, leaveDate: profile.dateLeave,
posMasterNo: posMaster == null ? null : posMaster.posMasterNo, posMasterNo: posMaster == null ? null : posMaster.posMasterNo,
posLevelName: `${profile?.posType?.posTypeShortName ?? ""} ${profile?.posLevel?.posLevelName ?? ""}`, posLevelName: `${profile?.posType?.posTypeShortName ?? ""} ${profile?.posLevel?.posLevelName ?? ""}`.trim(),
posLevelRank: profile.posLevel == null ? null : profile.posLevel.posLevelRank, posLevelRank: profile.posLevel == null ? null : profile.posLevel.posLevelRank,
posLevelId: profile.posLevel == null ? null : profile.posLevel.id, posLevelId: profile.posLevel == null ? null : profile.posLevel.id,
posTypeName: profile.posType == null ? null : profile.posType.posTypeName, posTypeName: profile.posType == null ? null : profile.posType.posTypeName,
@ -6502,7 +6589,8 @@ export class ProfileEmployeeController extends Controller {
child4ShortName: child4 == null ? null : child4.orgChild4ShortName, child4ShortName: child4 == null ? null : child4.orgChild4ShortName,
node: null, node: null,
nodeId: null, nodeId: null,
posNo: null, posNo: _PosNo,
org: _Org,
salary: profile.amount, salary: profile.amount,
education: education:
profile && profile.profileEducations.length > 0 profile && profile.profileEducations.length > 0
@ -6517,27 +6605,22 @@ export class ProfileEmployeeController extends Controller {
_profile.node = 4; _profile.node = 4;
_profile.nodeId = _profile.child4Id; _profile.nodeId = _profile.child4Id;
_profile.nodeShortName = _profile.child4ShortName; _profile.nodeShortName = _profile.child4ShortName;
_profile.posNo = `${_profile.child4ShortName} ${_numPart}`;
} else if (_profile.child3Id != null) { } else if (_profile.child3Id != null) {
_profile.node = 3; _profile.node = 3;
_profile.nodeId = _profile.child3Id; _profile.nodeId = _profile.child3Id;
_profile.nodeShortName = _profile.child3ShortName; _profile.nodeShortName = _profile.child3ShortName;
_profile.posNo = `${_profile.child3ShortName} ${_numPart}`;
} else if (_profile.child2Id != null) { } else if (_profile.child2Id != null) {
_profile.node = 2; _profile.node = 2;
_profile.nodeId = _profile.child2Id; _profile.nodeId = _profile.child2Id;
_profile.nodeShortName = _profile.child2ShortName; _profile.nodeShortName = _profile.child2ShortName;
_profile.posNo = `${_profile.child2ShortName} ${_numPart}`;
} else if (_profile.child1Id != null) { } else if (_profile.child1Id != null) {
_profile.node = 1; _profile.node = 1;
_profile.nodeId = _profile.child1Id; _profile.nodeId = _profile.child1Id;
_profile.nodeShortName = _profile.child1ShortName; _profile.nodeShortName = _profile.child1ShortName;
_profile.posNo = `${_profile.child1ShortName} ${_numPart}`;
} else if (_profile.rootId != null) { } else if (_profile.rootId != null) {
_profile.node = 0; _profile.node = 0;
_profile.nodeId = _profile.rootId; _profile.nodeId = _profile.rootId;
_profile.nodeShortName = _profile.rootShortName; _profile.nodeShortName = _profile.rootShortName;
_profile.posNo = `${_profile.rootShortName} ${_numPart}`;
} }
return new HttpSuccess(_profile); return new HttpSuccess(_profile);
} }

View file

@ -23,6 +23,7 @@ import { ProfileEmployee } from "../entities/ProfileEmployee";
import { In, IsNull, LessThan, MoreThan, Not } from "typeorm"; import { In, IsNull, LessThan, MoreThan, Not } from "typeorm";
import permission from "../interfaces/permission"; import permission from "../interfaces/permission";
import { setLogDataDiff } from "../interfaces/utils"; import { setLogDataDiff } from "../interfaces/utils";
import { ExecuteSalaryReportService } from "../services/ExecuteSalaryReportService";
import { normalizeDurationSumSimple } from "../utils/tenure"; import { normalizeDurationSumSimple } from "../utils/tenure";
import { import {
TenurePositionOfficer, TenurePositionOfficer,
@ -1380,91 +1381,10 @@ export class ProfileSalaryController extends Controller {
@Post("update") @Post("update")
public async updateSalary(@Request() req: RequestWithUser, @Body() body: CreateProfileSalary) { public async updateSalary(@Request() req: RequestWithUser, @Body() body: CreateProfileSalary) {
if (!body.profileId) { await new ExecuteSalaryReportService().executeOfficerSalaryUpdate([body], {
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); user: { sub: req.user.sub, name: req.user.name },
} req,
const profile = await this.profileRepo.findOneBy({ id: body.profileId });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id);
const dest_item = await this.salaryRepo.findOne({
where: { profileId: body.profileId },
order: { order: "DESC" },
}); });
const before = null;
let _posNumCodeSit: string = "";
let _posNumCodeSitAbb: string = "";
const _command = await this.commandRepository.findOne({
where: { id: body.commandId ?? "" },
});
if (_command) {
if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") {
const orgRootDeputy = await this.orgRootRepository.findOne({
where: {
isDeputy: true,
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
relations: ["orgRevision"],
});
_posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร";
_posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป.";
} else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") {
_posNumCodeSit = "กรุงเทพมหานคร";
_posNumCodeSitAbb = "กทม.";
} else {
let _profileAdmin = await this.profileRepo.findOne({
where: {
keycloak: _command?.createdUserId.toString(),
current_holders: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
},
relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"],
});
_posNumCodeSit =
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ??
"";
_posNumCodeSitAbb =
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot
.orgRootShortName ?? "";
}
}
const data = new ProfileSalary();
data.posNumCodeSit = _posNumCodeSit;
data.posNumCodeSitAbb = _posNumCodeSitAbb;
const meta = {
order: dest_item == null ? 1 : dest_item.order + 1,
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
};
Object.assign(data, { ...body, ...meta });
const history = new ProfileSalaryHistory();
Object.assign(history, { ...data, id: undefined });
await this.salaryRepo.save(data, { data: req });
setLogDataDiff(req, { before, after: data });
history.profileSalaryId = data.id;
await this.salaryHistoryRepo.save(history, { data: req });
let _null: any = null;
profile.amount = body.amount ?? _null;
profile.amountSpecial = body.amountSpecial ?? _null;
profile.positionSalaryAmount = body.positionSalaryAmount ?? _null;
profile.mouthSalaryAmount = body.mouthSalaryAmount ?? _null;
await this.profileRepo.save(profile);
return new HttpSuccess(); return new HttpSuccess();
} }

View file

@ -27,6 +27,7 @@ import { Profile } from "../entities/Profile";
import { In, LessThan, IsNull, MoreThan } from "typeorm"; import { In, LessThan, IsNull, MoreThan } from "typeorm";
import permission from "../interfaces/permission"; import permission from "../interfaces/permission";
import { setLogDataDiff } from "../interfaces/utils"; import { setLogDataDiff } from "../interfaces/utils";
import { ExecuteSalaryReportService } from "../services/ExecuteSalaryReportService";
import { normalizeDurationSumSimple } from "../utils/tenure"; import { normalizeDurationSumSimple } from "../utils/tenure";
import { Command } from "../entities/Command"; import { Command } from "../entities/Command";
import { OrgRoot } from "../entities/OrgRoot"; import { OrgRoot } from "../entities/OrgRoot";
@ -507,94 +508,10 @@ export class ProfileSalaryEmployeeController extends Controller {
@Request() req: RequestWithUser, @Request() req: RequestWithUser,
@Body() body: CreateProfileSalaryEmployee, @Body() body: CreateProfileSalaryEmployee,
) { ) {
if (!body.profileEmployeeId) { await new ExecuteSalaryReportService().executeEmployeeSalaryUpdate([body], {
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileEmployeeId"); user: { sub: req.user.sub, name: req.user.name },
} req,
const profile = await this.profileRepo.findOneBy({ id: body.profileEmployeeId });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id);
const dest_item = await this.salaryRepo.findOne({
where: { profileEmployeeId: body.profileEmployeeId },
order: { order: "DESC" },
}); });
const before = null;
let _posNumCodeSit: string = "";
let _posNumCodeSitAbb: string = "";
const _command = await this.commandRepository.findOne({
where: { id: body.commandId ?? "" },
});
if (_command) {
if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") {
const orgRootDeputy = await this.orgRootRepository.findOne({
where: {
isDeputy: true,
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
relations: ["orgRevision"],
});
_posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร";
_posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป.";
} else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") {
_posNumCodeSit = "กรุงเทพมหานคร";
_posNumCodeSitAbb = "กทม.";
} else {
let _profileAdmin = await this.profileGovementRepo.findOne({
where: {
keycloak: _command?.createdUserId.toString(),
current_holders: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
},
relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"],
});
_posNumCodeSit =
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ??
"";
_posNumCodeSitAbb =
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot
.orgRootShortName ?? "";
}
}
const data = new ProfileSalary();
data.posNumCodeSit = _posNumCodeSit;
data.posNumCodeSitAbb = _posNumCodeSitAbb;
const meta = {
order: dest_item == null ? 1 : dest_item.order + 1,
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
};
Object.assign(data, { ...body, ...meta });
const history = new ProfileSalaryHistory();
Object.assign(history, { ...data, id: undefined });
await this.salaryRepo.save(data, { data: req });
setLogDataDiff(req, { before, after: data });
history.profileSalaryId = data.id;
await this.salaryHistoryRepo.save(history, { data: req });
let _null: any = null;
profile.amount = body.amount ?? _null;
profile.amountSpecial = body.amountSpecial ?? _null;
profile.positionSalaryAmount = body.positionSalaryAmount ?? _null;
profile.mouthSalaryAmount = body.mouthSalaryAmount ?? _null;
profile.salaryLevel = body.salaryLevel ?? _null;
profile.group = body.group ?? _null;
await this.profileRepo.save(profile);
return new HttpSuccess(); return new HttpSuccess();
} }

View file

@ -865,6 +865,7 @@ export class WorkflowController extends Controller {
type?: string | null; type?: string | null;
sortBy?: string | null; sortBy?: string | null;
descending?: boolean; descending?: boolean;
isAllDirector?: boolean;
}, },
) { ) {
const userKeycloak = body.keycloakId ?? request.user.sub; const userKeycloak = body.keycloakId ?? request.user.sub;
@ -953,38 +954,46 @@ export class WorkflowController extends Controller {
let mainConditions: any[] = []; let mainConditions: any[] = [];
if (type.trim().toUpperCase() === "OPERATE" || body.type === "employee") { if (type.trim().toUpperCase() === "OPERATE" || body.type === "employee") {
mainConditions = [
{ ...baseCondition, orgRootId: In(roodIds), orgChild1Id: IsNull() }, if (body.isAllDirector === true) {
{ mainConditions = [
...baseCondition, { ...baseCondition, orgRootId: In(roodIds) }
orgRootId: In(roodIds), ];
orgChild1Id: posMasterUser.orgChild1Id, }
orgChild2Id: IsNull(), else {
}, mainConditions = [
{ { ...baseCondition, orgRootId: In(roodIds), orgChild1Id: IsNull() },
...baseCondition, {
orgRootId: In(roodIds), ...baseCondition,
orgChild1Id: posMasterUser.orgChild1Id, orgRootId: In(roodIds),
orgChild2Id: posMasterUser.orgChild2Id, orgChild1Id: posMasterUser.orgChild1Id,
orgChild3Id: IsNull(), orgChild2Id: IsNull(),
}, },
{ {
...baseCondition, ...baseCondition,
orgRootId: In(roodIds), orgRootId: In(roodIds),
orgChild1Id: posMasterUser.orgChild1Id, orgChild1Id: posMasterUser.orgChild1Id,
orgChild2Id: posMasterUser.orgChild2Id, orgChild2Id: posMasterUser.orgChild2Id,
orgChild3Id: posMasterUser.orgChild3Id, orgChild3Id: IsNull(),
orgChild4Id: IsNull(), },
}, {
{ ...baseCondition,
...baseCondition, orgRootId: In(roodIds),
orgRootId: In(roodIds), orgChild1Id: posMasterUser.orgChild1Id,
orgChild1Id: posMasterUser.orgChild1Id, orgChild2Id: posMasterUser.orgChild2Id,
orgChild2Id: posMasterUser.orgChild2Id, orgChild3Id: posMasterUser.orgChild3Id,
orgChild3Id: posMasterUser.orgChild3Id, orgChild4Id: IsNull(),
orgChild4Id: posMasterUser.orgChild4Id, },
}, {
]; ...baseCondition,
orgRootId: In(roodIds),
orgChild1Id: posMasterUser.orgChild1Id,
orgChild2Id: posMasterUser.orgChild2Id,
orgChild3Id: posMasterUser.orgChild3Id,
orgChild4Id: posMasterUser.orgChild4Id,
},
];
}
} else if (isLowLevel) { } else if (isLowLevel) {
mainConditions = [ mainConditions = [
{ {

View file

@ -174,6 +174,46 @@ export class OrgChild1 extends EntityBase {
}) })
JOB_CODE: string; JOB_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "ROOT_CODE",
default: null,
})
ROOT_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD1_CODE",
default: null,
})
CHILD1_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD2_CODE",
default: null,
})
CHILD2_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD3_CODE",
default: null,
})
CHILD3_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD4_CODE",
default: null,
})
CHILD4_CODE: string;
@ManyToOne(() => OrgRoot, (orgRoot) => orgRoot.orgChild1s) @ManyToOne(() => OrgRoot, (orgRoot) => orgRoot.orgChild1s)
@JoinColumn({ name: "orgRootId" }) @JoinColumn({ name: "orgRootId" })
orgRoot: OrgRoot; orgRoot: OrgRoot;
@ -228,6 +268,21 @@ export class CreateOrgChild1 {
@Column() @Column()
JOB_CODE?: string | null; JOB_CODE?: string | null;
@Column()
ROOT_CODE?: string | null;
@Column()
CHILD1_CODE?: string | null;
@Column()
CHILD2_CODE?: string | null;
@Column()
CHILD3_CODE?: string | null;
@Column()
CHILD4_CODE?: string | null;
@Column() @Column()
orgChild1PhoneEx?: string; orgChild1PhoneEx?: string;
@ -283,6 +338,21 @@ export class UpdateOrgChild1 {
@Column() @Column()
JOB_CODE?: string | null; JOB_CODE?: string | null;
@Column()
ROOT_CODE?: string | null;
@Column()
CHILD1_CODE?: string | null;
@Column()
CHILD2_CODE?: string | null;
@Column()
CHILD3_CODE?: string | null;
@Column()
CHILD4_CODE?: string | null;
@Column() @Column()
orgChild1PhoneEx?: string; orgChild1PhoneEx?: string;

View file

@ -146,6 +146,46 @@ export class OrgChild2 extends EntityBase {
}) })
JOB_CODE: string; JOB_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "ROOT_CODE",
default: null,
})
ROOT_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD1_CODE",
default: null,
})
CHILD1_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD2_CODE",
default: null,
})
CHILD2_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD3_CODE",
default: null,
})
CHILD3_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD4_CODE",
default: null,
})
CHILD4_CODE: string;
@Column({ @Column({
nullable: true, nullable: true,
length: 40, length: 40,
@ -222,6 +262,21 @@ export class CreateOrgChild2 {
@Column() @Column()
JOB_CODE?: string; JOB_CODE?: string;
@Column()
ROOT_CODE?: string;
@Column()
CHILD1_CODE?: string;
@Column()
CHILD2_CODE?: string;
@Column()
CHILD3_CODE?: string;
@Column()
CHILD4_CODE?: string;
@Column() @Column()
orgChild2PhoneEx?: string; orgChild2PhoneEx?: string;
@ -269,6 +324,21 @@ export class UpdateOrgChild2 {
@Column() @Column()
JOB_CODE?: string | null; JOB_CODE?: string | null;
@Column()
ROOT_CODE?: string | null;
@Column()
CHILD1_CODE?: string | null;
@Column()
CHILD2_CODE?: string | null;
@Column()
CHILD3_CODE?: string | null;
@Column()
CHILD4_CODE?: string | null;
@Column() @Column()
orgChild2PhoneEx?: string; orgChild2PhoneEx?: string;

View file

@ -153,6 +153,46 @@ export class OrgChild3 extends EntityBase {
}) })
JOB_CODE: string; JOB_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "ROOT_CODE",
default: null,
})
ROOT_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD1_CODE",
default: null,
})
CHILD1_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD2_CODE",
default: null,
})
CHILD2_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD3_CODE",
default: null,
})
CHILD3_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD4_CODE",
default: null,
})
CHILD4_CODE: string;
@Column({ @Column({
nullable: true, nullable: true,
length: 40, length: 40,
@ -230,6 +270,21 @@ export class CreateOrgChild3 {
@Column() @Column()
JOB_CODE?: string; JOB_CODE?: string;
@Column()
ROOT_CODE?: string;
@Column()
CHILD1_CODE?: string;
@Column()
CHILD2_CODE?: string;
@Column()
CHILD3_CODE?: string;
@Column()
CHILD4_CODE?: string;
@Column() @Column()
orgChild3PhoneEx?: string; orgChild3PhoneEx?: string;
@ -279,6 +334,21 @@ export class UpdateOrgChild3 {
@Column() @Column()
JOB_CODE?: string | null; JOB_CODE?: string | null;
@Column()
ROOT_CODE?: string | null;
@Column()
CHILD1_CODE?: string | null;
@Column()
CHILD2_CODE?: string | null;
@Column()
CHILD3_CODE?: string | null;
@Column()
CHILD4_CODE?: string | null;
@Column() @Column()
orgChild3PhoneEx?: string; orgChild3PhoneEx?: string;

View file

@ -158,6 +158,46 @@ export class OrgChild4 extends EntityBase {
}) })
JOB_CODE: string; JOB_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "ROOT_CODE",
default: null,
})
ROOT_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD1_CODE",
default: null,
})
CHILD1_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD2_CODE",
default: null,
})
CHILD2_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD3_CODE",
default: null,
})
CHILD3_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD4_CODE",
default: null,
})
CHILD4_CODE: string;
@Column({ @Column({
nullable: true, nullable: true,
length: 40, length: 40,
@ -236,6 +276,21 @@ export class CreateOrgChild4 {
@Column() @Column()
JOB_CODE?: string; JOB_CODE?: string;
@Column()
ROOT_CODE?: string;
@Column()
CHILD1_CODE?: string;
@Column()
CHILD2_CODE?: string;
@Column()
CHILD3_CODE?: string;
@Column()
CHILD4_CODE?: string;
@Column() @Column()
orgChild4PhoneEx?: string; orgChild4PhoneEx?: string;
@ -283,6 +338,21 @@ export class UpdateOrgChild4 {
@Column() @Column()
JOB_CODE?: string | null; JOB_CODE?: string | null;
@Column()
ROOT_CODE?: string | null;
@Column()
CHILD1_CODE?: string | null;
@Column()
CHILD2_CODE?: string | null;
@Column()
CHILD3_CODE?: string | null;
@Column()
CHILD4_CODE?: string | null;
@Column() @Column()
orgChild4PhoneEx?: string; orgChild4PhoneEx?: string;

View file

@ -166,6 +166,46 @@ export class OrgRoot extends EntityBase {
}) })
JOB_CODE: string; JOB_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "ROOT_CODE",
default: null,
})
ROOT_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD1_CODE",
default: null,
})
CHILD1_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD2_CODE",
default: null,
})
CHILD2_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD3_CODE",
default: null,
})
CHILD3_CODE: string;
@Column({
nullable: true,
length: 3,
comment: "CHILD4_CODE",
default: null,
})
CHILD4_CODE: string;
@ManyToOne(() => OrgRevision, (orgRevision) => orgRevision.orgRoots) @ManyToOne(() => OrgRevision, (orgRevision) => orgRevision.orgRoots)
@JoinColumn({ name: "orgRevisionId" }) @JoinColumn({ name: "orgRevisionId" })
orgRevision: OrgRevision; orgRevision: OrgRevision;
@ -226,6 +266,21 @@ export class CreateOrgRoot {
@Column() @Column()
JOB_CODE?: string; JOB_CODE?: string;
@Column()
ROOT_CODE?: string;
@Column()
CHILD1_CODE?: string;
@Column()
CHILD2_CODE?: string;
@Column()
CHILD3_CODE?: string;
@Column()
CHILD4_CODE?: string;
@Column() @Column()
orgRootPhoneEx?: string; orgRootPhoneEx?: string;
@ -281,6 +336,21 @@ export class UpdateOrgRoot {
@Column() @Column()
JOB_CODE?: string | null; JOB_CODE?: string | null;
@Column()
ROOT_CODE?: string | null;
@Column()
CHILD1_CODE?: string | null;
@Column()
CHILD2_CODE?: string | null;
@Column()
CHILD3_CODE?: string | null;
@Column()
CHILD4_CODE?: string | null;
@Column() @Column()
orgRootPhoneEx?: string; orgRootPhoneEx?: string;

View file

@ -1014,7 +1014,7 @@ export class UpdateInformationProfileEmployee {
} }
export type UpdateProfileEmployee = { export type UpdateProfileEmployee = {
prefix: string; prefix?: string | null;
rank?: string | null; rank?: string | null;
firstName: string; firstName: string;
lastName: string; lastName: string;

View file

@ -4,7 +4,7 @@ import { PosMaster } from "../entities/PosMaster";
import { Position } from "../entities/Position"; import { Position } from "../entities/Position";
import { EmployeePosMaster } from "../entities/EmployeePosMaster"; import { EmployeePosMaster } from "../entities/EmployeePosMaster";
import { EmployeePosition } from "../entities/EmployeePosition"; import { EmployeePosition } from "../entities/EmployeePosition";
import { In, IsNull, MoreThan, Not } from "typeorm"; import { EntityManager, In, IsNull, MoreThan, Not } from "typeorm";
import { RequestWithUser } from "../middlewares/user"; import { RequestWithUser } from "../middlewares/user";
import { Command } from "../entities/Command"; import { Command } from "../entities/Command";
import { ProfileSalary } from "../entities/ProfileSalary"; import { ProfileSalary } from "../entities/ProfileSalary";
@ -254,14 +254,23 @@ export function calculateRetireYear(birthDate: Date) {
return yy + 61; return yy + 61;
} }
export async function removeProfileInOrganize(profileId: string, type: string) { export async function removeProfileInOrganize(
const currentRevision = await AppDataSource.getRepository(OrgRevision) profileId: string,
type: string,
manager?: EntityManager,
) {
// ถ้าส่ง manager เข้ามา → ทุก query/update อยู่ใน transaction ของ caller (all-or-nothing)
// ถ้าไม่ส่ง → ใช้ global DataSource เหมือนเดิม (backward compatible)
const ds = manager ?? AppDataSource;
const currentRevision = await ds
.getRepository(OrgRevision)
.createQueryBuilder("orgRevision") .createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = false") .where("orgRevision.orgRevisionIsDraft = false")
.andWhere("orgRevision.orgRevisionIsCurrent = true") .andWhere("orgRevision.orgRevisionIsCurrent = true")
.getOne(); .getOne();
const draftRevision = await AppDataSource.getRepository(OrgRevision) const draftRevision = await ds
.getRepository(OrgRevision)
.createQueryBuilder("orgRevision") .createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = true") .where("orgRevision.orgRevisionIsDraft = true")
.andWhere("orgRevision.orgRevisionIsCurrent = false") .andWhere("orgRevision.orgRevisionIsCurrent = false")
@ -271,26 +280,30 @@ export async function removeProfileInOrganize(profileId: string, type: string) {
return; return;
} }
if (type === "OFFICER") { if (type === "OFFICER") {
const findProfileInposMaster = await AppDataSource.getRepository(PosMaster) const findProfileInposMaster = await ds
.getRepository(PosMaster)
.createQueryBuilder("posMaster") .createQueryBuilder("posMaster")
.where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: currentRevision?.id }) .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: currentRevision?.id })
.andWhere("posMaster.current_holderId = :profileId", { profileId }) .andWhere("posMaster.current_holderId = :profileId", { profileId })
.getOne(); .getOne();
await AppDataSource.getRepository(PosMaster) await ds
.getRepository(PosMaster)
.createQueryBuilder() .createQueryBuilder()
.update(PosMaster) .update(PosMaster)
.set({ current_holderId: null, isSit: false }) .set({ current_holderId: null, isSit: false })
.where("id = :id", { id: findProfileInposMaster?.id }) .where("id = :id", { id: findProfileInposMaster?.id })
.execute(); .execute();
const findProfileInposMasterDraft = await AppDataSource.getRepository(PosMaster) const findProfileInposMasterDraft = await ds
.getRepository(PosMaster)
.createQueryBuilder("posMaster") .createQueryBuilder("posMaster")
.where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: draftRevision?.id }) .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: draftRevision?.id })
.andWhere("posMaster.next_holderId = :profileId", { profileId }) .andWhere("posMaster.next_holderId = :profileId", { profileId })
.getOne(); .getOne();
await AppDataSource.getRepository(PosMaster) await ds
.getRepository(PosMaster)
.createQueryBuilder() .createQueryBuilder()
.update(PosMaster) .update(PosMaster)
.set({ next_holderId: null, isSit: false }) .set({ next_holderId: null, isSit: false })
@ -300,7 +313,8 @@ export async function removeProfileInOrganize(profileId: string, type: string) {
if (!findProfileInposMaster && !findProfileInposMasterDraft) { if (!findProfileInposMaster && !findProfileInposMasterDraft) {
return; return;
} }
const findPosition = await AppDataSource.getRepository(Position) const findPosition = await ds
.getRepository(Position)
.createQueryBuilder("position") .createQueryBuilder("position")
.where("position.posMasterId = :posMasterId", { posMasterId: findProfileInposMaster?.id }) .where("position.posMasterId = :posMasterId", { posMasterId: findProfileInposMaster?.id })
.getMany(); .getMany();
@ -308,7 +322,8 @@ export async function removeProfileInOrganize(profileId: string, type: string) {
if (!findPosition) { if (!findPosition) {
return; return;
} }
await AppDataSource.getRepository(Position) await ds
.getRepository(Position)
.createQueryBuilder() .createQueryBuilder()
.update(Position) .update(Position)
.set({ positionIsSelected: false }) .set({ positionIsSelected: false })
@ -316,14 +331,16 @@ export async function removeProfileInOrganize(profileId: string, type: string) {
.execute(); .execute();
} }
if (type === "EMPLOYEE") { if (type === "EMPLOYEE") {
const findProfileInEmpPosMaster = await AppDataSource.getRepository(EmployeePosMaster) const findProfileInEmpPosMaster = await ds
.getRepository(EmployeePosMaster)
.createQueryBuilder("employeePosMaster") .createQueryBuilder("employeePosMaster")
.where("employeePosMaster.orgRevisionId = :orgRevisionId", { .where("employeePosMaster.orgRevisionId = :orgRevisionId", {
orgRevisionId: currentRevision?.id, orgRevisionId: currentRevision?.id,
}) })
.andWhere("employeePosMaster.current_holderId = :profileId", { profileId }) .andWhere("employeePosMaster.current_holderId = :profileId", { profileId })
.getOne(); .getOne();
await AppDataSource.getRepository(EmployeePosMaster) await ds
.getRepository(EmployeePosMaster)
.createQueryBuilder() .createQueryBuilder()
.update(EmployeePosMaster) .update(EmployeePosMaster)
.set({ current_holderId: null, isSit: false }) .set({ current_holderId: null, isSit: false })
@ -333,7 +350,8 @@ export async function removeProfileInOrganize(profileId: string, type: string) {
if (!findProfileInEmpPosMaster) { if (!findProfileInEmpPosMaster) {
return; return;
} }
const findEmpPosition = await AppDataSource.getRepository(EmployeePosition) const findEmpPosition = await ds
.getRepository(EmployeePosition)
.createQueryBuilder("employeePosition") .createQueryBuilder("employeePosition")
.where("employeePosition.posMasterId = :posMasterId", { .where("employeePosition.posMasterId = :posMasterId", {
posMasterId: findProfileInEmpPosMaster?.id, posMasterId: findProfileInEmpPosMaster?.id,
@ -344,7 +362,8 @@ export async function removeProfileInOrganize(profileId: string, type: string) {
return; return;
} }
await AppDataSource.getRepository(EmployeePosition) await ds
.getRepository(EmployeePosition)
.createQueryBuilder() .createQueryBuilder()
.update(EmployeePosition) .update(EmployeePosition)
.set({ positionIsSelected: false }) .set({ positionIsSelected: false })
@ -353,8 +372,10 @@ export async function removeProfileInOrganize(profileId: string, type: string) {
} }
} }
export async function removePostMasterAct(profileId: string) { export async function removePostMasterAct(profileId: string, manager?: EntityManager) {
const currentRevision = await AppDataSource.getRepository(OrgRevision) const ds = manager ?? AppDataSource;
const currentRevision = await ds
.getRepository(OrgRevision)
.createQueryBuilder("orgRevision") .createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = false") .where("orgRevision.orgRevisionIsDraft = false")
.andWhere("orgRevision.orgRevisionIsCurrent = true") .andWhere("orgRevision.orgRevisionIsCurrent = true")
@ -364,7 +385,8 @@ export async function removePostMasterAct(profileId: string) {
return; return;
} }
const findProfileInposMaster = await AppDataSource.getRepository(PosMaster) const findProfileInposMaster = await ds
.getRepository(PosMaster)
.createQueryBuilder("posMaster") .createQueryBuilder("posMaster")
.where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: currentRevision?.id }) .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: currentRevision?.id })
.andWhere("posMaster.current_holderId = :profileId", { profileId }) .andWhere("posMaster.current_holderId = :profileId", { profileId })
@ -374,11 +396,12 @@ export async function removePostMasterAct(profileId: string) {
return; return;
} }
const posMasterAct = await AppDataSource.getRepository(PosMasterAct) const posMasterAct = await ds
.getRepository(PosMasterAct)
.createQueryBuilder("posMasterAct") .createQueryBuilder("posMasterAct")
.where("posMasterAct.posMasterChildId = :posMasterChildId", { posMasterChildId: findProfileInposMaster.id }) .where("posMasterAct.posMasterChildId = :posMasterChildId", { posMasterChildId: findProfileInposMaster.id })
.getMany(); .getMany();
await AppDataSource.getRepository(PosMasterAct).remove(posMasterAct); await ds.getRepository(PosMasterAct).remove(posMasterAct);
} }
export async function checkReturnCommandType(commandId: string) { export async function checkReturnCommandType(commandId: string) {

View file

@ -0,0 +1,22 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class UpdateRootC1ToC4AddFieldCode1781517610929 implements MigrationInterface {
name = 'UpdateRootC1ToC4AddFieldCode1781517610929'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`orgChild4\` ADD \`CHILD4_CODE\` varchar(3) NULL COMMENT 'CHILD4_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild3\` ADD \`CHILD3_CODE\` varchar(3) NULL COMMENT 'CHILD3_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild2\` ADD \`CHILD2_CODE\` varchar(3) NULL COMMENT 'CHILD2_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild1\` ADD \`CHILD1_CODE\` varchar(3) NULL COMMENT 'CHILD1_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgRoot\` ADD \`ROOT_CODE\` varchar(3) NULL COMMENT 'ROOT_CODE'`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`orgRoot\` DROP COLUMN \`ROOT_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild1\` DROP COLUMN \`CHILD1_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild2\` DROP COLUMN \`CHILD2_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild3\` DROP COLUMN \`CHILD3_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild4\` DROP COLUMN \`CHILD4_CODE\``);
}
}

View file

@ -0,0 +1,51 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class UpdateAddFieldCodeAllTables1781577597453 implements MigrationInterface {
name = 'UpdateAddFieldCodeAllTables1781577597453'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`orgChild4\` ADD \`ROOT_CODE\` varchar(3) NULL COMMENT 'ROOT_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild4\` ADD \`CHILD1_CODE\` varchar(3) NULL COMMENT 'CHILD1_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild4\` ADD \`CHILD2_CODE\` varchar(3) NULL COMMENT 'CHILD2_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild4\` ADD \`CHILD3_CODE\` varchar(3) NULL COMMENT 'CHILD3_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild3\` ADD \`ROOT_CODE\` varchar(3) NULL COMMENT 'ROOT_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild3\` ADD \`CHILD1_CODE\` varchar(3) NULL COMMENT 'CHILD1_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild3\` ADD \`CHILD2_CODE\` varchar(3) NULL COMMENT 'CHILD2_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild3\` ADD \`CHILD4_CODE\` varchar(3) NULL COMMENT 'CHILD4_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild2\` ADD \`ROOT_CODE\` varchar(3) NULL COMMENT 'ROOT_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild2\` ADD \`CHILD1_CODE\` varchar(3) NULL COMMENT 'CHILD1_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild2\` ADD \`CHILD3_CODE\` varchar(3) NULL COMMENT 'CHILD3_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild2\` ADD \`CHILD4_CODE\` varchar(3) NULL COMMENT 'CHILD4_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild1\` ADD \`ROOT_CODE\` varchar(3) NULL COMMENT 'ROOT_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild1\` ADD \`CHILD2_CODE\` varchar(3) NULL COMMENT 'CHILD2_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild1\` ADD \`CHILD3_CODE\` varchar(3) NULL COMMENT 'CHILD3_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgChild1\` ADD \`CHILD4_CODE\` varchar(3) NULL COMMENT 'CHILD4_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgRoot\` ADD \`CHILD1_CODE\` varchar(3) NULL COMMENT 'CHILD1_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgRoot\` ADD \`CHILD2_CODE\` varchar(3) NULL COMMENT 'CHILD2_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgRoot\` ADD \`CHILD3_CODE\` varchar(3) NULL COMMENT 'CHILD3_CODE'`);
await queryRunner.query(`ALTER TABLE \`orgRoot\` ADD \`CHILD4_CODE\` varchar(3) NULL COMMENT 'CHILD4_CODE'`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`orgRoot\` DROP COLUMN \`CHILD4_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgRoot\` DROP COLUMN \`CHILD3_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgRoot\` DROP COLUMN \`CHILD2_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgRoot\` DROP COLUMN \`CHILD1_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild1\` DROP COLUMN \`CHILD4_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild1\` DROP COLUMN \`CHILD3_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild1\` DROP COLUMN \`CHILD2_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild1\` DROP COLUMN \`ROOT_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild2\` DROP COLUMN \`CHILD4_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild2\` DROP COLUMN \`CHILD3_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild2\` DROP COLUMN \`CHILD1_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild2\` DROP COLUMN \`ROOT_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild3\` DROP COLUMN \`CHILD4_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild3\` DROP COLUMN \`CHILD2_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild3\` DROP COLUMN \`CHILD1_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild3\` DROP COLUMN \`ROOT_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild4\` DROP COLUMN \`CHILD3_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild4\` DROP COLUMN \`CHILD2_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild4\` DROP COLUMN \`CHILD1_CODE\``);
await queryRunner.query(`ALTER TABLE \`orgChild4\` DROP COLUMN \`ROOT_CODE\``);
}
}

View file

@ -3,6 +3,7 @@ import { CommandRecive } from "../entities/CommandRecive";
import { Command } from "../entities/Command"; import { Command } from "../entities/Command";
import { OrgRoot } from "../entities/OrgRoot"; import { OrgRoot } from "../entities/OrgRoot";
import { Profile } from "../entities/Profile"; import { Profile } from "../entities/Profile";
import { EntityManager } from "typeorm";
export interface PosNumCodeSitResult { export interface PosNumCodeSitResult {
posNumCodeSit: string; posNumCodeSit: string;
@ -17,43 +18,42 @@ export interface PosNumCodeSitResult {
* *
* @param reciveId commandRecive.Id * @param reciveId commandRecive.Id
* @param code * @param code
* @param manager operation transaction caller (all-or-nothing)
* @returns Promise<void> * @returns Promise<void>
*/ */
export async function reOrderCommandRecivesAndDelete( export async function reOrderCommandRecivesAndDelete(
reciveId: string reciveId: string,
manager?: EntityManager,
): Promise<void> { ): Promise<void> {
const commandReciveRepo = AppDataSource.getRepository(CommandRecive); const ds = manager ?? AppDataSource;
const commandRepo = AppDataSource.getRepository(Command); const commandReciveRepo = ds.getRepository(CommandRecive);
const commandRepo = ds.getRepository(Command);
// ค้นหาข้อมูลผู้ได้รับคำสั่งตาม reciveId // ค้นหาข้อมูลผู้ได้รับคำสั่งตาม reciveId
const commandRecive = await commandReciveRepo.findOne({ const commandRecive = await commandReciveRepo.findOne({
where: { id: reciveId } where: { id: reciveId },
}); });
if (commandRecive == null) if (commandRecive == null) return;
return;
const commandId = commandRecive.commandId; const commandId = commandRecive.commandId;
// ลบตาม refId // ลบตาม refId
await commandReciveRepo.delete(commandRecive.id); await commandReciveRepo.delete(commandRecive.id);
const commandReciveList = await commandReciveRepo.find({ const commandReciveList = await commandReciveRepo.find({
where: { commandId: commandId }, where: { commandId: commandId },
order: { order: "ASC" }, order: { order: "ASC" },
}); });
// ลำดับผู้ได้รับคำสั่งใหม่ // ลำดับผู้ได้รับคำสั่งใหม่
if (commandReciveList.length > 0) { if (commandReciveList.length > 0) {
await Promise.all( for (let i = 0; i < commandReciveList.length; i++) {
commandReciveList.map(async (p, i) => { commandReciveList[i].order = i + 1;
p.order = i + 1; await commandReciveRepo.save(commandReciveList[i]);
await commandReciveRepo.save(p); }
})
);
} else { } else {
// ถ้าไม่มีผู้ได้รับคำสั่งเหลือเลย ให้ยกเลิกคำสั่ง // ถ้าไม่มีผู้ได้รับคำสั่งเหลือเลย ให้ยกเลิกคำสั่ง
await commandRepo.update({ id: commandId }, { status: "CANCEL" }); await commandRepo.update({ id: commandId }, { status: "CANCEL" });
} }
} }
/** /**

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,860 @@
import { Double, EntityManager, In, Like } from "typeorm";
import { AppDataSource } from "../database/data-source";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
import Extension from "../interfaces/extension";
import CallAPI from "../interfaces/call-api";
import { setLogDataDiff } from "../interfaces/utils";
import {
CreatePosMasterHistoryEmployee,
CreatePosMasterHistoryEmployeeTemp,
} from "./PositionService";
import {
addUserRoles,
createUser,
getRoles,
getUserByUsername,
getRoleMappings,
} from "../keycloak";
import { Command } from "../entities/Command";
import { Profile } from "../entities/Profile";
import { ProfileEmployee } from "../entities/ProfileEmployee";
import { OrgRoot } from "../entities/OrgRoot";
import { OrgRevision } from "../entities/OrgRevision";
import { RoleKeycloak } from "../entities/RoleKeycloak";
import { EmployeePosMaster } from "../entities/EmployeePosMaster";
import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster";
import { EmployeePosition } from "../entities/EmployeePosition";
import { PosMaster } from "../entities/PosMaster";
import { Position } from "../entities/Position";
import { ProfileSalary } from "../entities/ProfileSalary";
import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory";
import { PosMasterAct } from "../entities/PosMasterAct";
import { ProfileActposition } from "../entities/ProfileActposition";
import { ProfileActpositionHistory } from "../entities/ProfileActpositionHistory";
import { promisify } from "util";
const redis = require("redis");
const REDIS_HOST = process.env.REDIS_HOST;
const REDIS_PORT = process.env.REDIS_PORT;
/**
* Input: refIds consumer rabbitmq build ( body.refIds endpoint /excecute)
* C-PM-21, C-PM-38, C-PM-40
*/
export interface CommandRefItem {
refId: string;
commandId?: string | null;
amount: Double | null;
amountSpecial?: Double | null;
positionSalaryAmount: Double | null;
mouthSalaryAmount: Double | null;
commandNo: string | null;
commandYear: number;
commandDateAffect?: Date | string | null;
commandDateSign?: Date | string | null;
commandCode?: string | null;
commandName?: string | null;
remark: string | null;
}
/**
* Context audit/log ( ExecuteSalaryService)
*/
export interface OrgCommandExecutionContext {
user: { sub: string; name: string };
req?: any;
}
/**
* Service "ยิงเข้าตัว" (HTTP loopback org )
*
* commandType:
* - C-PM-21 : command21/employee/report/excecute ( )
* - C-PM-38 : command38/officer/report/excecute ( next_holder )
* - C-PM-40 : command40/officer/report/excecute ()
*
* - endpoint commandXX/.../excecute 3 service (thin wrapper)
* - consumer rabbitmq handler service (Linear Flow / )
* PostData(path + "/excecute") HTTP loopback org
*
* Behavior preserve CommandController
*
* Batch semantics: all-or-nothing transaction (sequential)
* throw rollback batch propagate error ()
*
* side-effect DB transaction:
* - Keycloak operations (createUser/addUserRoles/getRoleMappings) C-PM-21 transaction
* preserve behavior Keycloak rollback DB rollback
* Keycloak
* - .NET call (C-PM-21) transaction commit .NET rollback
* - Redis cache clear (C-PM-40) transaction commit ( del cache key idempotent)
* - CreatePosMasterHistoryEmployeeTemp nested transaction ( manager)
*/
export class ExecuteOrgCommandService {
private commandRepository = AppDataSource.getRepository(Command);
private profileRepository = AppDataSource.getRepository(Profile);
private profileEmployeeRepository = AppDataSource.getRepository(ProfileEmployee);
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
private roleKeycloakRepo = AppDataSource.getRepository(RoleKeycloak);
private posMasterRepository = AppDataSource.getRepository(PosMaster);
private posMasterActRepository = AppDataSource.getRepository(PosMasterAct);
// ─────────────────────────────────────────────────────────────
// แก้ปัญหา _posNumCodeSit/_command resolution ที่ซ้ำกันในทุก endpoint
// (เดิมอยู่ใน controller — ย้ายมานี่ ทำครั้งเดียวก่อนเข้า transaction)
// ─────────────────────────────────────────────────────────────
private async resolvePosNumCodeSit(
commandId: string | null | undefined,
): Promise<{ command: Command | null; posNumCodeSit: string; posNumCodeSitAbb: string }> {
let posNumCodeSit = "";
let posNumCodeSitAbb = "";
const command = commandId
? await this.commandRepository.findOne({ where: { id: commandId } })
: null;
if (command) {
if (command?.isBangkok?.toLocaleUpperCase() == "OFFICE") {
const orgRootDeputy = await this.orgRootRepository.findOne({
where: {
isDeputy: true,
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
relations: ["orgRevision"],
});
posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร";
posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป.";
} else if (command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") {
posNumCodeSit = "กรุงเทพมหานคร";
posNumCodeSitAbb = "กทม.";
} else {
let profileAdmin = await this.profileRepository.findOne({
where: {
keycloak: command?.createdUserId.toString(),
current_holders: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
},
relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"],
});
posNumCodeSit =
profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ??
"";
posNumCodeSitAbb =
profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot
.orgRootShortName ?? "";
}
}
return { command, posNumCodeSit, posNumCodeSitAbb };
}
// ═══════════════════════════════════════════════════════════════
// C-PM-21 : command21/employee/report/excecute
// ลูกจ้างชั่วคราว → พนักงานประจำ (บรรจุ)
// ═══════════════════════════════════════════════════════════════
/**
* @returns profileEmps .NET ( consumer rabbitmq .NET )
* thin-wrapper endpoint .NET method
*/
async executeCommand21Employee(
data: CommandRefItem[],
ctx: OrgCommandExecutionContext,
options?: { callDotNet?: boolean },
): Promise<{ profileEmps: any[] }> {
const req = ctx.req;
const callDotNet = options?.callDotNet ?? true;
const commandId = data?.find((x) => x.commandId)?.commandId ?? "";
console.log(
`[ExecuteOrgCommandService] executeCommand21Employee — commandId: ${commandId}, count: ${data?.length ?? 0}`,
);
const roleKeycloak = await this.roleKeycloakRepo.findOne({
where: { name: Like("USER") },
});
const { command: _command, posNumCodeSit: _posNumCodeSit, posNumCodeSitAbb: _posNumCodeSitAbb } =
await this.resolvePosNumCodeSit(commandId);
const profileEmps: any[] = [];
await AppDataSource.transaction(async (manager) => {
for (const item of data ?? []) {
try {
await this.processOneCommand21(
item,
ctx,
manager,
roleKeycloak,
_command,
_posNumCodeSit,
_posNumCodeSitAbb,
profileEmps,
);
} catch (err) {
const reason =
err instanceof HttpError
? err.message
: err instanceof Error
? err.message
: "unexpected error";
console.error(
`[ExecuteOrgCommandService] Failed C-PM-21, commandId=${commandId}, refId=${item.refId}: ${reason}`,
err,
);
throw err; // → rollback ทั้ง transaction + propagate เป็น batch failure
}
}
});
// .NET call ทำหลัง commit (เหมือน endpoint เดิมที่เรียกหลัง Promise.all) — .NET ไม่ rollback ได้
if (callDotNet && profileEmps.length > 0) {
await new CallAPI()
.PostData(req, "/placement/appointment/employee-appoint-21/report/excecute", {
profileEmps,
})
.catch((error) => {
throw new Error(`Failed. Cannot update status. ${error?.message ?? ""}`);
});
}
console.log(
`[ExecuteOrgCommandService] Completed C-PM-21 — ${profileEmps.length} profiles sent to .NET`,
);
return { profileEmps };
}
private async processOneCommand21(
item: CommandRefItem,
ctx: OrgCommandExecutionContext,
manager: EntityManager,
roleKeycloak: RoleKeycloak | null,
_command: Command | null,
_posNumCodeSit: string,
_posNumCodeSitAbb: string,
profileEmps: any[],
): Promise<void> {
const req = ctx.req;
const profileEmployeeRepository = manager.getRepository(ProfileEmployee);
const employeePosMasterRepository = manager.getRepository(EmployeePosMaster);
const employeeTempPosMasterRepository = manager.getRepository(EmployeeTempPosMaster);
const employeePositionRepository = manager.getRepository(EmployeePosition);
const salaryRepo = manager.getRepository(ProfileSalary);
const salaryHistoryRepo = manager.getRepository(ProfileSalaryHistory);
const profile = await profileEmployeeRepository.findOne({
where: { id: item.refId },
relations: ["roleKeycloaks"],
});
if (!profile) {
throw new HttpError(HttpStatusCode.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const orgRevision = await this.orgRevisionRepository.findOne({
where: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
});
const _posMaster = await employeePosMasterRepository.findOne({
where: {
orgRevisionId: orgRevision?.id,
id: profile.posmasterIdTemp,
},
relations: {
orgRoot: true,
orgChild1: true,
orgChild2: true,
orgChild3: true,
orgChild4: true,
},
});
const orgRootRef = _posMaster?.orgRoot ?? null;
const orgChild1Ref = _posMaster?.orgChild1 ?? null;
const orgChild2Ref = _posMaster?.orgChild2 ?? null;
const orgChild3Ref = _posMaster?.orgChild3 ?? null;
const orgChild4Ref = _posMaster?.orgChild4 ?? null;
let orgShortName = "";
if (_posMaster != null) {
if (_posMaster.orgChild1Id === null) {
orgShortName = _posMaster.orgRoot?.orgRootShortName;
} else if (_posMaster.orgChild2Id === null) {
orgShortName = _posMaster.orgChild1?.orgChild1ShortName;
} else if (_posMaster.orgChild3Id === null) {
orgShortName = _posMaster.orgChild2?.orgChild2ShortName;
} else if (_posMaster.orgChild4Id === null) {
orgShortName = _posMaster.orgChild3?.orgChild3ShortName;
} else {
orgShortName = _posMaster.orgChild4?.orgChild4ShortName;
}
}
const dest_item = await salaryRepo.findOne({
where: { profileEmployeeId: item.refId },
order: { order: "DESC" },
});
const before = null;
const dataSalary = new ProfileSalary();
dataSalary.posNumCodeSit = _posNumCodeSit;
dataSalary.posNumCodeSitAbb = _posNumCodeSitAbb;
const meta = {
profileEmployeeId: profile.id,
amount: item.amount,
amountSpecial: item.amountSpecial,
positionSalaryAmount: item.positionSalaryAmount,
mouthSalaryAmount: item.mouthSalaryAmount,
position: profile.positionTemp,
positionName: profile.positionTemp,
positionType: profile.posTypeNameTemp,
positionLevel: profile.posLevelNameTemp,
order: dest_item == null ? 1 : dest_item.order + 1,
orgRoot: orgRootRef?.orgRootName ?? null,
orgChild1: orgChild1Ref?.orgChild1Name ?? null,
orgChild2: orgChild2Ref?.orgChild2Name ?? null,
orgChild3: orgChild3Ref?.orgChild3Name ?? null,
orgChild4: orgChild4Ref?.orgChild4Name ?? null,
createdUserId: ctx.user.sub,
createdFullName: ctx.user.name,
lastUpdateUserId: ctx.user.sub,
lastUpdateFullName: ctx.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
commandNo: item.commandNo,
commandYear: item.commandYear,
posNo: profile.posMasterNoTemp ?? "",
posNoAbb: orgShortName,
commandDateAffect: item.commandDateAffect,
commandDateSign: item.commandDateSign,
commandCode: item.commandCode,
commandName: item.commandName,
remark: item.remark,
};
Object.assign(dataSalary, meta);
const history = new ProfileSalaryHistory();
Object.assign(history, { ...dataSalary, id: undefined });
await salaryRepo.save(dataSalary, { data: req });
setLogDataDiff(req, { before, after: dataSalary });
history.profileSalaryId = dataSalary.id;
await salaryHistoryRepo.save(history, { data: req });
const posMaster = await employeePosMasterRepository.findOne({
where: { id: profile.posmasterIdTemp },
relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"],
});
if (posMaster == null)
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้");
const posMasterOld = await employeePosMasterRepository.findOne({
where: {
current_holderId: profile.id,
orgRevisionId: posMaster.orgRevisionId,
},
});
if (posMasterOld != null) {
posMasterOld.current_holderId = null;
posMasterOld.lastUpdatedAt = new Date();
}
const positionOld = await employeePositionRepository.findOne({
where: {
posMasterId: posMasterOld?.id,
positionIsSelected: true,
},
});
if (positionOld != null) {
positionOld.positionIsSelected = false;
await employeePositionRepository.save(positionOld);
}
const checkPosition = await employeePositionRepository.find({
where: {
posMasterId: profile.posmasterIdTemp,
positionIsSelected: true,
},
});
if (checkPosition.length > 0) {
const clearPosition = checkPosition.map((positions) => ({
...positions,
positionIsSelected: false,
}));
await employeePositionRepository.save(clearPosition);
}
posMaster.current_holderId = profile.id;
posMaster.lastUpdatedAt = new Date();
posMaster.next_holderId = null;
if (posMasterOld != null) {
await employeePosMasterRepository.save(posMasterOld);
await CreatePosMasterHistoryEmployee(posMasterOld.id, req, undefined, manager);
}
await employeePosMasterRepository.save(posMaster);
await CreatePosMasterHistoryEmployee(posMaster.id, req, undefined, manager);
const clsTempPosmaster = await employeeTempPosMasterRepository.find({
where: {
current_holderId: profile.id,
orgRevisionId: posMaster.orgRevisionId,
},
});
if (clsTempPosmaster.length > 0) {
const clearTempPosmaster = clsTempPosmaster.map((posMasterTemp) => ({
...posMasterTemp,
current_holderId: null,
next_holderId: null,
}));
await employeeTempPosMasterRepository.save(clearTempPosmaster);
const checkTempPosition = await employeePositionRepository.find({
where: {
posMasterTempId: In(clearTempPosmaster.map((x) => x.id)),
positionIsSelected: true,
},
});
if (checkTempPosition.length > 0) {
const clearTempPosition = checkTempPosition.map((positions) => ({
...positions,
positionIsSelected: false,
}));
await employeePositionRepository.save(clearTempPosition);
}
await Promise.all(
clsTempPosmaster.map(
async (posMasterTemp) =>
await CreatePosMasterHistoryEmployeeTemp(posMasterTemp.id, req),
),
);
}
const positionNew = await employeePositionRepository.findOne({
where: {
id: profile.positionIdTemp,
posMasterId: profile.posmasterIdTemp,
},
});
if (positionNew != null) {
// Create Keycloak
const checkUser = await getUserByUsername(profile.citizenId);
if (checkUser.length == 0) {
let password = profile.citizenId;
if (profile.birthDate != null) {
const _date = new Date(profile.birthDate.toDateString())
.getDate()
.toString()
.padStart(2, "0");
const _month = (new Date(profile.birthDate.toDateString()).getMonth() + 1)
.toString()
.padStart(2, "0");
const _year = new Date(profile.birthDate.toDateString()).getFullYear() + 543;
password = `${_date}${_month}${_year}`;
}
// กรอง "." ออกจาก firstName ก่อนส่งไป keycloak
const sanitizedFirstName = profile.firstName?.replace(/\./g, "") ?? "";
const userKeycloakId = await createUser(profile.citizenId, password, {
firstName: sanitizedFirstName,
lastName: profile.lastName,
});
const list = await getRoles();
if (!Array.isArray(list))
throw new Error("Failed. Cannot get role(s) data from the server.");
const result = await addUserRoles(
userKeycloakId,
list
.filter((v) => v.name === "USER")
.map((x) => ({
id: x.id,
name: x.name,
})),
);
profile.keycloak =
userKeycloakId && typeof userKeycloakId == "string" ? userKeycloakId : "";
profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : [];
// End Create Keycloak
} else {
const rolesData = await getRoleMappings(checkUser[0].id);
if (rolesData) {
const _roleKeycloak = await this.roleKeycloakRepo.find({
where: { name: In(rolesData.map((x: any) => x.name)) },
});
profile.roleKeycloaks =
_roleKeycloak && _roleKeycloak.length > 0 ? _roleKeycloak : [];
}
profile.keycloak = checkUser[0].id;
}
positionNew.positionIsSelected = true;
profile.posLevelId = positionNew.posLevelId;
profile.posTypeId = positionNew.posTypeId;
profile.position = positionNew.positionName;
profile.employeeOc = posMaster?.orgRoot?.orgRootName ?? null;
profile.positionEmployeePositionId = positionNew.positionName;
profile.statusTemp = "DONE";
profile.employeeClass = "PERM";
const _null: any = null;
profile.employeeWage = item.amount == null ? _null : item.amount.toString();
profile.dateStart = _command ? _command.commandExcecuteDate : new Date();
profile.dateAppoint = _command ? _command.commandExcecuteDate : new Date();
profile.amount = item.amount == null ? _null : item.amount;
profile.amountSpecial = item.amountSpecial == null ? _null : item.amountSpecial;
profileEmps.push({
profileId: profile.id,
prefix: profile.prefix,
firstName: profile.firstName,
lastName: profile.lastName,
citizenId: profile.citizenId,
root: posMaster.orgRoot.orgRootName,
rootId: posMaster.orgRootId,
rootShortName: posMaster.orgRoot.orgRootShortName,
rootDnaId: posMaster.orgRoot?.ancestorDNA ?? _null,
child1DnaId: posMaster.orgChild1?.ancestorDNA ?? _null,
child2DnaId: posMaster.orgChild2?.ancestorDNA ?? _null,
child3DnaId: posMaster.orgChild3?.ancestorDNA ?? _null,
child4DnaId: posMaster.orgChild4?.ancestorDNA ?? _null,
});
await profileEmployeeRepository.save(profile);
await employeePositionRepository.save(positionNew);
await CreatePosMasterHistoryEmployee(posMaster.id, req, undefined, manager);
//ลบออกคนออกจากโครงสร้างลูกจ้างชั่วคราว
const posMasterTemp = await employeeTempPosMasterRepository.findOne({
where: {
orgRevisionId: orgRevision?.id,
current_holderId: profile.id,
},
});
if (posMasterTemp) {
await employeeTempPosMasterRepository.update(posMasterTemp.id, {
current_holderId: _null,
});
await CreatePosMasterHistoryEmployeeTemp(posMasterTemp.id, req);
}
}
}
// ═══════════════════════════════════════════════════════════════
// C-PM-38 : command38/officer/report/excecute
// เงินเดือน next_holder ของข้าราชการ
// ═══════════════════════════════════════════════════════════════
async executeCommand38Officer(
data: CommandRefItem[],
ctx: OrgCommandExecutionContext,
): Promise<void> {
const req = ctx.req;
const commandId = data?.find((x) => x.commandId)?.commandId ?? "";
console.log(
`[ExecuteOrgCommandService] executeCommand38Officer — commandId: ${commandId}, count: ${data?.length ?? 0}`,
);
const { posNumCodeSit: _posNumCodeSit, posNumCodeSitAbb: _posNumCodeSitAbb } =
await this.resolvePosNumCodeSit(commandId);
await AppDataSource.transaction(async (manager) => {
for (const item of data ?? []) {
try {
await this.processOneCommand38(
item,
ctx,
manager,
_posNumCodeSit,
_posNumCodeSitAbb,
);
} catch (err) {
const reason =
err instanceof HttpError
? err.message
: err instanceof Error
? err.message
: "unexpected error";
console.error(
`[ExecuteOrgCommandService] Failed C-PM-38, commandId=${commandId}, refId=${item.refId}: ${reason}`,
err,
);
throw err; // → rollback ทั้ง transaction + propagate เป็น batch failure
}
}
});
console.log(`[ExecuteOrgCommandService] Completed C-PM-38 — ${data?.length ?? 0} items`);
}
private async processOneCommand38(
item: CommandRefItem,
ctx: OrgCommandExecutionContext,
manager: EntityManager,
_posNumCodeSit: string,
_posNumCodeSitAbb: string,
): Promise<void> {
const req = ctx.req;
const posMasterRepository = manager.getRepository(PosMaster);
const profileRepository = manager.getRepository(Profile);
const positionRepository = manager.getRepository(Position);
const salaryRepo = manager.getRepository(ProfileSalary);
const salaryHistoryRepo = manager.getRepository(ProfileSalaryHistory);
const posMaster = await posMasterRepository.findOne({
where: { id: item.refId },
relations: [
"orgRoot",
"orgChild1",
"orgChild2",
"orgChild3",
"orgChild4",
"current_holder",
"current_holder.posLevel",
"current_holder.posType",
],
});
if (!posMaster) {
throw new HttpError(HttpStatusCode.BAD_REQUEST, "ไม่พบตำแหน่งดังกล่าว");
}
if (posMaster.next_holderId != null) {
const orgRootRef = posMaster?.orgRoot ?? null;
const orgChild1Ref = posMaster?.orgChild1 ?? null;
const orgChild2Ref = posMaster?.orgChild2 ?? null;
const orgChild3Ref = posMaster?.orgChild3 ?? null;
const orgChild4Ref = posMaster?.orgChild4 ?? null;
const shortName =
posMaster != null && posMaster.orgChild4 != null
? `${posMaster.orgChild4.orgChild4ShortName}`
: posMaster != null && posMaster.orgChild3 != null
? `${posMaster.orgChild3.orgChild3ShortName}`
: posMaster != null && posMaster.orgChild2 != null
? `${posMaster.orgChild2.orgChild2ShortName}`
: posMaster != null && posMaster.orgChild1 != null
? `${posMaster.orgChild1.orgChild1ShortName}`
: posMaster != null && posMaster?.orgRoot != null
? `${posMaster.orgRoot.orgRootShortName}`
: null;
const profile = await profileRepository.findOne({
where: { id: posMaster.next_holderId },
});
const position = await positionRepository.findOne({
where: {
posMasterId: posMaster.id,
positionIsSelected: true,
},
relations: ["posType", "posLevel"],
});
const dest_item = await salaryRepo.findOne({
where: { profileId: profile?.id },
order: { order: "DESC" },
});
const before = null;
const dataSalary = new ProfileSalary();
dataSalary.posNumCodeSit = _posNumCodeSit;
dataSalary.posNumCodeSitAbb = _posNumCodeSitAbb;
const meta = {
profileId: profile?.id,
date: new Date(),
amount: item.amount,
commandId: item.commandId,
positionSalaryAmount: item.positionSalaryAmount,
mouthSalaryAmount: item.mouthSalaryAmount,
position: position?.positionName ?? null,
positionType: position?.posType?.posTypeName ?? null,
positionLevel: position?.posLevel?.posLevelName ?? null,
order: dest_item == null ? 1 : dest_item.order + 1,
orgRoot: orgRootRef?.orgRootName ?? null,
orgChild1: orgChild1Ref?.orgChild1Name ?? null,
orgChild2: orgChild2Ref?.orgChild2Name ?? null,
orgChild3: orgChild3Ref?.orgChild3Name ?? null,
orgChild4: orgChild4Ref?.orgChild4Name ?? null,
createdUserId: ctx.user.sub,
createdFullName: ctx.user.name,
lastUpdateUserId: ctx.user.sub,
lastUpdateFullName: ctx.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
commandNo: item.commandNo,
commandYear: item.commandYear,
posNo: posMaster.posMasterNo,
posNoAbb: shortName,
commandDateAffect: item.commandDateAffect,
commandDateSign: item.commandDateSign,
commandCode: item.commandCode,
commandName: item.commandName,
remark: item.remark,
};
Object.assign(dataSalary, meta);
const history = new ProfileSalaryHistory();
Object.assign(history, { ...dataSalary, id: undefined });
await salaryRepo.save(dataSalary, { data: req });
setLogDataDiff(req, { before, after: dataSalary });
history.profileSalaryId = dataSalary.id;
await salaryHistoryRepo.save(history, { data: req });
}
}
// ═══════════════════════════════════════════════════════════════
// C-PM-40 : command40/officer/report/excecute
// รักษาการ (ProfileActposition)
// ═══════════════════════════════════════════════════════════════
async executeCommand40Officer(
data: CommandRefItem[],
ctx: OrgCommandExecutionContext,
): Promise<void> {
const commandId = data?.find((x) => x.commandId)?.commandId ?? "";
console.log(
`[ExecuteOrgCommandService] executeCommand40Officer — commandId: ${commandId}, count: ${data?.length ?? 0}`,
);
// 3. ตรวจสอบว่ามี data[0] หรือไม่
const firstRef = data[0];
if (!firstRef) {
throw new HttpError(HttpStatusCode.BAD_REQUEST, "ไม่พบข้อมูล refIds");
}
const profileIdsToClearCache = new Set<string>();
await AppDataSource.transaction(async (manager) => {
// 1. Bulk update status
await manager.getRepository(PosMasterAct).update(
{ id: In(data.map((x) => x.refId)) },
{ statusReport: "DONE" },
);
// 2. ดึงข้อมูลครบทุก relation ที่จำเป็น
const posMasters = await manager.getRepository(PosMasterAct).find({
where: { id: In(data.map((x) => x.refId)) },
relations: [
"posMasterChild",
"posMasterChild.current_holder",
"posMaster",
"posMaster.current_holder",
"posMaster.positions",
"posMaster.orgRoot",
"posMaster.orgChild1",
"posMaster.orgChild2",
"posMaster.orgChild3",
"posMaster.orgChild4",
],
});
for (const item of posMasters) {
try {
// 4. ตรวจสอบข้อมูลที่จำเป็นทั้งหมด
if (!item.posMasterChild?.current_holderId || !item.posMaster) {
console.warn(`ข้ามรายการ ${item.id}: ข้อมูลไม่ครบ`);
continue;
}
if (item.posMasterChild.current_holderId) {
profileIdsToClearCache.add(item.posMasterChild.current_holderId);
}
// 5. สร้าง orgShortName แบบปลอดภัย
const orgShortName =
[
item.posMaster?.orgChild4?.orgChild4ShortName,
item.posMaster?.orgChild3?.orgChild3ShortName,
item.posMaster?.orgChild2?.orgChild2ShortName,
item.posMaster?.orgChild1?.orgChild1ShortName,
item.posMaster?.orgRoot?.orgRootShortName,
].find(Boolean) ?? "";
// 6. หา position ที่ถูกเลือกแบบปลอดภัย
const selectedPosition = item.posMaster?.positions;
const positionName =
selectedPosition
?.map((pos) => pos.positionName)
.filter(Boolean)
.join(", ") ?? "-";
// 7. สร้าง metaAct แบบปลอดภัย
const metaAct = {
profileId: item.posMasterChild.current_holderId,
dateStart: firstRef.commandDateAffect ?? null,
dateEnd: null,
position: positionName,
status: true,
commandId: firstRef.commandId ?? null,
createdUserId: ctx.user.sub,
createdFullName: ctx.user.name,
lastUpdateUserId: ctx.user.sub,
lastUpdateFullName: ctx.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
commandNo: firstRef.commandNo ?? null,
refCommandNo: `${firstRef.commandNo ?? ""}/${firstRef.commandYear ? Extension.ToThaiYear(firstRef.commandYear) : ""}`,
commandYear: firstRef.commandYear ? Extension.ToThaiYear(firstRef.commandYear) : null,
posNo:
orgShortName && item.posMaster?.posMasterNo
? `${orgShortName} ${item.posMaster.posMasterNo}`
: item.posMaster?.posMasterNo ?? "-",
posNoAbb: orgShortName,
commandDateAffect: firstRef.commandDateAffect ?? null,
commandDateSign: firstRef.commandDateSign ?? null,
commandCode: firstRef.commandCode ?? null,
commandName: firstRef.commandName ?? null,
remark: firstRef.remark ?? null,
};
// 8. ปิดสถานะรักษาการ
const actpositionRepository = manager.getRepository(ProfileActposition);
const actpositionHistoryRepository = manager.getRepository(ProfileActpositionHistory);
const existingActPositions = await actpositionRepository.find({
where: {
profileId: item.posMasterChild.current_holderId,
status: true,
isDeleted: false,
},
});
if (existingActPositions.length > 0) {
const updatedActPositions = existingActPositions.map((_data) => ({
..._data,
status: false,
dateEnd: new Date(),
}));
await actpositionRepository.save(updatedActPositions);
}
// 9. บันทึกข้อมูลใหม่
const dataAct = new ProfileActposition();
Object.assign(dataAct, metaAct);
const historyAct = new ProfileActpositionHistory();
Object.assign(historyAct, { ...dataAct, id: undefined });
await actpositionRepository.save(dataAct);
historyAct.profileActpositionId = dataAct.id;
await actpositionHistoryRepository.save(historyAct);
} catch (error) {
console.error(`Error processing item ${item.id}:`, error);
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
`เกิดข้อผิดพลาดในการประมวลผลรายการ ${item.id}`,
);
}
}
});
// Redis cache clear ทำหลัง commit (del cache key — idempotent)
if (profileIdsToClearCache.size > 0) {
await Promise.all(
Array.from(profileIdsToClearCache).map(async (profileId) => {
const redisClient = await redis.createClient({
host: REDIS_HOST,
port: REDIS_PORT,
});
const delAsync = promisify(redisClient.del).bind(redisClient);
await delAsync("role_" + profileId);
await delAsync("menu_" + profileId);
redisClient.quit();
}),
);
}
console.log(`[ExecuteOrgCommandService] Completed C-PM-40 — ${data?.length ?? 0} items`);
}
}

View file

@ -0,0 +1,499 @@
import { Double, EntityManager } from "typeorm";
import { AppDataSource } from "../database/data-source";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
import { Profile } from "../entities/Profile";
import { ProfileSalary } from "../entities/ProfileSalary";
import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory";
import { OrgRoot } from "../entities/OrgRoot";
import { PosMaster } from "../entities/PosMaster";
import { Position } from "../entities/Position";
import { Command } from "../entities/Command";
import { getOrgFullName, getPosMasterNo } from "../utils/org-formatting";
import { logPositionIsSelectedChange, setLogDataDiff } from "../interfaces/utils";
import { CreatePosMasterHistoryOfficer } from "./PositionService";
/**
* Input: ข้อมูล 1 endpoint excexute/salary-current
* (C-PM-03, 04, 05, 06, 07, 39, 47 + salary )
*/
export interface SalaryCurrentItem {
profileId: string;
amount?: Double | null;
amountSpecial?: Double | null;
positionSalaryAmount?: Double | null;
mouthSalaryAmount?: Double | null;
positionExecutive: string | null;
positionExecutiveField?: string | null;
positionArea?: string | null;
positionType: string | null;
positionLevel: string | null;
positionTypeId?: string | null;
positionLevelId?: string | null;
posmasterId: string;
positionId: string;
posExecutiveId?: string | null;
positionField?: string | null;
commandId?: string | null;
orgRoot?: string | null;
orgChild1?: string | null;
orgChild2?: string | null;
orgChild3?: string | null;
orgChild4?: string | null;
commandNo: string | null;
commandYear: number | null;
posNo: string | null;
posNoAbb: string | null;
commandDateAffect?: Date | string | null;
commandDateSign?: Date | string | null;
positionName: string | null;
commandCode?: string | null;
commandName?: string | null;
remark: string | null;
}
/**
* Context audit/log
*/
export interface SalaryCurrentExecutionContext {
user: { sub: string; name: string };
req?: any;
}
/**
* Service ProfileSalary + ()
*
* commandType: C-PM-03, 04, 05, 06, 07, 39, 47
*
* - endpoint /org/command/excexute/salary-current service (thin wrapper)
* - consumer rabbitmq handler service (Linear Flow)
*
* Behavior preserve CommandController.newSalaryAndUpdateCurrent
*
* Batch semantics: all-or-nothing transaction (sequential)
* throw rollback batch propagate error ()
* return result success count
*/
export class ExecuteSalaryCurrentService {
private commandRepository = AppDataSource.getRepository(Command);
private profileRepository = AppDataSource.getRepository(Profile);
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
/**
* ProfileSalary + batch
*
* @returns success/failure
*/
async executeSalaryCurrent(
data: SalaryCurrentItem[],
ctx: SalaryCurrentExecutionContext,
): Promise<void> {
const commandId = data?.find((x) => x.commandId)?.commandId ?? "unknown";
const commandCode = data?.find((x) => x.commandCode)?.commandCode ?? "unknown";
console.log(
`[ExecuteSalaryCurrentService] Starting executeSalaryCurrent — commandCode: ${commandCode}, commandId: ${commandId}`,
);
console.log(`[ExecuteSalaryCurrentService] Request body count: ${data?.length ?? 0}`);
// ─────────────────────────────────────────────────────────────
// Normalize date fields (ผ่าน handler จะได้ string → ต้องแปลงเป็น Date)
// ─────────────────────────────────────────────────────────────
const toDate = (v: any): Date | null => {
if (v == null || v === "") return null;
if (v instanceof Date) return isNaN(v.getTime()) ? null : v;
const d = new Date(v);
return isNaN(d.getTime()) ? null : d;
};
for (const item of data ?? []) {
const it = item as any;
it.commandDateAffect = toDate(it.commandDateAffect);
it.commandDateSign = toDate(it.commandDateSign);
}
let _posNumCodeSit: string = "";
let _posNumCodeSitAbb: string = "";
const _command = await this.commandRepository.findOne({
where: { id: data.find((x) => x.commandId)?.commandId ?? "" },
});
if (_command) {
if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") {
const orgRootDeputy = await this.orgRootRepository.findOne({
where: {
isDeputy: true,
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
relations: ["orgRevision"],
});
_posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร";
_posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป.";
} else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") {
_posNumCodeSit = "กรุงเทพมหานคร";
_posNumCodeSitAbb = "กทม.";
} else {
let _profileAdmin = await this.profileRepository.findOne({
where: {
keycloak: _command?.createdUserId.toString(),
current_holders: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
},
relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"],
});
_posNumCodeSit =
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ??
"";
_posNumCodeSitAbb =
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot
.orgRootShortName ?? "";
}
}
// ─────────────────────────────────────────────────────────────
// Single transaction ครอบทั้ง batch (all-or-nothing)
// ทุกคนใช้ manager ตัวเดียวกัน — คนใด throw จะ rollback ทั้ง batch
// และ propagate error ออกไป (ล้มเหลวทั้งหมด) โดย log error ของคนที่ทำให้ fail ก่อน rethrow
// ─────────────────────────────────────────────────────────────
await AppDataSource.transaction(async (manager) => {
for (const item of data ?? []) {
try {
await this.processOne(item, ctx, manager, _posNumCodeSit, _posNumCodeSitAbb);
} catch (err) {
const reason =
err instanceof HttpError
? err.message
: err instanceof Error
? err.message
: "unexpected error";
console.error(
`[ExecuteSalaryCurrentService] Failed — commandCode: ${commandCode}, commandId: ${commandId}, profileId: ${item.profileId}, reason: ${reason}`,
err,
);
throw err; // → rollback ทั้ง transaction + propagate เป็น batch failure
}
}
});
}
/**
* 1 transaction (manager)
* save manager.getRepository(...) transaction
* throw rollback ( partial commit)
*/
private async processOne(
item: SalaryCurrentItem,
ctx: SalaryCurrentExecutionContext,
manager: EntityManager,
_posNumCodeSit: string,
_posNumCodeSitAbb: string,
): Promise<void> {
const req = ctx.req;
const profileRepository = manager.getRepository(Profile);
const salaryRepo = manager.getRepository(ProfileSalary);
const salaryHistoryRepo = manager.getRepository(ProfileSalaryHistory);
const posMasterRepository = manager.getRepository(PosMaster);
const positionRepository = manager.getRepository(Position);
const profile: any = await profileRepository.findOneBy({ id: item.profileId });
if (!profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลทะเบียนประวัตินี้ profileId: ${item.profileId}`);
}
let _null: any = null;
const dest_item = await salaryRepo.findOne({
where: { profileId: item.profileId },
order: { order: "DESC" },
});
const before = null;
const dataSalary = new ProfileSalary();
const meta = {
order: dest_item == null ? 1 : dest_item.order + 1,
createdUserId: ctx.user.sub,
createdFullName: ctx.user.name,
lastUpdateUserId: ctx.user.sub,
lastUpdateFullName: ctx.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
};
dataSalary.posNumCodeSit = _posNumCodeSit;
dataSalary.posNumCodeSitAbb = _posNumCodeSitAbb;
Object.assign(dataSalary, { ...item, ...meta });
const history = new ProfileSalaryHistory();
Object.assign(history, { ...dataSalary, id: undefined });
await salaryRepo.save(dataSalary, { data: req });
setLogDataDiff(req, { before, after: dataSalary });
history.commandId = item.commandId ?? _null;
history.profileSalaryId = dataSalary.id;
await salaryHistoryRepo.save(history, { data: req });
// STEP 1: หา posMaster ที่จะใช้งานตาม id ที่ส่งมา
console.log(
`[ExecuteSalaryCurrentService] STEP 1: Finding posMaster — posmasterId: ${item.posmasterId}, profileId: ${item.profileId}`,
);
let posMaster = await posMasterRepository.findOne({
where: { id: item.posmasterId },
relations: {
orgRevision: true,
orgRoot: true,
orgChild1: true,
orgChild2: true,
orgChild3: true,
orgChild4: true,
},
});
console.log(
`[ExecuteSalaryCurrentService] STEP 1: posMaster found: ${!!posMaster}, ancestorDNA: ${posMaster?.ancestorDNA ?? "null"}, orgRevisionId: ${posMaster?.orgRevisionId ?? "null"}`,
);
// เช็คว่า posMaster ที่หามาอยู่ในโครงสร้างปัจจุบันหรือไม่
const isCurrent =
posMaster?.orgRevision?.orgRevisionIsCurrent === true &&
posMaster?.orgRevision?.orgRevisionIsDraft === false;
console.log(`[ExecuteSalaryCurrentService] STEP 1: isCurrent: ${isCurrent}`);
// ถ้าไม่อยู่ในโครงสร้างปัจจุบัน ให้หาตัวใหม่จาก ancestorDNA
if (!isCurrent && posMaster?.ancestorDNA) {
console.log(
`[ExecuteSalaryCurrentService] STEP 1: Not current — re-resolving via ancestorDNA: ${posMaster.ancestorDNA}`,
);
posMaster = await posMasterRepository.findOne({
where: {
ancestorDNA: posMaster.ancestorDNA,
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
relations: {
orgRevision: true,
orgRoot: true,
orgChild1: true,
orgChild2: true,
orgChild3: true,
orgChild4: true,
},
});
console.log(
`[ExecuteSalaryCurrentService] STEP 1: ancestorDNA re-resolve — found: ${!!posMaster}`,
);
}
if (posMaster == null) {
console.error(
`[ExecuteSalaryCurrentService] STEP 1: PosMaster not found — posmasterId: ${item.posmasterId}`,
);
throw new HttpError(HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลตำแหน่งนี้ posMasterId: ${item.posmasterId}`);
}
const posMasterOld = await posMasterRepository.findOne({
where: {
current_holderId: item.profileId,
orgRevisionId: posMaster.orgRevisionId,
},
});
if (posMasterOld != null) {
posMasterOld.current_holderId = null;
posMasterOld.lastUpdatedAt = new Date();
}
const positionOld = await positionRepository.findOne({
where: {
posMasterId: posMasterOld?.id,
positionIsSelected: true,
},
});
if (positionOld != null) {
logPositionIsSelectedChange(positionOld.id, positionOld.positionIsSelected, false, {
posMasterId: posMasterOld?.id,
userId: ctx.user.sub,
endpoint: "updateMaster",
action: "command_change_reset_old_position",
});
positionOld.positionIsSelected = false;
await positionRepository.save(positionOld);
}
const checkPosition = await positionRepository.find({
where: {
posMasterId: posMaster!.id, // ใช้ posMaster ตัวใหม่ (ที่อาจจะเปลี่ยนจาก ancestorDNA)
positionIsSelected: true,
},
});
if (checkPosition.length > 0) {
console.log(
`[positionIsSelected-DEBUG] Command change: clearing ${checkPosition.length} positions (posMasterId: ${posMaster!.id}, userId: ${ctx.user.sub}, endpoint: updateMaster)`,
);
const clearPosition = checkPosition.map((positions) => {
logPositionIsSelectedChange(positions.id, positions.positionIsSelected, false, {
posMasterId: posMaster!.id,
userId: ctx.user.sub,
endpoint: "updateMaster",
action: "command_change_clear_positions",
});
return {
...positions,
positionIsSelected: false,
};
});
await positionRepository.save(clearPosition);
}
posMaster.current_holderId = item.profileId;
posMaster.lastUpdatedAt = new Date();
// posMaster.conditionReason = _null;
// posMaster.isCondition = false;
if (posMasterOld != null) {
await posMasterRepository.save(posMasterOld);
// ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน
console.log(
`[ExecuteSalaryCurrentService] Creating PosMasterHistory — posMasterId: ${posMasterOld.id}, profileId: ${item.profileId} (old)`,
);
await CreatePosMasterHistoryOfficer(posMasterOld.id, req, null, null, manager);
}
await posMasterRepository.save(posMaster);
// STEP 2: กำหนด position ใหม่
// Match position ตามลำดับ priority:
// Condition 1: match จาก positionId
// Condition 2: match 7 ฟิลด์ (positionName, posTypeId, posLevelId, positionField, positionArea, positionExecutiveField, posExecutiveId)
// Condition 3: match 3 ฟิลด์ (positionName, posTypeId, posLevelId)
// Fallback: เลือก position แรกใน posMaster
let positionNew: Position | null = null;
// Resolve ID: ใช้ positionTypeId/positionLevelId ก่อน ถ้าไม่มี fallback เป็น positionType/positionLevel
const posTypeId = item.positionTypeId || item.positionType;
const posLevelId = item.positionLevelId || item.positionLevel;
console.log(
`[ExecuteSalaryCurrentService] STEP 2: Resolving position — posMasterId: ${posMaster.id}, positionId: ${item.positionId ?? "null"}, positionName: ${item.positionName ?? "null"}, posTypeId: ${posTypeId ?? "null"}, posLevelId: ${posLevelId ?? "null"}`,
);
// ═══════════════════════════════════════════════════════════
// CONDITION 1: เช็คจาก positionId ตรง
// ═══════════════════════════════════════════════════════════
if (item.positionId) {
const positionById = await positionRepository.findOne({
where: {
id: item.positionId,
posMasterId: posMaster.id, // ต้องอยู่ใน posMaster ที่ถูกต้อง
},
relations: ["posExecutive"],
});
console.log(
`[ExecuteSalaryCurrentService] STEP 2 / Condition 1: match: ${!!positionById}`,
);
if (positionById) {
positionNew = positionById;
}
}
// ═══════════════════════════════════════════════════════════
// CONDITION 2: Match 7 ฟิลด์ (ถ้า Condition 1 ไม่ match)
// ═══════════════════════════════════════════════════════════
if (!positionNew && item.positionName && posTypeId && posLevelId) {
// สร้าง where clause แบบ dynamic - ใส่เฉพาะฟิลด์ที่มีค่า
const whereCondition: any = {
posMasterId: posMaster.id,
positionName: item.positionName,
posTypeId: posTypeId,
posLevelId: posLevelId,
};
// เพิ่มเฉพาะฟิลด์ที่มีค่า (ไม่ใช่ null, undefined, หรือ string ว่าง)
if (item.positionField) {
whereCondition.positionField = item.positionField;
}
if (item.posExecutiveId) {
whereCondition.posExecutiveId = item.posExecutiveId;
}
if (item.positionExecutiveField) {
whereCondition.positionExecutiveField = item.positionExecutiveField;
}
if (item.positionArea) {
whereCondition.positionArea = item.positionArea;
}
const positionBy7Fields = await positionRepository.findOne({
where: whereCondition,
relations: ["posExecutive"],
order: { orderNo: "ASC" },
});
console.log(
`[ExecuteSalaryCurrentService] STEP 2 / Condition 2: match: ${!!positionBy7Fields}`,
whereCondition,
);
if (positionBy7Fields) {
positionNew = positionBy7Fields;
}
}
// ═══════════════════════════════════════════════════════════
// CONDITION 3: Match 3 ฟิลด์ (ถ้า Condition 2 ไม่ match)
// ═══════════════════════════════════════════════════════════
if (!positionNew && item.positionName && posTypeId && posLevelId) {
const positionBy3Fields = await positionRepository.findOne({
where: {
posMasterId: posMaster.id,
positionName: item.positionName,
posTypeId: posTypeId,
posLevelId: posLevelId,
},
relations: ["posExecutive"],
order: { orderNo: "ASC" },
});
console.log(
`[ExecuteSalaryCurrentService] STEP 2 / Condition 3: match: ${!!positionBy3Fields}`,
);
if (positionBy3Fields) {
positionNew = positionBy3Fields;
}
}
console.log(
`[ExecuteSalaryCurrentService] STEP 2: Resolved positionNew: ${positionNew ? positionNew.id : "null (no match — profile position not updated)"}`,
);
// ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ
if (positionNew != null) {
positionNew.positionIsSelected = true;
// อัพเดท org และ posMasterNo ตลอดไม่ต้องดัก isSit
profile.posMasterNo = getPosMasterNo(posMaster);
profile.org = getOrgFullName(posMaster);
if (!posMaster.isSit) {
profile.posLevelId = positionNew.posLevelId;
profile.posTypeId = positionNew.posTypeId;
profile.position = positionNew.positionName;
profile.positionField = positionNew.positionField ?? null;
profile.posExecutive = positionNew.posExecutive?.posExecutiveName ?? null;
profile.positionArea = positionNew.positionArea ?? null;
profile.positionExecutiveField = positionNew.positionExecutiveField ?? null;
}
profile.amount = item.amount ?? null;
profile.amountSpecial = item.amountSpecial ?? null;
await profileRepository.save(profile);
await positionRepository.save(positionNew);
}
// ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน
console.log(
`[ExecuteSalaryCurrentService] Creating PosMasterHistory — posMasterId: ${posMaster.id}, profileId: ${item.profileId}`,
);
await CreatePosMasterHistoryOfficer(posMaster.id, req, null, null, manager);
console.log(
`[ExecuteSalaryCurrentService] Completed processOne — profileId: ${item.profileId}, posMasterId: ${posMaster.id}`,
);
}
}

View file

@ -0,0 +1,316 @@
import { Double, EntityManager } from "typeorm";
import { AppDataSource } from "../database/data-source";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
import HttpStatus from "../interfaces/http-status";
import { Profile } from "../entities/Profile";
import { ProfileEmployee } from "../entities/ProfileEmployee";
import { ProfileSalary } from "../entities/ProfileSalary";
import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory";
import { OrgRoot } from "../entities/OrgRoot";
import { EmployeePosMaster } from "../entities/EmployeePosMaster";
import { EmployeePosition } from "../entities/EmployeePosition";
import { Command } from "../entities/Command";
import { setLogDataDiff } from "../interfaces/utils";
import { CreatePosMasterHistoryEmployee } from "./PositionService";
/**
* Input: ข้อมูล 1 endpoint excexute/salary-employee-current
* (C-PM-22, 24 + salary )
*/
export interface SalaryEmployeeCurrentItem {
profileId: string;
amount?: Double | null;
amountSpecial?: Double | null;
positionSalaryAmount?: Double | null;
mouthSalaryAmount?: Double | null;
positionType: string | null;
positionLevel: string | null;
posmasterId: string;
positionId: string;
commandId?: string | null;
orgRoot?: string | null;
orgChild1?: string | null;
orgChild2?: string | null;
orgChild3?: string | null;
orgChild4?: string | null;
commandNo: string | null;
commandYear: number | null;
posNo: string | null;
posNoAbb: string | null;
commandDateAffect?: Date | string | null;
commandDateSign?: Date | string | null;
positionName: string | null;
commandCode?: string | null;
commandName?: string | null;
remark: string | null;
}
/**
* Context audit/log
*/
export interface SalaryEmployeeCurrentExecutionContext {
user: { sub: string; name: string };
req?: any;
}
/**
* Service ProfileSalary + ()
*
* commandType: C-PM-22, 24
*
* - endpoint /org/command/excexute/salary-employee-current service (thin wrapper)
* - consumer rabbitmq handler service (Linear Flow)
*
* Behavior preserve CommandController.newSalaryEmployeeAndUpdateCurrent
*
* Batch semantics: all-or-nothing transaction (sequential)
* throw rollback batch propagate error ()
* return result success count
*/
export class ExecuteSalaryEmployeeCurrentService {
private commandRepository = AppDataSource.getRepository(Command);
private profileRepository = AppDataSource.getRepository(Profile);
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
/**
* ProfileSalary + batch
*/
async executeSalaryEmployeeCurrent(
data: SalaryEmployeeCurrentItem[],
ctx: SalaryEmployeeCurrentExecutionContext,
): Promise<void> {
const commandId = data?.find((x) => x.commandId)?.commandId ?? "unknown";
const commandCode = data?.find((x) => x.commandCode)?.commandCode ?? "unknown";
console.log(
`[ExecuteSalaryEmployeeCurrentService] Starting executeSalaryEmployeeCurrent — commandCode: ${commandCode}, commandId: ${commandId}`,
);
console.log(`[ExecuteSalaryEmployeeCurrentService] Request body count: ${data?.length ?? 0}`);
// ─────────────────────────────────────────────────────────────
// Normalize date fields (ผ่าน handler จะได้ string → ต้องแปลงเป็น Date)
// ─────────────────────────────────────────────────────────────
const toDate = (v: any): Date | null => {
if (v == null || v === "") return null;
if (v instanceof Date) return isNaN(v.getTime()) ? null : v;
const d = new Date(v);
return isNaN(d.getTime()) ? null : d;
};
for (const item of data ?? []) {
const it = item as any;
it.commandDateAffect = toDate(it.commandDateAffect);
it.commandDateSign = toDate(it.commandDateSign);
}
let _posNumCodeSit: string = "";
let _posNumCodeSitAbb: string = "";
const _command = await this.commandRepository.findOne({
where: { id: data.find((x) => x.commandId)?.commandId ?? "" },
});
if (_command) {
if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") {
const orgRootDeputy = await this.orgRootRepository.findOne({
where: {
isDeputy: true,
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
relations: ["orgRevision"],
});
_posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร";
_posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป.";
} else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") {
_posNumCodeSit = "กรุงเทพมหานคร";
_posNumCodeSitAbb = "กทม.";
} else {
let _profileAdmin = await this.profileRepository.findOne({
where: {
keycloak: _command?.createdUserId.toString(),
current_holders: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
},
relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"],
});
_posNumCodeSit =
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ??
"";
_posNumCodeSitAbb =
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot
.orgRootShortName ?? "";
}
}
// ─────────────────────────────────────────────────────────────
// Single transaction ครอบทั้ง batch (all-or-nothing)
// ทุกคนใช้ manager ตัวเดียวกัน — คนใด throw จะ rollback ทั้ง batch
// และ propagate error ออกไป (ล้มเหลวทั้งหมด) โดย log error ของคนที่ทำให้ fail ก่อน rethrow
// ─────────────────────────────────────────────────────────────
await AppDataSource.transaction(async (manager) => {
for (const item of data ?? []) {
try {
await this.processOne(item, ctx, manager, _posNumCodeSit, _posNumCodeSitAbb);
} catch (err) {
const reason =
err instanceof HttpError
? err.message
: err instanceof Error
? err.message
: "unexpected error";
console.error(
`[ExecuteSalaryEmployeeCurrentService] Failed commandCode=${commandCode}, commandId=${commandId}, profileId=${item.profileId}: ${reason}`,
err,
);
throw err; // → rollback ทั้ง transaction + propagate เป็น batch failure
}
}
});
}
/**
* 1 transaction (manager)
* save manager.getRepository(...) transaction
* throw rollback + batch ( partial commit)
*/
private async processOne(
item: SalaryEmployeeCurrentItem,
ctx: SalaryEmployeeCurrentExecutionContext,
manager: EntityManager,
_posNumCodeSit: string,
_posNumCodeSitAbb: string,
): Promise<void> {
const req = ctx.req;
const profileEmployeeRepository = manager.getRepository(ProfileEmployee);
const salaryRepo = manager.getRepository(ProfileSalary);
const salaryHistoryRepo = manager.getRepository(ProfileSalaryHistory);
const employeePosMasterRepository = manager.getRepository(EmployeePosMaster);
const employeePositionRepository = manager.getRepository(EmployeePosition);
const profile: any = await profileEmployeeRepository.findOneBy({ id: item.profileId });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const dest_item = await salaryRepo.findOne({
where: { profileEmployeeId: item.profileId },
order: { order: "DESC" },
});
const before = null;
const dataSalary = new ProfileSalary();
dataSalary.posNumCodeSit = _posNumCodeSit;
dataSalary.posNumCodeSitAbb = _posNumCodeSitAbb;
const meta = {
order: dest_item == null ? 1 : dest_item.order + 1,
createdUserId: ctx.user.sub,
createdFullName: ctx.user.name,
lastUpdateUserId: ctx.user.sub,
lastUpdateFullName: ctx.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
};
Object.assign(dataSalary, {
...item,
...meta,
profileEmployeeId: item.profileId,
profileId: undefined,
});
const history = new ProfileSalaryHistory();
Object.assign(history, { ...dataSalary, id: undefined });
await salaryRepo.save(dataSalary, { data: req });
setLogDataDiff(req, { before, after: dataSalary });
history.profileSalaryId = dataSalary.id;
await salaryHistoryRepo.save(history, { data: req });
const posMaster = await employeePosMasterRepository.findOne({
where: { id: item.posmasterId },
relations: ["orgRoot"],
});
if (posMaster == null)
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้");
const posMasterOld = await employeePosMasterRepository.findOne({
where: {
current_holderId: item.profileId,
orgRevisionId: posMaster.orgRevisionId,
},
});
if (posMasterOld != null) {
posMasterOld.current_holderId = null;
posMasterOld.lastUpdatedAt = new Date();
}
// if (posMasterOld != null) posMasterOld.next_holderId = null;
const positionOld = await employeePositionRepository.findOne({
where: {
posMasterId: posMasterOld?.id,
positionIsSelected: true,
},
});
if (positionOld != null) {
positionOld.positionIsSelected = false;
await employeePositionRepository.save(positionOld);
}
const checkPosition = await employeePositionRepository.find({
where: {
posMasterId: item.posmasterId,
positionIsSelected: true,
},
});
if (checkPosition.length > 0) {
const clearPosition = checkPosition.map((positions) => ({
...positions,
positionIsSelected: false,
}));
await employeePositionRepository.save(clearPosition);
}
posMaster.current_holderId = item.profileId;
posMaster.lastUpdatedAt = new Date();
posMaster.next_holderId = null;
if (posMasterOld != null) {
await employeePosMasterRepository.save(posMasterOld);
// ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน
console.log(
`[ExecuteSalaryEmployeeCurrentService] Creating PosMasterHistory — posMasterId: ${posMasterOld.id}, profileId: ${item.profileId} (old)`,
);
await CreatePosMasterHistoryEmployee(posMasterOld.id, req, null, manager);
}
await employeePosMasterRepository.save(posMaster);
const positionNew = await employeePositionRepository.findOne({
where: {
id: item.positionId,
posMasterId: item.posmasterId,
},
});
if (positionNew != null) {
positionNew.positionIsSelected = true;
profile.posLevelId = positionNew.posLevelId;
profile.posTypeId = positionNew.posTypeId;
profile.position = positionNew.positionName;
profile.employeeOc = posMaster?.orgRoot?.orgRootName ?? null;
profile.positionEmployeePositionId = positionNew.positionName;
profile.amount = item.amount ?? null;
profile.amountSpecial = item.amountSpecial ?? null;
await profileEmployeeRepository.save(profile);
await employeePositionRepository.save(positionNew);
}
// ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน
console.log(
`[ExecuteSalaryEmployeeCurrentService] Creating PosMasterHistory — posMasterId: ${posMaster.id}, profileId: ${item.profileId}`,
);
await CreatePosMasterHistoryEmployee(posMaster.id, req, null, manager);
console.log(
`[ExecuteSalaryEmployeeCurrentService] Completed processOne — profileId: ${item.profileId}, posMasterId: ${posMaster.id}`,
);
}
}

View file

@ -0,0 +1,382 @@
import { Double, EntityManager } from "typeorm";
import { AppDataSource } from "../database/data-source";
import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status";
import { Profile } from "../entities/Profile";
import { ProfileEmployee } from "../entities/ProfileEmployee";
import { ProfileSalary } from "../entities/ProfileSalary";
import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory";
import { OrgRoot } from "../entities/OrgRoot";
import { OrgRevision } from "../entities/OrgRevision";
import { EmployeePosMaster } from "../entities/EmployeePosMaster";
import { CommandRecive } from "../entities/CommandRecive";
import { Command } from "../entities/Command";
import { checkCommandType, removeProfileInOrganize, setLogDataDiff } from "../interfaces/utils";
import { reOrderCommandRecivesAndDelete } from "./CommandService";
import { CreatePosMasterHistoryEmployee } from "./PositionService";
import { deleteUser } from "../keycloak";
/**
* Input: ข้อมูล 1 endpoint excexute/salary-employee-leave
* (C-PM-23, 42, 43 // )
*/
export interface SalaryEmployeeLeaveItem {
profileId: string;
amount?: Double | null;
amountSpecial?: Double | null;
positionSalaryAmount?: Double | null;
mouthSalaryAmount?: Double | null;
positionType: string | null;
positionLevel: string | null;
isLeave: boolean;
leaveReason?: string | null;
dateLeave?: Date | string | null;
isGovernment?: boolean | null;
commandId?: string | null;
orgRoot?: string | null;
orgChild1?: string | null;
orgChild2?: string | null;
orgChild3?: string | null;
orgChild4?: string | null;
positionExecutive?: string | null;
positionExecutiveField?: string | null;
positionArea?: string | null;
commandNo: string | null;
commandYear: number | null;
posNo: string | null;
posNoAbb: string | null;
commandDateAffect?: Date | string | null;
commandDateSign?: Date | string | null;
positionName: string | null;
commandCode?: string | null;
commandName?: string | null;
remark: string | null;
resignId: string | null;
}
/**
* Context audit/log
*/
export interface SalaryEmployeeLeaveExecutionContext {
user: { sub: string; name: string };
req?: any;
}
/**
* Service ProfileSalary + handle leave/
*
* commandType: C-PM-23, 42, 43
*
* - endpoint /org/command/excexute/salary-employee-leave service (thin wrapper)
* - consumer rabbitmq handler service (Linear Flow)
*
* Behavior preserve CommandController.newSalaryEmployeeAndUpdateLeave
*
* Batch semantics: all-or-nothing transaction (sequential)
* throw rollback batch propagate error ()
* return result success count
*
* Keycloak: operation (deleteUser) transaction preserve behavior
* Keycloak rollback DB rollback Keycloak operation
* Keycloak
*/
export class ExecuteSalaryEmployeeLeaveService {
private commandRepository = AppDataSource.getRepository(Command);
private commandReciveRepository = AppDataSource.getRepository(CommandRecive);
private profileRepository = AppDataSource.getRepository(Profile);
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
/**
* ProfileSalary + handle leave batch
*/
async executeSalaryEmployeeLeave(
data: SalaryEmployeeLeaveItem[],
ctx: SalaryEmployeeLeaveExecutionContext,
): Promise<void> {
const commandId = data?.find((x) => x.commandId)?.commandId ?? "unknown";
const commandCode = data?.find((x) => x.commandCode)?.commandCode ?? "unknown";
console.log(
`[ExecuteSalaryEmployeeLeaveService] Starting executeSalaryEmployeeLeave — commandCode: ${commandCode}, commandId: ${commandId}`,
);
console.log(`[ExecuteSalaryEmployeeLeaveService] Request body count: ${data?.length ?? 0}`);
// ─────────────────────────────────────────────────────────────
// Normalize date fields (ผ่าน handler จะได้ string → ต้องแปลงเป็น Date)
// ─────────────────────────────────────────────────────────────
const toDate = (v: any): Date | null => {
if (v == null || v === "") return null;
if (v instanceof Date) return isNaN(v.getTime()) ? null : v;
const d = new Date(v);
return isNaN(d.getTime()) ? null : d;
};
for (const item of data ?? []) {
const it = item as any;
it.dateLeave = toDate(it.dateLeave);
it.commandDateAffect = toDate(it.commandDateAffect);
it.commandDateSign = toDate(it.commandDateSign);
}
let _posNumCodeSit: string = "";
let _posNumCodeSitAbb: string = "";
const _command = await this.commandRepository.findOne({
where: { id: data.find((x) => x.commandId)?.commandId ?? "" },
relations: { commandType: true },
});
if (_command) {
if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") {
const orgRootDeputy = await this.orgRootRepository.findOne({
where: {
isDeputy: true,
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
relations: ["orgRevision"],
});
_posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร";
_posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป.";
} else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") {
_posNumCodeSit = "กรุงเทพมหานคร";
_posNumCodeSitAbb = "กทม.";
} else {
let _profileAdmin = await this.profileRepository.findOne({
where: {
keycloak: _command?.createdUserId.toString(),
current_holders: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
},
relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"],
});
_posNumCodeSit =
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ??
"";
_posNumCodeSitAbb =
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot
.orgRootShortName ?? "";
}
}
const today = new Date().setHours(0, 0, 0, 0);
// ─────────────────────────────────────────────────────────────
// Single transaction ครอบทั้ง batch (all-or-nothing)
// ทุกคนใช้ manager ตัวเดียวกัน — คนใด throw จะ rollback ทั้ง batch
// และ propagate error ออกไป (ล้มเหลวทั้งหมด) โดย log error ของคนที่ทำให้ fail ก่อน rethrow
// ─────────────────────────────────────────────────────────────
await AppDataSource.transaction(async (manager) => {
for (const item of data ?? []) {
try {
await this.processOne(item, ctx, manager, _command, _posNumCodeSit, _posNumCodeSitAbb, today);
} catch (err) {
const reason =
err instanceof HttpError
? err.message
: err instanceof Error
? err.message
: "unexpected error";
console.error(
`[ExecuteSalaryEmployeeLeaveService] Failed commandCode=${commandCode}, commandId=${commandId}, profileId=${item.profileId}: ${reason}`,
err,
);
throw err; // → rollback ทั้ง transaction + propagate เป็น batch failure
}
}
});
}
/**
* 1 transaction (manager)
* save manager.getRepository(...) transaction
* throw rollback + batch ( partial commit)
*/
private async processOne(
item: SalaryEmployeeLeaveItem,
ctx: SalaryEmployeeLeaveExecutionContext,
manager: EntityManager,
_command: Command | null,
_posNumCodeSit: string,
_posNumCodeSitAbb: string,
today: number,
): Promise<void> {
const req = ctx.req;
const commandReciveRepository = manager.getRepository(CommandRecive);
const profileEmployeeRepository = manager.getRepository(ProfileEmployee);
const salaryRepo = manager.getRepository(ProfileSalary);
const salaryHistoryRepo = manager.getRepository(ProfileSalaryHistory);
const employeePosMasterRepository = manager.getRepository(EmployeePosMaster);
const orgRevisionRepo = manager.getRepository(OrgRevision);
const profile = await profileEmployeeRepository.findOne({
where: { id: item.profileId },
relations: {
roleKeycloaks: true,
posType: true,
posLevel: true,
},
});
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const code = _command?.commandType?.code;
//ออกคำสั่งยกเลิกลาออก ลบเฉพาะคนที่ขอยกเลิกลาออก
if (item.resignId && code && ["C-PM-42"].includes(code)) {
const commandResign = await commandReciveRepository.findOne({
where: { refId: item.resignId },
relations: { command: true },
});
const executeDate = commandResign
? new Date(commandResign.command.commandExcecuteDate).setHours(0, 0, 0, 0)
: today;
if (
commandResign &&
_command.status !== "REPORTED" &&
(_command.status !== "WAITING" || today < executeDate)
) {
// ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน
await reOrderCommandRecivesAndDelete(commandResign!.id, manager);
}
}
let _commandYear = item.commandYear;
if (item.commandYear) {
_commandYear = item.commandYear > 2500 ? item.commandYear : item.commandYear + 543;
}
const dest_item = await salaryRepo.findOne({
where: { profileEmployeeId: item.profileId },
order: { order: "DESC" },
});
const before = null;
const dataSalary = new ProfileSalary();
dataSalary.posNumCodeSit = _posNumCodeSit;
dataSalary.posNumCodeSitAbb = _posNumCodeSitAbb;
const meta = {
order: dest_item == null ? 1 : dest_item.order + 1,
createdUserId: ctx.user.sub,
createdFullName: ctx.user.name,
lastUpdateUserId: ctx.user.sub,
lastUpdateFullName: ctx.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
};
Object.assign(dataSalary, {
...item,
...meta,
profileEmployeeId: item.profileId,
profileId: undefined,
});
const history = new ProfileSalaryHistory();
Object.assign(history, { ...dataSalary, id: undefined });
dataSalary.dateGovernment = (item.commandDateAffect as Date) ?? meta.createdAt;
await salaryRepo.save(dataSalary, { data: req });
setLogDataDiff(req, { before, after: dataSalary });
history.profileSalaryId = dataSalary.id;
await salaryHistoryRepo.save(history, { data: req });
const _null: any = null;
profile.isLeave = item.isLeave;
profile.leaveReason = item.leaveReason ?? _null;
profile.dateLeave = item.dateLeave ?? _null;
profile.lastUpdateUserId = ctx.user.sub;
profile.lastUpdateFullName = ctx.user.name;
profile.lastUpdatedAt = new Date();
// บันทึกประวัติก่อนลบตำแหน่ง
const clearProfile = await checkCommandType(String(item.commandId));
const curRevision = await orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
});
let orgRootRef = null;
let orgChild1Ref = null;
let orgChild2Ref = null;
let orgChild3Ref = null;
let orgChild4Ref = null;
if (curRevision) {
const curPosMaster = await employeePosMasterRepository.findOne({
where: {
current_holderId: profile.id,
orgRevisionId: curRevision.id,
},
relations: {
orgRoot: true,
orgChild1: true,
orgChild2: true,
orgChild3: true,
orgChild4: true,
},
});
orgRootRef = curPosMaster?.orgRoot ?? null;
orgChild1Ref = curPosMaster?.orgChild1 ?? null;
orgChild2Ref = curPosMaster?.orgChild2 ?? null;
orgChild3Ref = curPosMaster?.orgChild3 ?? null;
orgChild4Ref = curPosMaster?.orgChild4 ?? null;
if (curPosMaster) {
// ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน
console.log(
`[ExecuteSalaryEmployeeLeaveService] Creating PosMasterHistory — posMasterId: ${curPosMaster.id}, profileId: ${item.profileId}, type: DELETE`,
);
await CreatePosMasterHistoryEmployee(curPosMaster.id, req, "DELETE", manager);
}
}
// ลบตำแหน่ง
if (item.isLeave == true) {
// ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน
await removeProfileInOrganize(profile.id, "EMPLOYEE", manager);
}
if (clearProfile.status) {
if (profile.keycloak != null && profile.keycloak != "" && profile.isDelete === false) {
// Keycloak deleteUser ทำภายใน transaction — ถ้า DB rollback หลังจากนี้ Keycloak จะถูกลบไปแล้ว
// (Keycloak ไม่สามารถ rollback ได้)
const delUserKeycloak = await deleteUser(profile.keycloak);
if (delUserKeycloak) {
// Task #228
// profile.keycloak = _null;
profile.roleKeycloaks = [];
profile.isActive = false;
profile.isDelete = true;
}
}
profile.leaveCommandId = item.commandId ?? _null;
profile.leaveCommandNo = `${item.commandNo}/${_commandYear}`;
profile.leaveRemark = clearProfile.leaveRemark ?? _null;
profile.leaveDate = item.commandDateAffect ?? _null;
profile.leaveType = clearProfile.LeaveType ?? _null;
//ออกจากราชการ ไม่ต้องลบตำแหน่งในทะเบียน (issue #1516)
// profile.position = _null;
// profile.posTypeId = _null;
// profile.posLevelId = _null;
}
await profileEmployeeRepository.save(profile);
// if (profile.id) {
// await this.keycloakAttributeService.clearOrgDnaAttributes(
// [profile.id],
// "PROFILE_EMPLOYEE",
// );
// }
// Task #2190
if (code && ["C-PM-23", "C-PM-43"].includes(code)) {
let organizeName = "";
if (orgRootRef) {
const names = [
orgChild4Ref?.orgChild4Name,
orgChild3Ref?.orgChild3Name,
orgChild2Ref?.orgChild2Name,
orgChild1Ref?.orgChild1Name,
orgRootRef?.orgRootName,
].filter(Boolean);
organizeName = names.join(" ");
}
}
console.log(
`[ExecuteSalaryEmployeeLeaveService] Completed processOne — profileId: ${item.profileId}`,
);
}
}

View file

@ -0,0 +1,615 @@
import { Double, EntityManager } from "typeorm";
import { AppDataSource } from "../database/data-source";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
import { Profile } from "../entities/Profile";
import { ProfileEmployee } from "../entities/ProfileEmployee";
import { ProfileSalary } from "../entities/ProfileSalary";
import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory";
import { ProfileDiscipline } from "../entities/ProfileDiscipline";
import { ProfileDisciplineHistory } from "../entities/ProfileDisciplineHistory";
import { OrgRoot } from "../entities/OrgRoot";
import { OrgRevision } from "../entities/OrgRevision";
import { EmployeePosMaster } from "../entities/EmployeePosMaster";
import { Command } from "../entities/Command";
import {
checkCommandType,
removePostMasterAct,
removeProfileInOrganize,
setLogDataDiff,
} from "../interfaces/utils";
import {
CreatePosMasterHistoryEmployee,
CreatePosMasterHistoryOfficer,
} from "./PositionService";
import { deleteUser } from "../keycloak";
/**
* Input: ข้อมูล 1 endpoint excexute/salary-leave-discipline
* (C-PM-19, 20, 25, 26, 27, 28, 29, 30, 31, 32 /)
*
* profileType "OFFICER" , /null
*/
export interface SalaryLeaveDisciplineItem {
profileId: string;
profileType?: string | null;
isLeave: boolean | null;
leaveReason?: string | null;
dateLeave?: Date | string | null;
detail?: string | null;
level?: string | null;
unStigma?: string | null;
commandId?: string | null;
amount?: Double | null;
amountSpecial?: Double | null;
positionSalaryAmount?: Double | null;
mouthSalaryAmount?: Double | null;
isGovernment?: boolean | null;
commandNo: string | null;
commandYear: number | null;
commandDateAffect?: Date | string | null;
commandDateSign?: Date | string | null;
commandCode?: string | null;
commandName?: string | null;
remark: string | null;
orgRoot?: string | null;
orgChild1?: string | null;
orgChild2?: string | null;
orgChild3?: string | null;
orgChild4?: string | null;
posNo?: string | null;
posNoAbb?: string | null;
}
/**
* Context audit/log
*/
export interface SalaryLeaveDisciplineExecutionContext {
user: { sub: string; name: string };
req?: any;
}
/**
* Service ProfileSalary + ProfileDiscipline + handle leave
*
* commandType: C-PM-19, 20, 25, 26, 27, 28, 29, 30, 31, 32
*
* - endpoint /org/command/excexute/salary-leave-discipline service (thin wrapper)
* - consumer rabbitmq handler service (Linear Flow)
*
* Behavior preserve CommandController.newSalaryAndUpdateLeaveDiscipline
* OFFICER save ProfileSalary + ProfileDiscipline comment out
* ( preserve behavior EMPLOYEE save )
*
* Batch semantics: all-or-nothing transaction (sequential)
* throw rollback batch propagate error ()
* return result success count
*
* Keycloak: operation (deleteUser) transaction preserve behavior
* Keycloak rollback DB rollback Keycloak operation
* Keycloak
*/
export class ExecuteSalaryLeaveDisciplineService {
private commandRepository = AppDataSource.getRepository(Command);
private profileRepository = AppDataSource.getRepository(Profile);
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
/**
* batch
*
* @returns success/failure
*/
async executeSalaryLeaveDiscipline(
data: SalaryLeaveDisciplineItem[],
ctx: SalaryLeaveDisciplineExecutionContext,
): Promise<void> {
const commandId = data?.find((x) => x.commandId)?.commandId ?? "unknown";
const commandCode = data?.find((x) => x.commandCode)?.commandCode ?? "unknown";
console.log(
`[ExecuteSalaryLeaveDisciplineService] Starting executeSalaryLeaveDiscipline — commandCode: ${commandCode}, commandId: ${commandId}`,
);
console.log(`[ExecuteSalaryLeaveDisciplineService] Request body count: ${data?.length ?? 0}`);
// ─────────────────────────────────────────────────────────────
// Normalize date fields (ผ่าน handler จะได้ string → ต้องแปลงเป็น Date)
// ─────────────────────────────────────────────────────────────
const toDate = (v: any): Date | null => {
if (v == null || v === "") return null;
if (v instanceof Date) return isNaN(v.getTime()) ? null : v;
const d = new Date(v);
return isNaN(d.getTime()) ? null : d;
};
for (const item of data ?? []) {
const it = item as any;
it.dateLeave = toDate(it.dateLeave);
it.commandDateAffect = toDate(it.commandDateAffect);
it.commandDateSign = toDate(it.commandDateSign);
}
let _posNumCodeSit: string = "";
let _posNumCodeSitAbb: string = "";
const _command = await this.commandRepository.findOne({
relations: ["commandType"],
where: { id: data.find((x) => x.commandId)?.commandId ?? "" },
});
if (_command) {
if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") {
const orgRootDeputy = await this.orgRootRepository.findOne({
where: {
isDeputy: true,
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
relations: ["orgRevision"],
});
_posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร";
_posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป.";
} else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") {
_posNumCodeSit = "กรุงเทพมหานคร";
_posNumCodeSitAbb = "กทม.";
} else {
let _profileAdmin = await this.profileRepository.findOne({
where: {
keycloak: _command?.createdUserId.toString(),
current_holders: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
},
relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"],
});
_posNumCodeSit =
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ??
"";
_posNumCodeSitAbb =
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot
.orgRootShortName ?? "";
}
}
// ─────────────────────────────────────────────────────────────
// Single transaction ครอบทั้ง batch (all-or-nothing)
// ทุกคนใช้ manager ตัวเดียวกัน — คนใด throw จะ rollback ทั้ง batch
// และ propagate error ออกไป (ล้มเหลวทั้งหมด) โดย log error ของคนที่ทำให้ fail ก่อน rethrow
// ─────────────────────────────────────────────────────────────
await AppDataSource.transaction(async (manager) => {
for (const item of data ?? []) {
try {
await this.processOne(item, ctx, manager, _command, _posNumCodeSit, _posNumCodeSitAbb);
} catch (err) {
const reason =
err instanceof HttpError
? err.message
: err instanceof Error
? err.message
: "unexpected error";
console.error(
`[ExecuteSalaryLeaveDisciplineService] Failed commandCode=${commandCode}, commandId=${commandId}, profileId=${item.profileId}: ${reason}`,
err,
);
throw err; // → rollback ทั้ง transaction + propagate เป็น batch failure
}
}
});
}
/**
* 1 transaction (manager)
* save manager.getRepository(...) transaction
* throw rollback + batch ( partial commit)
*/
private async processOne(
item: SalaryLeaveDisciplineItem,
ctx: SalaryLeaveDisciplineExecutionContext,
manager: EntityManager,
_command: Command | null,
_posNumCodeSit: string,
_posNumCodeSitAbb: string,
): Promise<void> {
const req = ctx.req;
const profileRepository = manager.getRepository(Profile);
const profileEmployeeRepository = manager.getRepository(ProfileEmployee);
const salaryRepo = manager.getRepository(ProfileSalary);
const salaryHistoryRepo = manager.getRepository(ProfileSalaryHistory);
const disciplineRepository = manager.getRepository(ProfileDiscipline);
const disciplineHistoryRepository = manager.getRepository(ProfileDisciplineHistory);
const orgRevisionRepo = manager.getRepository(OrgRevision);
const employeePosMasterRepository = manager.getRepository(EmployeePosMaster);
let _commandYear = item.commandYear;
if (item.commandYear) {
_commandYear = item.commandYear > 2500 ? item.commandYear : item.commandYear + 543;
}
const orgRevision = await orgRevisionRepo.findOne({
where: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
});
let orgRootRef: any = null;
let orgChild1Ref: any = null;
let orgChild2Ref: any = null;
let orgChild3Ref: any = null;
let orgChild4Ref: any = null;
const code = _command?.commandType?.code;
// ═══════════════════════════════════════════════════════════
// OFFICER (ข้าราชการ)
// ═══════════════════════════════════════════════════════════
if (item.profileType && item.profileType.trim().toUpperCase() == "OFFICER") {
const profile: any = await profileRepository.findOne({
relations: [
"posLevel",
"posType",
"current_holders",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
"current_holders.positions",
"current_holders.positions.posExecutive",
"roleKeycloaks",
],
where: { id: item.profileId },
});
if (!profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้");
}
const lastSalary = await salaryRepo.findOne({
where: { profileId: item.profileId },
select: ["order"],
order: { order: "DESC" },
});
const nextOrder = lastSalary ? lastSalary.order + 1 : 1;
//ลบตำแหน่งที่รักษาการแทน (await + ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน)
if (code && ["C-PM-19", "C-PM-20"].includes(code)) {
await removePostMasterAct(profile.id, manager);
}
const orgRevisionRef =
profile?.current_holders?.find((x: any) => x.orgRevisionId == orgRevision?.id) ?? null;
orgRootRef = orgRevisionRef?.orgRoot ?? null;
orgChild1Ref = orgRevisionRef?.orgChild1 ?? null;
orgChild2Ref = orgRevisionRef?.orgChild2 ?? null;
orgChild3Ref = orgRevisionRef?.orgChild3 ?? null;
orgChild4Ref = orgRevisionRef?.orgChild4 ?? null;
const position =
profile.current_holders
.filter((x: any) => x.orgRevisionId == orgRevision?.id)[0]
?.positions?.filter((pos: any) => pos.positionIsSelected === true)[0] ?? null;
// ประวัติตำแหน่ง
const data = new ProfileSalary();
data.posNumCodeSit = _posNumCodeSit;
data.posNumCodeSitAbb = _posNumCodeSitAbb;
const meta = {
profileId: profile.id,
commandId: item.commandId,
position: profile.position,
positionName: profile.position,
positionType: profile?.posType?.posTypeName ?? null,
positionLevel: profile?.posLevel?.posLevelName ?? null,
positionExecutive: position?.posExecutive?.posExecutiveName ?? null,
amount: item.amount ? item.amount : null,
positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null,
amountSpecial: item.amountSpecial ? item.amountSpecial : null,
mouthSalaryAmount: item.mouthSalaryAmount ? item.mouthSalaryAmount : null,
order: nextOrder,
orgRoot: item.orgRoot,
orgChild1: item.orgChild1,
orgChild2: item.orgChild2,
orgChild3: item.orgChild3,
orgChild4: item.orgChild4,
createdUserId: ctx.user.sub,
createdFullName: ctx.user.name,
lastUpdateUserId: ctx.user.sub,
lastUpdateFullName: ctx.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
dateGovernment: item.commandDateAffect ?? new Date(),
isGovernment: item.isGovernment,
commandNo: item.commandNo,
commandYear: item.commandYear,
posNo: item.posNo,
posNoAbb: item.posNoAbb,
commandDateAffect: item.commandDateAffect,
commandDateSign: item.commandDateSign,
commandCode: item.commandCode,
commandName: item.commandName,
remark: item.remark,
};
Object.assign(data, meta);
const history = new ProfileSalaryHistory();
Object.assign(history, { ...data, id: undefined });
// ── preserve: OFFICER branch ไม่ save ProfileSalary (comment ตามต้นฉบับ) ──
// await salaryRepo.save(data, { data: req });
// history.profileSalaryId = data.id;
// await salaryHistoryRepo.save(history, { data: req });
// ประวัติวินัย
const dataDis = new ProfileDiscipline();
const metaDis = {
date: item.commandDateAffect,
refCommandDate: item.commandDateSign,
refCommandNo: `${item.commandNo}/${item.commandYear}`,
refCommandId: item.commandId,
createdUserId: ctx.user.sub,
createdFullName: ctx.user.name,
lastUpdateUserId: ctx.user.sub,
lastUpdateFullName: ctx.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
};
Object.assign(dataDis, { ...item, ...metaDis });
const historyDis = new ProfileDisciplineHistory();
Object.assign(historyDis, { ...dataDis, id: undefined });
// ── preserve: OFFICER branch ไม่ save ProfileDiscipline (comment ตามต้นฉบับ) ──
// await disciplineRepository.save(dataDis, { data: req });
// historyDis.profileDisciplineId = dataDis.id;
// await disciplineHistoryRepository.save(historyDis, { data: req });
// ทะเบียนประวัติ
if (item.isLeave != null) {
const _profile: any = await profileRepository.findOne({
where: { id: item.profileId },
relations: ["roleKeycloaks"],
});
if (!_profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้");
}
const _null: any = null;
_profile.isLeave = item.isLeave;
_profile.leaveReason = item.leaveReason ?? _null;
_profile.dateLeave = item.dateLeave ?? _null;
_profile.lastUpdateUserId = ctx.user.sub;
_profile.lastUpdateFullName = ctx.user.name;
_profile.lastUpdatedAt = new Date();
if (item.isLeave == true) {
if (orgRevisionRef) {
// ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน
await CreatePosMasterHistoryOfficer(orgRevisionRef.id, req, "DELETE", null, manager);
}
// ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน
await removeProfileInOrganize(_profile.id, "OFFICER", manager);
}
const clearProfile = await checkCommandType(String(item.commandId));
if (clearProfile.status) {
if (
_profile.keycloak != null &&
_profile.keycloak != "" &&
_profile.isDelete === false
) {
// Keycloak ทำภายใน transaction — ไม่สามารถ rollback ได้ (ดู docstring ของ class)
const delUserKeycloak = await deleteUser(_profile.keycloak);
if (delUserKeycloak) {
// Task #228
// _profile.keycloak = _null;
_profile.roleKeycloaks = [];
_profile.isActive = false;
_profile.isDelete = true;
}
}
_profile.leaveCommandId = item.commandId ?? _null;
_profile.leaveCommandNo = `${item.commandNo}/${_commandYear}`;
_profile.leaveRemark = clearProfile.leaveRemark ?? _null;
_profile.leaveDate = item.commandDateAffect ?? _null;
_profile.leaveType = clearProfile.LeaveType ?? _null;
//ออกจากราชการ ไม่ต้องลบตำแหน่งในทะเบียน (issue #1516)
// _profile.position = _null;
// _profile.posTypeId = _null;
// _profile.posLevelId = _null;
}
await profileRepository.save(_profile, { data: req });
setLogDataDiff(req, { before: null, after: _profile });
}
}
// ═══════════════════════════════════════════════════════════
// EMPLOYEE (ลูกจ้าง)
// ═══════════════════════════════════════════════════════════
else {
const profile: any = await profileEmployeeRepository.findOne({
relations: [
"posLevel",
"posType",
"current_holders",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
"roleKeycloaks",
],
where: { id: item.profileId },
});
if (!profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้");
}
const lastSalary = await salaryRepo.findOne({
where: { profileEmployeeId: item.profileId },
select: ["order"],
order: { order: "DESC" },
});
const nextOrder = lastSalary ? lastSalary.order + 1 : 1;
const orgRevisionRef =
profile?.current_holders?.find((x: any) => x.orgRevisionId == orgRevision?.id) ?? null;
orgRootRef = orgRevisionRef?.orgRoot ?? null;
orgChild1Ref = orgRevisionRef?.orgChild1 ?? null;
orgChild2Ref = orgRevisionRef?.orgChild2 ?? null;
orgChild3Ref = orgRevisionRef?.orgChild3 ?? null;
orgChild4Ref = orgRevisionRef?.orgChild4 ?? null;
// ประวัติตำแหน่ง
const data = new ProfileSalary();
data.posNumCodeSit = _posNumCodeSit;
data.posNumCodeSitAbb = _posNumCodeSitAbb;
const meta = {
profileEmployeeId: profile.id,
commandId: item.commandId,
position: profile.position,
positionName: profile.position,
positionType: profile?.posType?.posTypeName ?? null,
positionLevel:
profile?.posType && profile?.posLevel
? `${profile?.posType?.posTypeShortName} ${profile?.posLevel?.posLevelName}`
: null,
amount: item.amount ? item.amount : null,
positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null,
mouthSalaryAmount: item.mouthSalaryAmount ? item.mouthSalaryAmount : null,
order: nextOrder,
orgRoot: item.orgRoot,
orgChild1: item.orgChild1,
orgChild2: item.orgChild2,
orgChild3: item.orgChild3,
orgChild4: item.orgChild4,
createdUserId: ctx.user.sub,
createdFullName: ctx.user.name,
lastUpdateUserId: ctx.user.sub,
lastUpdateFullName: ctx.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
dateGovernment: item.commandDateAffect ?? new Date(),
isGovernment: item.isGovernment,
commandNo: item.commandNo,
commandYear: item.commandYear,
posNo: item.posNo,
posNoAbb: item.posNoAbb,
commandDateAffect: item.commandDateAffect,
commandDateSign: item.commandDateSign,
commandCode: item.commandCode,
commandName: item.commandName,
remark: item.remark,
};
Object.assign(data, meta);
const history = new ProfileSalaryHistory();
Object.assign(history, { ...data, id: undefined });
await salaryRepo.save(data, { data: req });
setLogDataDiff(req, { before: null, after: data });
history.profileSalaryId = data.id;
await salaryHistoryRepo.save(history, { data: req });
// ประวัติวินัย
const dataDis = new ProfileDiscipline();
const metaDis = {
createdUserId: ctx.user.sub,
createdFullName: ctx.user.name,
lastUpdateUserId: ctx.user.sub,
lastUpdateFullName: ctx.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
};
Object.assign(dataDis, {
...item,
...metaDis,
date: item.commandDateAffect,
refCommandDate: item.commandDateSign,
refCommandNo: item.commandNo,
profileEmployeeId: item.profileId,
profileId: undefined,
});
const historyDis = new ProfileDisciplineHistory();
Object.assign(historyDis, { ...dataDis, id: undefined });
await disciplineRepository.save(dataDis, { data: req });
setLogDataDiff(req, { before: null, after: dataDis });
historyDis.profileDisciplineId = dataDis.id;
await disciplineHistoryRepository.save(historyDis, { data: req });
// ทะเบียนประวัติ
if (item.isLeave != null) {
const _profile: any = await profileEmployeeRepository.findOne({
where: { id: item.profileId },
relations: ["roleKeycloaks"],
});
if (!_profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้");
}
const _null: any = null;
_profile.isLeave = item.isLeave;
_profile.leaveReason = item.leaveReason ?? _null;
_profile.dateLeave = item.dateLeave ?? _null;
_profile.lastUpdateUserId = ctx.user.sub;
_profile.lastUpdateFullName = ctx.user.name;
_profile.lastUpdatedAt = new Date();
if (item.isLeave == true) {
// บันทึกประวัติก่อนลบตำแหน่ง
const curRevision = await orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
});
if (curRevision) {
const curPosMaster = await employeePosMasterRepository.findOne({
where: {
current_holderId: _profile.id,
orgRevisionId: curRevision.id,
},
});
if (curPosMaster) {
// ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน
await CreatePosMasterHistoryEmployee(curPosMaster.id, req, "DELETE", manager);
}
}
// ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน
await removeProfileInOrganize(_profile.id, "EMPLOYEE", manager);
}
const clearProfile = await checkCommandType(String(item.commandId));
if (clearProfile.status) {
if (
_profile.keycloak != null &&
_profile.keycloak != "" &&
_profile.isDelete === false
) {
// Keycloak deleteUser ทำภายใน transaction — ถ้า DB rollback หลังจากนี้ Keycloak จะถูกลบไปแล้ว
// (Keycloak ไม่สามารถ rollback ได้)
const delUserKeycloak = await deleteUser(_profile.keycloak);
if (delUserKeycloak) {
// Task #228
// _profile.keycloak = _null;
_profile.roleKeycloaks = [];
_profile.isActive = false;
_profile.isDelete = true;
}
}
_profile.leaveCommandId = item.commandId ?? _null;
_profile.leaveCommandNo = `${item.commandNo}/${_commandYear}`;
_profile.leaveRemark = clearProfile.leaveRemark ?? _null;
_profile.leaveDate = item.commandDateAffect ?? _null;
_profile.leaveType = clearProfile.LeaveType ?? _null;
//ออกจากราชการ ไม่ต้องลบตำแหน่งในทะเบียน (issue #1516)
// _profile.position = _null;
// _profile.posTypeId = _null;
// _profile.posLevelId = _null;
}
await profileEmployeeRepository.save(_profile, { data: req });
setLogDataDiff(req, { before: null, after: _profile });
}
}
// Task #2190 (preserve: organizeName computed แต่ยังไม่ได้ใช้ในต้นฉบับ — เก็บไว้ตาม behavior เดิม)
if (_command && ["C-PM-19", "C-PM-20"].includes(_command.commandType.code)) {
let organizeName = "";
if (orgRootRef) {
const names = [
orgChild4Ref?.orgChild4Name,
orgChild3Ref?.orgChild3Name,
orgChild2Ref?.orgChild2Name,
orgChild1Ref?.orgChild1Name,
orgRootRef?.orgRootName,
].filter(Boolean);
organizeName = names.join(" ");
}
}
console.log(
`[ExecuteSalaryLeaveDisciplineService] Completed processOne — profileId: ${item.profileId}`,
);
}
}

View file

@ -0,0 +1,708 @@
import { Double, EntityManager, In, Like } from "typeorm";
import { AppDataSource } from "../database/data-source";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
import { Profile } from "../entities/Profile";
import { ProfileSalary } from "../entities/ProfileSalary";
import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory";
import { OrgRoot } from "../entities/OrgRoot";
import { OrgRevision } from "../entities/OrgRevision";
import { PosMaster } from "../entities/PosMaster";
import { Position } from "../entities/Position";
import { RoleKeycloak } from "../entities/RoleKeycloak";
import { CommandRecive } from "../entities/CommandRecive";
import { Command } from "../entities/Command";
import {
checkCommandType,
checkReturnCommandType,
removePostMasterAct,
removeProfileInOrganize,
setLogDataDiff,
} from "../interfaces/utils";
import { reOrderCommandRecivesAndDelete } from "./CommandService";
import { CreatePosMasterHistoryOfficer } from "./PositionService";
import { getOrgFullName, getPosMasterNo } from "../utils/org-formatting";
import {
addUserRoles,
createUser,
deleteUser,
getRoleMappings,
getRoles,
getUserByUsername,
updateUserAttributes,
} from "../keycloak";
/**
* Input: ข้อมูล 1 endpoint excexute/salary-leave
* (C-PM-08, 09, 17, 18, 41, 48 // )
*/
export interface SalaryLeaveItem {
profileId: string;
amount?: Double | null;
amountSpecial?: Double | null;
positionSalaryAmount?: Double | null;
mouthSalaryAmount?: Double | null;
positionExecutive: string | null;
positionExecutiveField?: string | null;
positionArea?: string | null;
positionType: string | null;
positionLevel: string | null;
isLeave: boolean;
leaveReason?: string | null;
dateLeave?: Date | string | null;
posExecutiveId?: string | null;
positionField?: string | null;
commandId?: string | null;
isGovernment?: boolean | null;
orgRoot?: string | null;
orgChild1?: string | null;
orgChild2?: string | null;
orgChild3?: string | null;
orgChild4?: string | null;
commandNo: string | null;
commandYear: number | null;
posNo: string | null;
posNoAbb: string | null;
commandDateAffect?: Date | string | null;
commandDateSign?: Date | string | null;
positionName: string | null;
commandCode?: string | null;
commandName?: string | null;
remark: string | null;
positionId?: string | null;
positionTypeNew?: string | null;
positionLevelNew?: string | null;
positionNameNew?: string | null;
posmasterId?: string | null;
posTypeNameNew?: string | null;
posLevelNameNew?: string | null;
posNoNew?: string | null;
posNoAbbNew?: string | null;
orgRootNew?: string | null;
orgChild1New?: string | null;
orgChild2New?: string | null;
orgChild3New?: string | null;
orgChild4New?: string | null;
resignId?: string | null;
}
/**
* Context audit/log
*/
export interface SalaryLeaveExecutionContext {
user: { sub: string; name: string };
req?: any;
}
/**
* Service ProfileSalary + handle leave/
*
* commandType: C-PM-08, 09, 17, 18, 41, 48
*
* - endpoint /org/command/excexute/salary-leave service (thin wrapper)
* - consumer rabbitmq handler service (Linear Flow)
*
* Behavior preserve CommandController.newSalaryAndUpdateLeave
*
* Batch semantics: all-or-nothing transaction (sequential)
* throw rollback batch propagate error ()
* return result success count
*
* Keycloak: operations (deleteUser/createUser/addUserRoles/updateUserAttributes)
* transaction preserve behavior Keycloak rollback
* DB rollback Keycloak operation Keycloak
*/
export class ExecuteSalaryLeaveService {
private commandRepository = AppDataSource.getRepository(Command);
private commandReciveRepository = AppDataSource.getRepository(CommandRecive);
private profileRepository = AppDataSource.getRepository(Profile);
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
private roleKeycloakRepo = AppDataSource.getRepository(RoleKeycloak);
/**
* ProfileSalary + handle leave/ batch
*
* @returns success/failure
*/
async executeSalaryLeave(data: SalaryLeaveItem[], ctx: SalaryLeaveExecutionContext): Promise<void> {
const commandId = data?.find((x) => x.commandId)?.commandId ?? "unknown";
const commandCode = data?.find((x) => x.commandCode)?.commandCode ?? "unknown";
console.log(
`[ExecuteSalaryLeaveService] Starting executeSalaryLeave — commandCode: ${commandCode}, commandId: ${commandId}`,
);
console.log(`[ExecuteSalaryLeaveService] Request body count: ${data?.length ?? 0}`);
// ─────────────────────────────────────────────────────────────
// Normalize date fields (ผ่าน handler จะได้ string → ต้องแปลงเป็น Date)
// ─────────────────────────────────────────────────────────────
const toDate = (v: any): Date | null => {
if (v == null || v === "") return null;
if (v instanceof Date) return isNaN(v.getTime()) ? null : v;
const d = new Date(v);
return isNaN(d.getTime()) ? null : d;
};
for (const item of data ?? []) {
const it = item as any;
it.dateLeave = toDate(it.dateLeave);
it.commandDateAffect = toDate(it.commandDateAffect);
it.commandDateSign = toDate(it.commandDateSign);
}
const roleKeycloak = await this.roleKeycloakRepo.findOne({
where: { name: Like("USER") },
});
let _posNumCodeSit: string = "";
let _posNumCodeSitAbb: string = "";
const _command = await this.commandRepository.findOne({
relations: ["commandType"],
where: { id: data.find((x) => x.commandId)?.commandId ?? "" },
});
if (_command) {
if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") {
const orgRootDeputy = await this.orgRootRepository.findOne({
where: {
isDeputy: true,
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
relations: ["orgRevision"],
});
_posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร";
_posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป.";
} else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") {
_posNumCodeSit = "กรุงเทพมหานคร";
_posNumCodeSitAbb = "กทม.";
} else {
let _profileAdmin = await this.profileRepository.findOne({
where: {
keycloak: _command?.createdUserId.toString(),
current_holders: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
},
relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"],
});
_posNumCodeSit =
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ??
"";
_posNumCodeSitAbb =
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot
.orgRootShortName ?? "";
}
}
const today = new Date().setHours(0, 0, 0, 0);
// ─────────────────────────────────────────────────────────────
// Single transaction ครอบทั้ง batch (all-or-nothing)
// ทุกคนใช้ manager ตัวเดียวกัน — คนใด throw จะ rollback ทั้ง batch
// และ propagate error ออกไป (ล้มเหลวทั้งหมด) โดย log error ของคนที่ทำให้ fail ก่อน rethrow
// ─────────────────────────────────────────────────────────────
await AppDataSource.transaction(async (manager) => {
for (const item of data ?? []) {
try {
await this.processOne(
item,
ctx,
manager,
_command,
_posNumCodeSit,
_posNumCodeSitAbb,
today,
roleKeycloak,
);
} catch (err) {
const reason =
err instanceof HttpError
? err.message
: err instanceof Error
? err.message
: "unexpected error";
console.error(
`[ExecuteSalaryLeaveService] Failed commandCode=${commandCode}, commandId=${commandId}, profileId=${item.profileId}: ${reason}`,
err,
);
throw err; // → rollback ทั้ง transaction + propagate เป็น batch failure
}
}
});
}
/**
* 1 transaction (manager)
* save manager.getRepository(...) transaction
* throw rollback + batch ( partial commit)
*/
private async processOne(
item: SalaryLeaveItem,
ctx: SalaryLeaveExecutionContext,
manager: EntityManager,
_command: Command | null,
_posNumCodeSit: string,
_posNumCodeSitAbb: string,
today: number,
roleKeycloak: RoleKeycloak | null,
): Promise<void> {
const req = ctx.req;
const commandReciveRepository = manager.getRepository(CommandRecive);
const profileRepository = manager.getRepository(Profile);
const salaryRepo = manager.getRepository(ProfileSalary);
const salaryHistoryRepo = manager.getRepository(ProfileSalaryHistory);
const posMasterRepository = manager.getRepository(PosMaster);
const positionRepository = manager.getRepository(Position);
const orgRevisionRepo = manager.getRepository(OrgRevision);
const roleKeycloakRepo = manager.getRepository(RoleKeycloak);
const profile = await profileRepository.findOne({
where: { id: item.profileId },
relations: {
roleKeycloaks: true,
},
});
if (!profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้");
}
//ลบตำแหน่งที่รักษาการแทน
const code = _command?.commandType?.code;
if (code && ["C-PM-08", "C-PM-17", "C-PM-18", "C-PM-48"].includes(code)) {
// await (เดิมไม่ await = fire-and-forget bug) + ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction
await removePostMasterAct(profile.id, manager);
}
//ออกคำสั่งยกเลิกลาออก ลบเฉพาะคนที่ขอยกเลิกลาออก
else if (item.resignId && code && ["C-PM-41"].includes(code)) {
const commandResign = await commandReciveRepository.findOne({
where: { refId: item.resignId },
relations: { command: true },
});
const executeDate = commandResign
? new Date(commandResign.command.commandExcecuteDate).setHours(0, 0, 0, 0)
: today;
if (
commandResign &&
_command.status !== "REPORTED" &&
(_command.status !== "WAITING" || today < executeDate)
) {
// ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน
await reOrderCommandRecivesAndDelete(commandResign!.id, manager);
}
}
let _commandYear = item.commandYear;
if (item.commandYear) {
_commandYear = item.commandYear > 2500 ? item.commandYear : item.commandYear + 543;
}
const returnWork = await checkReturnCommandType(String(item.commandId));
const dest_item = await salaryRepo.findOne({
where: { profileId: item.profileId },
order: { order: "DESC" },
});
const before = null;
const dataSalary = new ProfileSalary();
dataSalary.posNumCodeSit = _posNumCodeSit;
dataSalary.posNumCodeSitAbb = _posNumCodeSitAbb;
dataSalary.dateGovernment = (item.commandDateAffect as Date) ?? new Date();
dataSalary.order = dest_item == null ? 1 : dest_item.order + 1;
const meta = {
createdUserId: ctx.user.sub,
createdFullName: ctx.user.name,
lastUpdateUserId: ctx.user.sub,
lastUpdateFullName: ctx.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
};
if (!returnWork) {
Object.assign(dataSalary, { ...item, ...meta });
const history = new ProfileSalaryHistory();
Object.assign(history, { ...dataSalary, id: undefined });
await salaryRepo.save(dataSalary, { data: req });
setLogDataDiff(req, { before, after: dataSalary });
history.profileSalaryId = dataSalary.id;
await salaryHistoryRepo.save(history, { data: req });
}
const _null: any = null;
profile.isLeave = item.isLeave;
profile.leaveReason = item.leaveReason ?? _null;
profile.dateLeave = item.dateLeave ?? _null;
profile.lastUpdateUserId = ctx.user.sub;
profile.lastUpdateFullName = ctx.user.name;
profile.lastUpdatedAt = new Date();
const clearProfile = await checkCommandType(String(item.commandId));
//ปั๊มประวัติก่อนลบตำแหน่ง
const curRevision = await orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
});
let orgRootRef = null;
let orgChild1Ref = null;
let orgChild2Ref = null;
let orgChild3Ref = null;
let orgChild4Ref = null;
if (curRevision) {
const curPosMaster = await posMasterRepository.findOne({
where: {
current_holderId: profile.id,
orgRevisionId: curRevision.id,
},
relations: {
orgRoot: true,
orgChild1: true,
orgChild2: true,
orgChild3: true,
orgChild4: true,
},
});
orgRootRef = curPosMaster?.orgRoot ?? null;
orgChild1Ref = curPosMaster?.orgChild1 ?? null;
orgChild2Ref = curPosMaster?.orgChild2 ?? null;
orgChild3Ref = curPosMaster?.orgChild3 ?? null;
orgChild4Ref = curPosMaster?.orgChild4 ?? null;
if (curPosMaster && clearProfile.LeaveType != "RETIRE_OUT_EMP") {
// ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน
console.log(
`[ExecuteSalaryLeaveService] Creating PosMasterHistory — posMasterId: ${curPosMaster.id}, profileId: ${item.profileId}, type: DELETE`,
);
await CreatePosMasterHistoryOfficer(curPosMaster.id, req, "DELETE", null, manager);
}
}
//ลบตำแหน่ง
if (item.isLeave == true) {
// ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน
await removeProfileInOrganize(profile.id, "OFFICER", manager);
}
if (clearProfile.status) {
if (profile.keycloak != null && profile.keycloak != "" && profile.isDelete === false) {
// Keycloak ทำภายใน transaction — ไม่สามารถ rollback ได้ (ดู docstring ของ class)
const delUserKeycloak = await deleteUser(profile.keycloak);
if (delUserKeycloak) {
// Task #228
// profile.keycloak = _null;
profile.roleKeycloaks = [];
profile.isActive = false;
profile.isDelete = true;
}
}
profile.leaveCommandId = item.commandId ?? _null;
profile.leaveCommandNo = `${item.commandNo}/${_commandYear}`;
profile.leaveRemark = clearProfile.leaveRemark ?? _null;
profile.leaveDate = item.commandDateAffect ?? _null;
profile.leaveType = clearProfile.LeaveType ?? _null;
//ออกจากราชการ ไม่ต้องลบตำแหน่งในทะเบียน (issue #1516)
// profile.position = _null;
// profile.posTypeId = _null;
// profile.posLevelId = _null;
}
if (item.isGovernment == true) {
if (returnWork) {
//ปลดตำแหน่งเดิมที่ไม่ถูกปลดออกจากกิ่งครั้งเมื่อออกคำสั่งพักราชการหรือออกราชการไว้
await removeProfileInOrganize(profile.id, "OFFICER", manager);
//ปั๊มตำแหน่งใหม่
// หา posMaster และเช็ค orgRevisionIsCurrent
let posMaster = await posMasterRepository.findOne({
where: { id: item.posmasterId?.toString() },
relations: {
orgRevision: true,
orgRoot: true,
orgChild1: true,
orgChild2: true,
orgChild3: true,
orgChild4: true,
},
});
// เช็คว่า posMaster ที่หามาอยู่ในโครงสร้างปัจจุบันหรือไม่
const isCurrent =
posMaster?.orgRevision?.orgRevisionIsCurrent === true &&
posMaster?.orgRevision?.orgRevisionIsDraft === false;
// ถ้าไม่อยู่ในโครงสร้างปัจจุบัน ให้หาตัวใหม่จาก ancestorDNA
if (!isCurrent && posMaster?.ancestorDNA) {
posMaster = await posMasterRepository.findOne({
where: {
ancestorDNA: posMaster.ancestorDNA,
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
relations: {
orgRevision: true,
orgRoot: true,
orgChild1: true,
orgChild2: true,
orgChild3: true,
orgChild4: true,
},
});
}
if (posMaster) {
const checkPosition = await positionRepository.find({
where: {
posMasterId: posMaster.id,
positionIsSelected: true,
},
});
if (checkPosition.length > 0) {
const clearPosition = checkPosition.map((positions) => ({
...positions,
positionIsSelected: false,
}));
await positionRepository.save(clearPosition);
}
posMaster.current_holderId = profile.id;
posMaster.lastUpdatedAt = new Date();
// posMaster.conditionReason = _null;
// posMaster.isCondition = false;
await posMasterRepository.save(posMaster);
// Match position ตามลำดับ priority:
// Condition 1: match จาก positionId
// Condition 2: match 7 ฟิลด์ (positionName, posTypeId, posLevelId, positionField, positionArea, positionExecutiveField, posExecutiveId)
// Condition 3: match 3 ฟิลด์ (positionName, posTypeId, posLevelId)
// Fallback: เลือก position แรกใน posMaster
let positionNew: Position | null = null;
// ═══════════════════════════════════════════════════════════
// CONDITION 1: เช็คจาก positionId ตรง
// ═══════════════════════════════════════════════════════════
if (item.positionId) {
const positionById = await positionRepository.findOne({
where: {
id: item.positionId,
posMasterId: posMaster.id, // ต้องอยู่ใน posMaster ที่ถูกต้อง
},
relations: ["posExecutive"],
});
if (positionById) {
positionNew = positionById;
}
}
// ═══════════════════════════════════════════════════════════
// CONDITION 2: Match 7 ฟิลด์ (ถ้า Condition 1 ไม่ match)
// ═══════════════════════════════════════════════════════════
if (!positionNew && item.positionNameNew && item.positionTypeNew && item.positionLevelNew) {
// สร้าง where clause แบบ dynamic - ใส่เฉพาะฟิลด์ที่มีค่า
const whereCondition: any = {
posMasterId: posMaster.id,
positionName: item.positionNameNew,
posTypeId: item.positionTypeNew,
posLevelId: item.positionLevelNew,
};
if (item.positionField) {
whereCondition.positionField = item.positionField;
}
if (item.posExecutiveId) {
whereCondition.posExecutiveId = item.posExecutiveId;
}
if (item.positionExecutiveField) {
whereCondition.positionExecutiveField = item.positionExecutiveField;
}
if (item.positionArea) {
whereCondition.positionArea = item.positionArea;
}
const positionBy7Fields = await positionRepository.findOne({
where: whereCondition,
relations: ["posExecutive"],
order: { orderNo: "ASC" },
});
if (positionBy7Fields) {
positionNew = positionBy7Fields;
}
}
// ═══════════════════════════════════════════════════════════
// CONDITION 3: Match 3 ฟิลด์ (ถ้า Condition 2 ไม่ match)
// ═══════════════════════════════════════════════════════════
if (!positionNew && item.positionNameNew && item.positionTypeNew && item.positionLevelNew) {
const positionBy3Fields = await positionRepository.findOne({
where: {
posMasterId: posMaster.id,
positionName: item.positionNameNew,
posTypeId: item.positionTypeNew,
posLevelId: item.positionLevelNew,
},
relations: ["posExecutive"],
order: { orderNo: "ASC" },
});
if (positionBy3Fields) {
positionNew = positionBy3Fields;
}
}
// // FALLBACK: เลือก position แรก (ถ้าไม่เจอทั้ง 2 condition)
// if (!positionNew) {
// const fallbackPositions = await positionRepository.find({
// where: {
// posMasterId: posMaster.id,
// },
// relations: ["posExecutive"],
// order: {
// orderNo: "ASC",
// },
// take: 1,
// });
// if (fallbackPositions.length > 0) {
// positionNew = fallbackPositions[0];
// }
// }
if (positionNew) {
positionNew.positionIsSelected = true;
await positionRepository.save(positionNew, { data: req });
}
// ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน
console.log(
`[ExecuteSalaryLeaveService] Creating PosMasterHistory — posMasterId: ${posMaster.id}, profileId: ${item.profileId}`,
);
await CreatePosMasterHistoryOfficer(posMaster.id, req, null, null, manager);
profile.posMasterNo = getPosMasterNo(posMaster);
profile.org = getOrgFullName(posMaster);
}
const newMapProfileSalary = {
profileId: profile.id,
commandId: item.commandId,
positionName: item.positionNameNew ?? null,
positionType: item.posTypeNameNew ?? null,
positionLevel: item.posLevelNameNew ?? null,
amount: item.amount ? item.amount : null,
positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null,
amountSpecial: item.amountSpecial ? item.amountSpecial : null,
mouthSalaryAmount: item.mouthSalaryAmount ? item.mouthSalaryAmount : null,
posNo: item.posNoNew,
posNoAbb: item.posNoAbbNew,
orgRoot: item.orgRootNew,
orgChild1: item.orgChild1New,
orgChild2: item.orgChild2New,
orgChild3: item.orgChild3New,
orgChild4: item.orgChild4New,
isGovernment: item.isGovernment,
commandNo: item.commandNo,
commandYear: item.commandYear,
commandDateAffect: item.commandDateAffect,
commandDateSign: item.commandDateSign,
commandCode: item.commandCode,
commandName: item.commandName,
remark: item.remark,
};
Object.assign(dataSalary, { ...newMapProfileSalary, ...meta });
const history = new ProfileSalaryHistory();
Object.assign(history, { ...dataSalary, id: undefined });
await salaryRepo.save(dataSalary);
history.profileSalaryId = dataSalary.id;
await salaryHistoryRepo.save(history);
profile.leaveReason = _null;
profile.leaveCommandId = _null;
profile.leaveCommandNo = _null;
profile.leaveRemark = _null;
profile.leaveDate = _null;
profile.leaveType = _null;
profile.position = item.positionNameNew ?? _null;
profile.posTypeId = item.positionTypeNew ?? _null;
profile.posLevelId = item.positionLevelNew ?? _null;
}
let userKeycloakId;
const checkUser = await getUserByUsername(profile.citizenId);
//ถ้ายังไม่มี user keycloak ให้สร้างใหม่
if (checkUser.length == 0) {
let password = profile.citizenId;
if (profile.birthDate != null) {
const _date = new Date(profile.birthDate.toDateString())
.getDate()
.toString()
.padStart(2, "0");
const _month = (new Date(profile.birthDate.toDateString()).getMonth() + 1)
.toString()
.padStart(2, "0");
const _year = new Date(profile.birthDate.toDateString()).getFullYear() + 543;
password = `${_date}${_month}${_year}`;
}
// กรอง "." ออกจาก firstName ก่อนส่งไป keycloak
const sanitizedFirstName = profile.firstName?.replace(/\./g, "") ?? "";
// Keycloak ทำภายใน transaction — ไม่สามารถ rollback ได้ (ดู docstring ของ class)
userKeycloakId = await createUser(profile.citizenId, password, {
firstName: sanitizedFirstName,
lastName: profile.lastName,
});
const list = await getRoles();
let result = false;
if (Array.isArray(list) && userKeycloakId) {
result = await addUserRoles(
userKeycloakId,
list
.filter((v) => v.name === "USER")
.map((x) => ({
id: x.id,
name: x.name,
})),
);
}
profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : [];
profile.keycloak =
userKeycloakId && typeof userKeycloakId === "string" ? userKeycloakId : "";
}
//ถ้ามีอยู่แล้วให้ใช้อันเดิม
else {
const rolesData = await getRoleMappings(checkUser[0].id);
if (rolesData) {
const _roleKeycloak = await roleKeycloakRepo.find({
where: { name: In(rolesData.map((x: any) => x.name)) },
});
profile.roleKeycloaks =
_roleKeycloak && _roleKeycloak.length > 0 ? _roleKeycloak : [];
}
profile.keycloak = checkUser[0].id;
}
profile.amount = item.amount ?? _null;
profile.amountSpecial = item.amountSpecial ?? _null;
profile.isActive = true;
profile.isDelete = false;
}
await profileRepository.save(profile);
// if (profile.id) {
// await this.keycloakAttributeService.clearOrgDnaAttributes(
// [profile.id],
// "PROFILE",
// );
// }
// update user attribute in keycloak
await updateUserAttributes(profile.keycloak ?? "", {
profileId: [profile.id],
prefix: [profile.prefix || ""],
});
// Task #2190
if (code && ["C-PM-17", "C-PM-18", "C-PM-48"].includes(code)) {
let organizeName = "";
if (orgRootRef) {
const names = [
orgChild4Ref?.orgChild4Name,
orgChild3Ref?.orgChild3Name,
orgChild2Ref?.orgChild2Name,
orgChild1Ref?.orgChild1Name,
orgRootRef?.orgRootName,
].filter(Boolean);
organizeName = names.join(" ");
}
}
console.log(
`[ExecuteSalaryLeaveService] Completed processOne — profileId: ${item.profileId}`,
);
}
}

View file

@ -0,0 +1,539 @@
import { Double, EntityManager, In, Repository } from "typeorm";
import { AppDataSource } from "../database/data-source";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
import { Profile } from "../entities/Profile";
import { ProfileSalary } from "../entities/ProfileSalary";
import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory";
import { Command } from "../entities/Command";
import { OrgRoot } from "../entities/OrgRoot";
import { OrgRevision } from "../entities/OrgRevision";
import { checkCommandType, removeProfileInOrganize } from "../interfaces/utils";
import { CreatePosMasterHistoryOfficer } from "./PositionService";
import { deleteUser } from "../keycloak";
/**
* Input: ข้อมูล 1 probation return ( Linear Flow refactor)
* - C-PM-11 : excexute/salary-probation ()
* - C-PM-12 : excexute/salary-probation-leave ( )
*
* shape body.data endpoint /org/command/excexute/salary-probation(-leave)
*/
export interface ProbationSalaryItem {
profileId: string;
commandId?: string | null;
amount?: Double | null;
amountSpecial?: Double | null;
positionSalaryAmount?: Double | null;
mouthSalaryAmount?: Double | null;
commandNo: string | null;
commandYear: number | null;
commandDateAffect?: Date | string | null;
commandDateSign?: Date | string | null;
positionName?: string | null;
commandCode?: string | null;
commandName?: string | null;
remark: string | null;
isGovernment?: boolean | null; // C-PM-12 เท่านั้น
}
/**
* Context audit/log ( ExecuteSalaryService)
*/
export interface ProbationExecutionContext {
user: { sub: string; name: string };
req?: any;
}
/**
* Service (probation) C-PM-11, C-PM-12
*
* circular callback: org AMQ probation PostData("/org/command/excexute/salary-probation(-leave)")
* Linear Flow: org AMQ probation (return salary data) service (no callback)
*
* - C-PM-11 : executeProbationPass ProfileSalary + history, isProbation=false
* - C-PM-12 : executeProbationLeave leave logic + deleteUser(Keycloak) + ProfileSalary + history
*
* - endpoint /org/command/excexute/salary-probation(-leave) service (thin wrapper)
* - consumer rabbitmq handler service (Linear Flow)
*
* Behavior preserve CommandController
* (newSalaryAndUpdateLeaveDisciplinefgh / ExecuteCommand12Async )
*
* Batch semantics: all-or-nothing transaction (sequential)
* throw rollback batch propagate error ()
*
* Keycloak: deleteUser (C-PM-12) transaction preserve behavior
* (consistent ExecuteSalaryService C-PM-13/15/16) Keycloak rollback
* DB rollback deleteUser user Keycloak
*/
export class ExecuteSalaryProbationService {
private commandRepository = AppDataSource.getRepository(Command);
private profileRepository = AppDataSource.getRepository(Profile);
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
// ─────────────────────────────────────────────────────────────
// แก้ปัญหา _posNumCodeSit resolution ที่ซ้ำกันในทุก endpoint
// (เดิมอยู่ใน controller — ย้ายมานี่ ทำครั้งเดียวก่อนเข้า transaction)
// ─────────────────────────────────────────────────────────────
private async resolvePosNumCodeSit(
commandId: string | null | undefined,
): Promise<{ posNumCodeSit: string; posNumCodeSitAbb: string }> {
let posNumCodeSit = "";
let posNumCodeSitAbb = "";
const command = commandId
? await this.commandRepository.findOne({ where: { id: commandId } })
: null;
if (command) {
if (command?.isBangkok?.toLocaleUpperCase() == "OFFICE") {
const orgRootDeputy = await this.orgRootRepository.findOne({
where: {
isDeputy: true,
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
relations: ["orgRevision"],
});
posNumCodeSit = orgRootDeputy ? orgRootDeputy.orgRootName : "สำนักปลัดกรุงเทพมหานคร";
posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy.orgRootShortName : "สนป.";
} else if (command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") {
posNumCodeSit = "กรุงเทพมหานคร";
posNumCodeSitAbb = "กทม.";
} else {
const profileAdmin = await this.profileRepository.findOne({
where: {
keycloak: command?.createdUserId.toString(),
current_holders: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
},
relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"],
});
posNumCodeSit =
profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ??
"";
posNumCodeSitAbb =
profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot
.orgRootShortName ?? "";
}
}
return { posNumCodeSit, posNumCodeSitAbb };
}
// normalize date (AMQ path ส่ง string มา → แปลงเป็น Date / null ถ้า invalid)
private toDate(v: any): Date | null {
if (v == null || v === "") return null;
if (v instanceof Date) return isNaN(v.getTime()) ? null : v;
const d = new Date(v);
return isNaN(d.getTime()) ? null : d;
}
// ═══════════════════════════════════════════════════════════════
// C-PM-11 : ผ่านทดลองปฏิบัติหน้าที่ราชการ
// สร้าง ProfileSalary + history แล้ว set isProbation=false
// ═══════════════════════════════════════════════════════════════
async executeProbationPass(
data: ProbationSalaryItem[],
ctx: ProbationExecutionContext,
): Promise<void> {
const commandId = data?.find((x) => x.commandId)?.commandId ?? "";
console.log(
`[ExecuteSalaryProbationService] executeProbationPass (C-PM-11) — commandId: ${commandId}, count: ${data?.length ?? 0}`,
);
// ─────────────────────────────────────────────────────────────
// Normalize date fields (ผ่าน AMQ handler จะได้ string → ต้องแปลงเป็น Date)
// ─────────────────────────────────────────────────────────────
for (const item of data ?? []) {
const it = item as any;
it.commandDateAffect = this.toDate(it.commandDateAffect);
it.commandDateSign = this.toDate(it.commandDateSign);
}
const { posNumCodeSit: _posNumCodeSit, posNumCodeSitAbb: _posNumCodeSitAbb } =
await this.resolvePosNumCodeSit(commandId);
const profileIds = (data ?? []).map((x) => x.profileId).filter(Boolean);
await AppDataSource.transaction(async (manager) => {
const profileRepository = manager.getRepository(Profile);
const salaryRepo = manager.getRepository(ProfileSalary);
const salaryHistoryRepo = manager.getRepository(ProfileSalaryHistory);
const orgRevisionRepo = manager.getRepository(OrgRevision);
for (const item of data ?? []) {
try {
await this.processOneProbationPass(
item,
ctx,
_posNumCodeSit,
_posNumCodeSitAbb,
salaryRepo,
salaryHistoryRepo,
profileRepository,
orgRevisionRepo,
);
} catch (err) {
const reason =
err instanceof HttpError
? err.message
: err instanceof Error
? err.message
: "unexpected error";
console.error(
`[ExecuteSalaryProbationService] Failed C-PM-11, commandId=${commandId}, profileId=${item.profileId}: ${reason}`,
err,
);
throw err; // → rollback ทั้ง transaction + propagate เป็น batch failure
}
}
// C-PM-11: ผ่านทดลองงาน → isProbation = false (bulk update ใน transaction เดียวกัน)
if (profileIds.length > 0) {
await profileRepository.update({ id: In(profileIds) }, { isProbation: false });
}
});
console.log(`[ExecuteSalaryProbationService] Completed C-PM-11 — ${data?.length ?? 0} items`);
}
private async processOneProbationPass(
item: ProbationSalaryItem,
ctx: ProbationExecutionContext,
_posNumCodeSit: string,
_posNumCodeSitAbb: string,
salaryRepo: Repository<ProfileSalary>,
salaryHistoryRepo: Repository<ProfileSalaryHistory>,
profileRepository: Repository<Profile>,
orgRevisionRepo: Repository<OrgRevision>,
): Promise<void> {
// current orgRevision (อ่านครั้งเดียวต่อคน — preserve query pattern ของ endpoint เดิม)
const orgRevision = await orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
});
const profile: any = await profileRepository.findOne({
relations: [
"posType",
"posLevel",
"current_holders",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
where: { id: item.profileId },
});
if (!profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้");
}
const lastSalary = await salaryRepo.findOne({
where: { profileId: item.profileId },
select: ["order"],
order: { order: "DESC" },
});
const nextOrder = lastSalary ? lastSalary.order + 1 : 1;
const orgRevisionRef =
profile?.current_holders?.find((x: any) => x.orgRevisionId == orgRevision?.id) ?? null;
const shortName =
orgRevisionRef?.orgChild4?.orgChild4ShortName ??
orgRevisionRef?.orgChild3?.orgChild3ShortName ??
orgRevisionRef?.orgChild2?.orgChild2ShortName ??
orgRevisionRef?.orgChild1?.orgChild1ShortName ??
orgRevisionRef?.orgRoot?.orgRootShortName ??
null;
const posNo = orgRevisionRef?.posMasterNo?.toString() ?? null;
// NOTE: endpoint เดิมไม่ได้ load relation "current_holders.positions" → position เป็น null (preserve)
const position =
profile.current_holders
?.filter((x: any) => x.orgRevisionId == orgRevision?.id)[0]
?.positions?.filter((pos: any) => pos.positionIsSelected === true)[0] ?? null;
const dataSalary = new ProfileSalary();
dataSalary.posNumCodeSit = _posNumCodeSit;
dataSalary.posNumCodeSitAbb = _posNumCodeSitAbb;
const meta = {
profileId: item.profileId,
commandId: item.commandId,
positionName: profile.position,
positionType: profile?.posType?.posTypeName ?? null,
positionLevel: profile?.posLevel?.posLevelName ?? null,
positionExecutive: position?.posExecutive?.posExecutiveName ?? null,
amount: item.amount ? item.amount : null,
amountSpecial: item.amountSpecial ? item.amountSpecial : null,
positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null,
mouthSalaryAmount: item.mouthSalaryAmount ? item.mouthSalaryAmount : null,
order: nextOrder,
orgRoot: orgRevisionRef?.orgRoot?.orgRootName ?? null,
orgChild1: orgRevisionRef?.orgChild1?.orgChild1Name ?? null,
orgChild2: orgRevisionRef?.orgChild2?.orgChild2Name ?? null,
orgChild3: orgRevisionRef?.orgChild3?.orgChild3Name ?? null,
orgChild4: orgRevisionRef?.orgChild4?.orgChild4Name ?? null,
createdUserId: ctx.user.sub,
createdFullName: ctx.user.name,
lastUpdateUserId: ctx.user.sub,
lastUpdateFullName: ctx.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
commandNo: item.commandNo,
commandYear: item.commandYear,
posNo: posNo,
posNoAbb: shortName,
commandDateAffect: item.commandDateAffect,
commandDateSign: item.commandDateSign,
commandCode: item.commandCode,
commandName: item.commandName,
remark: item.remark,
};
Object.assign(dataSalary, meta);
const history = new ProfileSalaryHistory();
Object.assign(history, { ...dataSalary, id: undefined });
await salaryRepo.save(dataSalary);
history.profileSalaryId = dataSalary.id;
await salaryHistoryRepo.save(history);
}
// ═══════════════════════════════════════════════════════════════
// C-PM-12 : ออกจากราชการเพราะผลการทดลองปฏิบัติหน้าที่ราชการต่ำกว่ามาตรฐาน
// leave logic (removeProfileInOrganize + deleteUser Keycloak) + สร้าง ProfileSalary + history
// ═══════════════════════════════════════════════════════════════
async executeProbationLeave(
data: ProbationSalaryItem[],
ctx: ProbationExecutionContext,
): Promise<void> {
const commandId = data?.find((x) => x.commandId)?.commandId ?? "";
console.log(
`[ExecuteSalaryProbationService] executeProbationLeave (C-PM-12) — commandId: ${commandId}, count: ${data?.length ?? 0}`,
);
for (const item of data ?? []) {
const it = item as any;
it.commandDateAffect = this.toDate(it.commandDateAffect);
it.commandDateSign = this.toDate(it.commandDateSign);
}
const { posNumCodeSit: _posNumCodeSit, posNumCodeSitAbb: _posNumCodeSitAbb } =
await this.resolvePosNumCodeSit(commandId);
await AppDataSource.transaction(async (manager) => {
const profileRepository = manager.getRepository(Profile);
const salaryRepo = manager.getRepository(ProfileSalary);
const salaryHistoryRepo = manager.getRepository(ProfileSalaryHistory);
const orgRevisionRepo = manager.getRepository(OrgRevision);
for (const item of data ?? []) {
try {
await this.processOneProbationLeave(
item,
ctx,
manager,
_posNumCodeSit,
_posNumCodeSitAbb,
salaryRepo,
salaryHistoryRepo,
profileRepository,
orgRevisionRepo,
);
} catch (err) {
const reason =
err instanceof HttpError
? err.message
: err instanceof Error
? err.message
: "unexpected error";
console.error(
`[ExecuteSalaryProbationService] Failed C-PM-12, commandId=${commandId}, profileId=${item.profileId}: ${reason}`,
err,
);
throw err; // → rollback ทั้ง transaction + propagate เป็น batch failure
}
}
});
console.log(`[ExecuteSalaryProbationService] Completed C-PM-12 — ${data?.length ?? 0} items`);
}
private async processOneProbationLeave(
item: ProbationSalaryItem,
ctx: ProbationExecutionContext,
manager: EntityManager,
_posNumCodeSit: string,
_posNumCodeSitAbb: string,
salaryRepo: Repository<ProfileSalary>,
salaryHistoryRepo: Repository<ProfileSalaryHistory>,
profileRepository: Repository<Profile>,
orgRevisionRepo: Repository<OrgRevision>,
): Promise<void> {
const req = ctx.req;
const profile: any = await profileRepository.findOne({
relations: [
"posType",
"posLevel",
"current_holders",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
"current_holders.positions",
"current_holders.positions.posExecutive",
],
where: { id: item.profileId },
});
if (!profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์");
}
const lastSalary = await salaryRepo.findOne({
where: { profileId: item.profileId },
select: ["order"],
order: { order: "DESC" },
});
const nextOrder = lastSalary ? lastSalary.order + 1 : 1;
let _commandYear = item.commandYear;
if (item.commandYear) {
_commandYear = item.commandYear > 2500 ? item.commandYear : item.commandYear + 543;
}
// _profile (load แยกสำหรับ mutation เกี่ยวกับ Keycloak/leave — preserve pattern เดิม)
const _profile: any = await profileRepository.findOne({
where: { id: item.profileId },
relations: ["roleKeycloaks"],
});
if (!_profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์");
}
let dateLeave_: any = item.commandDateAffect;
_profile.isLeave = true;
_profile.leaveReason =
"คำสั่งให้ข้าราชการออกจากราชการเพราะผลการทดลองปฏิบัติหน้าที่ราชการต่ำกว่ามาตรฐานที่กำหนด";
_profile.dateLeave = dateLeave_;
_profile.lastUpdateUserId = ctx.user.sub;
_profile.lastUpdateFullName = ctx.user.name;
_profile.lastUpdatedAt = new Date();
const orgRevision = await orgRevisionRepo.findOne({
where: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
});
const orgRevisionRef =
profile?.current_holders?.find((x: any) => x.orgRevisionId == orgRevision?.id) ?? null;
const orgRootRef = orgRevisionRef?.orgRoot ?? null;
const orgChild1Ref = orgRevisionRef?.orgChild1 ?? null;
const orgChild2Ref = orgRevisionRef?.orgChild2 ?? null;
const orgChild3Ref = orgRevisionRef?.orgChild3 ?? null;
const orgChild4Ref = orgRevisionRef?.orgChild4 ?? null;
const matchHolder = profile.current_holders?.find((x: any) => x.orgRevisionId == orgRevision?.id);
const shortName =
!profile.current_holders || profile.current_holders.length == 0
? null
: matchHolder != null && matchHolder?.orgChild4 != null
? `${matchHolder.orgChild4.orgChild4ShortName}`
: matchHolder != null && matchHolder?.orgChild3 != null
? `${matchHolder.orgChild3.orgChild3ShortName}`
: matchHolder != null && matchHolder?.orgChild2 != null
? `${matchHolder.orgChild2.orgChild2ShortName}`
: matchHolder != null && matchHolder?.orgChild1 != null
? `${matchHolder.orgChild1.orgChild1ShortName}`
: matchHolder != null && matchHolder?.orgRoot != null
? `${matchHolder.orgRoot.orgRootShortName}`
: null;
const posNo = `${matchHolder?.posMasterNo}`;
const position =
matchHolder?.positions?.filter((pos: any) => pos.positionIsSelected === true)[0] ?? null;
const profileSalary: ProfileSalary = Object.assign(new ProfileSalary(), {
profileId: item.profileId,
commandId: item.commandId,
positionName: profile.position,
positionType: profile?.posType?.posTypeName ?? null,
positionLevel: profile?.posLevel?.posLevelName ?? null,
positionExecutive: position?.posExecutive?.posExecutiveName ?? null,
amount: item.amount ? item.amount : null,
amountSpecial: item.amountSpecial ? item.amountSpecial : null,
positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null,
mouthSalaryAmount: item.mouthSalaryAmount ? item.mouthSalaryAmount : null,
order: nextOrder,
orgRoot: orgRootRef?.orgRootName ?? null,
orgChild1: orgChild1Ref?.orgChild1Name ?? null,
orgChild2: orgChild2Ref?.orgChild2Name ?? null,
orgChild3: orgChild3Ref?.orgChild3Name ?? null,
orgChild4: orgChild4Ref?.orgChild4Name ?? null,
createdUserId: ctx.user.sub,
createdFullName: ctx.user.name,
lastUpdateUserId: ctx.user.sub,
lastUpdateFullName: ctx.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
dateGovernment: item.commandDateAffect ?? new Date(),
isGovernment: item.isGovernment,
commandNo: item.commandNo,
commandYear: item.commandYear,
posNo: posNo,
posNoAbb: shortName,
commandDateAffect: item.commandDateAffect,
commandDateSign: item.commandDateSign,
commandCode: item.commandCode,
commandName: item.commandName,
remark: item.remark,
posNumCodeSit: _posNumCodeSit,
posNumCodeSitAbb: _posNumCodeSitAbb,
});
if (orgRevisionRef) {
await CreatePosMasterHistoryOfficer(orgRevisionRef.id, req, "DELETE", null, manager);
}
await removeProfileInOrganize(profile.id, "OFFICER", manager);
const clearProfile = await checkCommandType(String(item.commandId));
const _null: any = null;
if (clearProfile.status) {
// Keycloak deleteUser ทำภายใน transaction (preserve behavior เดิม — Keycloak ไม่ rollback ได้)
if (_profile.keycloak != null && _profile.keycloak != "" && _profile.isDelete === false) {
const delUserKeycloak = await deleteUser(_profile.keycloak);
if (delUserKeycloak) {
// Task #228
_profile.roleKeycloaks = [];
_profile.isActive = false;
_profile.isDelete = true;
}
}
_profile.leaveCommandId = item.commandId ?? _null;
_profile.leaveCommandNo = `${item.commandNo}/${_commandYear}`;
_profile.leaveRemark = clearProfile.leaveRemark ?? _null;
_profile.leaveDate = item.commandDateAffect ?? _null;
_profile.leaveType = clearProfile.LeaveType ?? _null;
//ออกจากราชการ ไม่ต้องลบตำแหน่งในทะเบียน (issue #1516)
// _profile.position = _null;
// _profile.posTypeId = _null;
// _profile.posLevelId = _null;
}
await Promise.all([
profileRepository.save(_profile),
salaryRepo.save(profileSalary),
]);
const history = new ProfileSalaryHistory();
Object.assign(history, { ...profileSalary, id: undefined });
history.profileSalaryId = profileSalary.id;
await salaryHistoryRepo.save(history);
console.log(
`[ExecuteSalaryProbationService] processOneProbationLeave done — profileId: ${item.profileId}`,
);
}
}

View file

@ -0,0 +1,318 @@
import { EntityManager, Repository } from "typeorm";
import { AppDataSource } from "../database/data-source";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
import permission from "../interfaces/permission";
import { setLogDataDiff } from "../interfaces/utils";
import { Profile } from "../entities/Profile";
import { ProfileEmployee } from "../entities/ProfileEmployee";
import {
CreateProfileSalary,
CreateProfileSalaryEmployee,
ProfileSalary,
} from "../entities/ProfileSalary";
import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory";
import { Command } from "../entities/Command";
import { OrgRoot } from "../entities/OrgRoot";
/**
* Context audit/log ( ExecuteSalaryService)
*/
export interface SalaryReportExecutionContext {
user: { sub: string; name: string };
req?: any;
}
/**
* Service salary service
*
* - C-PM-33, C-PM-34, C-PM-35, C-PM-45 : officer /org/profile/salary/update
* - C-PM-36, C-PM-37, C-PM-46 : employee /org/profile-employee/salary/update
*
* circular callback: org AMQ salary service PostData("/org/profile/(employee/)salary/update")
* Linear Flow: org AMQ salary service (return salary data) service (no callback)
*
* - executeOfficerSalaryUpdate : สร้าง ProfileSalary + history + Profile (amount*)
* - executeEmployeeSalaryUpdate : สร้าง ProfileSalary + history + ProfileEmployee (amount* + salaryLevel/group)
*
* Behavior preserve
* ProfileSalaryController.updateSalary / ProfileSalaryEmployeeController.updateSalary
* ( permission check + setLogDataDiff + save({data: req}))
*
* Batch semantics: all-or-nothing transaction batch
* throw (validation/permission) rollback batch propagate error
*
* permission check: ทำ per-item transaction (preserve behavior )
* HTTP loopback /org/permission/user/... token ctx.req batch
* ( salary endpoint permission check)
*/
export class ExecuteSalaryReportService {
private commandRepository = AppDataSource.getRepository(Command);
private profileRepository = AppDataSource.getRepository(Profile);
private profileEmployeeRepository = AppDataSource.getRepository(ProfileEmployee);
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
// ─────────────────────────────────────────────────────────────
// resolve _posNumCodeSit/Abb จาก command (ทำครั้งเดียวก่อนเข้า transaction)
// admin-lookup ใช้ Profile (officer: profileRepo / employee: profileGovementRepo — ทั้งคู่คือ Profile)
// ─────────────────────────────────────────────────────────────
private async resolvePosNumCodeSit(
commandId: string | null | undefined,
): Promise<{ posNumCodeSit: string; posNumCodeSitAbb: string }> {
let posNumCodeSit = "";
let posNumCodeSitAbb = "";
const command = commandId
? await this.commandRepository.findOne({ where: { id: commandId } })
: null;
if (command) {
if (command?.isBangkok?.toLocaleUpperCase() == "OFFICE") {
const orgRootDeputy = await this.orgRootRepository.findOne({
where: {
isDeputy: true,
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
relations: ["orgRevision"],
});
posNumCodeSit = orgRootDeputy ? orgRootDeputy.orgRootName : "สำนักปลัดกรุงเทพมหานคร";
posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy.orgRootShortName : "สนป.";
} else if (command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") {
posNumCodeSit = "กรุงเทพมหานคร";
posNumCodeSitAbb = "กทม.";
} else {
const profileAdmin = await this.profileRepository.findOne({
where: {
keycloak: command?.createdUserId.toString(),
current_holders: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
},
relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"],
});
posNumCodeSit =
profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ??
"";
posNumCodeSitAbb =
profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot
.orgRootShortName ?? "";
}
}
return { posNumCodeSit, posNumCodeSitAbb };
}
// ═══════════════════════════════════════════════════════════════
// C-PM-33/34/35/45 : officer salary update
// ═══════════════════════════════════════════════════════════════
async executeOfficerSalaryUpdate(
data: CreateProfileSalary[],
ctx: SalaryReportExecutionContext,
): Promise<void> {
const commandId = data?.find((x) => x.commandId)?.commandId ?? "";
console.log(
`[ExecuteSalaryReportService] executeOfficerSalaryUpdate — commandId: ${commandId}, count: ${data?.length ?? 0}`,
);
const { posNumCodeSit: _posNumCodeSit, posNumCodeSitAbb: _posNumCodeSitAbb } =
await this.resolvePosNumCodeSit(commandId);
await AppDataSource.transaction(async (manager) => {
const profileRepository = manager.getRepository(Profile);
const salaryRepo = manager.getRepository(ProfileSalary);
const salaryHistoryRepo = manager.getRepository(ProfileSalaryHistory);
for (const item of data ?? []) {
try {
await this.processOneOfficer(
item,
ctx,
_posNumCodeSit,
_posNumCodeSitAbb,
profileRepository,
salaryRepo,
salaryHistoryRepo,
);
} catch (err) {
const reason =
err instanceof HttpError
? err.message
: err instanceof Error
? err.message
: "unexpected error";
console.error(
`[ExecuteSalaryReportService] Failed officer, commandId=${commandId}, profileId=${item.profileId}: ${reason}`,
err,
);
throw err; // → rollback ทั้ง transaction + propagate เป็น batch failure
}
}
});
console.log(`[ExecuteSalaryReportService] Completed officer — ${data?.length ?? 0} items`);
}
private async processOneOfficer(
item: CreateProfileSalary,
ctx: SalaryReportExecutionContext,
_posNumCodeSit: string,
_posNumCodeSitAbb: string,
profileRepository: Repository<Profile>,
salaryRepo: Repository<ProfileSalary>,
salaryHistoryRepo: Repository<ProfileSalaryHistory>,
): Promise<void> {
const req = ctx.req;
if (!item.profileId) {
throw new HttpError(HttpStatusCode.BAD_REQUEST, "กรุณากรอก profileId");
}
const profile = await profileRepository.findOneBy({ id: item.profileId });
if (!profile) {
throw new HttpError(HttpStatusCode.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id);
const dest_item = await salaryRepo.findOne({
where: { profileId: item.profileId },
order: { order: "DESC" },
});
const before = null;
const data = new ProfileSalary();
data.posNumCodeSit = _posNumCodeSit;
data.posNumCodeSitAbb = _posNumCodeSitAbb;
const meta = {
order: dest_item == null ? 1 : dest_item.order + 1,
createdUserId: ctx.user.sub,
createdFullName: ctx.user.name,
lastUpdateUserId: ctx.user.sub,
lastUpdateFullName: ctx.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
};
Object.assign(data, { ...item, ...meta });
const history = new ProfileSalaryHistory();
Object.assign(history, { ...data, id: undefined });
await salaryRepo.save(data, { data: req });
setLogDataDiff(req, { before, after: data });
history.profileSalaryId = data.id;
await salaryHistoryRepo.save(history, { data: req });
const _null: any = null;
profile.amount = item.amount ?? _null;
profile.amountSpecial = item.amountSpecial ?? _null;
profile.positionSalaryAmount = item.positionSalaryAmount ?? _null;
profile.mouthSalaryAmount = item.mouthSalaryAmount ?? _null;
await profileRepository.save(profile);
}
// ═══════════════════════════════════════════════════════════════
// C-PM-36/37/46 : employee salary update
// ═══════════════════════════════════════════════════════════════
async executeEmployeeSalaryUpdate(
data: CreateProfileSalaryEmployee[],
ctx: SalaryReportExecutionContext,
): Promise<void> {
const commandId = data?.find((x) => x.commandId)?.commandId ?? "";
console.log(
`[ExecuteSalaryReportService] executeEmployeeSalaryUpdate — commandId: ${commandId}, count: ${data?.length ?? 0}`,
);
const { posNumCodeSit: _posNumCodeSit, posNumCodeSitAbb: _posNumCodeSitAbb } =
await this.resolvePosNumCodeSit(commandId);
await AppDataSource.transaction(async (manager) => {
const profileEmployeeRepository = manager.getRepository(ProfileEmployee);
const salaryRepo = manager.getRepository(ProfileSalary);
const salaryHistoryRepo = manager.getRepository(ProfileSalaryHistory);
for (const item of data ?? []) {
try {
await this.processOneEmployee(
item,
ctx,
_posNumCodeSit,
_posNumCodeSitAbb,
profileEmployeeRepository,
salaryRepo,
salaryHistoryRepo,
);
} catch (err) {
const reason =
err instanceof HttpError
? err.message
: err instanceof Error
? err.message
: "unexpected error";
console.error(
`[ExecuteSalaryReportService] Failed employee, commandId=${commandId}, profileEmployeeId=${item.profileEmployeeId}: ${reason}`,
err,
);
throw err; // → rollback ทั้ง transaction + propagate เป็น batch failure
}
}
});
console.log(`[ExecuteSalaryReportService] Completed employee — ${data?.length ?? 0} items`);
}
private async processOneEmployee(
item: CreateProfileSalaryEmployee,
ctx: SalaryReportExecutionContext,
_posNumCodeSit: string,
_posNumCodeSitAbb: string,
profileEmployeeRepository: Repository<ProfileEmployee>,
salaryRepo: Repository<ProfileSalary>,
salaryHistoryRepo: Repository<ProfileSalaryHistory>,
): Promise<void> {
const req = ctx.req;
if (!item.profileEmployeeId) {
throw new HttpError(HttpStatusCode.BAD_REQUEST, "กรุณากรอก profileEmployeeId");
}
const profile = await profileEmployeeRepository.findOneBy({ id: item.profileEmployeeId });
if (!profile) {
throw new HttpError(HttpStatusCode.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id);
const dest_item = await salaryRepo.findOne({
where: { profileEmployeeId: item.profileEmployeeId },
order: { order: "DESC" },
});
const before = null;
const data = new ProfileSalary();
data.posNumCodeSit = _posNumCodeSit;
data.posNumCodeSitAbb = _posNumCodeSitAbb;
const meta = {
order: dest_item == null ? 1 : dest_item.order + 1,
createdUserId: ctx.user.sub,
createdFullName: ctx.user.name,
lastUpdateUserId: ctx.user.sub,
lastUpdateFullName: ctx.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
};
Object.assign(data, { ...item, ...meta });
const history = new ProfileSalaryHistory();
Object.assign(history, { ...data, id: undefined });
await salaryRepo.save(data, { data: req });
setLogDataDiff(req, { before, after: data });
history.profileSalaryId = data.id;
await salaryHistoryRepo.save(history, { data: req });
const _null: any = null;
profile.amount = item.amount ?? _null;
profile.amountSpecial = item.amountSpecial ?? _null;
profile.positionSalaryAmount = item.positionSalaryAmount ?? _null;
profile.mouthSalaryAmount = item.mouthSalaryAmount ?? _null;
profile.salaryLevel = item.salaryLevel ?? _null;
profile.group = item.group ?? _null;
await profileEmployeeRepository.save(profile);
}
}

View file

@ -0,0 +1,391 @@
import { Double, EntityManager } from "typeorm";
import { AppDataSource } from "../database/data-source";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
import { Profile } from "../entities/Profile";
import { ProfileSalary } from "../entities/ProfileSalary";
import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory";
import { ProfileAssistance } from "../entities/ProfileAssistance";
import { ProfileAssistanceHistory } from "../entities/ProfileAssistanceHistory";
import { OrgRoot } from "../entities/OrgRoot";
import { PosMaster } from "../entities/PosMaster";
import { Command } from "../entities/Command";
import {
checkCommandType,
removePostMasterAct,
removeProfileInOrganize,
setLogDataDiff,
} from "../interfaces/utils";
import { CreatePosMasterHistoryOfficer } from "./PositionService";
import { deleteUser } from "../keycloak";
/**
* Input: ข้อมูล 1 endpoint excexute/salary
* (C-PM-13 , C-PM-15 , C-PM-16 /)
*/
export interface SalaryItem {
profileId: string;
amount?: Double | null;
amountSpecial?: Double | null;
positionSalaryAmount?: Double | null;
mouthSalaryAmount?: Double | null;
positionExecutive: string | null;
positionExecutiveField?: string | null;
positionArea?: string | null;
positionType: string | null;
positionLevel: string | null;
commandId?: string | null;
leaveReason?: string | null;
dateLeave?: Date | string | null;
isLeave?: boolean;
orgRoot?: string | null;
orgChild1?: string | null;
orgChild2?: string | null;
orgChild3?: string | null;
orgChild4?: string | null;
officerOrg?: string | null;
dateStart?: Date | string | null;
dateEnd?: Date | string | null;
commandNo: string | null;
commandYear: number | null;
posNo: string | null;
posNoAbb: string | null;
commandDateAffect?: Date | string | null;
commandDateSign?: Date | string | null;
positionName: string | null;
commandCode?: string | null;
commandName?: string | null;
remark: string | null;
refId?: string | null;
}
/**
* Context audit/log ( ExecuteOfficerProfileService)
*/
export interface SalaryExecutionContext {
user: { sub: string; name: string };
req?: any;
}
/**
* Service ProfileSalary + handle leave//
*
* commandType: C-PM-13, 15, 16
*
* - endpoint /org/command/excexute/salary service (thin wrapper)
* - consumer rabbitmq handler service (Linear Flow)
*
* Behavior preserve CommandController.newSalaryAndUpdate
*
* Batch semantics: all-or-nothing transaction (sequential)
* throw rollback batch propagate error ()
* return result success count
*
* Keycloak: operation (deleteUser) transaction preserve behavior
* Keycloak rollback DB rollback Keycloak operation
* Keycloak
*/
export class ExecuteSalaryService {
private commandRepository = AppDataSource.getRepository(Command);
private profileRepository = AppDataSource.getRepository(Profile);
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
/**
* ProfileSalary + handle leave/assistance batch
*
* @returns success/failure
*/
async executeSalary(data: SalaryItem[], ctx: SalaryExecutionContext): Promise<void> {
const commandId = data?.find((x) => x.commandId)?.commandId ?? "unknown";
const commandCode = data?.find((x) => x.commandCode)?.commandCode ?? "unknown";
console.log(
`[ExecuteSalaryService] Starting executeSalary — commandCode: ${commandCode}, commandId: ${commandId}`,
);
console.log(`[ExecuteSalaryService] Request body count: ${data?.length ?? 0}`);
// ─────────────────────────────────────────────────────────────
// Normalize date fields (ผ่าน handler จะได้ string → ต้องแปลงเป็น Date)
// ─────────────────────────────────────────────────────────────
const toDate = (v: any): Date | null => {
if (v == null || v === "") return null;
if (v instanceof Date) return isNaN(v.getTime()) ? null : v;
const d = new Date(v);
return isNaN(d.getTime()) ? null : d;
};
for (const item of data ?? []) {
const it = item as any;
it.dateLeave = toDate(it.dateLeave);
it.dateStart = toDate(it.dateStart);
it.dateEnd = toDate(it.dateEnd);
it.commandDateAffect = toDate(it.commandDateAffect);
it.commandDateSign = toDate(it.commandDateSign);
}
let _posNumCodeSit: string = "";
let _posNumCodeSitAbb: string = "";
const _command = await this.commandRepository.findOne({
relations: ["commandType"],
where: { id: data.find((x) => x.commandId)?.commandId ?? "" },
});
if (_command) {
if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") {
const orgRootDeputy = await this.orgRootRepository.findOne({
where: {
isDeputy: true,
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
relations: ["orgRevision"],
});
_posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร";
_posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป.";
} else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") {
_posNumCodeSit = "กรุงเทพมหานคร";
_posNumCodeSitAbb = "กทม.";
} else {
let _profileAdmin = await this.profileRepository.findOne({
where: {
keycloak: _command?.createdUserId.toString(),
current_holders: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
},
relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"],
});
_posNumCodeSit =
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ??
"";
_posNumCodeSitAbb =
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot
.orgRootShortName ?? "";
}
}
// ─────────────────────────────────────────────────────────────
// Single transaction ครอบทั้ง batch (all-or-nothing)
// ทุกคนใช้ manager ตัวเดียวกัน — คนใด throw จะ rollback ทั้ง batch
// และ propagate error ออกไป (ล้มเหลวทั้งหมด) โดย log error ของคนที่ทำให้ fail ก่อน rethrow
// ─────────────────────────────────────────────────────────────
let successCount = 0;
await AppDataSource.transaction(async (manager) => {
for (const item of data ?? []) {
try {
await this.processOne(item, ctx, manager, _command, _posNumCodeSit, _posNumCodeSitAbb);
} catch (err) {
const reason =
err instanceof HttpError
? err.message
: err instanceof Error
? err.message
: "unexpected error";
console.error(
`[ExecuteSalaryService] Failed commandCode=${commandCode}, commandId=${commandId}, profileId=${item.profileId}: ${reason}`,
err,
);
throw err; // → rollback ทั้ง transaction + propagate เป็น batch failure
}
}
});
}
/**
* 1 transaction (manager)
* save manager.getRepository(...) transaction
* throw rollback + batch ( partial commit)
*
* หมายเหตุ: Keycloak deleteUser transaction rollback
*/
private async processOne(
item: SalaryItem,
ctx: SalaryExecutionContext,
manager: EntityManager,
_command: Command | null,
_posNumCodeSit: string,
_posNumCodeSitAbb: string,
): Promise<void> {
const req = ctx.req;
const profileRepository = manager.getRepository(Profile);
const salaryRepo = manager.getRepository(ProfileSalary);
const salaryHistoryRepo = manager.getRepository(ProfileSalaryHistory);
const posMasterRepository = manager.getRepository(PosMaster);
const assistanceRepository = manager.getRepository(ProfileAssistance);
const assistanceHistoryRepository = manager.getRepository(ProfileAssistanceHistory);
const profile: any = await profileRepository.findOne({
where: { id: item.profileId },
relations: {
roleKeycloaks: true,
posType: true,
posLevel: true,
},
});
if (!profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้");
}
const posMaster: any = await posMasterRepository.findOne({
where: {
current_holderId: item.profileId,
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
},
relations: {
orgRevision: true,
orgRoot: true,
orgChild1: true,
orgChild2: true,
orgChild3: true,
orgChild4: true,
},
});
const orgRevisionRef = posMaster ? posMaster.id : null;
const orgRootRef = orgRevisionRef?.orgRoot ?? null;
const orgChild1Ref = orgRevisionRef?.orgChild1 ?? null;
const orgChild2Ref = orgRevisionRef?.orgChild2 ?? null;
const orgChild3Ref = orgRevisionRef?.orgChild3 ?? null;
const orgChild4Ref = orgRevisionRef?.orgChild4 ?? null;
//ลบตำแหน่งที่รักษาการแทน
const code = _command?.commandType?.code;
if (code && ["C-PM-13"].includes(code)) {
// await (เดิมไม่ await = fire-and-forget bug) + ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction
await removePostMasterAct(profile.id, manager);
}
let _commandYear = item.commandYear;
if (item.commandYear) {
_commandYear = item.commandYear > 2500 ? item.commandYear : item.commandYear + 543;
}
const dest_item = await salaryRepo.findOne({
where: { profileId: item.profileId },
order: { order: "DESC" },
});
const before = null;
const dataSalary = new ProfileSalary();
dataSalary.posNumCodeSit = _posNumCodeSit;
dataSalary.posNumCodeSitAbb = _posNumCodeSitAbb;
const meta = {
order: dest_item == null ? 1 : dest_item.order + 1,
createdUserId: ctx.user.sub,
createdFullName: ctx.user.name,
lastUpdateUserId: ctx.user.sub,
lastUpdateFullName: ctx.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
};
if (item.isLeave != undefined && item.isLeave == true) {
console.log(
`[ExecuteSalaryService] Creating PosMasterHistory — posMasterId: ${orgRevisionRef}, profileId: ${item.profileId}, type: DELETE`,
);
await CreatePosMasterHistoryOfficer(orgRevisionRef, req, "DELETE", null, manager);
await removeProfileInOrganize(profile.id, "OFFICER", manager);
}
const clearProfile = await checkCommandType(String(item.commandId));
const _null: any = null;
if (clearProfile.status) {
// Keycloak deleteUser ทำก่อนเข้า transaction-bound save ด้านล่าง
// (ทำภายใน transaction เดียวกัน เพราะถ้า fail ต้อง rollback DB ด้วย)
// หมายเหตุ: Keycloak ไม่สามารถ rollback ได้ → ถ้า DB rollback หลังจากนี้ Keycloak จะถูกลบไปแล้ว
if (profile.keycloak != null && profile.keycloak != "" && profile.isDelete === false) {
const delUserKeycloak = await deleteUser(profile.keycloak);
if (delUserKeycloak) {
// Task #228
// profile.keycloak = _null;
profile.roleKeycloaks = [];
profile.isActive = false;
profile.isDelete = true;
}
}
profile.isLeave = item.isLeave;
profile.leaveCommandId = item.commandId ?? _null;
profile.leaveCommandNo = `${item.commandNo}/${_commandYear}`;
profile.leaveRemark = clearProfile.leaveRemark ?? _null;
profile.leaveDate = item.commandDateAffect ?? _null;
profile.leaveType = clearProfile.LeaveType ?? _null;
//ออกจากราชการ ไม่ต้องลบตำแหน่งในทะเบียน (issue #1516)
// profile.position = _null;
// profile.posTypeId = _null;
// profile.posLevelId = _null;
profile.leaveReason = item.leaveReason ?? _null;
profile.dateLeave = item.dateLeave ?? _null;
profile.amount = item.amount ?? _null;
profile.amountSpecial = item.amountSpecial ?? _null;
await profileRepository.save(profile, { data: req });
// if (profile.id) {
// await this.keycloakAttributeService.clearOrgDnaAttributes(
// [profile.id],
// "PROFILE",
// );
// }
}
Object.assign(dataSalary, { ...item, ...meta });
const history = new ProfileSalaryHistory();
Object.assign(history, { ...dataSalary, id: undefined });
await salaryRepo.save(dataSalary, { data: req });
setLogDataDiff(req, { before, after: dataSalary });
history.profileSalaryId = dataSalary.id;
await salaryHistoryRepo.save(history, { data: req });
if (_command) {
if (["C-PM-15", "C-PM-16"].includes(_command.commandType.code)) {
// ประวัติคำสั่งให้ช่วยราชการ
const dataAssis = new ProfileAssistance();
const metaAssis = {
profileId: item.profileId,
agency: item.officerOrg,
dateStart: item.dateStart,
dateEnd: item.dateEnd,
commandNo: `${item.commandNo}/${_commandYear}`,
commandName: item.commandName,
refId: item.refId,
refCommandDate: new Date(),
commandId: item.commandId,
createdUserId: ctx.user.sub,
createdFullName: ctx.user.name,
lastUpdateUserId: ctx.user.sub,
lastUpdateFullName: ctx.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
status: _command.commandType.code == "C-PM-15" ? "PENDING" : "DONE",
};
Object.assign(dataAssis, metaAssis);
const historyAssis = new ProfileAssistanceHistory();
Object.assign(historyAssis, { ...dataAssis, id: undefined });
await assistanceRepository.save(dataAssis);
historyAssis.profileAssistanceId = dataAssis.id;
await assistanceHistoryRepository.save(historyAssis);
}
// Task #2190
else if (_command.commandType.code == "C-PM-13") {
let organizeName = "";
if (orgRootRef) {
const names = [
orgChild4Ref?.orgChild4Name,
orgChild3Ref?.orgChild3Name,
orgChild2Ref?.orgChild2Name,
orgChild1Ref?.orgChild1Name,
orgRootRef?.orgRootName,
].filter(Boolean);
organizeName = names.join(" ");
}
}
}
console.log(
`[ExecuteSalaryService] Completed processOne — profileId: ${item.profileId}`,
);
}
}

View file

@ -12,6 +12,7 @@ import { Position } from "../entities/Position";
import { ProfileEducation } from "../entities/ProfileEducation"; import { ProfileEducation } from "../entities/ProfileEducation";
import { RequestWithUser } from "../middlewares/user"; import { RequestWithUser } from "../middlewares/user";
import { Profile } from "../entities/Profile"; import { Profile } from "../entities/Profile";
import { ProfileEmployee } from "../entities/ProfileEmployee";
/** /**
* function * function
@ -216,74 +217,105 @@ export async function CreatePosMasterHistoryEmployee(
posMasterId: string, posMasterId: string,
request: RequestWithUser | null, request: RequestWithUser | null,
type?: string | null, type?: string | null,
manager?: EntityManager,
): Promise<boolean> { ): Promise<boolean> {
try { const execute = async (transactionManager: EntityManager) => {
await AppDataSource.transaction(async (manager) => { const repoPosmaster = transactionManager.getRepository(EmployeePosMaster);
const repoPosmaster = manager.getRepository(EmployeePosMaster); const repoHistory = transactionManager.getRepository(PosMasterEmployeeHistory);
const repoHistory = manager.getRepository(PosMasterEmployeeHistory); const repoProfileEmployee = transactionManager.getRepository(ProfileEmployee);
const pm = await repoPosmaster.findOne({ const pm = await repoPosmaster.findOne({
where: { id: posMasterId }, where: { id: posMasterId },
relations: [ relations: [
"positions", "positions",
"positions.posLevel", "positions.posLevel",
"positions.posType", "positions.posType",
// "positions.posExecutive", // "positions.posExecutive",
"orgRoot", "orgRoot",
"orgChild1", "orgChild1",
"orgChild2", "orgChild2",
"orgChild3", "orgChild3",
"orgChild4", "orgChild4",
"current_holder", "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;
if (!type || type != "DELETE") {
h.profileEmployeeId = pm.current_holder?.id || _null;
h.prefix = pm.current_holder?.prefix || _null;
h.firstName = pm.current_holder?.firstName || _null;
h.lastName = pm.current_holder?.lastName || _null;
h.position = selectedPosition?.positionName ?? _null;
h.posType = selectedPosition?.posType?.posTypeName ?? _null;
h.posLevel = selectedPosition?.posLevel?.posLevelName ?? _null;
}
h.rootDnaId = pm.orgRoot?.ancestorDNA || _null;
h.child1DnaId = pm.orgChild1?.ancestorDNA || _null;
h.child2DnaId = pm.orgChild2?.ancestorDNA || _null;
h.child3DnaId = pm.orgChild3?.ancestorDNA || _null;
h.child4DnaId = pm.orgChild4?.ancestorDNA || _null;
h.posMasterNoPrefix = pm.posMasterNoPrefix ?? _null;
h.posMasterNo = pm.posMasterNo ?? _null;
h.posMasterNoSuffix = pm.posMasterNoSuffix ?? _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);
}); });
if (!pm) return;
if (!pm.ancestorDNA) return;
const _null: any = null;
const h = new PosMasterEmployeeHistory();
const selectedPosition =
pm.positions.length > 0
? pm.positions.find((p) => p.positionIsSelected === true) ?? null
: null;
let position = selectedPosition?.positionName ?? _null;
let posTypeName = selectedPosition?.posType?.posTypeName ?? _null;
let posLevelName = selectedPosition?.posType && selectedPosition?.posLevel
? `${selectedPosition?.posType?.posTypeShortName ?? ""} ${selectedPosition?.posLevel?.posLevelName ?? ""}`.trim()
: _null;
if (pm.isSit && pm.current_holderId) {
const profile = await repoProfileEmployee.findOne({
where: { id: pm.current_holderId },
relations: ["posType", "posLevel"]
});
position = profile?.position ?? _null;
posTypeName = profile?.posType?.posTypeName ?? _null;
posLevelName = profile?.posType && profile?.posLevel
? `${profile?.posType?.posTypeShortName ?? ""} ${profile?.posLevel?.posLevelName ?? ""}`.trim()
: _null;
}
h.ancestorDNA = pm.ancestorDNA;
if (!type || type != "DELETE") {
h.profileEmployeeId = pm.current_holder?.id || _null;
h.prefix = pm.current_holder?.prefix || _null;
h.firstName = pm.current_holder?.firstName || _null;
h.lastName = pm.current_holder?.lastName || _null;
h.position = position;
h.posType = posTypeName;
h.posLevel = posLevelName;
}
h.rootDnaId = pm.orgRoot?.ancestorDNA || _null;
h.child1DnaId = pm.orgChild1?.ancestorDNA || _null;
h.child2DnaId = pm.orgChild2?.ancestorDNA || _null;
h.child3DnaId = pm.orgChild3?.ancestorDNA || _null;
h.child4DnaId = pm.orgChild4?.ancestorDNA || _null;
h.posMasterNoPrefix = pm.posMasterNoPrefix ?? _null;
h.posMasterNo = pm.posMasterNo ?? _null;
h.posMasterNoSuffix = pm.posMasterNoSuffix ?? _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);
};
try {
if (manager) {
await execute(manager);
return true;
}
await AppDataSource.transaction(async (transactionManager) => {
await execute(transactionManager);
});
return true; return true;
} catch (err) { } catch (err) {
if (manager) {
console.error("CreatePosMasterHistoryEmployee error (external transaction):", err);
throw err;
}
console.error("CreatePosMasterHistoryEmployee transaction error:", err); console.error("CreatePosMasterHistoryEmployee transaction error:", err);
return false; return false;
} }
@ -477,10 +509,10 @@ export async function BatchSavePosMasterHistoryOfficer(
const profileChanged = existing && existing.profileId !== op.profileId; const profileChanged = existing && existing.profileId !== op.profileId;
const positionChanged = const positionChanged =
existing && existing &&
existing.position !== op.pm?.position && (existing.position !== op.pm?.position ||
existing.posType !== op.pm?.posType && existing.posType !== op.pm?.posType ||
existing.posLevel !== op.pm?.posLevel && existing.posLevel !== op.pm?.posLevel ||
existing.posExecutive !== op.pm?.posExecutive; existing.posExecutive !== op.pm?.posExecutive);
// ถ้าไม่มี record เดิม หรือ profile เปลี่ยน หรือ position เปลี่ยน ให้สร้าง record ใหม่ // ถ้าไม่มี record เดิม หรือ profile เปลี่ยน หรือ position เปลี่ยน ให้สร้าง record ใหม่
if (shouldInsert || profileChanged || positionChanged) { if (shouldInsert || profileChanged || positionChanged) {

View file

@ -29,6 +29,16 @@ import { sendWebSocket } from "./webSocket";
import { PayloadSendNoti } from "../interfaces/utils"; import { PayloadSendNoti } from "../interfaces/utils";
import { PermissionProfile } from "../entities/PermissionProfile"; import { PermissionProfile } from "../entities/PermissionProfile";
import { PosMasterHistory } from "../entities/PosMasterHistory"; import { PosMasterHistory } from "../entities/PosMasterHistory";
import { ExecuteOfficerProfileService } from "./ExecuteOfficerProfileService";
import { ExecuteSalaryService } from "./ExecuteSalaryService";
import { ExecuteSalaryCurrentService } from "./ExecuteSalaryCurrentService";
import { ExecuteSalaryEmployeeCurrentService } from "./ExecuteSalaryEmployeeCurrentService";
import { ExecuteSalaryLeaveService } from "./ExecuteSalaryLeaveService";
import { ExecuteSalaryEmployeeLeaveService } from "./ExecuteSalaryEmployeeLeaveService";
import { ExecuteSalaryLeaveDisciplineService } from "./ExecuteSalaryLeaveDisciplineService";
import { ExecuteOrgCommandService } from "./ExecuteOrgCommandService";
import { ExecuteSalaryProbationService } from "./ExecuteSalaryProbationService";
import { ExecuteSalaryReportService } from "./ExecuteSalaryReportService";
const redis = require("redis"); const redis = require("redis");
const REDIS_HOST = process.env.REDIS_HOST; const REDIS_HOST = process.env.REDIS_HOST;
@ -320,13 +330,249 @@ async function handler(msg: amqp.ConsumeMessage): Promise<boolean> {
20, 20,
); );
for (const chunk of chunks) { // ─────────────────────────────────────────────────────────────
await new CallAPI().PostData( // Linear Flow
{ headers: { authorization: token } }, // รับ resultData จาก .NET แล้วเรียก Service ตรงๆ ตาม commandType (ไม่ผ่าน HTTP loopback)
path + "/excecute", // - ExecuteOfficerProfileService : C-PM-01, 02, 14 (บรรจุ/รับโอน)
{ refIds: chunk }, // - ExecuteSalaryCurrentService : C-PM-03, 04, 05, 06, 07, 39, 47 (แต่งตั้ง-เลื่อน-ย้าย)
false, // - ExecuteSalaryEmployeeCurrentService : C-PM-22, 24 (ลูกจ้าง ปรับระดับชั้นงาน-ย้าย)
); // - ExecuteSalaryService : C-PM-13, 15, 16 (ให้โอน/ให้ช่วยราชการ/ให้กลับเข้าราชการ)
// - ExecuteSalaryLeaveService : C-PM-08, 09, 17, 18, 41, 48 (ข้าราชการ leave/กลับเข้าราชการ)
// - ExecuteSalaryEmployeeLeaveService : C-PM-23, 42, 43 (ลูกจ้าง leave)
// - ExecuteSalaryLeaveDisciplineService : C-PM-19, 20, 25, 26, 27, 28, 29, 30, 31, 32 (คำสั่งวินัย)
// - ExecuteOrgCommandService : C-PM-21, 38, 40 (org-self — path ชี้กลับ org เอง
// เรียก Service ตรงๆ ไม่ผ่าน HTTP loopback เพราะ PostData(path+"/excecute") = ยิงเข้าตัว)
// - คำสั่งอื่น ยังใช้ Circular Flow เดิม
// ─────────────────────────────────────────────────────────────
const code = command.commandType?.code;
const isOfficerProfile = ["C-PM-01", "C-PM-02", "C-PM-14"].includes(code);
const isSalaryCurrent = ["C-PM-03", "C-PM-04", "C-PM-05", "C-PM-06", "C-PM-07", "C-PM-39", "C-PM-47"].includes(code);
const isSalaryEmployeeCurrent = ["C-PM-22", "C-PM-24"].includes(code);
const isSalary = ["C-PM-13", "C-PM-15", "C-PM-16"].includes(code);
const isSalaryLeave = ["C-PM-08", "C-PM-09", "C-PM-17", "C-PM-18", "C-PM-41", "C-PM-48"].includes(code);
const isSalaryEmployeeLeave = ["C-PM-23", "C-PM-42", "C-PM-43"].includes(code);
const isSalaryLeaveDiscipline = ["C-PM-19", "C-PM-20", "C-PM-25", "C-PM-26", "C-PM-27", "C-PM-28",
"C-PM-29", "C-PM-30", "C-PM-31", "C-PM-32",
].includes(code);
// C-PM-21/38/40: path ชี้กลับ org เอง (ไม่ใช่ .NET) → ต้องเรียก Service ตรงๆ ไม่ผ่าน loopback
const isCommand21 = code === "C-PM-21";
const isCommand38 = code === "C-PM-38";
const isCommand40 = code === "C-PM-40";
const isOrgSelfLinear = isCommand21 || isCommand38 || isCommand40;
// C-PM-10/11/12: ยิงไป probation service — เป็น branch แยก (ไม่ใช่ .NET linear flow)
// - C-PM-10: fire-only (probation update ในตัวเอง ไม่มี org-side action)
// - C-PM-11/12: probation return salary data → เรียก ExecuteSalaryProbationService ตรงๆ
const isProbation = ["C-PM-10", "C-PM-11", "C-PM-12"].includes(code);
// C-PM-33/34/35/45 (officer) + C-PM-36/37/46 (employee): ยิงไป salary service
// เป็น branch แยก — salary return salary data → เรียก ExecuteSalaryReportService ตรงๆ
const isSalaryServiceOfficer = ["C-PM-33", "C-PM-34", "C-PM-35", "C-PM-45"].includes(code);
const isSalaryServiceEmployee = ["C-PM-36", "C-PM-37", "C-PM-46"].includes(code);
const isSalaryService = isSalaryServiceOfficer || isSalaryServiceEmployee;
const isLinearFlow =
isOfficerProfile ||
isSalaryCurrent ||
isSalaryEmployeeCurrent ||
isSalary ||
isSalaryLeave ||
isSalaryEmployeeLeave ||
isSalaryLeaveDiscipline;
// Org-self (C-PM-21/38/40): เรียก Service ตรงๆ (Linear Flow / ทำต่อ) ไม่ผ่าน HTTP loopback
// เพราะ path ของ command เหล่านี้ชี้กลับ org เอง → PostData(path + "/excecute") = ยิงเข้าตัว
if (isOrgSelfLinear) {
console.log(`[AMQ] Linear Flow org-self (${code}) — เรียก Service ตรงๆ (no loopback)`);
const pseudoReq = { headers: { authorization: token }, user };
const ctx = {
user: { sub: user?.sub ?? "system", name: user?.name ?? "System" },
req: pseudoReq,
};
const flatRefIds = chunks.flat();
if (isCommand21) {
await new ExecuteOrgCommandService().executeCommand21Employee(flatRefIds, ctx);
} else if (isCommand38) {
await new ExecuteOrgCommandService().executeCommand38Officer(flatRefIds, ctx);
} else if (isCommand40) {
await new ExecuteOrgCommandService().executeCommand40Officer(flatRefIds, ctx);
}
console.log(`[AMQ] Processed ${flatRefIds.length} items via ExecuteOrgCommandService (${code})`);
} else if (isProbation) {
// Probation Linear Flow (C-PM-10/11/12)
// - C-PM-10: fire-only — probation อัปเดต appoint ในตัวเอง ไม่มี org-side action
// - C-PM-11/12: fire → probation return salary data → route ไป ExecuteSalaryProbationService
// แทนการ callback เข้า org (Circular Flow เดิม)
console.log(`[AMQ] Probation Linear Flow (${code})`);
if (code === "C-PM-10") {
for (const chunk of chunks) {
await new CallAPI().PostData(
{ headers: { authorization: token } },
path + "/excecute",
{ refIds: chunk },
false,
);
}
console.log(`[AMQ] C-PM-10 fire-only — no org-side action`);
} else {
let resultData: any[] = [];
for (const chunk of chunks) {
const res = await new CallAPI().PostData(
{ headers: { authorization: token } },
path + "/excecute",
{ refIds: chunk },
false,
);
// รองรับทั้ง array และ { data: [...] } (contract ของ probation หลัง Linear Flow)
if (res && Array.isArray(res.data)) {
resultData.push(...res.data);
} else if (Array.isArray(res)) {
resultData.push(...res);
}
}
if (resultData.length > 0) {
const pseudoReq = { headers: { authorization: token }, user };
const ctx = {
user: { sub: user?.sub ?? "system", name: user?.name ?? "System" },
req: pseudoReq,
};
if (code === "C-PM-11") {
await new ExecuteSalaryProbationService().executeProbationPass(resultData, ctx);
console.log(
`[AMQ] Processed ${resultData.length} profiles via ExecuteSalaryProbationService (C-PM-11)`,
);
} else if (code === "C-PM-12") {
await new ExecuteSalaryProbationService().executeProbationLeave(resultData, ctx);
console.log(
`[AMQ] Processed ${resultData.length} profiles via ExecuteSalaryProbationService (C-PM-12)`,
);
}
}
}
} else if (isSalaryService) {
// Salary Service Linear Flow (C-PM-33/34/35/45 officer, C-PM-36/37/46 employee)
// fire → salary service return salary data → route ไป ExecuteSalaryReportService
// แทนการ callback เข้า /org/profile(/-employee)/salary/update (Circular Flow เดิม)
console.log(`[AMQ] Salary Service Linear Flow (${code})`);
let resultData: any[] = [];
for (const chunk of chunks) {
const res = await new CallAPI().PostData(
{ headers: { authorization: token } },
path + "/excecute",
{ refIds: chunk },
false,
);
// รองรับทั้ง array และ { data: [...] } (contract ของ salary service หลัง Linear Flow)
if (res && Array.isArray(res.data)) {
resultData.push(...res.data);
} else if (Array.isArray(res)) {
resultData.push(...res);
}
}
if (resultData.length > 0) {
const pseudoReq = { headers: { authorization: token }, user };
const ctx = {
user: { sub: user?.sub ?? "system", name: user?.name ?? "System" },
req: pseudoReq,
};
if (isSalaryServiceOfficer) {
await new ExecuteSalaryReportService().executeOfficerSalaryUpdate(resultData, ctx);
console.log(
`[AMQ] Processed ${resultData.length} profiles via ExecuteSalaryReportService (officer)`,
);
} else {
await new ExecuteSalaryReportService().executeEmployeeSalaryUpdate(resultData, ctx);
console.log(
`[AMQ] Processed ${resultData.length} profiles via ExecuteSalaryReportService (employee)`,
);
}
}
} else if (isLinearFlow) {
console.log(`[AMQ] Linear Flow (${code})`);
const isCpm32 = code === "C-PM-32";
let resultData: any[] = [];
let resultData1: any[] = []; //เฉพาะ C-PM-32 (ฝั่ง "การพิจารณาลงโทษ")
for (const chunk of chunks) {
const res = await new CallAPI().PostData(
{ headers: { authorization: token } },
path + "/excecute",
{ refIds: chunk },
false,
);
if (isCpm32 && res && !Array.isArray(res)) {
// C-PM-32: response เป็น object { data, data1 } → แยก 2 track
console.log(
`[AMQ] C-PM-32 split response — data: ${res.data?.length ?? 0}, data1: ${res.data1?.length ?? 0}`,
);
if (Array.isArray(res.data)) resultData.push(...res.data);
if (Array.isArray(res.data1)) resultData1.push(...res.data1);
} else if (Array.isArray(res)) {
console.log(`[AMQ] Push result data (${res.length})`);
resultData.push(...res);
}
}
console.log(`[AMQ] Received ${resultData.length} profiles from .NET (${code})`);
// Route ไป service ที่ถูกต้องตาม commandType
if (resultData.length > 0 || resultData1.length > 0) {
// สร้าง pseudo-req สำหรับ setLogDataDiff/save({data: req})
const pseudoReq = {
headers: { authorization: token },
user,
};
const ctx = {
user: { sub: user?.sub ?? "system", name: user?.name ?? "System" },
req: pseudoReq,
};
if (isOfficerProfile) {
await new ExecuteOfficerProfileService().executeCreateOfficerProfile(resultData, ctx);
console.log(`[AMQ] Processed ${resultData.length} profiles via ExecuteOfficerProfileService`);
} else if (isSalaryCurrent) {
await new ExecuteSalaryCurrentService().executeSalaryCurrent(resultData, ctx);
console.log(`[AMQ] Processed ${resultData.length} profiles via ExecuteSalaryCurrentService`);
} else if (isSalaryEmployeeCurrent) {
await new ExecuteSalaryEmployeeCurrentService().executeSalaryEmployeeCurrent(resultData, ctx);
console.log(`[AMQ] Processed ${resultData.length} profiles via ExecuteSalaryEmployeeCurrentService`);
} else if (isSalary) {
await new ExecuteSalaryService().executeSalary(resultData, ctx);
console.log(`[AMQ] Processed ${resultData.length} profiles via ExecuteSalaryService`);
} else if (isSalaryLeave) {
await new ExecuteSalaryLeaveService().executeSalaryLeave(resultData, ctx);
console.log(`[AMQ] Processed ${resultData.length} profiles via ExecuteSalaryLeaveService`);
} else if (isSalaryEmployeeLeave) {
await new ExecuteSalaryEmployeeLeaveService().executeSalaryEmployeeLeave(resultData, ctx);
console.log(`[AMQ] Processed ${resultData.length} profiles via ExecuteSalaryEmployeeLeaveService`);
} else if (isSalaryLeaveDiscipline) {
// C-PM-32 (คำสั่งยุติเรื่อง): response เป็น object { data, data1 }
// profileId เดียวกันอาจอยู่ในทั้ง 2 track → ต้องส่งให้ org แยก 2 ครั้ง ห้าม merge
if (resultData.length > 0) {
await new ExecuteSalaryLeaveDisciplineService().executeSalaryLeaveDiscipline(resultData, ctx);
console.log(
`[AMQ] Processed ${resultData.length} profiles via ExecuteSalaryLeaveDisciplineService`,
);
}
if (isCpm32 && resultData1.length > 0) {
await new ExecuteSalaryLeaveDisciplineService().executeSalaryLeaveDiscipline(resultData1, ctx);
console.log(
`[AMQ] Processed resultData1: ${resultData1.length} profiles via ExecuteSalaryLeaveDisciplineService`,
);
}
}
}
} else {
console.log(`[AMQ] Circular Flow (${code})`);
for (const chunk of chunks) {
await new CallAPI().PostData(
{ headers: { authorization: token } },
path + "/excecute",
{ refIds: chunk },
false,
);
}
} }
Object.assign(command, { status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt }); Object.assign(command, { status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt });
@ -695,7 +941,19 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
where: { where: {
orgRevisionId: orgRevisionPublish.id, orgRevisionId: orgRevisionPublish.id,
}, },
select: ["id", "current_holderId", "ancestorDNA"], select: [
"id",
"current_holderId",
"ancestorDNA",
"posMasterNo",
"posMasterNoPrefix",
"posMasterNoSuffix",
"orgRootId",
"orgChild1Id",
"orgChild2Id",
"orgChild3Id",
"orgChild4Id",
],
}); });
// Task #2160 ดึง posMasterAssign ของ revision เดิม // Task #2160 ดึง posMasterAssign ของ revision เดิม
@ -861,7 +1119,21 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
const newHolderId = item?.next_holderId; const newHolderId = item?.next_holderId;
const isHolderChanged = oldHolderId !== newHolderId; const isHolderChanged = oldHolderId !== newHolderId;
if (isHolderChanged) { // เช็คว่า holder เดิม แต่ตำแหน่งเปลี่ยน
const isSameHolder = oldHolderId === newHolderId && oldHolderId != null && newHolderId != null;
const isPositionChanged =
isSameHolder &&
oldPm &&
(oldPm.posMasterNo !== item.posMasterNo ||
oldPm.posMasterNoPrefix !== item.posMasterNoPrefix ||
oldPm.posMasterNoSuffix !== item.posMasterNoSuffix ||
oldPm.orgRootId !== item.orgRoot?.id ||
oldPm.orgChild1Id !== item.orgChild1?.id ||
oldPm.orgChild2Id !== item.orgChild2?.id ||
oldPm.orgChild3Id !== item.orgChild3?.id ||
oldPm.orgChild4Id !== item.orgChild4?.id);
if (isHolderChanged || isPositionChanged) {
const nextHolderProfile = const nextHolderProfile =
item.next_holderId != null && item.next_holderId !== "" item.next_holderId != null && item.next_holderId !== ""
? profilesMap.get(item.next_holderId) ? profilesMap.get(item.next_holderId)
@ -1711,6 +1983,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
} }
async function clearMenuAndRoleCache(): Promise<void> { async function clearMenuAndRoleCache(): Promise<void> {
console.log("[AMQ] clearMenuAndRoleCache: Starting...");
const redisClient = redis.createClient({ const redisClient = redis.createClient({
host: REDIS_HOST, host: REDIS_HOST,
port: REDIS_PORT, port: REDIS_PORT,
@ -1720,35 +1993,28 @@ async function clearMenuAndRoleCache(): Promise<void> {
const delAsync = promisify(redisClient.del).bind(redisClient); const delAsync = promisify(redisClient.del).bind(redisClient);
try { try {
const menuKeys = await keysAsync("menu_*"); // Clear menu and role cache (patterns that affect menu display)
if (menuKeys.length > 0) { const menuRolePatterns = ["menu_*", "role_*"];
await delAsync(...menuKeys);
console.log(`[AMQ] Cleared ${menuKeys.length} menu cache keys`); for (const pattern of menuRolePatterns) {
console.log(`[AMQ] Checking pattern: ${pattern}`);
const keys = await keysAsync(pattern);
console.log(`[AMQ] Found ${keys.length} keys for pattern: ${pattern}`);
if (keys.length > 0) {
// Delete in chunks of 1000 to avoid argument limit
const chunkSize = 1000;
for (let i = 0; i < keys.length; i += chunkSize) {
const chunk = keys.slice(i, i + chunkSize);
await delAsync(...chunk);
}
console.log(`[AMQ] Cleared ${keys.length} cache keys for pattern: ${pattern}`);
} else {
console.log(`[AMQ] No keys found for pattern: ${pattern}`);
}
} }
console.log("[AMQ] clearMenuAndRoleCache: Completed successfully");
const roleKeys = await keysAsync("role_*"); } catch (error) {
if (roleKeys.length > 0) { console.error("[AMQ] clearMenuAndRoleCache ERROR:", error);
await delAsync(...roleKeys);
console.log(`[AMQ] Cleared ${roleKeys.length} role cache keys`);
}
// const posMasterKeys = await keysAsync("posMaster_*");
// if (posMasterKeys.length > 0) {
// await delAsync(...posMasterKeys);
// console.log(`[AMQ] Cleared ${posMasterKeys.length} posMaster cache keys`);
// }
// const userKeys = await keysAsync("user_*");
// if (userKeys.length > 0) {
// await delAsync(...userKeys);
// console.log(`[AMQ] Cleared ${userKeys.length} user cache keys`);
// }
// const orgKeys = await keysAsync("org_*");
// if (orgKeys.length > 0) {
// await delAsync(...orgKeys);
// console.log(`[AMQ] Cleared ${orgKeys.length} org cache keys`);
// }
} finally { } finally {
redisClient.quit(); redisClient.quit();
} }