diff --git a/src/controllers/ProfileSalaryTempController.ts b/src/controllers/ProfileSalaryTempController.ts index cbdcf50a..e4cb6637 100644 --- a/src/controllers/ProfileSalaryTempController.ts +++ b/src/controllers/ProfileSalaryTempController.ts @@ -1290,24 +1290,97 @@ export class ProfileSalaryTempController extends Controller { @Request() req: RequestWithUser, @Body() body: { profileId: string; type: string }, ) { - if (body.type.toLocaleUpperCase() == "OFFICER") { - const profile = await this.profileRepo.findOneBy({ id: body.profileId }); + const queryRunner = AppDataSource.createQueryRunner(); + await queryRunner.connect(); + await queryRunner.startTransaction(); + + try { + const isOfficer = body.type.toUpperCase() === "OFFICER"; + + /* ========================= + * 1. Load Profile + * ========================= */ + const profile = isOfficer + ? await queryRunner.manager.findOne(Profile, { where: { id: body.profileId } }) + : await queryRunner.manager.findOne(ProfileEmployee, { where: { id: body.profileId } }); + if (!profile) { - throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบข้อมูล profile"); } + profile.statusCheckEdit = "CHECKED"; - await this.profileRepo.save(profile); - } else { - const profile = await this.profileEmployeeRepo.findOneBy({ id: body.profileId }); - if (!profile) { - throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + await queryRunner.manager.save(profile); + + /* ========================= + * 2. Load Salary Temp + * ========================= */ + const salaryTemps = await queryRunner.manager.find(ProfileSalaryTemp, { + where: isOfficer + ? { profileId: body.profileId } + : { profileEmployeeId: body.profileId }, + }); + + if (salaryTemps.length === 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลตำแหน่ง/เงินเดือนของ profile นี้"); } - profile.statusCheckEdit = "CHECKED"; - await this.profileEmployeeRepo.save(profile); + + /* ========================= + * 3. Split Update / Insert + * ========================= */ + const toUpdate = salaryTemps.filter(t => t.salaryId); + const toInsert = salaryTemps.filter(t => !t.salaryId); + const dateNow = new Date(); + const metaUpdate = { + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + lastUpdatedAt: dateNow, + }; + + /* ========================= + * 4. UPDATE + * ========================= */ + for (const temp of toUpdate) { + const { salaryId, id, ...data } = temp; + await queryRunner.manager.update( + ProfileSalary, + { id: salaryId }, + { + ...data, + ...metaUpdate, + }, + ); + } + + /* ========================= + * 5. INSERT (bulk) + * ========================= */ + if (toInsert.length > 0) { + const metaCreate = { + createdUserId: req.user.sub, + createdFullName: req.user.name, + createdAt: dateNow, + }; + const insertData = toInsert.map(({ id, ...data }) => ({ + ...data, + ...metaCreate, + ...metaUpdate, + })); + + await queryRunner.manager.insert(ProfileSalary, insertData); + } + + await queryRunner.commitTransaction(); + return new HttpSuccess(); + + } catch (error) { + await queryRunner.rollbackTransaction(); + throw error; + } finally { + await queryRunner.release(); } - return new HttpSuccess(); } + /** * API แก้ไขข้อมูล *