diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index 794ccea..6875654 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -2761,65 +2761,73 @@ export class SalaryPeriodController extends Controller { } }), ); + console.log("mlength",orgProfileEmployees.length) 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", - }, - }); + try { + 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 (salaryOrgNew != null) { - let salaryProfileNew = Object.assign(new SalaryProfileEmployee(), profile); - delete salaryProfileNew.id; - const beforeSalaryProfileNew = structuredClone(salaryProfileNew); + 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(); - 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") { - 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.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; + 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.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; + } + console.log(`step10`); + await this.salaryProfileEmployeeRepository.save(salaryProfileNew, { data: request }); + setLogDataDiff(request, { before: beforeSalaryProfileNew, after: salaryProfileNew }); + console.log( + `✅ [SNAP: ${snapshot}] บันทึก SalaryProfileEmployee: ${salaryProfileNew.citizenId} (${salaryProfileNew.fullName ?? "-"})`, + ); + } else { + console.warn( + `⚠️ [SNAP: ${snapshot}] ไม่พบ salaryOrgEmployee สำหรับ rootId: ${profile.rootId}`, + ); } - await this.salaryProfileEmployeeRepository.save(salaryProfileNew, { data: request }); - setLogDataDiff(request, { before: beforeSalaryProfileNew, after: salaryProfileNew }); - console.log( - `✅ [SNAP: ${snapshot}] บันทึก SalaryProfileEmployee: ${salaryProfileNew.citizenId} (${salaryProfileNew.fullName ?? "-"})`, - ); - } else { - console.warn( - `⚠️ [SNAP: ${snapshot}] ไม่พบ salaryOrgEmployee สำหรับ rootId: ${profile.rootId}`, - ); + } catch (err) { + console.error(`❌ error processing employee: ${profile.citizenId}`, err); } }), );