Merge branch 'develop' into adiDev

# Conflicts:
#	src/controllers/SalaryPeriodController.ts
This commit is contained in:
AdisakKanthawilang 2025-08-08 16:32:11 +07:00
commit 91965f170f

View file

@ -1696,8 +1696,7 @@ export class SalaryPeriodController extends Controller {
const result = salaryProfile.map((profile) => ({ const result = salaryProfile.map((profile) => ({
...profile, ...profile,
posExecutive: posExecutive: profile.positionExecutiveField
profile.positionExecutiveField
? `${profile.posExecutive}(${profile.positionExecutiveField})` ? `${profile.posExecutive}(${profile.positionExecutiveField})`
: profile.posExecutive ?? null, : profile.posExecutive ?? null,
})); }));
@ -2651,19 +2650,19 @@ export class SalaryPeriodController extends Controller {
let revisionId = await new CallAPI().GetData(request, "/org/unauthorize/revision/latest"); let revisionId = await new CallAPI().GetData(request, "/org/unauthorize/revision/latest");
let _null: any = null; let _null: any = null;
const beforeSalaryPeriod = structuredClone(salaryPeriod); // const beforeSalaryPeriod = structuredClone(salaryPeriod);
salaryPeriod.revisionId = revisionId; salaryPeriod.revisionId = revisionId;
salaryPeriod.lastUpdateUserId = request.user.sub; salaryPeriod.lastUpdateUserId = request.user.sub;
salaryPeriod.lastUpdateFullName = request.user.name; salaryPeriod.lastUpdateFullName = request.user.name;
salaryPeriod.lastUpdatedAt = new Date(); salaryPeriod.lastUpdatedAt = new Date();
await this.salaryPeriodRepository.save(salaryPeriod, { data: request }); await this.salaryPeriodRepository.save(salaryPeriod, { data: request });
setLogDataDiff(request, { before: beforeSalaryPeriod, after: salaryPeriod }); // setLogDataDiff(request, { before: beforeSalaryPeriod, after: salaryPeriod });
await Promise.all( await Promise.all(
orgs.map(async (root: any) => { orgs.map(async (root: any) => {
let salaryOrgNew = Object.assign(new SalaryOrg()); let salaryOrgNew = Object.assign(new SalaryOrg());
delete salaryOrgNew.id; delete salaryOrgNew.id;
const beforeSalaryOrgNew = structuredClone(salaryOrgNew); // const beforeSalaryOrgNew = structuredClone(salaryOrgNew);
salaryOrgNew.salaryPeriodId = salaryPeriod.id; salaryOrgNew.salaryPeriodId = salaryPeriod.id;
salaryOrgNew.status = "PENDING"; salaryOrgNew.status = "PENDING";
@ -2680,13 +2679,13 @@ export class SalaryPeriodController extends Controller {
salaryOrgNew.lastUpdatedAt = new Date(); salaryOrgNew.lastUpdatedAt = new Date();
salaryOrgNew.group = "GROUP1"; salaryOrgNew.group = "GROUP1";
await this.salaryOrgRepository.save(salaryOrgNew, { data: request }); await this.salaryOrgRepository.save(salaryOrgNew, { data: request });
setLogDataDiff(request, { before: beforeSalaryOrgNew, after: salaryOrgNew }); // setLogDataDiff(request, { before: beforeSalaryOrgNew, after: salaryOrgNew });
console.log(`✅ [SNAP: ${snapshot}] บันทึก salaryOrgNew: ${salaryOrgNew.id}`); console.log(`✅ [SNAP: ${snapshot}] บันทึก salaryOrgNew: ${salaryOrgNew.id}`);
if (salaryPeriod.period != "SPECIAL") { if (salaryPeriod.period != "SPECIAL") {
delete salaryOrgNew.id; delete salaryOrgNew.id;
salaryOrgNew.group = "GROUP2"; salaryOrgNew.group = "GROUP2";
await this.salaryOrgRepository.save(salaryOrgNew, { data: request }); await this.salaryOrgRepository.save(salaryOrgNew, { data: request });
setLogDataDiff(request, { before: beforeSalaryOrgNew, after: salaryOrgNew }); // setLogDataDiff(request, { before: beforeSalaryOrgNew, after: salaryOrgNew });
} }
}), }),
); );
@ -2694,7 +2693,7 @@ export class SalaryPeriodController extends Controller {
await Promise.all( await Promise.all(
orgs.map(async (root: any) => { orgs.map(async (root: any) => {
let salaryOrgNew = Object.assign(new SalaryOrgEmployee()); let salaryOrgNew = Object.assign(new SalaryOrgEmployee());
const beforeSalaryOrgNew = structuredClone(salaryOrgNew); // const beforeSalaryOrgNew = structuredClone(salaryOrgNew);
salaryOrgNew.salaryPeriodId = salaryPeriod.id; salaryOrgNew.salaryPeriodId = salaryPeriod.id;
salaryOrgNew.status = "PENDING"; salaryOrgNew.status = "PENDING";
@ -2711,13 +2710,13 @@ export class SalaryPeriodController extends Controller {
salaryOrgNew.createdAt = new Date(); salaryOrgNew.createdAt = new Date();
salaryOrgNew.lastUpdatedAt = new Date(); salaryOrgNew.lastUpdatedAt = new Date();
await this.salaryOrgEmployeeRepository.save(salaryOrgNew, { data: request }); await this.salaryOrgEmployeeRepository.save(salaryOrgNew, { data: request });
setLogDataDiff(request, { before: beforeSalaryOrgNew, after: salaryOrgNew }); // setLogDataDiff(request, { before: beforeSalaryOrgNew, after: salaryOrgNew });
console.log(`✅ [SNAP: ${snapshot}] บันทึก salaryOrgEmployeeNew: ${salaryOrgNew.id}`); console.log(`✅ [SNAP: ${snapshot}] บันทึก salaryOrgEmployeeNew: ${salaryOrgNew.id}`);
if (salaryPeriod.period != "SPECIAL") { if (salaryPeriod.period != "SPECIAL") {
delete salaryOrgNew.id; delete salaryOrgNew.id;
salaryOrgNew.group = "GROUP2"; salaryOrgNew.group = "GROUP2";
await this.salaryOrgEmployeeRepository.save(salaryOrgNew, { data: request }); await this.salaryOrgEmployeeRepository.save(salaryOrgNew, { data: request });
setLogDataDiff(request, { before: beforeSalaryOrgNew, after: salaryOrgNew }); // setLogDataDiff(request, { before: beforeSalaryOrgNew, after: salaryOrgNew });
} }
}), }),
); );
@ -2728,7 +2727,7 @@ export class SalaryPeriodController extends Controller {
where: { salaryPeriodId: salaryPeriod.id, snapshot }, where: { salaryPeriodId: salaryPeriod.id, snapshot },
}); });
const salaryOrgMap = new Map( const salaryOrgMap = new Map(
salaryOrgList.map((org) => [`${org.rootId}-${org.group}`, org]) salaryOrgList.map((org) => [`${org.rootId}-${org.group}`, org]),
); );
console.timeEnd("⏱ Step1: Load SalaryOrg (New)"); console.timeEnd("⏱ Step1: Load SalaryOrg (New)");
@ -2742,9 +2741,7 @@ export class SalaryPeriodController extends Controller {
.andWhere("org.snapshot = :snapshot", { snapshot: "SNAP1" }) .andWhere("org.snapshot = :snapshot", { snapshot: "SNAP1" })
.getMany(); .getMany();
salaryOldMap = new Map( salaryOldMap = new Map(salaryOldList.map((item) => [item.citizenId, item]));
salaryOldList.map((item) => [item.citizenId, item])
);
} }
console.timeEnd("⏱ Step2: Load SalaryProfileOld (SNAP1)"); console.timeEnd("⏱ Step2: Load SalaryProfileOld (SNAP1)");
@ -2798,8 +2795,8 @@ export class SalaryPeriodController extends Controller {
salaryProfileNew.isGood = salaryOld?.isGood ?? false; salaryProfileNew.isGood = salaryOld?.isGood ?? false;
} }
const beforeSalaryProfileNew = structuredClone(salaryProfileNew); // const beforeSalaryProfileNew = structuredClone(salaryProfileNew);
setLogDataDiff(request, { before: beforeSalaryProfileNew, after: salaryProfileNew }); // setLogDataDiff(request, { before: beforeSalaryProfileNew, after: salaryProfileNew });
allProfilesToSave.push(salaryProfileNew); allProfilesToSave.push(salaryProfileNew);
} }
@ -2837,7 +2834,7 @@ export class SalaryPeriodController extends Controller {
//console.log(`step8`); //console.log(`step8`);
let salaryProfileNew = Object.assign(new SalaryProfileEmployee(), profile); let salaryProfileNew = Object.assign(new SalaryProfileEmployee(), profile);
delete salaryProfileNew.id; delete salaryProfileNew.id;
const beforeSalaryProfileNew = structuredClone(salaryProfileNew); // const beforeSalaryProfileNew = structuredClone(salaryProfileNew);
salaryProfileNew.salaryOrgId = salaryOrgNew.id; salaryProfileNew.salaryOrgId = salaryOrgNew.id;
salaryProfileNew.revisionId = salaryPeriod.revisionId; salaryProfileNew.revisionId = salaryPeriod.revisionId;
@ -2961,7 +2958,9 @@ export class SalaryPeriodController extends Controller {
//เพิ่มคำนวน 15% //เพิ่มคำนวน 15%
_salaryOrg.total = _salaryOrg.salaryProfiles.length; _salaryOrg.total = _salaryOrg.salaryProfiles.length;
_salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); _salaryOrg.fifteenPercent = Math.floor(
(_salaryOrg.salaryProfiles.length * 15) / 100,
);
_salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100;
_salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); _salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100);
} else { } else {
@ -3044,10 +3043,11 @@ export class SalaryPeriodController extends Controller {
//เพิ่มคำนวน 15% //เพิ่มคำนวน 15%
_salaryOrg.total = _salaryOrg.salaryProfiles.length; _salaryOrg.total = _salaryOrg.salaryProfiles.length;
_salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); _salaryOrg.fifteenPercent = Math.floor(
(_salaryOrg.salaryProfiles.length * 15) / 100,
);
_salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100;
_salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); _salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100);
} else { } else {
_salaryOrg.currentAmount = salaryOrgSnap1.currentAmount; _salaryOrg.currentAmount = salaryOrgSnap1.currentAmount;
_salaryOrg.total = salaryOrgSnap1.total; _salaryOrg.total = salaryOrgSnap1.total;
@ -3061,7 +3061,6 @@ export class SalaryPeriodController extends Controller {
_salaryOrg.fifteenPoint = salaryOrgSnap1.fifteenPoint; _salaryOrg.fifteenPoint = salaryOrgSnap1.fifteenPoint;
_salaryOrg.quantityUsed = salaryOrgSnap1.quantityUsed; _salaryOrg.quantityUsed = salaryOrgSnap1.quantityUsed;
_salaryOrg.remainQuota = salaryOrgSnap1.remainQuota; _salaryOrg.remainQuota = salaryOrgSnap1.remainQuota;
} }
} else { } else {
const totalProfile = Extension.sumObjectValues(_salaryOrg.salaryProfiles, "amount"); const totalProfile = Extension.sumObjectValues(_salaryOrg.salaryProfiles, "amount");
@ -3076,7 +3075,6 @@ export class SalaryPeriodController extends Controller {
_salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); _salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100);
_salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100;
_salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); _salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100);
} }
_salaryOrg.createdUserId = request.user.sub; _salaryOrg.createdUserId = request.user.sub;
@ -3092,7 +3090,7 @@ export class SalaryPeriodController extends Controller {
} else if (salaryPeriod.period == "APR") { } else if (salaryPeriod.period == "APR") {
await Promise.all( await Promise.all(
salaryOrgNew.map(async (_salaryOrg: SalaryOrg) => { salaryOrgNew.map(async (_salaryOrg: SalaryOrg) => {
const before_salaryOrg = structuredClone(_salaryOrg); // const before_salaryOrg = structuredClone(_salaryOrg);
if (snapshot == "SNAP2") { if (snapshot == "SNAP2") {
const salaryOrgSnap1 = await this.salaryOrgRepository.findOne({ const salaryOrgSnap1 = await this.salaryOrgRepository.findOne({
@ -3105,7 +3103,9 @@ export class SalaryPeriodController extends Controller {
}); });
if (salaryOrgSnap1 == null) { if (salaryOrgSnap1 == null) {
_salaryOrg.total = _salaryOrg.salaryProfiles.length; _salaryOrg.total = _salaryOrg.salaryProfiles.length;
_salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); _salaryOrg.fifteenPercent = Math.floor(
(_salaryOrg.salaryProfiles.length * 15) / 100,
);
_salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100;
_salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); _salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100);
} else { } else {
@ -3129,12 +3129,12 @@ export class SalaryPeriodController extends Controller {
_salaryOrg.createdAt = new Date(); _salaryOrg.createdAt = new Date();
_salaryOrg.lastUpdatedAt = new Date(); _salaryOrg.lastUpdatedAt = new Date();
await this.salaryOrgRepository.save(_salaryOrg, { data: request }); await this.salaryOrgRepository.save(_salaryOrg, { data: request });
setLogDataDiff(request, { before: before_salaryOrg, after: _salaryOrg }); // setLogDataDiff(request, { before: before_salaryOrg, after: _salaryOrg });
}), }),
); );
await Promise.all( await Promise.all(
salaryOrgEmployeeNew.map(async (_salaryOrg: SalaryOrgEmployee) => { salaryOrgEmployeeNew.map(async (_salaryOrg: SalaryOrgEmployee) => {
const before_salaryOrg = structuredClone(_salaryOrg); // const before_salaryOrg = structuredClone(_salaryOrg);
if (snapshot == "SNAP2") { if (snapshot == "SNAP2") {
const salaryOrgSnap1 = await this.salaryOrgEmployeeRepository.findOne({ const salaryOrgSnap1 = await this.salaryOrgEmployeeRepository.findOne({
@ -3147,7 +3147,9 @@ export class SalaryPeriodController extends Controller {
}); });
if (salaryOrgSnap1 == null) { if (salaryOrgSnap1 == null) {
_salaryOrg.total = _salaryOrg.salaryProfiles.length; _salaryOrg.total = _salaryOrg.salaryProfiles.length;
_salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); _salaryOrg.fifteenPercent = Math.floor(
(_salaryOrg.salaryProfiles.length * 15) / 100,
);
_salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100;
_salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); _salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100);
// console.log("fifteenAPR: case 1"); // console.log("fifteenAPR: case 1");
@ -3174,13 +3176,11 @@ export class SalaryPeriodController extends Controller {
_salaryOrg.createdAt = new Date(); _salaryOrg.createdAt = new Date();
_salaryOrg.lastUpdatedAt = new Date(); _salaryOrg.lastUpdatedAt = new Date();
await this.salaryOrgEmployeeRepository.save(_salaryOrg, { data: request }); await this.salaryOrgEmployeeRepository.save(_salaryOrg, { data: request });
setLogDataDiff(request, { before: before_salaryOrg, after: _salaryOrg }); // setLogDataDiff(request, { before: before_salaryOrg, after: _salaryOrg });
}), }),
); );
} }
console.log( console.log(`✅✅✅ [SNAPSHOT:${snapshot} เสร็จสิ้น]) ✅✅✅`);
`✅✅✅ [SNAPSHOT:${snapshot} เสร็จสิ้น]) ✅✅✅`,
);
return new HttpSuccess(); return new HttpSuccess();
} catch (err) { } catch (err) {
console.error(`❌ error processing employee:`, err); console.error(`❌ error processing employee:`, err);