test snap

This commit is contained in:
kittapath 2025-07-15 23:52:51 +07:00
parent ab7544373d
commit 79686e5d45

View file

@ -2820,81 +2820,85 @@ 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");
await Promise.all( // await Promise.all(
orgProfileEmployees.map(async (profile: any) => { // 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 =
//console.log(`step10`); salaryOld == null ? 0 : salaryOld.positionSalaryAmountPer;
// console.log( salaryProfileNew.remark = salaryOld == null ? null : salaryOld.remark;
// `✅ [SNAP: ${snapshot}] Push SalaryProfileEmployee: ${salaryProfileNew.citizenId} (${salaryProfileNew.fullName ?? "-"})`, salaryProfileNew.isNext = salaryOld == null ? false : salaryOld.isNext;
// ); salaryProfileNew.isSpecial = salaryOld == null ? false : salaryOld.isSpecial;
await this.salaryProfileEmployeeRepository.save(salaryProfileNew, { data: request }); salaryProfileNew.isReserve = salaryOld == null ? false : salaryOld.isReserve;
// await this.salaryProfileEmployeeRepository.save(profilesEmpToSave, { chunk: 100, data: request }); salaryProfileNew.isRetired = salaryOld == null ? false : salaryOld.isRetired;
// setLogDataDiff(request, { before: beforeSalaryProfileNew, after: salaryProfileNew }); salaryProfileNew.isGood = salaryOld == null ? false : salaryOld.isGood;
} else {
console.warn(
`⚠️ [SNAP: ${snapshot}] ไม่พบ salaryOrgEmployee สำหรับ rootId: ${profile.rootId}`,
);
} }
}), // profilesEmpToSave.push(salaryProfileNew);
); //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"); console.timeEnd("⏱ SalaryProfileEmployee - Total Time");
} }