hrms-api-org/src/services/ExecuteSalaryCurrentService.ts

432 lines
19 KiB
TypeScript
Raw Normal View History

2026-06-18 18:29:03 +07:00
import { Double } 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
*/
export class ExecuteSalaryCurrentService {
private commandRepository = AppDataSource.getRepository(Command);
private profileRepository = AppDataSource.getRepository(Profile);
private salaryRepo = AppDataSource.getRepository(ProfileSalary);
private salaryHistoryRepo = AppDataSource.getRepository(ProfileSalaryHistory);
private posMasterRepository = AppDataSource.getRepository(PosMaster);
private positionRepository = AppDataSource.getRepository(Position);
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
/**
* ProfileSalary +
*/
async executeSalaryCurrent(
data: SalaryCurrentItem[],
ctx: SalaryCurrentExecutionContext,
): Promise<void> {
console.log("[ExecuteSalaryCurrentService] Starting executeSalaryCurrent");
console.log("[ExecuteSalaryCurrentService] Request body count:", data?.length);
const req = ctx.req;
// ─────────────────────────────────────────────────────────────
// 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 ?? "";
}
}
await Promise.all(
data.map(async (item) => {
const profile: any = await this.profileRepository.findOneBy({ id: item.profileId });
if (!profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้");
}
let _null: any = null;
const dest_item = await this.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 this.salaryRepo.save(dataSalary, { data: req });
setLogDataDiff(req, { before, after: dataSalary });
history.commandId = item.commandId ?? _null;
history.profileSalaryId = dataSalary.id;
await this.salaryHistoryRepo.save(history, { data: req });
// STEP 1: หา posMaster ที่จะใช้งานตาม id ที่ส่งมา
let posMaster = await this.posMasterRepository.findOne({
where: { id: item.posmasterId },
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 this.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 == null) {
console.error(
`[ExecuteSalaryCurrentService] PosMaster not found - posMasterId: ${item.posmasterId}, `,
);
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้");
}
const posMasterOld = await this.posMasterRepository.findOne({
where: {
current_holderId: item.profileId,
orgRevisionId: posMaster.orgRevisionId,
},
});
if (posMasterOld != null) {
posMasterOld.current_holderId = null;
posMasterOld.lastUpdatedAt = new Date();
}
const positionOld = await this.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 this.positionRepository.save(positionOld);
}
const checkPosition = await this.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 this.positionRepository.save(clearPosition);
}
posMaster.current_holderId = item.profileId;
posMaster.lastUpdatedAt = new Date();
// posMaster.conditionReason = _null;
// posMaster.isCondition = false;
if (posMasterOld != null) {
await this.posMasterRepository.save(posMasterOld);
await CreatePosMasterHistoryOfficer(posMasterOld.id, req);
}
await this.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;
// ═══════════════════════════════════════════════════════════
// CONDITION 1: เช็คจาก positionId ตรง
// ═══════════════════════════════════════════════════════════
if (item.positionId) {
const positionById = await this.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.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 this.positionRepository.findOne({
where: whereCondition,
relations: ["posExecutive"],
order: { orderNo: "ASC" },
});
if (positionBy7Fields) {
positionNew = positionBy7Fields;
}
}
// ═══════════════════════════════════════════════════════════
// CONDITION 3: Match 3 ฟิลด์ (ถ้า Condition 2 ไม่ match)
// ═══════════════════════════════════════════════════════════
if (!positionNew && item.positionName && posTypeId && posLevelId) {
const positionBy3Fields = await this.positionRepository.findOne({
where: {
posMasterId: posMaster.id,
positionName: item.positionName,
posTypeId: posTypeId,
posLevelId: posLevelId,
},
relations: ["posExecutive"],
order: { orderNo: "ASC" },
});
if (positionBy3Fields) {
positionNew = positionBy3Fields;
}
}
// // ═══════════════════════════════════════════════════════════
// // FALLBACK: ถ้าทั้ง 3 ไม่ match ให้เลือก position แรกใน posMaster
// // ═══════════════════════════════════════════════════════════
// if (!positionNew) {
// const fallbackPositions = await this.positionRepository.find({
// where: {
// posMasterId: posMaster.id,
// },
// relations: ["posExecutive"],
// order: {
// orderNo: "ASC",
// },
// take: 1,
// });
// if (fallbackPositions.length > 0) {
// positionNew = fallbackPositions[0];
// }
// }
// ถ้าไม่ใช่ตำแหน่งนั่งทับ (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 this.profileRepository.save(profile);
await this.positionRepository.save(positionNew);
}
await CreatePosMasterHistoryOfficer(posMaster.id, req);
}),
);
console.log("[ExecuteSalaryCurrentService] executeSalaryCurrent completed successfully");
}
}