From 79686e5d45e6d8bbfc7a801933f4d437f8bcaa3d Mon Sep 17 00:00:00 2001 From: kittapath <> Date: Tue, 15 Jul 2025 23:52:51 +0700 Subject: [PATCH] test snap --- src/controllers/SalaryPeriodController.ts | 148 +++++++++++----------- 1 file changed, 76 insertions(+), 72 deletions(-) diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index 6c2f410..da19654 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -2820,81 +2820,85 @@ export class SalaryPeriodController extends Controller { console.log("mlength", orgProfileEmployees.length); // const profilesEmpToSave: SalaryProfileEmployee[] = []; console.time("⏱ SalaryProfileEmployee - Total Time"); - await Promise.all( - 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); + // await Promise.all( + // orgProfileEmployees.map(async (profile: any) => { - salaryProfileNew.salaryOrgId = salaryOrgNew.id; - salaryProfileNew.revisionId = salaryPeriod.revisionId; - salaryProfileNew.createdUserId = request.user.sub; - salaryProfileNew.createdFullName = request.user.name; - salaryProfileNew.lastUpdateUserId = request.user.sub; - salaryProfileNew.lastUpdateFullName = request.user.name; - salaryProfileNew.createdAt = new Date(); - salaryProfileNew.lastUpdatedAt = new Date(); + const batchSize = 1000; + for (let i = 0; i < orgProfileEmployees.length; i += batchSize) { + const profile = orgProfileEmployees.slice(i, i + batchSize); + 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); - if (snapshot == "SNAP2") { - //console.log(`step9`); - const salaryOrgOld = await this.salaryOrgEmployeeRepository.find({ - where: { salaryPeriodId: salaryPeriod.id, snapshot: "SNAP1" }, - }); - const salaryOld = await this.salaryProfileEmployeeRepository.findOne({ - where: { - citizenId: salaryProfileNew.citizenId, - salaryOrgId: In(salaryOrgOld.map((x) => x.id)), - }, - }); - salaryProfileNew.type = salaryOld == null ? 0 : salaryOld.type; - // salaryProfileNew.salaryLevel = salaryOld && salaryOld.salaryLevelNew ? salaryOld.salaryLevelNew : salaryOld?.salaryLevel; - // salaryProfileNew.group = salaryOld && salaryOld.groupNew ? salaryOld.groupNew : salaryOld?.group; - salaryProfileNew.salaryLevelNew = - salaryOld && salaryOld.salaryLevelNew ? salaryOld.salaryLevelNew : null; - salaryProfileNew.groupNew = - salaryOld && salaryOld.groupNew ? salaryOld.groupNew : null; - salaryProfileNew.amount = salaryOld == null ? 0 : salaryOld.amount; - salaryProfileNew.amountSpecial = salaryOld == null ? 0 : salaryOld.amountSpecial; - salaryProfileNew.amountUse = salaryOld == null ? 0 : salaryOld.amountUse; - salaryProfileNew.positionSalaryAmount = - salaryOld == null ? 0 : salaryOld.positionSalaryAmount; - salaryProfileNew.positionSalaryDayAmount = - salaryOld == null ? 0 : salaryOld.positionSalaryDayAmount; - salaryProfileNew.positionSalaryAmountPer = - salaryOld == null ? 0 : salaryOld.positionSalaryAmountPer; - salaryProfileNew.remark = salaryOld == null ? null : salaryOld.remark; - salaryProfileNew.isNext = salaryOld == null ? false : salaryOld.isNext; - salaryProfileNew.isSpecial = salaryOld == null ? false : salaryOld.isSpecial; - salaryProfileNew.isReserve = salaryOld == null ? false : salaryOld.isReserve; - salaryProfileNew.isRetired = salaryOld == null ? false : salaryOld.isRetired; - salaryProfileNew.isGood = salaryOld == null ? false : salaryOld.isGood; - } - // 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}`, - ); + salaryProfileNew.salaryOrgId = salaryOrgNew.id; + salaryProfileNew.revisionId = salaryPeriod.revisionId; + salaryProfileNew.createdUserId = request.user.sub; + salaryProfileNew.createdFullName = request.user.name; + salaryProfileNew.lastUpdateUserId = request.user.sub; + salaryProfileNew.lastUpdateFullName = request.user.name; + salaryProfileNew.createdAt = new Date(); + salaryProfileNew.lastUpdatedAt = new Date(); + + if (snapshot == "SNAP2") { + //console.log(`step9`); + const salaryOrgOld = await this.salaryOrgEmployeeRepository.find({ + where: { salaryPeriodId: salaryPeriod.id, snapshot: "SNAP1" }, + }); + const salaryOld = await this.salaryProfileEmployeeRepository.findOne({ + where: { + citizenId: salaryProfileNew.citizenId, + salaryOrgId: In(salaryOrgOld.map((x) => x.id)), + }, + }); + salaryProfileNew.type = salaryOld == null ? 0 : salaryOld.type; + // salaryProfileNew.salaryLevel = salaryOld && salaryOld.salaryLevelNew ? salaryOld.salaryLevelNew : salaryOld?.salaryLevel; + // salaryProfileNew.group = salaryOld && salaryOld.groupNew ? salaryOld.groupNew : salaryOld?.group; + salaryProfileNew.salaryLevelNew = + salaryOld && salaryOld.salaryLevelNew ? salaryOld.salaryLevelNew : null; + salaryProfileNew.groupNew = + salaryOld && salaryOld.groupNew ? salaryOld.groupNew : null; + salaryProfileNew.amount = salaryOld == null ? 0 : salaryOld.amount; + salaryProfileNew.amountSpecial = salaryOld == null ? 0 : salaryOld.amountSpecial; + salaryProfileNew.amountUse = salaryOld == null ? 0 : salaryOld.amountUse; + salaryProfileNew.positionSalaryAmount = + salaryOld == null ? 0 : salaryOld.positionSalaryAmount; + salaryProfileNew.positionSalaryDayAmount = + salaryOld == null ? 0 : salaryOld.positionSalaryDayAmount; + salaryProfileNew.positionSalaryAmountPer = + salaryOld == null ? 0 : salaryOld.positionSalaryAmountPer; + salaryProfileNew.remark = salaryOld == null ? null : salaryOld.remark; + salaryProfileNew.isNext = salaryOld == null ? false : salaryOld.isNext; + salaryProfileNew.isSpecial = salaryOld == null ? false : salaryOld.isSpecial; + salaryProfileNew.isReserve = salaryOld == null ? false : salaryOld.isReserve; + salaryProfileNew.isRetired = salaryOld == null ? false : salaryOld.isRetired; + salaryProfileNew.isGood = salaryOld == null ? false : salaryOld.isGood; } - }), - ); + // 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"); }