diff --git a/src/controllers/OrganizationDotnetController.ts b/src/controllers/OrganizationDotnetController.ts index 8be7aa0f..e83d2911 100644 --- a/src/controllers/OrganizationDotnetController.ts +++ b/src/controllers/OrganizationDotnetController.ts @@ -9151,4 +9151,30 @@ export class OrganizationDotnetController extends Controller { }); return new HttpSuccess(filteredPosMasters); } + + /** + * API ตรวจสอบ profileId ที่ลาออกแล้ว + * @summary API ตรวจสอบ profileId ที่ลาออกแล้ว + */ + @Post("check-isLeave") + @Security("internalAuth") + async findProfileIsLeave( + @Body() + req: { profileIds: string[] } + ) { + const profile = await this.profileRepo.find({ + select: { id: true }, + where: { + id: In(req.profileIds), + isLeave: true + } + }); + + if (profile.length === 0) { + return new HttpSuccess([]); + } + + return new HttpSuccess(profile.map(p => p.id)); + } + } diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 434e7b77..0fcf3c3c 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -5779,6 +5779,22 @@ export class ProfileController extends Controller { } const record = await this.profileRepo.findOneBy({ id }); const before = structuredClone(record); + // เช็คว่ามี profileHistory ของ profile นี้หรือไม่ + const historyCount = await this.profileHistoryRepo.count({ + where: { profileId: id }, + }); + + // ถ้าไม่มีเลย ให้บันทึกข้อมูลเริ่มต้น (ก่อน update) ลงไปก่อน + if (historyCount === 0) { + await this.profileHistoryRepo.save( + Object.assign(new ProfileHistory(), { + ...before, + birthDateOld: before?.birthDate, + profileId: id, + id: undefined, + }), + ); + } if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้"); diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index cdf1a4e4..9b2537c0 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -2385,6 +2385,24 @@ export class ProfileEmployeeController extends Controller { Extension.CheckCitizen(body.citizenId); } const record = await this.profileRepo.findOneBy({ id }); + const before = structuredClone(record); + // เช็คว่ามี profileHistory ของ profile นี้หรือไม่ + const historyCount = await this.profileHistoryRepo.count({ + where: { profileEmployeeId: id }, + }); + + // ถ้าไม่มีเลย ให้บันทึกข้อมูลเริ่มต้น (ก่อน update) ลงไปก่อน + if (historyCount === 0) { + await this.profileHistoryRepo.save( + Object.assign(new ProfileEmployeeHistory(), { + ...before, + birthDateOld: before?.birthDate, + profileEmployeeId: id, + id: undefined, + }), + ); + } + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้"); if (body.employeeClass == null || body.employeeClass == undefined || body.employeeClass == "") { diff --git a/src/controllers/ProfileEmployeeTempController.ts b/src/controllers/ProfileEmployeeTempController.ts index a8c017ae..406dce69 100644 --- a/src/controllers/ProfileEmployeeTempController.ts +++ b/src/controllers/ProfileEmployeeTempController.ts @@ -1001,6 +1001,24 @@ export class ProfileEmployeeTempController extends Controller { } const record = await this.profileRepo.findOneBy({ id }); + const before = structuredClone(record); + // เช็คว่ามี profileHistory ของ profile นี้หรือไม่ + const historyCount = await this.profileHistoryRepo.count({ + where: { profileEmployeeId: id }, + }); + + // ถ้าไม่มีเลย ให้บันทึกข้อมูลเริ่มต้น (ก่อน update) ลงไปก่อน + if (historyCount === 0) { + await this.profileHistoryRepo.save( + Object.assign(new ProfileEmployeeHistory(), { + ...before, + birthDateOld: before?.birthDate, + profileEmployeeId: id, + id: undefined, + }), + ); + } + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้"); if (body.employeeClass == null || body.employeeClass == undefined || body.employeeClass == "") {