diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 2fb7e085..483c580d 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -5506,20 +5506,33 @@ export class ProfileController extends Controller { */ @Get("history/user") async getHistoryProfileByUser(@Request() request: RequestWithUser) { - const historyProfile = await this.profileHistoryRepo.find({ - relations: { - posLevel: true, - posType: true, - }, - where: { keycloak: request.user.sub }, - order: { - createdAt: "ASC", - }, + const profile = await this.profileRepo.findOne({ + where: { keycloak: request.user.sub } }); + if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - if (!historyProfile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + const profileHistory = await this.profileHistoryRepo.find({ + where: { profileId: profile.id }, + order: { createdAt: "ASC" } + }); + + if (profileHistory.length == 0) { + await this.profileHistoryRepo.save( + Object.assign(new ProfileHistory(), { + ...profile, + birthDateOld: profile?.birthDate, + profileId: profile.id, + id: undefined + }), + ); + const firstRecord = await this.profileHistoryRepo.find({ + where: { profileId: profile.id }, + order: { createdAt: "ASC" } + }); + return new HttpSuccess(firstRecord); + } - return new HttpSuccess(historyProfile); + return new HttpSuccess(profileHistory); } /** @@ -6468,20 +6481,33 @@ export class ProfileController extends Controller { @Get("history/{id}") async getProfileHistory(@Path() id: string, @Request() req: RequestWithUser) { //await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", id); //ไม่แน่ใจOFFปิดไว้ก่อน - const profile = await this.profileHistoryRepo.find({ - relations: { - posLevel: true, - posType: true, - }, - where: { profileId: id }, - order: { - createdAt: "ASC", - }, + const profile = await this.profileRepo.findOne({ + where: { id: id } }); - if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - return new HttpSuccess(profile); + const profileHistory = await this.profileHistoryRepo.find({ + where: { profileId: id }, + order: { createdAt: "ASC" } + }); + + if (profileHistory.length == 0) { + await this.profileHistoryRepo.save( + Object.assign(new ProfileHistory(), { + ...profile, + birthDateOld: profile?.birthDate, + profileId: id, + id: undefined + }), + ); + const firstRecord = await this.profileHistoryRepo.find({ + where: { profileId: id }, + order: { createdAt: "ASC" } + }); + return new HttpSuccess(firstRecord); + } + + return new HttpSuccess(profileHistory); } /** diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index 5fa532ca..72835a48 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -2289,20 +2289,33 @@ export class ProfileEmployeeController extends Controller { */ @Get("history/user") async getHistoryProfileByUser(@Request() request: RequestWithUser) { - const historyProfile = await this.profileHistoryRepo.find({ - relations: { - posLevel: true, - posType: true, - }, - where: { keycloak: request.user.sub }, - order: { - createdAt: "ASC", - }, + const profile = await this.profileRepo.findOne({ + where: { keycloak: request.user.sub } }); + if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - if (!historyProfile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + const profileHistory = await this.profileHistoryRepo.find({ + where: { profileEmployeeId: profile.id }, + order: { createdAt: "ASC" } + }); + + if (profileHistory.length == 0) { + await this.profileHistoryRepo.save( + Object.assign(new ProfileEmployeeHistory(), { + ...profile, + birthDateOld: profile?.birthDate, + profileEmployeeId: profile.id, + id: undefined + }), + ); + const firstRecord = await this.profileHistoryRepo.find({ + where: { profileEmployeeId: profile.id }, + order: { createdAt: "ASC" } + }); + return new HttpSuccess(firstRecord); + } - return new HttpSuccess(historyProfile); + return new HttpSuccess(profileHistory); } /** @@ -3193,20 +3206,33 @@ export class ProfileEmployeeController extends Controller { @Get("history/{id}") async getProfileHistory(@Path() id: string, @Request() req: RequestWithUser) { //await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_EMP", id); ไม่แน่ใจEMPปิดไว้ก่อน; - const profile = await this.profileHistoryRepo.find({ - relations: { - posLevel: true, - posType: true, - }, - where: { profileEmployeeId: id }, - order: { - createdAt: "ASC", - }, + const profile = await this.profileRepo.findOne({ + where: { id: id } }); - if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - return new HttpSuccess(profile); + const profileHistory = await this.profileHistoryRepo.find({ + where: { profileEmployeeId: id }, + order: { createdAt: "ASC" } + }); + + if (profileHistory.length == 0) { + await this.profileHistoryRepo.save( + Object.assign(new ProfileEmployeeHistory(), { + ...profile, + birthDateOld: profile?.birthDate, + profileEmployeeId: id, + id: undefined + }), + ); + const firstRecord = await this.profileHistoryRepo.find({ + where: { profileEmployeeId: id }, + order: { createdAt: "ASC" } + }); + return new HttpSuccess(firstRecord); + } + + return new HttpSuccess(profileHistory); } /** diff --git a/src/controllers/ProfileEmployeeTempController.ts b/src/controllers/ProfileEmployeeTempController.ts index 66d66dd7..72dd1c43 100644 --- a/src/controllers/ProfileEmployeeTempController.ts +++ b/src/controllers/ProfileEmployeeTempController.ts @@ -1294,20 +1294,33 @@ export class ProfileEmployeeTempController extends Controller { */ @Get("history/user") async getHistoryProfileByUser(@Request() request: RequestWithUser) { - const historyProfile = await this.profileHistoryRepo.find({ - relations: { - posLevel: true, - posType: true, - }, - where: { keycloak: request.user.sub }, - order: { - createdAt: "ASC", - }, + const profile = await this.profileRepo.findOne({ + where: { keycloak: request.user.sub } }); + if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - if (!historyProfile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + const profileHistory = await this.profileHistoryRepo.find({ + where: { profileEmployeeId: profile.id }, + order: { createdAt: "ASC" } + }); + + if (profileHistory.length == 0) { + await this.profileHistoryRepo.save( + Object.assign(new ProfileEmployeeHistory(), { + ...profile, + birthDateOld: profile?.birthDate, + profileEmployeeId: profile.id, + id: undefined + }), + ); + const firstRecord = await this.profileHistoryRepo.find({ + where: { profileEmployeeId: profile.id }, + order: { createdAt: "ASC" } + }); + return new HttpSuccess(firstRecord); + } - return new HttpSuccess(historyProfile); + return new HttpSuccess(profileHistory); } /** @@ -1814,20 +1827,33 @@ export class ProfileEmployeeTempController extends Controller { @Get("history/{id}") async getProfileHistory(@Path() id: string, @Request() req: RequestWithUser) { // await new permission().PermissionGet(req, "SYS_REGISTRY_TEMP");//ไม่แน่ใจTEMPปิดไว้ก่อน - const profile = await this.profileHistoryRepo.find({ - relations: { - posLevel: true, - posType: true, - }, - where: { profileEmployeeId: id }, - order: { - createdAt: "ASC", - }, + const profile = await this.profileRepo.findOne({ + where: { id: id } }); - if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - return new HttpSuccess(profile); + const profileHistory = await this.profileHistoryRepo.find({ + where: { profileEmployeeId: id }, + order: { createdAt: "ASC" } + }); + + if (profileHistory.length == 0) { + await this.profileHistoryRepo.save( + Object.assign(new ProfileEmployeeHistory(), { + ...profile, + birthDateOld: profile?.birthDate, + profileEmployeeId: id, + id: undefined + }), + ); + const firstRecord = await this.profileHistoryRepo.find({ + where: { profileEmployeeId: id }, + order: { createdAt: "ASC" } + }); + return new HttpSuccess(firstRecord); + } + + return new HttpSuccess(profileHistory); } /**