Merge branch 'main' into develop

# Conflicts:
#	src/controllers/SalaryPeriodController.ts
This commit is contained in:
kittapath 2025-09-15 21:40:17 +07:00
commit e1ca3c01c8

View file

@ -2820,89 +2820,87 @@ export class SalaryPeriodController extends Controller {
console.log("mlength", orgProfileEmployees.length); console.log("mlength", orgProfileEmployees.length);
const profilesEmpToSave: SalaryProfileEmployee[] = []; const profilesEmpToSave: SalaryProfileEmployee[] = [];
console.time("⏱ SalaryProfileEmployee - Total Time"); console.time("⏱ SalaryProfileEmployee - Total Time");
console.time("⏱ Step5: Process ProfileEmps"); // await Promise.all(
for (const profile of orgProfileEmployees) { // orgProfileEmployees.map(async (profile: any) => {
const salaryOrgNew = await this.salaryOrgEmployeeRepository.findOne({
where: {
salaryPeriodId: salaryPeriod.id,
rootId: profile.rootId,
snapshot: snapshot,
group: "GROUP1",
},
});
//console.log(`step7`);
if (salaryOrgNew != null) {
//console.log(`step8`);
let salaryProfileNew = Object.assign(new SalaryProfileEmployee(), profile);
delete salaryProfileNew.id;
// const beforeSalaryProfileNew = structuredClone(salaryProfileNew);
salaryProfileNew.salaryOrgId = salaryOrgNew.id; const batchSize = 1000;
salaryProfileNew.revisionId = salaryPeriod.revisionId; for (let i = 0; i < orgProfileEmployees.length; i += batchSize) {
salaryProfileNew.createdUserId = request.user.sub; const profile = orgProfileEmployees.slice(i, i + batchSize);
salaryProfileNew.createdFullName = request.user.name; const salaryOrgNew = await this.salaryOrgEmployeeRepository.findOne({
salaryProfileNew.lastUpdateUserId = request.user.sub; where: {
salaryProfileNew.lastUpdateFullName = request.user.name; salaryPeriodId: salaryPeriod.id,
salaryProfileNew.createdAt = new Date(); rootId: profile.rootId,
salaryProfileNew.lastUpdatedAt = new Date(); snapshot: snapshot,
group: "GROUP1",
},
});
//console.log(`step7`);
if (salaryOrgNew != null) {
//console.log(`step8`);
let salaryProfileNew = Object.assign(new SalaryProfileEmployee(), profile);
delete salaryProfileNew.id;
// const beforeSalaryProfileNew = structuredClone(salaryProfileNew);
if (snapshot == "SNAP2") { salaryProfileNew.salaryOrgId = salaryOrgNew.id;
//console.log(`step9`); salaryProfileNew.revisionId = salaryPeriod.revisionId;
const salaryOrgOld = await this.salaryOrgEmployeeRepository.find({ salaryProfileNew.createdUserId = request.user.sub;
where: { salaryPeriodId: salaryPeriod.id, snapshot: "SNAP1" }, salaryProfileNew.createdFullName = request.user.name;
}); salaryProfileNew.lastUpdateUserId = request.user.sub;
const salaryOld = await this.salaryProfileEmployeeRepository.findOne({ salaryProfileNew.lastUpdateFullName = request.user.name;
where: { salaryProfileNew.createdAt = new Date();
citizenId: salaryProfileNew.citizenId, salaryProfileNew.lastUpdatedAt = new Date();
salaryOrgId: In(salaryOrgOld.map((x) => x.id)),
}, if (snapshot == "SNAP2") {
}); //console.log(`step9`);
salaryProfileNew.type = salaryOld == null ? 0 : salaryOld.type; const salaryOrgOld = await this.salaryOrgEmployeeRepository.find({
// salaryProfileNew.salaryLevel = salaryOld && salaryOld.salaryLevelNew ? salaryOld.salaryLevelNew : salaryOld?.salaryLevel; where: { salaryPeriodId: salaryPeriod.id, snapshot: "SNAP1" },
// salaryProfileNew.group = salaryOld && salaryOld.groupNew ? salaryOld.groupNew : salaryOld?.group; });
salaryProfileNew.salaryLevelNew = const salaryOld = await this.salaryProfileEmployeeRepository.findOne({
salaryOld && salaryOld.salaryLevelNew ? salaryOld.salaryLevelNew : null; where: {
salaryProfileNew.groupNew = citizenId: salaryProfileNew.citizenId,
salaryOld && salaryOld.groupNew ? salaryOld.groupNew : null; salaryOrgId: In(salaryOrgOld.map((x) => x.id)),
salaryProfileNew.amount = salaryOld == null ? 0 : salaryOld.amount; },
salaryProfileNew.amountSpecial = salaryOld == null ? 0 : salaryOld.amountSpecial; });
salaryProfileNew.amountUse = salaryOld == null ? 0 : salaryOld.amountUse; salaryProfileNew.type = salaryOld == null ? 0 : salaryOld.type;
salaryProfileNew.positionSalaryAmount = // salaryProfileNew.salaryLevel = salaryOld && salaryOld.salaryLevelNew ? salaryOld.salaryLevelNew : salaryOld?.salaryLevel;
salaryOld == null ? 0 : salaryOld.positionSalaryAmount; // salaryProfileNew.group = salaryOld && salaryOld.groupNew ? salaryOld.groupNew : salaryOld?.group;
salaryProfileNew.positionSalaryDayAmount = salaryProfileNew.salaryLevelNew =
salaryOld == null ? 0 : salaryOld.positionSalaryDayAmount; salaryOld && salaryOld.salaryLevelNew ? salaryOld.salaryLevelNew : null;
salaryProfileNew.positionSalaryAmountPer = salaryProfileNew.groupNew =
salaryOld == null ? 0 : salaryOld.positionSalaryAmountPer; salaryOld && salaryOld.groupNew ? salaryOld.groupNew : null;
salaryProfileNew.remark = salaryOld == null ? null : salaryOld.remark; salaryProfileNew.amount = salaryOld == null ? 0 : salaryOld.amount;
salaryProfileNew.isNext = salaryOld == null ? false : salaryOld.isNext; salaryProfileNew.amountSpecial = salaryOld == null ? 0 : salaryOld.amountSpecial;
salaryProfileNew.isSpecial = salaryOld == null ? false : salaryOld.isSpecial; salaryProfileNew.amountUse = salaryOld == null ? 0 : salaryOld.amountUse;
salaryProfileNew.isReserve = salaryOld == null ? false : salaryOld.isReserve; salaryProfileNew.positionSalaryAmount =
salaryProfileNew.isRetired = salaryOld == null ? false : salaryOld.isRetired; salaryOld == null ? 0 : salaryOld.positionSalaryAmount;
salaryProfileNew.isGood = salaryOld == null ? false : salaryOld.isGood; salaryProfileNew.positionSalaryDayAmount =
} salaryOld == null ? 0 : salaryOld.positionSalaryDayAmount;
profilesEmpToSave.push(salaryProfileNew); salaryProfileNew.positionSalaryAmountPer =
await this.salaryProfileEmployeeRepository salaryOld == null ? 0 : salaryOld.positionSalaryAmountPer;
.createQueryBuilder() salaryProfileNew.remark = salaryOld == null ? null : salaryOld.remark;
.insert() salaryProfileNew.isNext = salaryOld == null ? false : salaryOld.isNext;
.into(SalaryProfileEmployee) salaryProfileNew.isSpecial = salaryOld == null ? false : salaryOld.isSpecial;
.values(profilesEmpToSave) salaryProfileNew.isReserve = salaryOld == null ? false : salaryOld.isReserve;
.orIgnore() salaryProfileNew.isRetired = salaryOld == null ? false : salaryOld.isRetired;
.execute(); salaryProfileNew.isGood = salaryOld == null ? false : salaryOld.isGood;
//console.log(`step10`);
// console.log(
// `✅ [SNAP: ${snapshot}] Push SalaryProfileEmployee: ${salaryProfileNew.citizenId} (${salaryProfileNew.fullName ?? "-"})`,
// );
// await this.salaryProfileEmployeeRepository.save(salaryProfileNew, { data: request });
// await this.salaryProfileEmployeeRepository.save(profilesEmpToSave, { chunk: 100, data: request });
// setLogDataDiff(request, { before: beforeSalaryProfileNew, after: salaryProfileNew });
} else {
console.warn(
`⚠️ [SNAP: ${snapshot}] ไม่พบ salaryOrgEmployee สำหรับ rootId: ${profile.rootId}`,
);
} }
}; // profilesEmpToSave.push(salaryProfileNew);
console.timeEnd("⏱ SalaryProfileEmployee - Total Time"); //console.log(`step10`);
} // console.log(
// `✅ [SNAP: ${snapshot}] Push SalaryProfileEmployee: ${salaryProfileNew.citizenId} (${salaryProfileNew.fullName ?? "-"})`,
// );
await this.salaryProfileEmployeeRepository.save(salaryProfileNew, { data: request });
// await this.salaryProfileEmployeeRepository.save(profilesEmpToSave, { chunk: 100, data: request });
// setLogDataDiff(request, { before: beforeSalaryProfileNew, after: salaryProfileNew });
} else {
console.warn(
`⚠️ [SNAP: ${snapshot}] ไม่พบ salaryOrgEmployee สำหรับ rootId: ${profile.rootId}`,
);
}
}
// );
console.timeEnd("⏱ SalaryProfileEmployee - Total Time");
}
const salaryOrgNew = await this.salaryOrgRepository.find({ const salaryOrgNew = await this.salaryOrgRepository.find({
where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot }, where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot },