update snap

This commit is contained in:
AdisakKanthawilang 2025-07-02 15:50:06 +07:00
parent 2fc4fd3121
commit 4ddcd270a5

View file

@ -2746,6 +2746,7 @@ export class SalaryPeriodController extends Controller {
}, },
}); });
console.log(`step3`); console.log(`step3`);
const profilesToSave: SalaryProfile[] = [];
if (salaryOrgNew != null) { if (salaryOrgNew != null) {
console.log(`step4`); console.log(`step4`);
let salaryProfileNew = Object.assign(new SalaryProfile(), profile); let salaryProfileNew = Object.assign(new SalaryProfile(), profile);
@ -2765,35 +2766,23 @@ export class SalaryPeriodController extends Controller {
if (snapshot == "SNAP2") { if (snapshot == "SNAP2") {
console.log(`step5`); console.log(`step5`);
try{ try{
const salaryOrgOld = await this.salaryOrgRepository.find({ // const salaryOrgOld = await this.salaryOrgRepository.find({
where: { salaryPeriodId: salaryPeriod.id, snapshot: "SNAP1" }, // where: { salaryPeriodId: salaryPeriod.id, snapshot: "SNAP1" },
}); // });
console.log(`ก่อนหา salaryOld`); // console.log(`ก่อนหา salaryOld`);
const salaryOld = await this.salaryProfileRepository.findOne({ // const salaryOld = await this.salaryProfileRepository.findOne({
where: {
citizenId: salaryProfileNew.citizenId,
salaryOrgId: In(salaryOrgOld.map((x) => x.id)),
},
});
// const salaryOrgOldList = await this.salaryOrgRepository.find({
// where: { // where: {
// salaryPeriodId: salaryPeriod.id, // citizenId: salaryProfileNew.citizenId,
// snapshot: "SNAP1", // salaryOrgId: In(salaryOrgOld.map((x) => x.id)),
// }, // },
// }); // });
const salaryOld = await this.salaryProfileRepository
// const salaryProfileOldList = await this.salaryProfileRepository.find({ .createQueryBuilder("profile")
// where: { .innerJoin("profile.salaryOrg", "org")
// salaryOrgId: In(salaryOrgOldList.map((org) => org.id)), .where("org.salaryPeriodId = :periodId", { periodId: salaryPeriod.id })
// }, .andWhere("org.snapshot = :snapshot", { snapshot: "SNAP1" })
// }); .andWhere("profile.citizenId = :citizenId", { citizenId: salaryProfileNew.citizenId })
.getOne();
// const salaryProfileMap = new Map<string, any>();
// salaryProfileOldList.forEach((item) => {
// salaryProfileMap.set(item.citizenId, item);
// });
// const salaryOld = salaryProfileMap.get(salaryProfileNew.citizenId);
salaryProfileNew.type = salaryOld == null ? "PENDING" : salaryOld.type; salaryProfileNew.type = salaryOld == null ? "PENDING" : salaryOld.type;
salaryProfileNew.amount = salaryOld == null ? 0 : salaryOld.amount; salaryProfileNew.amount = salaryOld == null ? 0 : salaryOld.amount;
@ -2811,18 +2800,21 @@ export class SalaryPeriodController extends Controller {
console.error(`❌ error processing employee:`, err); console.error(`❌ error processing employee:`, err);
} }
} }
profilesToSave.push(salaryProfileNew);
console.log(`step6`); console.log(`step6`);
await this.salaryProfileRepository.save(salaryProfileNew, { data: request }); // console.log(
// `✅ [SNAP: ${snapshot}] Push SalaryProfile: ${salaryProfileNew.citizenId} (${salaryProfileNew.fullName ?? "-"})`,
// );
// await this.salaryProfileRepository.save(salaryProfileNew, { data: request });
await this.salaryProfileRepository.save(profilesToSave, { chunk: 100, data: request });
setLogDataDiff(request, { before: beforeSalaryProfileNew, after: salaryProfileNew }); setLogDataDiff(request, { before: beforeSalaryProfileNew, after: salaryProfileNew });
console.log(
`✅ [SNAP: ${snapshot}] บันทึก SalaryProfile: ${salaryProfileNew.citizenId} (${salaryProfileNew.fullName ?? "-"})`,
);
} else { } else {
console.warn(`⚠️ [SNAP: ${snapshot}] ไม่พบ salaryOrg สำหรับ rootId: ${profile.rootId}`); console.warn(`⚠️ [SNAP: ${snapshot}] ไม่พบ salaryOrg สำหรับ rootId: ${profile.rootId}`);
} }
}), }),
); );
console.log("mlength", orgProfileEmployees.length); console.log("mlength", orgProfileEmployees.length);
// const profilesEmpToSave: SalaryProfileEmployee[] = [];
await Promise.all( await Promise.all(
orgProfileEmployees.map(async (profile: any) => { orgProfileEmployees.map(async (profile: any) => {
const salaryOrgNew = await this.salaryOrgEmployeeRepository.findOne({ const salaryOrgNew = await this.salaryOrgEmployeeRepository.findOne({
@ -2883,12 +2875,14 @@ export class SalaryPeriodController extends Controller {
salaryProfileNew.isRetired = salaryOld == null ? false : salaryOld.isRetired; salaryProfileNew.isRetired = salaryOld == null ? false : salaryOld.isRetired;
salaryProfileNew.isGood = salaryOld == null ? false : salaryOld.isGood; salaryProfileNew.isGood = salaryOld == null ? false : salaryOld.isGood;
} }
// profilesEmpToSave.push(salaryProfileNew);
console.log(`step10`); console.log(`step10`);
await this.salaryProfileEmployeeRepository.save(salaryProfileNew, { data: request });
setLogDataDiff(request, { before: beforeSalaryProfileNew, after: salaryProfileNew });
console.log( console.log(
`✅ [SNAP: ${snapshot}] บันทึก SalaryProfileEmployee: ${salaryProfileNew.citizenId} (${salaryProfileNew.fullName ?? "-"})`, `✅ [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 { } else {
console.warn( console.warn(
`⚠️ [SNAP: ${snapshot}] ไม่พบ salaryOrgEmployee สำหรับ rootId: ${profile.rootId}`, `⚠️ [SNAP: ${snapshot}] ไม่พบ salaryOrgEmployee สำหรับ rootId: ${profile.rootId}`,