From fd3f1fa5157b988d83b21a804d7c83a87a4e2b41 Mon Sep 17 00:00:00 2001 From: kittapath Date: Tue, 13 Aug 2024 17:43:30 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=20api=20=E0=B9=80?= =?UTF-8?q?=E0=B8=81=E0=B9=87=E0=B8=9A=E0=B8=9B=E0=B8=A3=E0=B8=B0=E0=B8=A7?= =?UTF-8?q?=E0=B8=B1=E0=B8=95=E0=B8=B4=E0=B9=81=E0=B8=81=E0=B9=89=E0=B8=97?= =?UTF-8?q?=E0=B8=B0=E0=B9=80=E0=B8=9A=E0=B8=B5=E0=B8=A2=E0=B8=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ProfileAbilityController.ts | 20 +- .../ProfileAbilityEmployeeController.ts | 20 +- .../ProfileAbilityEmployeeTempController.ts | 20 +- src/controllers/ProfileAddressController.ts | 12 +- .../ProfileAddressEmployeeController.ts | 12 +- .../ProfileAddressEmployeeTempController.ts | 12 +- .../ProfileAssessmentsController.ts | 21 +- .../ProfileAssessmentsEmployeeController.ts | 21 +- ...rofileAssessmentsEmployeeTempController.ts | 21 +- .../ProfileCertificateController.ts | 15 +- .../ProfileCertificateEmployeeController.ts | 15 +- ...rofileCertificateEmployeeTempController.ts | 15 +- .../ProfileChangeNameController.ts | 16 +- .../ProfileChangeNameEmployeeController.ts | 23 +- ...ProfileChangeNameEmployeeTempController.ts | 23 +- src/controllers/ProfileChildrenController.ts | 45 +- .../ProfileChildrenEmployeeController.ts | 44 +- .../ProfileChildrenEmployeeTempController.ts | 44 +- .../ProfileDisciplineController.ts | 16 +- .../ProfileDisciplineEmployeeController.ts | 16 +- ...ProfileDisciplineEmployeeTempController.ts | 16 +- src/controllers/ProfileDutyController.ts | 15 +- .../ProfileDutyEmployeeController.ts | 15 +- .../ProfileDutyEmployeeTempController.ts | 15 +- .../ProfileEducationsController.ts | 21 +- .../ProfileEducationsEmployeeController.ts | 20 +- ...ProfileEducationsEmployeeTempController.ts | 20 +- .../ProfileFamilyCoupleController.ts | 55 +-- .../ProfileFamilyCoupleEmployeeController.ts | 55 +-- ...ofileFamilyCoupleEmployeeTempController.ts | 54 +-- .../ProfileFamilyFatherController.ts | 45 +- .../ProfileFamilyFatherEmployeeController.ts | 45 +- ...ofileFamilyFatherEmployeeTempController.ts | 45 +- .../ProfileFamilyHistoryController.ts | 422 ------------------ .../ProfileFamilyHistoryEmployeeController.ts | 422 ------------------ .../ProfileFamilyMotherController.ts | 45 +- .../ProfileFamilyMotherEmployeeController.ts | 45 +- ...ofileFamilyMotherEmployeeTempController.ts | 45 +- .../ProfileGovernmentController.ts | 58 +-- .../ProfileGovernmentEmployeeController.ts | 61 +-- ...ProfileGovernmentEmployeeTempController.ts | 61 +-- src/controllers/ProfileHonorController.ts | 15 +- .../ProfileHonorEmployeeController.ts | 15 +- .../ProfileHonorEmployeeTempController.ts | 15 +- src/controllers/ProfileInsigniaController.ts | 15 +- .../ProfileInsigniaEmployeeController.ts | 15 +- .../ProfileInsigniaEmployeeTempController.ts | 15 +- src/controllers/ProfileLeaveController.ts | 15 +- .../ProfileLeaveEmployeeController.ts | 15 +- .../ProfileLeaveEmployeeTempController.ts | 15 +- src/controllers/ProfileNopaidController.ts | 15 +- .../ProfileNopaidEmployeeController.ts | 15 +- .../ProfileNopaidEmployeeTempController.ts | 15 +- src/controllers/ProfileOtherController.ts | 15 +- .../ProfileOtherEmployeeController.ts | 15 +- .../ProfileOtherEmployeeTempController.ts | 15 +- src/controllers/ProfileSalaryController.ts | 15 +- .../ProfileSalaryEmployeeController.ts | 15 +- .../ProfileSalaryEmployeeTempController.ts | 15 +- src/controllers/ProfileTrainingController.ts | 15 +- .../ProfileTrainingEmployeeController.ts | 15 +- .../ProfileTrainingEmployeeTempController.ts | 15 +- 62 files changed, 855 insertions(+), 1471 deletions(-) delete mode 100644 src/controllers/ProfileFamilyHistoryController.ts delete mode 100644 src/controllers/ProfileFamilyHistoryEmployeeController.ts diff --git a/src/controllers/ProfileAbilityController.ts b/src/controllers/ProfileAbilityController.ts index edd7ca4e..ddef5d14 100644 --- a/src/controllers/ProfileAbilityController.ts +++ b/src/controllers/ProfileAbilityController.ts @@ -152,15 +152,21 @@ export class ProfileAbilityController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileAbilityHistory(); + history.profileAbilityId = data.id; - await this.profileAbilityRepo.save(data); + await Promise.all([ + this.profileAbilityRepo.save(data), + (history.profileAbilityId = data.id), + this.profileAbilityHistoryRepo.save(history), + ]); return new HttpSuccess(); } @Patch("{abilityId}") public async editProfileAbility( - @Body() requestBody: UpdateProfileAbility, + @Body() body: UpdateProfileAbility, @Request() req: RequestWithUser, @Path() abilityId: string, ) { @@ -170,12 +176,16 @@ export class ProfileAbilityController extends Controller { const history = new ProfileAbilityHistory(); - Object.assign(history, { ...record, id: undefined }); - Object.assign(record, requestBody); + Object.assign(record, body); + Object.assign(history, body); history.profileAbilityId = abilityId; - history.lastUpdateFullName = req.user.name; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.profileAbilityRepo.save(record), diff --git a/src/controllers/ProfileAbilityEmployeeController.ts b/src/controllers/ProfileAbilityEmployeeController.ts index 7bac2718..eca1b434 100644 --- a/src/controllers/ProfileAbilityEmployeeController.ts +++ b/src/controllers/ProfileAbilityEmployeeController.ts @@ -154,15 +154,21 @@ export class ProfileAbilityEmployeeController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileAbilityHistory(); + history.profileAbilityId = data.id; - await this.profileAbilityRepo.save(data); + await Promise.all([ + this.profileAbilityRepo.save(data), + (history.profileAbilityId = data.id), + this.profileAbilityHistoryRepo.save(history), + ]); return new HttpSuccess(); } @Patch("{abilityId}") public async editProfileAbility( - @Body() requestBody: UpdateProfileAbility, + @Body() body: UpdateProfileAbility, @Request() req: RequestWithUser, @Path() abilityId: string, ) { @@ -172,12 +178,16 @@ export class ProfileAbilityEmployeeController extends Controller { const history = new ProfileAbilityHistory(); - Object.assign(history, { ...record, id: undefined }); - Object.assign(record, requestBody); + Object.assign(record, body); + Object.assign(history, body); history.profileAbilityId = abilityId; - history.lastUpdateFullName = req.user.name; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.profileAbilityRepo.save(record), diff --git a/src/controllers/ProfileAbilityEmployeeTempController.ts b/src/controllers/ProfileAbilityEmployeeTempController.ts index ba566742..723b3fa5 100644 --- a/src/controllers/ProfileAbilityEmployeeTempController.ts +++ b/src/controllers/ProfileAbilityEmployeeTempController.ts @@ -154,15 +154,21 @@ export class ProfileAbilityEmployeeTempController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileAbilityHistory(); + history.profileAbilityId = data.id; - await this.profileAbilityRepo.save(data); + await Promise.all([ + this.profileAbilityRepo.save(data), + (history.profileAbilityId = data.id), + this.profileAbilityHistoryRepo.save(history), + ]); return new HttpSuccess(); } @Patch("{abilityId}") public async editProfileAbility( - @Body() requestBody: UpdateProfileAbility, + @Body() body: UpdateProfileAbility, @Request() req: RequestWithUser, @Path() abilityId: string, ) { @@ -172,12 +178,16 @@ export class ProfileAbilityEmployeeTempController extends Controller { const history = new ProfileAbilityHistory(); - Object.assign(history, { ...record, id: undefined }); - Object.assign(record, requestBody); + Object.assign(record, body); + Object.assign(history, body); history.profileAbilityId = abilityId; - history.lastUpdateFullName = req.user.name; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.profileAbilityRepo.save(record), diff --git a/src/controllers/ProfileAddressController.ts b/src/controllers/ProfileAddressController.ts index 8054d9c3..e990ac24 100644 --- a/src/controllers/ProfileAddressController.ts +++ b/src/controllers/ProfileAddressController.ts @@ -178,7 +178,7 @@ export class ProfileAddressController extends Controller { */ @Patch("{profileId}") public async editProfileAddress( - @Body() requestBody: UpdateProfileAddress, + @Body() body: UpdateProfileAddress, @Request() req: RequestWithUser, @Path() profileId: string, ) { @@ -188,12 +188,16 @@ export class ProfileAddressController extends Controller { const history = new ProfileAddressHistory(); - Object.assign(history, { ...record, id: undefined }); - Object.assign(record, requestBody); + Object.assign(record, body); + Object.assign(history, body); history.profileId = profileId; - history.lastUpdateFullName = req.user.name; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.profileRepo.save(record), diff --git a/src/controllers/ProfileAddressEmployeeController.ts b/src/controllers/ProfileAddressEmployeeController.ts index b7ac24d0..8202b86e 100644 --- a/src/controllers/ProfileAddressEmployeeController.ts +++ b/src/controllers/ProfileAddressEmployeeController.ts @@ -179,7 +179,7 @@ export class ProfileAddressEmployeeController extends Controller { */ @Patch("{profileId}") public async editProfileAddress( - @Body() requestBody: UpdateProfileAddressEmployee, + @Body() body: UpdateProfileAddressEmployee, @Request() req: RequestWithUser, @Path() profileId: string, ) { @@ -189,12 +189,16 @@ export class ProfileAddressEmployeeController extends Controller { const history = new ProfileAddressHistory(); - Object.assign(history, { ...record, id: undefined }); - Object.assign(record, requestBody); + Object.assign(record, body); + Object.assign(history, body); history.profileEmployeeId = profileId; - history.lastUpdateFullName = req.user.name; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.profileEmployeeRepo.save(record), diff --git a/src/controllers/ProfileAddressEmployeeTempController.ts b/src/controllers/ProfileAddressEmployeeTempController.ts index 0a565840..b5f7596b 100644 --- a/src/controllers/ProfileAddressEmployeeTempController.ts +++ b/src/controllers/ProfileAddressEmployeeTempController.ts @@ -179,7 +179,7 @@ export class ProfileAddressEmployeeTempController extends Controller { */ @Patch("{profileId}") public async editProfileAddress( - @Body() requestBody: UpdateProfileAddressEmployee, + @Body() body: UpdateProfileAddressEmployee, @Request() req: RequestWithUser, @Path() profileId: string, ) { @@ -189,12 +189,16 @@ export class ProfileAddressEmployeeTempController extends Controller { const history = new ProfileAddressHistory(); - Object.assign(history, { ...record, id: undefined }); - Object.assign(record, requestBody); + Object.assign(record, body); + Object.assign(history, body); history.profileEmployeeId = profileId; - history.lastUpdateFullName = req.user.name; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.profileEmployeeRepo.save(record), diff --git a/src/controllers/ProfileAssessmentsController.ts b/src/controllers/ProfileAssessmentsController.ts index 4898acf4..a6b68b9f 100644 --- a/src/controllers/ProfileAssessmentsController.ts +++ b/src/controllers/ProfileAssessmentsController.ts @@ -164,15 +164,21 @@ export class ProfileAssessmentsController extends Controller { lastUpdateFullName: req.user.name, }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileAssessmentHistory(); + history.profileAssessmentId = data.id; - await this.profileAssessmentsRepository.save(data); + await Promise.all([ + this.profileAssessmentsRepository.save(data), + (history.profileAssessmentId = data.id), + this.profileAssessmentsHistoryRepository.save(history), + ]); return new HttpSuccess(); } @Patch("{assessmentId}") public async editProfileAssessment( - @Body() requestBody: UpdateProfileAssessment, + @Body() body: UpdateProfileAssessment, @Request() req: RequestWithUser, @Path() assessmentId: string, ) { @@ -182,17 +188,22 @@ export class ProfileAssessmentsController extends Controller { const history = new ProfileAssessmentHistory(); - Object.assign(history, { ...record, id: undefined }); - Object.assign(record, requestBody); + Object.assign(record, body); + Object.assign(history, body); history.profileAssessmentId = assessmentId; - history.lastUpdateFullName = req.user.name; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.profileAssessmentsRepository.save(record), this.profileAssessmentsHistoryRepository.save(history), ]); + return new HttpSuccess(); } diff --git a/src/controllers/ProfileAssessmentsEmployeeController.ts b/src/controllers/ProfileAssessmentsEmployeeController.ts index 8642aa31..df548051 100644 --- a/src/controllers/ProfileAssessmentsEmployeeController.ts +++ b/src/controllers/ProfileAssessmentsEmployeeController.ts @@ -166,15 +166,21 @@ export class ProfileAssessmentsEmployeeController extends Controller { lastUpdateFullName: req.user.name, }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileAssessmentHistory(); + history.profileAssessmentId = data.id; - await this.profileAssessmentsRepository.save(data); + await Promise.all([ + this.profileAssessmentsRepository.save(data), + (history.profileAssessmentId = data.id), + this.profileAssessmentsHistoryRepository.save(history), + ]); return new HttpSuccess(); } @Patch("{assessmentId}") public async editProfileAssessment( - @Body() requestBody: UpdateProfileAssessment, + @Body() body: UpdateProfileAssessment, @Request() req: RequestWithUser, @Path() assessmentId: string, ) { @@ -184,17 +190,22 @@ export class ProfileAssessmentsEmployeeController extends Controller { const history = new ProfileAssessmentHistory(); - Object.assign(history, { ...record, id: undefined }); - Object.assign(record, requestBody); + Object.assign(record, body); + Object.assign(history, body); history.profileAssessmentId = assessmentId; - history.lastUpdateFullName = req.user.name; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.profileAssessmentsRepository.save(record), this.profileAssessmentsHistoryRepository.save(history), ]); + return new HttpSuccess(); } diff --git a/src/controllers/ProfileAssessmentsEmployeeTempController.ts b/src/controllers/ProfileAssessmentsEmployeeTempController.ts index 5c0584bf..c420bd1d 100644 --- a/src/controllers/ProfileAssessmentsEmployeeTempController.ts +++ b/src/controllers/ProfileAssessmentsEmployeeTempController.ts @@ -166,15 +166,21 @@ export class ProfileAssessmentsEmployeeTempController extends Controller { lastUpdateFullName: req.user.name, }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileAssessmentHistory(); + history.profileAssessmentId = data.id; - await this.profileAssessmentsRepository.save(data); + await Promise.all([ + this.profileAssessmentsRepository.save(data), + (history.profileAssessmentId = data.id), + this.profileAssessmentsHistoryRepository.save(history), + ]); return new HttpSuccess(); } @Patch("{assessmentId}") public async editProfileAssessment( - @Body() requestBody: UpdateProfileAssessment, + @Body() body: UpdateProfileAssessment, @Request() req: RequestWithUser, @Path() assessmentId: string, ) { @@ -184,17 +190,22 @@ export class ProfileAssessmentsEmployeeTempController extends Controller { const history = new ProfileAssessmentHistory(); - Object.assign(history, { ...record, id: undefined }); - Object.assign(record, requestBody); + Object.assign(record, body); + Object.assign(history, body); history.profileAssessmentId = assessmentId; - history.lastUpdateFullName = req.user.name; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.profileAssessmentsRepository.save(record), this.profileAssessmentsHistoryRepository.save(history), ]); + return new HttpSuccess(); } diff --git a/src/controllers/ProfileCertificateController.ts b/src/controllers/ProfileCertificateController.ts index b2496bf9..085d21f7 100644 --- a/src/controllers/ProfileCertificateController.ts +++ b/src/controllers/ProfileCertificateController.ts @@ -142,8 +142,14 @@ export class ProfileCertificateController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileCertificateHistory(); + history.profileCertificateId = data.id; - await this.certificateRepo.save(data); + await Promise.all([ + this.certificateRepo.save(data), + (history.profileCertificateId = data.id), + this.certificateHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -161,11 +167,16 @@ export class ProfileCertificateController extends Controller { const history = new ProfileCertificateHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileCertificateId = certificateId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.certificateRepo.save(record), diff --git a/src/controllers/ProfileCertificateEmployeeController.ts b/src/controllers/ProfileCertificateEmployeeController.ts index f7ffa8e7..7327d0ea 100644 --- a/src/controllers/ProfileCertificateEmployeeController.ts +++ b/src/controllers/ProfileCertificateEmployeeController.ts @@ -142,8 +142,14 @@ export class ProfileCertificateEmployeeController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileCertificateHistory(); + history.profileCertificateId = data.id; - await this.certificateRepo.save(data); + await Promise.all([ + this.certificateRepo.save(data), + (history.profileCertificateId = data.id), + this.certificateHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -161,11 +167,16 @@ export class ProfileCertificateEmployeeController extends Controller { const history = new ProfileCertificateHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileCertificateId = certificateId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.certificateRepo.save(record), diff --git a/src/controllers/ProfileCertificateEmployeeTempController.ts b/src/controllers/ProfileCertificateEmployeeTempController.ts index 5125e2eb..86f1b011 100644 --- a/src/controllers/ProfileCertificateEmployeeTempController.ts +++ b/src/controllers/ProfileCertificateEmployeeTempController.ts @@ -142,8 +142,14 @@ export class ProfileCertificateEmployeeTempController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileCertificateHistory(); + history.profileCertificateId = data.id; - await this.certificateRepo.save(data); + await Promise.all([ + this.certificateRepo.save(data), + (history.profileCertificateId = data.id), + this.certificateHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -161,11 +167,16 @@ export class ProfileCertificateEmployeeTempController extends Controller { const history = new ProfileCertificateHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileCertificateId = certificateId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.certificateRepo.save(record), diff --git a/src/controllers/ProfileChangeNameController.ts b/src/controllers/ProfileChangeNameController.ts index 839c1283..2fff7871 100644 --- a/src/controllers/ProfileChangeNameController.ts +++ b/src/controllers/ProfileChangeNameController.ts @@ -140,15 +140,20 @@ export class ProfileChangeNameController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileChangeNameHistory(); + history.profileChangeNameId = data.id; - await this.changeNameRepository.save(data); + await Promise.all([ + this.changeNameRepository.save(data), + (history.profileChangeNameId = data.id), + this.changeNameHistoryRepository.save(history), + ]); profile.firstName = body.firstName ?? profile.firstName; profile.lastName = body.lastName ?? profile.lastName; profile.prefix = body.prefix ?? profile.prefix; await this.profileRepository.save(profile); - // ปิดไว้ก่อนเพราะ error ต้องใช้ keycloak ที่มีสิทธิ์ในการ update //update 17/07 if (profile != null && profile.keycloak != null) { const result = await updateName(profile.keycloak, profile.firstName, profile.lastName); if (!result) { @@ -172,11 +177,16 @@ export class ProfileChangeNameController extends Controller { const history = new ProfileChangeNameHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileChangeNameId = changeNameId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.changeNameRepository.save(record), diff --git a/src/controllers/ProfileChangeNameEmployeeController.ts b/src/controllers/ProfileChangeNameEmployeeController.ts index 563f6c72..f3fec960 100644 --- a/src/controllers/ProfileChangeNameEmployeeController.ts +++ b/src/controllers/ProfileChangeNameEmployeeController.ts @@ -27,6 +27,7 @@ import { } from "../entities/ProfileChangeName"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { updateName } from "../keycloak"; @Route("api/v1/org/profile-employee/changeName") @Tags("ProfileChangeNameEmployee") @Security("bearerAuth") @@ -140,14 +141,27 @@ export class ProfileChangeNameEmployeeController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileChangeNameHistory(); + history.profileChangeNameId = data.id; - await this.changeNameRepository.save(data); + await Promise.all([ + this.changeNameRepository.save(data), + (history.profileChangeNameId = data.id), + this.changeNameHistoryRepository.save(history), + ]); profile.firstName = body.firstName ?? profile.firstName; profile.lastName = body.lastName ?? profile.lastName; profile.prefix = body.prefix ?? profile.prefix; await this.profileEmployeeRepo.save(profile); + if (profile != null && profile.keycloak != null) { + const result = await updateName(profile.keycloak, profile.firstName, profile.lastName); + if (!result) { + throw new Error(result.errorMessage); + } + } + return new HttpSuccess(data.id); } @@ -164,11 +178,16 @@ export class ProfileChangeNameEmployeeController extends Controller { const history = new ProfileChangeNameHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileChangeNameId = changeNameId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.changeNameRepository.save(record), diff --git a/src/controllers/ProfileChangeNameEmployeeTempController.ts b/src/controllers/ProfileChangeNameEmployeeTempController.ts index 721737d9..5d0307e9 100644 --- a/src/controllers/ProfileChangeNameEmployeeTempController.ts +++ b/src/controllers/ProfileChangeNameEmployeeTempController.ts @@ -27,6 +27,7 @@ import { } from "../entities/ProfileChangeName"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { updateName } from "../keycloak"; @Route("api/v1/org/profile-temp/changeName") @Tags("ProfileChangeNameEmployee") @Security("bearerAuth") @@ -140,14 +141,27 @@ export class ProfileChangeNameEmployeeTempController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileChangeNameHistory(); + history.profileChangeNameId = data.id; - await this.changeNameRepository.save(data); + await Promise.all([ + this.changeNameRepository.save(data), + (history.profileChangeNameId = data.id), + this.changeNameHistoryRepository.save(history), + ]); profile.firstName = body.firstName ?? profile.firstName; profile.lastName = body.lastName ?? profile.lastName; profile.prefix = body.prefix ?? profile.prefix; await this.profileEmployeeRepo.save(profile); + if (profile != null && profile.keycloak != null) { + const result = await updateName(profile.keycloak, profile.firstName, profile.lastName); + if (!result) { + throw new Error(result.errorMessage); + } + } + return new HttpSuccess(data.id); } @@ -164,11 +178,16 @@ export class ProfileChangeNameEmployeeTempController extends Controller { const history = new ProfileChangeNameHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileChangeNameId = changeNameId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.changeNameRepository.save(record), diff --git a/src/controllers/ProfileChildrenController.ts b/src/controllers/ProfileChildrenController.ts index f00ef633..7d167b27 100644 --- a/src/controllers/ProfileChildrenController.ts +++ b/src/controllers/ProfileChildrenController.ts @@ -82,23 +82,16 @@ export class ProfileChildrenController extends Controller { Object.assign(data, { ...body, ...meta }); data.childrenCitizenId = Extension.CheckCitizen(String(data.childrenCitizenId)); - await this.childrenRepository.save(data); - if (data) { - const history: ProfileChildrenHistory = Object.assign(new ProfileChildrenHistory(), { - profileChildrenId: data.id, - childrenCareer: data.childrenCareer, - childrenFirstName: data.childrenFirstName, - childrenLastName: data.childrenLastName, - childrenPrefix: data.childrenPrefix, - childrenLive: data.childrenLive, - childrenCitizenId: data.childrenCitizenId, - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - }); - await this.childrenHistoryRepository.save(history); - } + + const history = new ProfileChildrenHistory(); + history.profileChildrenId = data.id; + + await Promise.all([ + this.childrenRepository.save(data), + (history.profileChildrenId = data.id), + this.childrenHistoryRepository.save(history), + ]); + return new HttpSuccess(); } @@ -113,19 +106,17 @@ export class ProfileChildrenController extends Controller { if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileChildrenHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); - record.lastUpdateUserId = req.user.sub; - record.lastUpdateFullName = req.user.name; + Object.assign(history, body); + record.childrenCitizenId = Extension.CheckCitizen(String(record.childrenCitizenId)); history.profileChildrenId = record.id; - history.childrenCareer = record.childrenCareer; - history.childrenFirstName = record.childrenFirstName; - history.childrenLastName = record.childrenLastName; - history.childrenPrefix = record.childrenPrefix; - history.childrenLive = record.childrenLive; - history.childrenCitizenId = record.childrenCitizenId; - (history.lastUpdateUserId = req.user.sub), (history.lastUpdateFullName = req.user.name); + record.lastUpdateUserId = req.user.sub; + record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.childrenRepository.save(record), this.childrenHistoryRepository.save(history), diff --git a/src/controllers/ProfileChildrenEmployeeController.ts b/src/controllers/ProfileChildrenEmployeeController.ts index efee398d..26cb109b 100644 --- a/src/controllers/ProfileChildrenEmployeeController.ts +++ b/src/controllers/ProfileChildrenEmployeeController.ts @@ -88,23 +88,15 @@ export class ProfileChildrenEmployeeController extends Controller { Object.assign(data, { ...body, ...meta }); data.childrenCitizenId = Extension.CheckCitizen(String(data.childrenCitizenId)); - await this.childrenRepository.save(data); - if (data) { - const history: ProfileChildrenHistory = Object.assign(new ProfileChildrenHistory(), { - profileChildrenId: data.id, - childrenCareer: data.childrenCareer, - childrenFirstName: data.childrenFirstName, - childrenLastName: data.childrenLastName, - childrenPrefix: data.childrenPrefix, - childrenLive: data.childrenLive, - childrenCitizenId: data.childrenCitizenId, - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - }); - await this.childrenHistoryRepository.save(history); - } + + const history = new ProfileChildrenHistory(); + history.profileChildrenId = data.id; + + await Promise.all([ + this.childrenRepository.save(data), + (history.profileChildrenId = data.id), + this.childrenHistoryRepository.save(history), + ]); return new HttpSuccess(); } @@ -120,19 +112,17 @@ export class ProfileChildrenEmployeeController extends Controller { if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileChildrenHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); - record.lastUpdateUserId = req.user.sub; - record.lastUpdateFullName = req.user.name; + Object.assign(history, body); + record.childrenCitizenId = Extension.CheckCitizen(String(record.childrenCitizenId)); history.profileChildrenId = record.id; - history.childrenCareer = record.childrenCareer; - history.childrenFirstName = record.childrenFirstName; - history.childrenLastName = record.childrenLastName; - history.childrenPrefix = record.childrenPrefix; - history.childrenLive = record.childrenLive; - history.childrenCitizenId = record.childrenCitizenId; - (history.lastUpdateUserId = req.user.sub), (history.lastUpdateFullName = req.user.name); + record.lastUpdateUserId = req.user.sub; + record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.childrenRepository.save(record), diff --git a/src/controllers/ProfileChildrenEmployeeTempController.ts b/src/controllers/ProfileChildrenEmployeeTempController.ts index 3cf88256..e217b582 100644 --- a/src/controllers/ProfileChildrenEmployeeTempController.ts +++ b/src/controllers/ProfileChildrenEmployeeTempController.ts @@ -88,23 +88,15 @@ export class ProfileChildrenEmployeeTempController extends Controller { Object.assign(data, { ...body, ...meta }); data.childrenCitizenId = Extension.CheckCitizen(String(data.childrenCitizenId)); - await this.childrenRepository.save(data); - if (data) { - const history: ProfileChildrenHistory = Object.assign(new ProfileChildrenHistory(), { - profileChildrenId: data.id, - childrenCareer: data.childrenCareer, - childrenFirstName: data.childrenFirstName, - childrenLastName: data.childrenLastName, - childrenPrefix: data.childrenPrefix, - childrenLive: data.childrenLive, - childrenCitizenId: data.childrenCitizenId, - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - }); - await this.childrenHistoryRepository.save(history); - } + + const history = new ProfileChildrenHistory(); + history.profileChildrenId = data.id; + + await Promise.all([ + this.childrenRepository.save(data), + (history.profileChildrenId = data.id), + this.childrenHistoryRepository.save(history), + ]); return new HttpSuccess(); } @@ -120,19 +112,17 @@ export class ProfileChildrenEmployeeTempController extends Controller { if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileChildrenHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); - record.lastUpdateUserId = req.user.sub; - record.lastUpdateFullName = req.user.name; + Object.assign(history, body); + record.childrenCitizenId = Extension.CheckCitizen(String(record.childrenCitizenId)); history.profileChildrenId = record.id; - history.childrenCareer = record.childrenCareer; - history.childrenFirstName = record.childrenFirstName; - history.childrenLastName = record.childrenLastName; - history.childrenPrefix = record.childrenPrefix; - history.childrenLive = record.childrenLive; - history.childrenCitizenId = record.childrenCitizenId; - (history.lastUpdateUserId = req.user.sub), (history.lastUpdateFullName = req.user.name); + record.lastUpdateUserId = req.user.sub; + record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.childrenRepository.save(record), diff --git a/src/controllers/ProfileDisciplineController.ts b/src/controllers/ProfileDisciplineController.ts index 948527d1..e7866e2e 100644 --- a/src/controllers/ProfileDisciplineController.ts +++ b/src/controllers/ProfileDisciplineController.ts @@ -146,7 +146,14 @@ export class ProfileDisciplineController extends Controller { Object.assign(data, { ...body, ...meta }); - await this.disciplineRepository.save(data); + const history = new ProfileDisciplineHistory(); + history.profileDisciplineId = data.id; + + await Promise.all([ + this.disciplineRepository.save(data), + (history.profileDisciplineId = data.id), + this.disciplineHistoryRepository.save(history), + ]); return new HttpSuccess(); } @@ -164,11 +171,16 @@ export class ProfileDisciplineController extends Controller { const history = new ProfileDisciplineHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileDisciplineId = disciplineId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.disciplineRepository.save(record), diff --git a/src/controllers/ProfileDisciplineEmployeeController.ts b/src/controllers/ProfileDisciplineEmployeeController.ts index 51486566..8bc9f765 100644 --- a/src/controllers/ProfileDisciplineEmployeeController.ts +++ b/src/controllers/ProfileDisciplineEmployeeController.ts @@ -137,7 +137,14 @@ export class ProfileDisciplineEmployeeController extends Controller { Object.assign(data, { ...body, ...meta }); - await this.disciplineRepository.save(data); + const history = new ProfileDisciplineHistory(); + history.profileDisciplineId = data.id; + + await Promise.all([ + this.disciplineRepository.save(data), + (history.profileDisciplineId = data.id), + this.disciplineHistoryRepository.save(history), + ]); return new HttpSuccess(); } @@ -155,11 +162,16 @@ export class ProfileDisciplineEmployeeController extends Controller { const history = new ProfileDisciplineHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileDisciplineId = disciplineId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.disciplineRepository.save(record), diff --git a/src/controllers/ProfileDisciplineEmployeeTempController.ts b/src/controllers/ProfileDisciplineEmployeeTempController.ts index 99eae4c0..13a46a7f 100644 --- a/src/controllers/ProfileDisciplineEmployeeTempController.ts +++ b/src/controllers/ProfileDisciplineEmployeeTempController.ts @@ -137,7 +137,14 @@ export class ProfileDisciplineEmployeeTempController extends Controller { Object.assign(data, { ...body, ...meta }); - await this.disciplineRepository.save(data); + const history = new ProfileDisciplineHistory(); + history.profileDisciplineId = data.id; + + await Promise.all([ + this.disciplineRepository.save(data), + (history.profileDisciplineId = data.id), + this.disciplineHistoryRepository.save(history), + ]); return new HttpSuccess(); } @@ -155,11 +162,16 @@ export class ProfileDisciplineEmployeeTempController extends Controller { const history = new ProfileDisciplineHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileDisciplineId = disciplineId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.disciplineRepository.save(record), diff --git a/src/controllers/ProfileDutyController.ts b/src/controllers/ProfileDutyController.ts index c46f21dd..34fccb83 100644 --- a/src/controllers/ProfileDutyController.ts +++ b/src/controllers/ProfileDutyController.ts @@ -149,8 +149,14 @@ export class ProfileDutyController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileDutyHistory(); + history.profileDutyId = data.id; - await this.dutyRepository.save(data); + await Promise.all([ + this.dutyRepository.save(data), + (history.profileDutyId = data.id), + this.dutyHistoryRepository.save(history), + ]); return new HttpSuccess(); } @@ -168,11 +174,16 @@ export class ProfileDutyController extends Controller { const history = new ProfileDutyHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileDutyId = dutyId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.dutyRepository.save(record), this.dutyHistoryRepository.save(history)]); diff --git a/src/controllers/ProfileDutyEmployeeController.ts b/src/controllers/ProfileDutyEmployeeController.ts index eee9bb48..6b6f9876 100644 --- a/src/controllers/ProfileDutyEmployeeController.ts +++ b/src/controllers/ProfileDutyEmployeeController.ts @@ -110,8 +110,14 @@ export class ProfileDutyEmployeeController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileDutyHistory(); + history.profileDutyId = data.id; - await this.dutyRepository.save(data); + await Promise.all([ + this.dutyRepository.save(data), + (history.profileDutyId = data.id), + this.dutyHistoryRepository.save(history), + ]); return new HttpSuccess(); } @@ -129,11 +135,16 @@ export class ProfileDutyEmployeeController extends Controller { const history = new ProfileDutyHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileDutyId = dutyId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.dutyRepository.save(record), this.dutyHistoryRepository.save(history)]); diff --git a/src/controllers/ProfileDutyEmployeeTempController.ts b/src/controllers/ProfileDutyEmployeeTempController.ts index 49f2958a..5ffdfaac 100644 --- a/src/controllers/ProfileDutyEmployeeTempController.ts +++ b/src/controllers/ProfileDutyEmployeeTempController.ts @@ -110,8 +110,14 @@ export class ProfileDutyEmployeeTempController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileDutyHistory(); + history.profileDutyId = data.id; - await this.dutyRepository.save(data); + await Promise.all([ + this.dutyRepository.save(data), + (history.profileDutyId = data.id), + this.dutyHistoryRepository.save(history), + ]); return new HttpSuccess(); } @@ -129,11 +135,16 @@ export class ProfileDutyEmployeeTempController extends Controller { const history = new ProfileDutyHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileDutyId = dutyId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.dutyRepository.save(record), this.dutyHistoryRepository.save(history)]); diff --git a/src/controllers/ProfileEducationsController.ts b/src/controllers/ProfileEducationsController.ts index 717b326c..2b7689b5 100644 --- a/src/controllers/ProfileEducationsController.ts +++ b/src/controllers/ProfileEducationsController.ts @@ -201,14 +201,21 @@ export class ProfileEducationsController extends Controller { Object.assign(data, { ...body, ...meta }); - await this.profileEducationRepo.save(data); + const history = new ProfileEducationHistory(); + history.profileEducationId = data.id; + + await Promise.all([ + this.profileEducationRepo.save(data), + (history.profileEducationId = data.id), + this.profileEducationHistoryRepo.save(history), + ]); return new HttpSuccess(); } @Patch("{educationId}") public async editProfileEducation( - @Body() requestBody: UpdateProfileEducation, + @Body() body: UpdateProfileEducation, @Request() req: RequestWithUser, @Path() educationId: string, ) { @@ -218,12 +225,16 @@ export class ProfileEducationsController extends Controller { const history = new ProfileEducationHistory(); - Object.assign(history, { ...record, id: undefined }); - Object.assign(record, requestBody); + Object.assign(record, body); + Object.assign(history, body); history.profileEducationId = educationId; - history.lastUpdateFullName = req.user.name; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.profileEducationRepo.save(record), diff --git a/src/controllers/ProfileEducationsEmployeeController.ts b/src/controllers/ProfileEducationsEmployeeController.ts index be789a8f..ec02db20 100644 --- a/src/controllers/ProfileEducationsEmployeeController.ts +++ b/src/controllers/ProfileEducationsEmployeeController.ts @@ -202,15 +202,21 @@ export class ProfileEducationsEmployeeController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileEducationHistory(); + history.profileEducationId = data.id; - await this.profileEducationRepo.save(data); + await Promise.all([ + this.profileEducationRepo.save(data), + (history.profileEducationId = data.id), + this.profileEducationHistoryRepo.save(history), + ]); return new HttpSuccess(); } @Patch("{educationId}") public async editProfileEducation( - @Body() requestBody: UpdateProfileEducation, + @Body() body: UpdateProfileEducation, @Request() req: RequestWithUser, @Path() educationId: string, ) { @@ -220,12 +226,16 @@ export class ProfileEducationsEmployeeController extends Controller { const history = new ProfileEducationHistory(); - Object.assign(history, { ...record, id: undefined }); - Object.assign(record, requestBody); + Object.assign(record, body); + Object.assign(history, body); history.profileEducationId = educationId; - history.lastUpdateFullName = req.user.name; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.profileEducationRepo.save(record), diff --git a/src/controllers/ProfileEducationsEmployeeTempController.ts b/src/controllers/ProfileEducationsEmployeeTempController.ts index ea2db787..33fb7dff 100644 --- a/src/controllers/ProfileEducationsEmployeeTempController.ts +++ b/src/controllers/ProfileEducationsEmployeeTempController.ts @@ -202,15 +202,21 @@ export class ProfileEducationsEmployeeTempController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileEducationHistory(); + history.profileEducationId = data.id; - await this.profileEducationRepo.save(data); + await Promise.all([ + this.profileEducationRepo.save(data), + (history.profileEducationId = data.id), + this.profileEducationHistoryRepo.save(history), + ]); return new HttpSuccess(); } @Patch("{educationId}") public async editProfileEducation( - @Body() requestBody: UpdateProfileEducation, + @Body() body: UpdateProfileEducation, @Request() req: RequestWithUser, @Path() educationId: string, ) { @@ -220,12 +226,16 @@ export class ProfileEducationsEmployeeTempController extends Controller { const history = new ProfileEducationHistory(); - Object.assign(history, { ...record, id: undefined }); - Object.assign(record, requestBody); + Object.assign(record, body); + Object.assign(history, body); history.profileEducationId = educationId; - history.lastUpdateFullName = req.user.name; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.profileEducationRepo.save(record), diff --git a/src/controllers/ProfileFamilyCoupleController.ts b/src/controllers/ProfileFamilyCoupleController.ts index 89d77613..378400cd 100644 --- a/src/controllers/ProfileFamilyCoupleController.ts +++ b/src/controllers/ProfileFamilyCoupleController.ts @@ -234,30 +234,18 @@ export class ProfileFamilyCoupleController extends Controller { familyCouple.createdFullName = req.user.name; familyCouple.lastUpdateUserId = req.user.sub; familyCouple.lastUpdateFullName = req.user.name; - await this.ProfileFamilyCouple.save(familyCouple); - if (familyCouple) { - profile.relationship = familyCouple.relationship; //update profile.relationship - const history: ProfileFamilyCoupleHistory = Object.assign(new ProfileFamilyCoupleHistory(), { - profileFamilyCoupleId: familyCouple.id, - couplePrefix: familyCouple.couplePrefix, - coupleFirstName: familyCouple.coupleFirstName, - coupleLastName: familyCouple.coupleLastName, - coupleLastNameOld: familyCouple.coupleLastNameOld, - coupleCareer: familyCouple.coupleCareer, - coupleCitizenId: familyCouple.coupleCitizenId, - coupleLive: familyCouple.coupleLive, - relationship: familyCouple.relationship, - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - }); - await Promise.all([ - this.profileRepo.save(profile), - this.ProfileFamilyCoupleHistory.save(history), - ]); - } + profile.relationship = familyCouple.relationship; //update profileEmployee.relationship + const history = new ProfileFamilyCoupleHistory(); + history.profileFamilyCoupleId = familyCouple.id; + + await Promise.all([ + this.profileRepo.save(profile), + this.ProfileFamilyCouple.save(familyCouple), + (history.profileFamilyCoupleId = familyCouple.id), + this.ProfileFamilyCoupleHistory.save(history), + ]); + return new HttpSuccess(familyCouple.id); } @@ -272,22 +260,17 @@ export class ProfileFamilyCoupleController extends Controller { if (!familyCouple) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileFamilyCoupleHistory(); - Object.assign(history, { ...familyCouple, id: undefined }); Object.assign(familyCouple, body); - (familyCouple.lastUpdateUserId = req.user.sub), - (familyCouple.lastUpdateFullName = req.user.name); + Object.assign(history, { ...familyCouple, id: undefined }); + familyCouple.coupleCitizenId = Extension.CheckCitizen(String(body.coupleCitizenId)); history.profileFamilyCoupleId = familyCouple.id; - (history.couplePrefix = familyCouple.couplePrefix), - (history.coupleFirstName = familyCouple.coupleFirstName), - (history.coupleLastName = familyCouple.coupleLastName), - (history.coupleLastNameOld = familyCouple.coupleLastNameOld), - (history.coupleCareer = familyCouple.coupleCareer), - (history.coupleCitizenId = familyCouple.coupleCitizenId), - (history.coupleLive = familyCouple.coupleLive), - (history.relationship = familyCouple.relationship), - (history.lastUpdateUserId = req.user.sub), - (history.lastUpdateFullName = req.user.name); + familyCouple.lastUpdateUserId = req.user.sub; + familyCouple.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.ProfileFamilyCouple.save(familyCouple), diff --git a/src/controllers/ProfileFamilyCoupleEmployeeController.ts b/src/controllers/ProfileFamilyCoupleEmployeeController.ts index fb270157..3e906346 100644 --- a/src/controllers/ProfileFamilyCoupleEmployeeController.ts +++ b/src/controllers/ProfileFamilyCoupleEmployeeController.ts @@ -234,30 +234,18 @@ export class ProfileFamilyCoupleEmployeeController extends Controller { familyCouple.createdFullName = req.user.name; familyCouple.lastUpdateUserId = req.user.sub; familyCouple.lastUpdateFullName = req.user.name; - await this.ProfileFamilyCouple.save(familyCouple); - if (familyCouple) { - profile.relationship = familyCouple.relationship; //update profileEmployee.relationship - const history: ProfileFamilyCoupleHistory = Object.assign(new ProfileFamilyCoupleHistory(), { - profileFamilyCoupleId: familyCouple.id, - couplePrefix: familyCouple.couplePrefix, - coupleFirstName: familyCouple.coupleFirstName, - coupleLastName: familyCouple.coupleLastName, - coupleLastNameOld: familyCouple.coupleLastNameOld, - coupleCareer: familyCouple.coupleCareer, - coupleCitizenId: familyCouple.coupleCitizenId, - coupleLive: familyCouple.coupleLive, - relationship: familyCouple.relationship, - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - }); - await Promise.all([ - this.profileRepo.save(profile), - this.ProfileFamilyCoupleHistory.save(history), - ]); - } + profile.relationship = familyCouple.relationship; //update profileEmployee.relationship + const history = new ProfileFamilyCoupleHistory(); + history.profileFamilyCoupleId = familyCouple.id; + + await Promise.all([ + this.profileRepo.save(profile), + this.ProfileFamilyCouple.save(familyCouple), + (history.profileFamilyCoupleId = familyCouple.id), + this.ProfileFamilyCoupleHistory.save(history), + ]); + return new HttpSuccess(familyCouple.id); } @@ -274,22 +262,17 @@ export class ProfileFamilyCoupleEmployeeController extends Controller { if (!familyCouple) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileFamilyCoupleHistory(); - Object.assign(history, { ...familyCouple, id: undefined }); Object.assign(familyCouple, body); - (familyCouple.lastUpdateUserId = req.user.sub), - (familyCouple.lastUpdateFullName = req.user.name); + Object.assign(history, { ...familyCouple, id: undefined }); + familyCouple.coupleCitizenId = Extension.CheckCitizen(String(body.coupleCitizenId)); history.profileFamilyCoupleId = familyCouple.id; - (history.couplePrefix = familyCouple.couplePrefix), - (history.coupleFirstName = familyCouple.coupleFirstName), - (history.coupleLastName = familyCouple.coupleLastName), - (history.coupleLastNameOld = familyCouple.coupleLastNameOld), - (history.coupleCareer = familyCouple.coupleCareer), - (history.coupleCitizenId = familyCouple.coupleCitizenId), - (history.coupleLive = familyCouple.coupleLive), - (history.relationship = familyCouple.relationship), - (history.lastUpdateUserId = req.user.sub), - (history.lastUpdateFullName = req.user.name); + familyCouple.lastUpdateUserId = req.user.sub; + familyCouple.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.ProfileFamilyCouple.save(familyCouple), diff --git a/src/controllers/ProfileFamilyCoupleEmployeeTempController.ts b/src/controllers/ProfileFamilyCoupleEmployeeTempController.ts index 947befca..b577035f 100644 --- a/src/controllers/ProfileFamilyCoupleEmployeeTempController.ts +++ b/src/controllers/ProfileFamilyCoupleEmployeeTempController.ts @@ -236,28 +236,17 @@ export class ProfileFamilyCoupleEmployeeTempController extends Controller { familyCouple.lastUpdateFullName = req.user.name; await this.ProfileFamilyCouple.save(familyCouple); - if (familyCouple) { - profile.relationship = familyCouple.relationship; //update profileEmployee.relationship - const history: ProfileFamilyCoupleHistory = Object.assign(new ProfileFamilyCoupleHistory(), { - profileFamilyCoupleId: familyCouple.id, - couplePrefix: familyCouple.couplePrefix, - coupleFirstName: familyCouple.coupleFirstName, - coupleLastName: familyCouple.coupleLastName, - coupleLastNameOld: familyCouple.coupleLastNameOld, - coupleCareer: familyCouple.coupleCareer, - coupleCitizenId: familyCouple.coupleCitizenId, - coupleLive: familyCouple.coupleLive, - relationship: familyCouple.relationship, - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - }); - await Promise.all([ - this.profileRepo.save(profile), - this.ProfileFamilyCoupleHistory.save(history), - ]); - } + profile.relationship = familyCouple.relationship; //update profileEmployee.relationship + const history = new ProfileFamilyCoupleHistory(); + history.profileFamilyCoupleId = familyCouple.id; + + await Promise.all([ + this.profileRepo.save(profile), + this.ProfileFamilyCouple.save(familyCouple), + (history.profileFamilyCoupleId = familyCouple.id), + this.ProfileFamilyCoupleHistory.save(history), + ]); + return new HttpSuccess(familyCouple.id); } @@ -274,22 +263,17 @@ export class ProfileFamilyCoupleEmployeeTempController extends Controller { if (!familyCouple) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileFamilyCoupleHistory(); - Object.assign(history, { ...familyCouple, id: undefined }); Object.assign(familyCouple, body); - (familyCouple.lastUpdateUserId = req.user.sub), - (familyCouple.lastUpdateFullName = req.user.name); + Object.assign(history, { ...familyCouple, id: undefined }); + familyCouple.coupleCitizenId = Extension.CheckCitizen(String(body.coupleCitizenId)); history.profileFamilyCoupleId = familyCouple.id; - (history.couplePrefix = familyCouple.couplePrefix), - (history.coupleFirstName = familyCouple.coupleFirstName), - (history.coupleLastName = familyCouple.coupleLastName), - (history.coupleLastNameOld = familyCouple.coupleLastNameOld), - (history.coupleCareer = familyCouple.coupleCareer), - (history.coupleCitizenId = familyCouple.coupleCitizenId), - (history.coupleLive = familyCouple.coupleLive), - (history.relationship = familyCouple.relationship), - (history.lastUpdateUserId = req.user.sub), - (history.lastUpdateFullName = req.user.name); + familyCouple.lastUpdateUserId = req.user.sub; + familyCouple.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.ProfileFamilyCouple.save(familyCouple), diff --git a/src/controllers/ProfileFamilyFatherController.ts b/src/controllers/ProfileFamilyFatherController.ts index 25ee303f..4fcaeb88 100644 --- a/src/controllers/ProfileFamilyFatherController.ts +++ b/src/controllers/ProfileFamilyFatherController.ts @@ -220,24 +220,16 @@ export class ProfileFamilyFatherController extends Controller { familyFather.createdFullName = req.user.name; familyFather.lastUpdateUserId = req.user.sub; familyFather.lastUpdateFullName = req.user.name; - await this.ProfileFamilyFather.save(familyFather); - if (familyFather) { - const history: ProfileFamilyFatherHistory = Object.assign(new ProfileFamilyFatherHistory(), { - profileFamilyFatherId: familyFather.id, - fatherPrefix: familyFather.fatherPrefix, - fatherFirstName: familyFather.fatherFirstName, - fatherLastName: familyFather.fatherLastName, - fatherCareer: familyFather.fatherCareer, - fatherCitizenId: familyFather.fatherCitizenId, - fatherLive: familyFather.fatherLive, - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - }); - await this.ProfileFamilyFatherHistory.save(history); - } + const history = new ProfileFamilyFatherHistory(); + history.profileFamilyFatherId = familyFather.id; + + await Promise.all([ + this.ProfileFamilyFather.save(familyFather), + (history.profileFamilyFatherId = familyFather.id), + this.ProfileFamilyFatherHistory.save(history), + ]); + return new HttpSuccess(familyFather.id); } @@ -252,20 +244,17 @@ export class ProfileFamilyFatherController extends Controller { if (!familyFather) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileFamilyFatherHistory(); - Object.assign(history, { ...familyFather, id: undefined }); Object.assign(familyFather, body); - (familyFather.lastUpdateUserId = req.user.sub), - (familyFather.lastUpdateFullName = req.user.name); + Object.assign(history, { ...familyFather, id: undefined }); + familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId)); history.profileFamilyFatherId = familyFather.id; //profileFamilyFatherId - (history.fatherPrefix = familyFather.fatherPrefix), - (history.fatherFirstName = familyFather.fatherFirstName), - (history.fatherLastName = familyFather.fatherLastName), - (history.fatherCareer = familyFather.fatherCareer), - (history.fatherCitizenId = familyFather.fatherCitizenId), - (history.fatherLive = familyFather.fatherLive), - (history.lastUpdateUserId = req.user.sub), - (history.lastUpdateFullName = req.user.name); + familyFather.lastUpdateUserId = req.user.sub; + familyFather.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.ProfileFamilyFather.save(familyFather), diff --git a/src/controllers/ProfileFamilyFatherEmployeeController.ts b/src/controllers/ProfileFamilyFatherEmployeeController.ts index ee4a0c54..645048cb 100644 --- a/src/controllers/ProfileFamilyFatherEmployeeController.ts +++ b/src/controllers/ProfileFamilyFatherEmployeeController.ts @@ -220,24 +220,16 @@ export class ProfileFamilyFatherEmployeeController extends Controller { familyFather.createdFullName = req.user.name; familyFather.lastUpdateUserId = req.user.sub; familyFather.lastUpdateFullName = req.user.name; - await this.ProfileFamilyFather.save(familyFather); - if (familyFather) { - const history: ProfileFamilyFatherHistory = Object.assign(new ProfileFamilyFatherHistory(), { - profileFamilyFatherId: familyFather.id, - fatherPrefix: familyFather.fatherPrefix, - fatherFirstName: familyFather.fatherFirstName, - fatherLastName: familyFather.fatherLastName, - fatherCareer: familyFather.fatherCareer, - fatherCitizenId: familyFather.fatherCitizenId, - fatherLive: familyFather.fatherLive, - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - }); - await this.ProfileFamilyFatherHistory.save(history); - } + const history = new ProfileFamilyFatherHistory(); + history.profileFamilyFatherId = familyFather.id; + + await Promise.all([ + this.ProfileFamilyFather.save(familyFather), + (history.profileFamilyFatherId = familyFather.id), + this.ProfileFamilyFatherHistory.save(history), + ]); + return new HttpSuccess(familyFather.id); } @@ -254,20 +246,17 @@ export class ProfileFamilyFatherEmployeeController extends Controller { if (!familyFather) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileFamilyFatherHistory(); - Object.assign(history, { ...familyFather, id: undefined }); Object.assign(familyFather, body); - (familyFather.lastUpdateUserId = req.user.sub), - (familyFather.lastUpdateFullName = req.user.name); + Object.assign(history, { ...familyFather, id: undefined }); + familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId)); history.profileFamilyFatherId = familyFather.id; - (history.fatherPrefix = familyFather.fatherPrefix), - (history.fatherFirstName = familyFather.fatherFirstName), - (history.fatherLastName = familyFather.fatherLastName), - (history.fatherCareer = familyFather.fatherCareer), - (history.fatherCitizenId = familyFather.fatherCitizenId), - (history.fatherLive = familyFather.fatherLive), - (history.lastUpdateUserId = req.user.sub), - (history.lastUpdateFullName = req.user.name); + familyFather.lastUpdateUserId = req.user.sub; + familyFather.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.ProfileFamilyFather.save(familyFather), diff --git a/src/controllers/ProfileFamilyFatherEmployeeTempController.ts b/src/controllers/ProfileFamilyFatherEmployeeTempController.ts index 9c9c30ed..a200f4ea 100644 --- a/src/controllers/ProfileFamilyFatherEmployeeTempController.ts +++ b/src/controllers/ProfileFamilyFatherEmployeeTempController.ts @@ -220,24 +220,16 @@ export class ProfileFamilyFatherEmployeeTempController extends Controller { familyFather.createdFullName = req.user.name; familyFather.lastUpdateUserId = req.user.sub; familyFather.lastUpdateFullName = req.user.name; - await this.ProfileFamilyFather.save(familyFather); - if (familyFather) { - const history: ProfileFamilyFatherHistory = Object.assign(new ProfileFamilyFatherHistory(), { - profileFamilyFatherId: familyFather.id, - fatherPrefix: familyFather.fatherPrefix, - fatherFirstName: familyFather.fatherFirstName, - fatherLastName: familyFather.fatherLastName, - fatherCareer: familyFather.fatherCareer, - fatherCitizenId: familyFather.fatherCitizenId, - fatherLive: familyFather.fatherLive, - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - }); - await this.ProfileFamilyFatherHistory.save(history); - } + const history = new ProfileFamilyFatherHistory(); + history.profileFamilyFatherId = familyFather.id; + + await Promise.all([ + this.ProfileFamilyFather.save(familyFather), + (history.profileFamilyFatherId = familyFather.id), + this.ProfileFamilyFatherHistory.save(history), + ]); + return new HttpSuccess(familyFather.id); } @@ -254,20 +246,17 @@ export class ProfileFamilyFatherEmployeeTempController extends Controller { if (!familyFather) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileFamilyFatherHistory(); - Object.assign(history, { ...familyFather, id: undefined }); Object.assign(familyFather, body); - (familyFather.lastUpdateUserId = req.user.sub), - (familyFather.lastUpdateFullName = req.user.name); + Object.assign(history, { ...familyFather, id: undefined }); + familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId)); history.profileFamilyFatherId = familyFather.id; - (history.fatherPrefix = familyFather.fatherPrefix), - (history.fatherFirstName = familyFather.fatherFirstName), - (history.fatherLastName = familyFather.fatherLastName), - (history.fatherCareer = familyFather.fatherCareer), - (history.fatherCitizenId = familyFather.fatherCitizenId), - (history.fatherLive = familyFather.fatherLive), - (history.lastUpdateUserId = req.user.sub), - (history.lastUpdateFullName = req.user.name); + familyFather.lastUpdateUserId = req.user.sub; + familyFather.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.ProfileFamilyFather.save(familyFather), diff --git a/src/controllers/ProfileFamilyHistoryController.ts b/src/controllers/ProfileFamilyHistoryController.ts deleted file mode 100644 index 096a8676..00000000 --- a/src/controllers/ProfileFamilyHistoryController.ts +++ /dev/null @@ -1,422 +0,0 @@ -import { - Body, - Controller, - Delete, - Example, - Get, - Patch, - Path, - Post, - Request, - Route, - Security, - Tags, -} from "tsoa"; -import { AppDataSource } from "../database/data-source"; -import { - CreateChildren, - CreateProfileFamily, - ProfileFamilyHistory, - UpdateProfileFamily, -} from "../entities/ProfileFamily"; -import HttpSuccess from "../interfaces/http-success"; -import HttpStatus from "../interfaces/http-status"; -import HttpError from "../interfaces/http-error"; -import { RequestWithUser } from "../middlewares/user"; -import { Profile } from "../entities/Profile"; -import { ProfileChildren } from "../entities/ProfileChildren"; -import { ProfileChildrenHistory } from "../entities/ProfileChildrenHistory"; - -@Route("api/v1/org/profile/family") -@Tags("ProfileFamilyHistory") -@Security("bearerAuth") -export class ProfileFamilyHistoryController extends Controller { - private profileRepo = AppDataSource.getRepository(Profile); - private familyHistoryRepo = AppDataSource.getRepository(ProfileFamilyHistory); - private childrenRepo = AppDataSource.getRepository(ProfileChildren); - private childrenHistoryRepo = AppDataSource.getRepository(ProfileChildrenHistory); - - @Get("user") - public async getFamilyHistoryUser(@Request() request: { user: Record }) { - const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub }); - if (!profile) { - throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); - } - const family = await this.familyHistoryRepo.find({ - take: 1, - order: { lastUpdatedAt: "DESC" }, - where: { profileId: profile.id }, - }); - const children = await this.childrenRepo.find({ - order: { createdAt: "ASC" }, - where: { profileId: profile.id }, - }); - return new HttpSuccess( - family.length > 0 - ? { - ...family[0], - children, - } - : null, - ); - } - - @Get("{profileId}") - @Example({ - status: 200, - message: "สำเร็จ", - result: { - id: "6207ae29-05ef-4abb-9a37-a887265d671e", - createdAt: "2024-03-19T11:00:29.769Z", - createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - lastUpdatedAt: "2024-03-19T11:00:29.769Z", - lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - createdFullName: "สาวิตรี ศรีสมัย", - lastUpdateFullName: "สาวิตรี ศรีสมัย", - couple: true, - couplePrefix: "string", - coupleFirstName: "string", - coupleLastName: "string", - coupleLastNameOld: "string", - coupleCareer: "string", - coupleCitizenId: "string", - coupleLive: true, - fatherPrefix: "string", - fatherFirstName: "string", - fatherLastName: "string", - fatherCareer: "string", - fatherCitizenId: "string", - fatherLive: true, - motherPrefix: "string", - motherFirstName: "string", - motherLastName: "string", - motherCareer: "string", - motherCitizenId: "string", - motherLive: true, - profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", - children: [ - { - id: "9717f95a-cd20-4edb-a8b9-443c1dd99f90", - createdAt: "2024-03-19T11:00:29.785Z", - createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - lastUpdatedAt: "2024-03-19T11:00:29.785Z", - lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - createdFullName: "สาวิตรี ศรีสมัย", - lastUpdateFullName: "สาวิตรี ศรีสมัย", - childrenCareer: "string", - childrenFirstName: "string", - childrenLastName: "string", - childrenPrefix: "string", - childrenLive: "string", - childrenCitizenId: "string", - profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", - }, - ], - }, - }) - public async getFamilyHistory(@Path() profileId: string) { - const family = await this.familyHistoryRepo.find({ - take: 1, - order: { lastUpdatedAt: "DESC" }, - where: { profileId }, - }); - const children = await this.childrenRepo.find({ - order: { createdAt: "ASC" }, - where: { profileId }, - }); - return new HttpSuccess( - family.length > 0 - ? { - ...family[0], - children, - } - : null, - ); - } - - /** - * - * @summary ประวัติแก้ไขครอบครัว by keycloak - * - */ - @Get("history/user") - public async familyHistoryUser(@Request() request: RequestWithUser) { - const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub }); - if (!profile) { - throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); - } - const family = await this.familyHistoryRepo.find({ - order: { lastUpdatedAt: "DESC" }, - where: { - profileId: profile.id, - }, - }); - - family.shift(); - - const record = await Promise.all( - family.map(async (v) => ({ - ...v, - children: await this.childrenHistoryRepo.find({ - order: { createdAt: "ASC" }, - }), - })), - ); - - return new HttpSuccess(record); - } - - @Get("history/{profileId}") - @Example({ - status: 200, - message: "สำเร็จ", - result: [ - { - id: "6207ae29-05ef-4abb-9a37-a887265d671e", - createdAt: "2024-03-19T11:00:29.769Z", - createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - lastUpdatedAt: "2024-03-19T11:00:29.769Z", - lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - createdFullName: "สาวิตรี ศรีสมัย", - lastUpdateFullName: "สาวิตรี ศรีสมัย", - couple: true, - couplePrefix: "string", - coupleFirstName: "string", - coupleLastName: "string", - coupleLastNameOld: "string", - coupleCareer: "string", - coupleCitizenId: "string", - coupleLive: true, - fatherPrefix: "string", - fatherFirstName: "string", - fatherLastName: "string", - fatherCareer: "string", - fatherCitizenId: "string", - fatherLive: true, - motherPrefix: "string", - motherFirstName: "string", - motherLastName: "string", - motherCareer: "string", - motherCitizenId: "string", - motherLive: true, - profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", - children: [ - { - id: "980835f8-d766-4b6b-b7dc-1726db889352", - createdAt: "2024-03-19T12:42:11.437Z", - createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - lastUpdatedAt: "2024-03-19T12:42:11.437Z", - lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - createdFullName: "สาวิตรี ศรีสมัย", - lastUpdateFullName: "สาวิตรี ศรีสมัย", - childrenCareer: "hey", - childrenFirstName: "hey", - childrenLastName: "hey", - childrenPrefix: "hey", - childrenLive: "hey", - childrenCitizenId: "hey", - profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", - profileFamilyHistoryId: "6207ae29-05ef-4abb-9a37-a887265d671e", - profileChildrenId: "222db098-3f08-4d0b-ac58-7914fa41032b", - }, - { - id: "cd539e67-3f34-4b8c-a9da-b7eb193fa0b4", - createdAt: "2024-03-19T12:42:11.438Z", - createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - lastUpdatedAt: "2024-03-19T12:42:11.438Z", - lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - createdFullName: "สาวิตรี ศรีสมัย", - lastUpdateFullName: "สาวิตรี ศรีสมัย", - childrenCareer: "ay", - childrenFirstName: "ay", - childrenLastName: "ay", - childrenPrefix: "ay", - childrenLive: "ay", - childrenCitizenId: "ay", - profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", - profileFamilyHistoryId: "6207ae29-05ef-4abb-9a37-a887265d671e", - profileChildrenId: "d0495491-9e9f-4d2e-bc3b-c2833955fa12", - }, - ], - }, - { - id: "60bbf8d1-f674-4d88-b3fb-b5603e408319", - createdAt: "2024-03-19T11:00:29.769Z", - createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - lastUpdatedAt: "2024-03-19T11:00:29.769Z", - lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - createdFullName: "สาวิตรี ศรีสมัย", - lastUpdateFullName: "สาวิตรี ศรีสมัย", - couple: true, - couplePrefix: "string", - coupleFirstName: "string", - coupleLastName: "string", - coupleLastNameOld: "string", - coupleCareer: "yeah", - coupleCitizenId: "yeah", - coupleLive: true, - fatherPrefix: "yeah", - fatherFirstName: "haaa", - fatherLastName: "yeah", - fatherCareer: "haaa", - fatherCitizenId: "yeah", - fatherLive: true, - motherPrefix: "string", - motherFirstName: "string", - motherLastName: "string", - motherCareer: "string", - motherCitizenId: "string", - motherLive: true, - profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", - children: [], - }, - ], - }) - public async familyHistory(@Path() profileId: string) { - const family = await this.familyHistoryRepo.find({ - order: { lastUpdatedAt: "DESC" }, - where: { profileId }, - }); - - family.shift(); - - const record = await Promise.all( - family.map(async (v) => ({ - ...v, - children: await this.childrenHistoryRepo.find({ - order: { createdAt: "ASC" }, - // where: { profileFamilyHistoryId: v.id }, - }), - })), - ); - - return new HttpSuccess(record); - } - - @Post() - public async newFamilyHistory( - @Request() req: RequestWithUser, - @Body() body: CreateProfileFamily, - ) { - if (!body.profileId) { - throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); - } - - const profile = await this.profileRepo.findOneBy({ id: body.profileId }); - - if (!profile) { - throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); - } - - const dataFamily = new ProfileFamilyHistory(); - - const meta = { - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - }; - - const { children, ...family } = body; - - Object.assign(dataFamily, { ...family, ...meta }); - - await this.familyHistoryRepo.save(dataFamily); - - const profileId = dataFamily.profileId; - - await Promise.all( - children.map(async (v) => { - const dataChildren = new ProfileChildrenHistory(); - - Object.assign(dataChildren, { ...v, profileId, ...meta }); - - await this.childrenRepo.save(dataChildren); - }), - ); - - return new HttpSuccess(); - } - - @Patch("{profileId}") - public async editFamilyHistory( - @Request() req: RequestWithUser, - @Body() body: UpdateProfileFamily, - @Path() profileId: string, - ) { - const familyRecord = ( - await this.familyHistoryRepo.find({ - take: 1, - order: { lastUpdatedAt: "DESC" }, - where: { profileId }, - }) - )[0]; - - if (!familyRecord) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - - const childrenRecord = await this.childrenRepo.findBy({ - profileId: familyRecord.profileId, - }); - - const historyData = new ProfileFamilyHistory(); - - const { children, ...family } = body; - - Object.assign(historyData, { ...familyRecord, ...family, id: undefined }); - historyData.lastUpdateFullName = req.user.name; - historyData.lastUpdateFullName = req.user.name; - - const newChild: CreateChildren[] = []; - - for (let child of children) { - newChild.push( - Object.assign(new ProfileChildren(), { - ...child, - profileId, - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - id: !childrenRecord.find((v) => v.id === child.id) ? undefined : child.id, - }), - ); - } - - await Promise.all([ - this.familyHistoryRepo.save({ ...historyData, lastUpdatedAt: undefined }), - ...newChild.map(async (v) => { - return await this.childrenRepo.save(v); - }), - ...childrenRecord.map(async (v) => { - if (!children.find((x) => x.id === v.id)) { - return await this.childrenRepo.delete({ id: v.id }); - } - }), - ...childrenRecord.map(async (v) => { - if (children.find((x) => x.id === v.id)) { - return await this.childrenHistoryRepo.save( - this.childrenHistoryRepo.create({ - ...v, - // profileFamilyHistoryId: familyRecord.id, - profileChildrenId: v.id, - id: undefined, - }), - ); - } - }), - ]); - - return new HttpSuccess(); - } - - @Delete("{profileId}") - public async deleteFamilyHistory(@Path() profileId: string) { - const result = await this.familyHistoryRepo.delete({ id: profileId }); - - if (result.affected == undefined || result.affected <= 0) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - } - - return new HttpSuccess(); - } -} diff --git a/src/controllers/ProfileFamilyHistoryEmployeeController.ts b/src/controllers/ProfileFamilyHistoryEmployeeController.ts deleted file mode 100644 index 34647a43..00000000 --- a/src/controllers/ProfileFamilyHistoryEmployeeController.ts +++ /dev/null @@ -1,422 +0,0 @@ -import { - Body, - Controller, - Delete, - Example, - Get, - Patch, - Path, - Post, - Request, - Route, - Security, - Tags, -} from "tsoa"; -import { AppDataSource } from "../database/data-source"; -import { - CreateChildren, - CreateProfileFamilyEmployee, - ProfileFamilyHistory, - UpdateProfileFamily, -} from "../entities/ProfileFamily"; -import HttpSuccess from "../interfaces/http-success"; -import HttpStatus from "../interfaces/http-status"; -import HttpError from "../interfaces/http-error"; -import { RequestWithUser } from "../middlewares/user"; -import { ProfileEmployee } from "../entities/ProfileEmployee"; -import { ProfileChildren } from "../entities/ProfileChildren"; -import { ProfileChildrenHistory } from "../entities/ProfileChildrenHistory"; - -@Route("api/v1/org/profile-employee/family") -@Tags("ProfileFamilyHistoryEmployee") -@Security("bearerAuth") -export class ProfileFamilyHistoryEmployeeController extends Controller { - private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); - private familyHistoryRepo = AppDataSource.getRepository(ProfileFamilyHistory); - private childrenRepo = AppDataSource.getRepository(ProfileChildren); - private childrenHistoryRepo = AppDataSource.getRepository(ProfileChildrenHistory); - - @Get("user") - public async getFamilyHistoryUser(@Request() request: { user: Record }) { - const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub }); - if (!profile) { - throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); - } - const family = await this.familyHistoryRepo.find({ - take: 1, - order: { lastUpdatedAt: "DESC" }, - where: { profileEmployeeId: profile.id }, - }); - const children = await this.childrenRepo.find({ - order: { createdAt: "ASC" }, - where: { profileEmployeeId: profile.id }, - }); - return new HttpSuccess( - family.length > 0 - ? { - ...family[0], - children, - } - : null, - ); - } - - @Get("{profileEmployeeId}") - @Example({ - status: 200, - message: "สำเร็จ", - result: { - id: "6207ae29-05ef-4abb-9a37-a887265d671e", - createdAt: "2024-03-19T11:00:29.769Z", - createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - lastUpdatedAt: "2024-03-19T11:00:29.769Z", - lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - createdFullName: "สาวิตรี ศรีสมัย", - lastUpdateFullName: "สาวิตรี ศรีสมัย", - couple: true, - couplePrefix: "string", - coupleFirstName: "string", - coupleLastName: "string", - coupleLastNameOld: "string", - coupleCareer: "string", - coupleCitizenId: "string", - coupleLive: true, - fatherPrefix: "string", - fatherFirstName: "string", - fatherLastName: "string", - fatherCareer: "string", - fatherCitizenId: "string", - fatherLive: true, - motherPrefix: "string", - motherFirstName: "string", - motherLastName: "string", - motherCareer: "string", - motherCitizenId: "string", - motherLive: true, - profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", - children: [ - { - id: "9717f95a-cd20-4edb-a8b9-443c1dd99f90", - createdAt: "2024-03-19T11:00:29.785Z", - createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - lastUpdatedAt: "2024-03-19T11:00:29.785Z", - lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - createdFullName: "สาวิตรี ศรีสมัย", - lastUpdateFullName: "สาวิตรี ศรีสมัย", - childrenCareer: "string", - childrenFirstName: "string", - childrenLastName: "string", - childrenPrefix: "string", - childrenLive: "string", - childrenCitizenId: "string", - profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", - }, - ], - }, - }) - public async getFamilyHistory(@Path() profileEmployeeId: string) { - const family = await this.familyHistoryRepo.find({ - take: 1, - order: { lastUpdatedAt: "DESC" }, - where: { profileEmployeeId }, - }); - const children = await this.childrenRepo.find({ - order: { createdAt: "ASC" }, - where: { profileEmployeeId }, - }); - return new HttpSuccess( - family.length > 0 - ? { - ...family[0], - children, - } - : null, - ); - } - - /** - * - * @summary ประวัติแก้ไขครอบครัว by keycloak - * - */ - @Get("history/user") - public async familyHistoryUser(@Request() request: RequestWithUser) { - const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub }); - if (!profile) { - throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); - } - const family = await this.familyHistoryRepo.find({ - order: { lastUpdatedAt: "DESC" }, - where: { - profileEmployeeId: profile.id, - }, - }); - - family.shift(); - - const record = await Promise.all( - family.map(async (v) => ({ - ...v, - children: await this.childrenHistoryRepo.find({ - order: { createdAt: "ASC" }, - }), - })), - ); - - return new HttpSuccess(record); - } - - @Get("history/{profileEmployeeId}") - @Example({ - status: 200, - message: "สำเร็จ", - result: [ - { - id: "6207ae29-05ef-4abb-9a37-a887265d671e", - createdAt: "2024-03-19T11:00:29.769Z", - createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - lastUpdatedAt: "2024-03-19T11:00:29.769Z", - lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - createdFullName: "สาวิตรี ศรีสมัย", - lastUpdateFullName: "สาวิตรี ศรีสมัย", - couple: true, - couplePrefix: "string", - coupleFirstName: "string", - coupleLastName: "string", - coupleLastNameOld: "string", - coupleCareer: "string", - coupleCitizenId: "string", - coupleLive: true, - fatherPrefix: "string", - fatherFirstName: "string", - fatherLastName: "string", - fatherCareer: "string", - fatherCitizenId: "string", - fatherLive: true, - motherPrefix: "string", - motherFirstName: "string", - motherLastName: "string", - motherCareer: "string", - motherCitizenId: "string", - motherLive: true, - profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", - children: [ - { - id: "980835f8-d766-4b6b-b7dc-1726db889352", - createdAt: "2024-03-19T12:42:11.437Z", - createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - lastUpdatedAt: "2024-03-19T12:42:11.437Z", - lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - createdFullName: "สาวิตรี ศรีสมัย", - lastUpdateFullName: "สาวิตรี ศรีสมัย", - childrenCareer: "hey", - childrenFirstName: "hey", - childrenLastName: "hey", - childrenPrefix: "hey", - childrenLive: "hey", - childrenCitizenId: "hey", - profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", - profileFamilyHistoryId: "6207ae29-05ef-4abb-9a37-a887265d671e", - profileChildrenId: "222db098-3f08-4d0b-ac58-7914fa41032b", - }, - { - id: "cd539e67-3f34-4b8c-a9da-b7eb193fa0b4", - createdAt: "2024-03-19T12:42:11.438Z", - createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - lastUpdatedAt: "2024-03-19T12:42:11.438Z", - lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - createdFullName: "สาวิตรี ศรีสมัย", - lastUpdateFullName: "สาวิตรี ศรีสมัย", - childrenCareer: "ay", - childrenFirstName: "ay", - childrenLastName: "ay", - childrenPrefix: "ay", - childrenLive: "ay", - childrenCitizenId: "ay", - profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", - profileFamilyHistoryId: "6207ae29-05ef-4abb-9a37-a887265d671e", - profileChildrenId: "d0495491-9e9f-4d2e-bc3b-c2833955fa12", - }, - ], - }, - { - id: "60bbf8d1-f674-4d88-b3fb-b5603e408319", - createdAt: "2024-03-19T11:00:29.769Z", - createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - lastUpdatedAt: "2024-03-19T11:00:29.769Z", - lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - createdFullName: "สาวิตรี ศรีสมัย", - lastUpdateFullName: "สาวิตรี ศรีสมัย", - couple: true, - couplePrefix: "string", - coupleFirstName: "string", - coupleLastName: "string", - coupleLastNameOld: "string", - coupleCareer: "yeah", - coupleCitizenId: "yeah", - coupleLive: true, - fatherPrefix: "yeah", - fatherFirstName: "haaa", - fatherLastName: "yeah", - fatherCareer: "haaa", - fatherCitizenId: "yeah", - fatherLive: true, - motherPrefix: "string", - motherFirstName: "string", - motherLastName: "string", - motherCareer: "string", - motherCitizenId: "string", - motherLive: true, - profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", - children: [], - }, - ], - }) - public async familyHistory(@Path() profileEmployeeId: string) { - const family = await this.familyHistoryRepo.find({ - order: { lastUpdatedAt: "DESC" }, - where: { profileEmployeeId }, - }); - - family.shift(); - - const record = await Promise.all( - family.map(async (v) => ({ - ...v, - children: await this.childrenHistoryRepo.find({ - order: { createdAt: "ASC" }, - // where: { profileFamilyHistoryId: v.id }, - }), - })), - ); - - return new HttpSuccess(record); - } - - @Post() - public async newFamilyHistory( - @Request() req: RequestWithUser, - @Body() body: CreateProfileFamilyEmployee, - ) { - if (!body.profileEmployeeId) { - throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileEmployeeId"); - } - - const profile = await this.profileEmployeeRepo.findOneBy({ id: body.profileEmployeeId }); - - if (!profile) { - throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); - } - - const dataFamily = new ProfileFamilyHistory(); - - const meta = { - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - }; - - const { children, ...family } = body; - - Object.assign(dataFamily, { ...family, ...meta }); - - await this.familyHistoryRepo.save(dataFamily); - - const profileEmployeeId = dataFamily.profileEmployeeId; - - await Promise.all( - children.map(async (v) => { - const dataChildren = new ProfileChildrenHistory(); - - Object.assign(dataChildren, { ...v, profileEmployeeId, ...meta }); - - await this.childrenRepo.save(dataChildren); - }), - ); - - return new HttpSuccess(); - } - - @Patch("{profileEmployeeId}") - public async editFamilyHistory( - @Request() req: RequestWithUser, - @Body() body: UpdateProfileFamily, - @Path() profileEmployeeId: string, - ) { - const familyRecord = ( - await this.familyHistoryRepo.find({ - take: 1, - order: { lastUpdatedAt: "DESC" }, - where: { profileEmployeeId }, - }) - )[0]; - - if (!familyRecord) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - - const childrenRecord = await this.childrenRepo.findBy({ - profileEmployeeId: familyRecord.profileEmployeeId, - }); - - const historyData = new ProfileFamilyHistory(); - - const { children, ...family } = body; - - Object.assign(historyData, { ...familyRecord, ...family, id: undefined }); - historyData.lastUpdateFullName = req.user.name; - historyData.lastUpdateFullName = req.user.name; - - const newChild: CreateChildren[] = []; - - for (let child of children) { - newChild.push( - Object.assign(new ProfileChildren(), { - ...child, - profileEmployeeId, - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - id: !childrenRecord.find((v) => v.id === child.id) ? undefined : child.id, - }), - ); - } - - await Promise.all([ - this.familyHistoryRepo.save({ ...historyData, lastUpdatedAt: undefined }), - ...newChild.map(async (v) => { - return await this.childrenRepo.save(v); - }), - ...childrenRecord.map(async (v) => { - if (!children.find((x) => x.id === v.id)) { - return await this.childrenRepo.delete({ id: v.id }); - } - }), - ...childrenRecord.map(async (v) => { - if (children.find((x) => x.id === v.id)) { - return await this.childrenHistoryRepo.save( - this.childrenHistoryRepo.create({ - ...v, - // profileFamilyHistoryId: familyRecord.id, - profileChildrenId: v.id, - id: undefined, - }), - ); - } - }), - ]); - - return new HttpSuccess(); - } - - @Delete("{profileEmployeeId}") - public async deleteFamilyHistory(@Path() profileEmployeeId: string) { - const result = await this.familyHistoryRepo.delete({ id: profileEmployeeId }); - - if (result.affected == undefined || result.affected <= 0) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - } - - return new HttpSuccess(); - } -} diff --git a/src/controllers/ProfileFamilyMotherController.ts b/src/controllers/ProfileFamilyMotherController.ts index 032613f9..b275837e 100644 --- a/src/controllers/ProfileFamilyMotherController.ts +++ b/src/controllers/ProfileFamilyMotherController.ts @@ -220,24 +220,16 @@ export class ProfileFamilyMotherController extends Controller { familyMother.createdFullName = req.user.name; familyMother.lastUpdateUserId = req.user.sub; familyMother.lastUpdateFullName = req.user.name; - await this.ProfileFamilyMother.save(familyMother); - if (familyMother) { - const history: ProfileFamilyMotherHistory = Object.assign(new ProfileFamilyMotherHistory(), { - profileFamilyMotherId: familyMother.id, - motherPrefix: familyMother.motherPrefix, - motherFirstName: familyMother.motherFirstName, - motherLastName: familyMother.motherLastName, - motherCareer: familyMother.motherCareer, - motherCitizenId: familyMother.motherCitizenId, - motherLive: familyMother.motherLive, - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - }); - await this.ProfileFamilyMotherHistory.save(history); - } + const history = new ProfileFamilyMotherHistory(); + history.profileFamilyMotherId = familyMother.id; + + await Promise.all([ + this.ProfileFamilyMother.save(familyMother), + (history.profileFamilyMotherId = familyMother.id), + this.ProfileFamilyMotherHistory.save(history), + ]); + return new HttpSuccess(familyMother.id); } @@ -252,20 +244,17 @@ export class ProfileFamilyMotherController extends Controller { if (!familyMother) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileFamilyMotherHistory(); - Object.assign(history, { ...familyMother, id: undefined }); Object.assign(familyMother, body); - (familyMother.lastUpdateUserId = req.user.sub), - (familyMother.lastUpdateFullName = req.user.name); + Object.assign(history, { ...familyMother, id: undefined }); + familyMother.motherCitizenId = Extension.CheckCitizen(String(body.motherCitizenId)); history.profileFamilyMotherId = familyMother.id; - (history.motherPrefix = familyMother.motherPrefix), - (history.motherFirstName = familyMother.motherFirstName), - (history.motherLastName = familyMother.motherLastName), - (history.motherCareer = familyMother.motherCareer), - (history.motherCitizenId = familyMother.motherCitizenId), - (history.motherLive = familyMother.motherLive), - (history.lastUpdateUserId = req.user.sub), - (history.lastUpdateFullName = req.user.name); + familyMother.lastUpdateUserId = req.user.sub; + familyMother.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.ProfileFamilyMother.save(familyMother), diff --git a/src/controllers/ProfileFamilyMotherEmployeeController.ts b/src/controllers/ProfileFamilyMotherEmployeeController.ts index a2d9b052..3f0d97ba 100644 --- a/src/controllers/ProfileFamilyMotherEmployeeController.ts +++ b/src/controllers/ProfileFamilyMotherEmployeeController.ts @@ -220,24 +220,16 @@ export class ProfileFamilyMotherEmployeeController extends Controller { familyMother.createdFullName = req.user.name; familyMother.lastUpdateUserId = req.user.sub; familyMother.lastUpdateFullName = req.user.name; - await this.ProfileFamilyMother.save(familyMother); - if (familyMother) { - const history: ProfileFamilyMotherHistory = Object.assign(new ProfileFamilyMotherHistory(), { - profileFamilyMotherId: familyMother.id, - motherPrefix: familyMother.motherPrefix, - motherFirstName: familyMother.motherFirstName, - motherLastName: familyMother.motherLastName, - motherCareer: familyMother.motherCareer, - motherCitizenId: familyMother.motherCitizenId, - motherLive: familyMother.motherLive, - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - }); - await this.ProfileFamilyMotherHistory.save(history); - } + const history = new ProfileFamilyMotherHistory(); + history.profileFamilyMotherId = familyMother.id; + + await Promise.all([ + this.ProfileFamilyMother.save(familyMother), + (history.profileFamilyMotherId = familyMother.id), + this.ProfileFamilyMotherHistory.save(history), + ]); + return new HttpSuccess(familyMother.id); } @@ -254,20 +246,17 @@ export class ProfileFamilyMotherEmployeeController extends Controller { if (!familyMother) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileFamilyMotherHistory(); - Object.assign(history, { ...familyMother, id: undefined }); Object.assign(familyMother, body); - (familyMother.lastUpdateUserId = req.user.sub), - (familyMother.lastUpdateFullName = req.user.name); + Object.assign(history, { ...familyMother, id: undefined }); + familyMother.motherCitizenId = Extension.CheckCitizen(String(body.motherCitizenId)); history.profileFamilyMotherId = familyMother.id; - (history.motherPrefix = familyMother.motherPrefix), - (history.motherFirstName = familyMother.motherFirstName), - (history.motherLastName = familyMother.motherLastName), - (history.motherCareer = familyMother.motherCareer), - (history.motherCitizenId = familyMother.motherCitizenId), - (history.motherLive = familyMother.motherLive), - (history.lastUpdateUserId = req.user.sub), - (history.lastUpdateFullName = req.user.name); + familyMother.lastUpdateUserId = req.user.sub; + familyMother.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.ProfileFamilyMother.save(familyMother), diff --git a/src/controllers/ProfileFamilyMotherEmployeeTempController.ts b/src/controllers/ProfileFamilyMotherEmployeeTempController.ts index 0bbbe348..3cd34967 100644 --- a/src/controllers/ProfileFamilyMotherEmployeeTempController.ts +++ b/src/controllers/ProfileFamilyMotherEmployeeTempController.ts @@ -220,24 +220,16 @@ export class ProfileFamilyMotherEmployeeTempController extends Controller { familyMother.createdFullName = req.user.name; familyMother.lastUpdateUserId = req.user.sub; familyMother.lastUpdateFullName = req.user.name; - await this.ProfileFamilyMother.save(familyMother); - if (familyMother) { - const history: ProfileFamilyMotherHistory = Object.assign(new ProfileFamilyMotherHistory(), { - profileFamilyMotherId: familyMother.id, - motherPrefix: familyMother.motherPrefix, - motherFirstName: familyMother.motherFirstName, - motherLastName: familyMother.motherLastName, - motherCareer: familyMother.motherCareer, - motherCitizenId: familyMother.motherCitizenId, - motherLive: familyMother.motherLive, - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - }); - await this.ProfileFamilyMotherHistory.save(history); - } + const history = new ProfileFamilyMotherHistory(); + history.profileFamilyMotherId = familyMother.id; + + await Promise.all([ + this.ProfileFamilyMother.save(familyMother), + (history.profileFamilyMotherId = familyMother.id), + this.ProfileFamilyMotherHistory.save(history), + ]); + return new HttpSuccess(familyMother.id); } @@ -254,20 +246,17 @@ export class ProfileFamilyMotherEmployeeTempController extends Controller { if (!familyMother) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileFamilyMotherHistory(); - Object.assign(history, { ...familyMother, id: undefined }); Object.assign(familyMother, body); - (familyMother.lastUpdateUserId = req.user.sub), - (familyMother.lastUpdateFullName = req.user.name); + Object.assign(history, { ...familyMother, id: undefined }); + familyMother.motherCitizenId = Extension.CheckCitizen(String(body.motherCitizenId)); history.profileFamilyMotherId = familyMother.id; - (history.motherPrefix = familyMother.motherPrefix), - (history.motherFirstName = familyMother.motherFirstName), - (history.motherLastName = familyMother.motherLastName), - (history.motherCareer = familyMother.motherCareer), - (history.motherCitizenId = familyMother.motherCitizenId), - (history.motherLive = familyMother.motherLive), - (history.lastUpdateUserId = req.user.sub), - (history.lastUpdateFullName = req.user.name); + familyMother.lastUpdateUserId = req.user.sub; + familyMother.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.ProfileFamilyMother.save(familyMother), diff --git a/src/controllers/ProfileGovernmentController.ts b/src/controllers/ProfileGovernmentController.ts index 73905a42..01c88bac 100644 --- a/src/controllers/ProfileGovernmentController.ts +++ b/src/controllers/ProfileGovernmentController.ts @@ -254,34 +254,6 @@ export class ProfileGovernmentHistoryController extends Controller { return new HttpSuccess(record); } - // @Post() - // public async newGov(@Request() req: RequestWithUser, @Body() body: CreateProfileGovernment) { - // if (!body.profileId) { - // throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); - // } - - // const profile = await this.profileRepo.findOneBy({ id: body.profileId }); - - // if (!profile) { - // throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); - // } - - // const data = new ProfileGovernment(); - - // const meta = { - // createdUserId: req.user.sub, - // createdFullName: req.user.name, - // lastUpdateUserId: req.user.sub, - // lastUpdateFullName: req.user.name, - // }; - - // Object.assign(data, { ...body, ...meta }); - - // await this.govRepo.save(data); - - // return new HttpSuccess(); - // } - /** * * @summary แก้ไขข้อมูลราชการ @@ -300,29 +272,21 @@ export class ProfileGovernmentHistoryController extends Controller { if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - const historyData = new ProfileGovernment(); + const history = new ProfileGovernment(); - Object.assign(historyData, { ...record, ...body, id: undefined }); Object.assign(record, body); - record.lastUpdateFullName = req.user.name; - record.lastUpdateFullName = req.user.name; - historyData.profileId = profileId; - historyData.lastUpdateFullName = req.user.name; - historyData.lastUpdateFullName = req.user.name; + Object.assign(history, body); - await Promise.all([this.profileRepo.save(record), this.govRepo.save(historyData)]); + history.profileId = profileId; + record.lastUpdateUserId = req.user.sub; + record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; + + await Promise.all([this.profileRepo.save(record), this.govRepo.save(history)]); return new HttpSuccess(); } - - // @Delete("{profileId}") - // public async deleteGov(@Path() profileId: string) { - // const result = await this.govRepo.delete({ profileId: profileId }); - - // if (result.affected == undefined || result.affected <= 0) { - // throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - // } - - // return new HttpSuccess(); - // } } diff --git a/src/controllers/ProfileGovernmentEmployeeController.ts b/src/controllers/ProfileGovernmentEmployeeController.ts index 65f7991c..191f03c6 100644 --- a/src/controllers/ProfileGovernmentEmployeeController.ts +++ b/src/controllers/ProfileGovernmentEmployeeController.ts @@ -248,35 +248,6 @@ export class ProfileGovernmentEmployeeController extends Controller { return new HttpSuccess(record); } - // /** - // * - // * @summary เพิ่มข้อมูลราชการ - // * - // */ - // @Post() - // public async newGov(@Request() req: RequestWithUser, @Body() body: CreateProfileEmployeeGovernment) { - // if (!body.profileEmployeeId) { - // throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileEmployeeId"); - // } - - // const profile = await this.profileEmployeeRepo.findOneBy({ id: body.profileEmployeeId }); - // if (!profile) { - // throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); - // } - - // const data = new ProfileGovernment(); - // const meta = { - // createdUserId: req.user.sub, - // createdFullName: req.user.name, - // lastUpdateUserId: req.user.sub, - // lastUpdateFullName: req.user.name, - // }; - - // Object.assign(data, { ...body, ...meta }); - // await this.govRepo.save(data); - // return new HttpSuccess(); - // } - /** * * @summary แก้ไขข้อมูลราชการ @@ -294,30 +265,20 @@ export class ProfileGovernmentEmployeeController extends Controller { }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - const historyData = new ProfileGovernment(); + const history = new ProfileGovernment(); - Object.assign(historyData, { ...record, ...body, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + + history.profileEmployeeId = profileEmployeeId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; - record.lastUpdateFullName = req.user.name; - historyData.profileEmployeeId = profileEmployeeId; - historyData.lastUpdateFullName = req.user.name; - historyData.lastUpdateFullName = req.user.name; - await Promise.all([this.profileEmployeeRepo.save(record), this.govRepo.save(historyData)]); + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; + + await Promise.all([this.profileEmployeeRepo.save(record), this.govRepo.save(history)]); return new HttpSuccess(); } - - // /** - // * - // * @summary ลบข้อมูลราชการ - // * - // */ - // @Delete("{profileEmployeeId}") - // public async deleteGov(@Path() profileEmployeeId: string) { - // const result = await this.govRepo.delete({ profileEmployeeId: profileEmployeeId }); - // if (result.affected == undefined || result.affected <= 0) { - // throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - // } - // return new HttpSuccess(); - // } } diff --git a/src/controllers/ProfileGovernmentEmployeeTempController.ts b/src/controllers/ProfileGovernmentEmployeeTempController.ts index e5f77de2..92a9808a 100644 --- a/src/controllers/ProfileGovernmentEmployeeTempController.ts +++ b/src/controllers/ProfileGovernmentEmployeeTempController.ts @@ -248,35 +248,6 @@ export class ProfileGovernmentEmployeeTempController extends Controller { return new HttpSuccess(record); } - // /** - // * - // * @summary เพิ่มข้อมูลราชการ - // * - // */ - // @Post() - // public async newGov(@Request() req: RequestWithUser, @Body() body: CreateProfileEmployeeGovernment) { - // if (!body.profileEmployeeId) { - // throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileEmployeeId"); - // } - - // const profile = await this.profileEmployeeRepo.findOneBy({ id: body.profileEmployeeId }); - // if (!profile) { - // throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); - // } - - // const data = new ProfileGovernment(); - // const meta = { - // createdUserId: req.user.sub, - // createdFullName: req.user.name, - // lastUpdateUserId: req.user.sub, - // lastUpdateFullName: req.user.name, - // }; - - // Object.assign(data, { ...body, ...meta }); - // await this.govRepo.save(data); - // return new HttpSuccess(); - // } - /** * * @summary แก้ไขข้อมูลราชการ @@ -294,30 +265,20 @@ export class ProfileGovernmentEmployeeTempController extends Controller { }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - const historyData = new ProfileGovernment(); + const history = new ProfileGovernment(); - Object.assign(historyData, { ...record, ...body, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + + history.profileEmployeeId = profileEmployeeId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; - record.lastUpdateFullName = req.user.name; - historyData.profileEmployeeId = profileEmployeeId; - historyData.lastUpdateFullName = req.user.name; - historyData.lastUpdateFullName = req.user.name; - await Promise.all([this.profileEmployeeRepo.save(record), this.govRepo.save(historyData)]); + history.lastUpdateUserId = req.user.sub; + history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; + + await Promise.all([this.profileEmployeeRepo.save(record), this.govRepo.save(history)]); return new HttpSuccess(); } - - // /** - // * - // * @summary ลบข้อมูลราชการ - // * - // */ - // @Delete("{profileEmployeeId}") - // public async deleteGov(@Path() profileEmployeeId: string) { - // const result = await this.govRepo.delete({ profileEmployeeId: profileEmployeeId }); - // if (result.affected == undefined || result.affected <= 0) { - // throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - // } - // return new HttpSuccess(); - // } } diff --git a/src/controllers/ProfileHonorController.ts b/src/controllers/ProfileHonorController.ts index 70e7b447..9274b59c 100644 --- a/src/controllers/ProfileHonorController.ts +++ b/src/controllers/ProfileHonorController.ts @@ -159,8 +159,14 @@ export class ProfileHonorController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileHonorHistory(); + history.profileHonorId = data.id; - await this.honorRepo.save(data); + await Promise.all([ + this.honorRepo.save(data), + (history.profileHonorId = data.id), + this.honorHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -178,11 +184,16 @@ export class ProfileHonorController extends Controller { const history = new ProfileHonorHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileHonorId = honorId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.honorRepo.save(record), this.honorHistoryRepo.save(history)]); diff --git a/src/controllers/ProfileHonorEmployeeController.ts b/src/controllers/ProfileHonorEmployeeController.ts index 439117a3..74c26ac3 100644 --- a/src/controllers/ProfileHonorEmployeeController.ts +++ b/src/controllers/ProfileHonorEmployeeController.ts @@ -163,8 +163,14 @@ export class ProfileHonorEmployeeController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileHonorHistory(); + history.profileHonorId = data.id; - await this.honorRepo.save(data); + await Promise.all([ + this.honorRepo.save(data), + (history.profileHonorId = data.id), + this.honorHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -182,11 +188,16 @@ export class ProfileHonorEmployeeController extends Controller { const history = new ProfileHonorHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileHonorId = honorId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.honorRepo.save(record), this.honorHistoryRepo.save(history)]); diff --git a/src/controllers/ProfileHonorEmployeeTempController.ts b/src/controllers/ProfileHonorEmployeeTempController.ts index 9965e006..11b954c2 100644 --- a/src/controllers/ProfileHonorEmployeeTempController.ts +++ b/src/controllers/ProfileHonorEmployeeTempController.ts @@ -163,8 +163,14 @@ export class ProfileHonorEmployeeTempController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileHonorHistory(); + history.profileHonorId = data.id; - await this.honorRepo.save(data); + await Promise.all([ + this.honorRepo.save(data), + (history.profileHonorId = data.id), + this.honorHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -182,11 +188,16 @@ export class ProfileHonorEmployeeTempController extends Controller { const history = new ProfileHonorHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileHonorId = honorId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.honorRepo.save(record), this.honorHistoryRepo.save(history)]); diff --git a/src/controllers/ProfileInsigniaController.ts b/src/controllers/ProfileInsigniaController.ts index 2c616fa4..e455f124 100644 --- a/src/controllers/ProfileInsigniaController.ts +++ b/src/controllers/ProfileInsigniaController.ts @@ -194,8 +194,14 @@ export class ProfileInsigniaController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileInsigniaHistory(); + history.profileInsigniaId = data.id; - await this.insigniaRepo.save(data); + await Promise.all([ + this.insigniaRepo.save(data), + (history.profileInsigniaId = data.id), + this.insigniaHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -220,11 +226,16 @@ export class ProfileInsigniaController extends Controller { const history = new ProfileInsigniaHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileInsigniaId = insigniaId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.insigniaRepo.save(record), this.insigniaHistoryRepo.save(history)]); diff --git a/src/controllers/ProfileInsigniaEmployeeController.ts b/src/controllers/ProfileInsigniaEmployeeController.ts index 8d8f48cc..4b1e8944 100644 --- a/src/controllers/ProfileInsigniaEmployeeController.ts +++ b/src/controllers/ProfileInsigniaEmployeeController.ts @@ -197,8 +197,14 @@ export class ProfileInsigniaEmployeeController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileInsigniaHistory(); + history.profileInsigniaId = data.id; - await this.insigniaRepo.save(data); + await Promise.all([ + this.insigniaRepo.save(data), + (history.profileInsigniaId = data.id), + this.insigniaHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -223,11 +229,16 @@ export class ProfileInsigniaEmployeeController extends Controller { const history = new ProfileInsigniaHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileInsigniaId = insigniaId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.insigniaRepo.save(record), this.insigniaHistoryRepo.save(history)]); diff --git a/src/controllers/ProfileInsigniaEmployeeTempController.ts b/src/controllers/ProfileInsigniaEmployeeTempController.ts index 1d4ab598..281c6523 100644 --- a/src/controllers/ProfileInsigniaEmployeeTempController.ts +++ b/src/controllers/ProfileInsigniaEmployeeTempController.ts @@ -197,8 +197,14 @@ export class ProfileInsigniaEmployeeTempController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileInsigniaHistory(); + history.profileInsigniaId = data.id; - await this.insigniaRepo.save(data); + await Promise.all([ + this.insigniaRepo.save(data), + (history.profileInsigniaId = data.id), + this.insigniaHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -223,11 +229,16 @@ export class ProfileInsigniaEmployeeTempController extends Controller { const history = new ProfileInsigniaHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileInsigniaId = insigniaId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.insigniaRepo.save(record), this.insigniaHistoryRepo.save(history)]); diff --git a/src/controllers/ProfileLeaveController.ts b/src/controllers/ProfileLeaveController.ts index cfb2f9a2..cda9eaaa 100644 --- a/src/controllers/ProfileLeaveController.ts +++ b/src/controllers/ProfileLeaveController.ts @@ -274,8 +274,14 @@ export class ProfileLeaveController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileLeaveHistory(); + history.profileLeaveId = data.id; - await this.leaveRepo.save(data); + await Promise.all([ + this.leaveRepo.save(data), + (history.profileLeaveId = data.id), + this.leaveHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -300,11 +306,16 @@ export class ProfileLeaveController extends Controller { const history = new ProfileLeaveHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileLeaveId = leaveId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.leaveRepo.save(record), this.leaveHistoryRepo.save(history)]); diff --git a/src/controllers/ProfileLeaveEmployeeController.ts b/src/controllers/ProfileLeaveEmployeeController.ts index 6b69524f..a3d8ca97 100644 --- a/src/controllers/ProfileLeaveEmployeeController.ts +++ b/src/controllers/ProfileLeaveEmployeeController.ts @@ -95,8 +95,14 @@ export class ProfileLeaveEmployeeController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileLeaveHistory(); + history.profileLeaveId = data.id; - await this.leaveRepo.save(data); + await Promise.all([ + this.leaveRepo.save(data), + (history.profileLeaveId = data.id), + this.leaveHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -121,11 +127,16 @@ export class ProfileLeaveEmployeeController extends Controller { const history = new ProfileLeaveHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileLeaveId = leaveId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.leaveRepo.save(record), this.leaveHistoryRepo.save(history)]); diff --git a/src/controllers/ProfileLeaveEmployeeTempController.ts b/src/controllers/ProfileLeaveEmployeeTempController.ts index 7bd4812a..cd379713 100644 --- a/src/controllers/ProfileLeaveEmployeeTempController.ts +++ b/src/controllers/ProfileLeaveEmployeeTempController.ts @@ -95,8 +95,14 @@ export class ProfileLeaveEmployeeTempController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileLeaveHistory(); + history.profileLeaveId = data.id; - await this.leaveRepo.save(data); + await Promise.all([ + this.leaveRepo.save(data), + (history.profileLeaveId = data.id), + this.leaveHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -121,11 +127,16 @@ export class ProfileLeaveEmployeeTempController extends Controller { const history = new ProfileLeaveHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileLeaveId = leaveId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.leaveRepo.save(record), this.leaveHistoryRepo.save(history)]); diff --git a/src/controllers/ProfileNopaidController.ts b/src/controllers/ProfileNopaidController.ts index e055722b..dd37cc07 100644 --- a/src/controllers/ProfileNopaidController.ts +++ b/src/controllers/ProfileNopaidController.ts @@ -117,8 +117,14 @@ export class ProfileNopaidController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileNopaidHistory(); + history.profileNopaidId = data.id; - await this.nopaidRepository.save(data); + await Promise.all([ + this.nopaidRepository.save(data), + (history.profileNopaidId = data.id), + this.nopaidHistoryRepository.save(history), + ]); return new HttpSuccess(); } @@ -136,11 +142,16 @@ export class ProfileNopaidController extends Controller { const history = new ProfileNopaidHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileNopaidId = nopaidId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.nopaidRepository.save(record), diff --git a/src/controllers/ProfileNopaidEmployeeController.ts b/src/controllers/ProfileNopaidEmployeeController.ts index bdf6bf7b..0c26493f 100644 --- a/src/controllers/ProfileNopaidEmployeeController.ts +++ b/src/controllers/ProfileNopaidEmployeeController.ts @@ -88,8 +88,14 @@ export class ProfileNopaidEmployeeController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileNopaidHistory(); + history.profileNopaidId = data.id; - await this.nopaidRepository.save(data); + await Promise.all([ + this.nopaidRepository.save(data), + (history.profileNopaidId = data.id), + this.nopaidHistoryRepository.save(history), + ]); return new HttpSuccess(); } @@ -107,11 +113,16 @@ export class ProfileNopaidEmployeeController extends Controller { const history = new ProfileNopaidHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileNopaidId = nopaidId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.nopaidRepository.save(record), diff --git a/src/controllers/ProfileNopaidEmployeeTempController.ts b/src/controllers/ProfileNopaidEmployeeTempController.ts index d3fec7d7..a0a0775a 100644 --- a/src/controllers/ProfileNopaidEmployeeTempController.ts +++ b/src/controllers/ProfileNopaidEmployeeTempController.ts @@ -88,8 +88,14 @@ export class ProfileNopaidEmployeeTempController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileNopaidHistory(); + history.profileNopaidId = data.id; - await this.nopaidRepository.save(data); + await Promise.all([ + this.nopaidRepository.save(data), + (history.profileNopaidId = data.id), + this.nopaidHistoryRepository.save(history), + ]); return new HttpSuccess(); } @@ -107,11 +113,16 @@ export class ProfileNopaidEmployeeTempController extends Controller { const history = new ProfileNopaidHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileNopaidId = nopaidId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.nopaidRepository.save(record), diff --git a/src/controllers/ProfileOtherController.ts b/src/controllers/ProfileOtherController.ts index d6cb2bde..f3c74700 100644 --- a/src/controllers/ProfileOtherController.ts +++ b/src/controllers/ProfileOtherController.ts @@ -114,8 +114,14 @@ export class ProfileOtherController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileOtherHistory(); + history.profileOtherId = data.id; - await this.otherRepository.save(data); + await Promise.all([ + this.otherRepository.save(data), + (history.profileOtherId = data.id), + this.otherHistoryRepository.save(history), + ]); return new HttpSuccess(); } @@ -133,11 +139,16 @@ export class ProfileOtherController extends Controller { const history = new ProfileOtherHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileOtherId = otherId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.otherRepository.save(record), diff --git a/src/controllers/ProfileOtherEmployeeController.ts b/src/controllers/ProfileOtherEmployeeController.ts index b843dc6b..8581b247 100644 --- a/src/controllers/ProfileOtherEmployeeController.ts +++ b/src/controllers/ProfileOtherEmployeeController.ts @@ -85,8 +85,14 @@ export class ProfileOtherEmployeeController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileOtherHistory(); + history.profileOtherId = data.id; - await this.otherRepository.save(data); + await Promise.all([ + this.otherRepository.save(data), + (history.profileOtherId = data.id), + this.otherHistoryRepository.save(history), + ]); return new HttpSuccess(); } @@ -104,11 +110,16 @@ export class ProfileOtherEmployeeController extends Controller { const history = new ProfileOtherHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileOtherId = otherId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.otherRepository.save(record), diff --git a/src/controllers/ProfileOtherEmployeeTempController.ts b/src/controllers/ProfileOtherEmployeeTempController.ts index e164d56e..cde9bcd2 100644 --- a/src/controllers/ProfileOtherEmployeeTempController.ts +++ b/src/controllers/ProfileOtherEmployeeTempController.ts @@ -85,8 +85,14 @@ export class ProfileOtherEmployeeTempController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileOtherHistory(); + history.profileOtherId = data.id; - await this.otherRepository.save(data); + await Promise.all([ + this.otherRepository.save(data), + (history.profileOtherId = data.id), + this.otherHistoryRepository.save(history), + ]); return new HttpSuccess(); } @@ -104,11 +110,16 @@ export class ProfileOtherEmployeeTempController extends Controller { const history = new ProfileOtherHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileOtherId = otherId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([ this.otherRepository.save(record), diff --git a/src/controllers/ProfileSalaryController.ts b/src/controllers/ProfileSalaryController.ts index 1048e341..324f0301 100644 --- a/src/controllers/ProfileSalaryController.ts +++ b/src/controllers/ProfileSalaryController.ts @@ -167,8 +167,14 @@ export class ProfileSalaryController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileSalaryHistory(); + history.profileSalaryId = data.id; - await this.salaryRepo.save(data); + await Promise.all([ + this.salaryRepo.save(data), + (history.profileSalaryId = data.id), + this.salaryHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -186,11 +192,16 @@ export class ProfileSalaryController extends Controller { const history = new ProfileSalaryHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileSalaryId = salaryId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.salaryRepo.save(record), this.salaryHistoryRepo.save(history)]); diff --git a/src/controllers/ProfileSalaryEmployeeController.ts b/src/controllers/ProfileSalaryEmployeeController.ts index 047560f7..5d04bc39 100644 --- a/src/controllers/ProfileSalaryEmployeeController.ts +++ b/src/controllers/ProfileSalaryEmployeeController.ts @@ -96,8 +96,14 @@ export class ProfileSalaryEmployeeController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileSalaryHistory(); + history.profileSalaryId = data.id; - await this.salaryRepo.save(data); + await Promise.all([ + this.salaryRepo.save(data), + (history.profileSalaryId = data.id), + this.salaryHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -115,11 +121,16 @@ export class ProfileSalaryEmployeeController extends Controller { const history = new ProfileSalaryHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileSalaryId = salaryId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.salaryRepo.save(record), this.salaryHistoryRepo.save(history)]); diff --git a/src/controllers/ProfileSalaryEmployeeTempController.ts b/src/controllers/ProfileSalaryEmployeeTempController.ts index 1b53016a..d7a167b4 100644 --- a/src/controllers/ProfileSalaryEmployeeTempController.ts +++ b/src/controllers/ProfileSalaryEmployeeTempController.ts @@ -96,8 +96,14 @@ export class ProfileSalaryEmployeeTempController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileSalaryHistory(); + history.profileSalaryId = data.id; - await this.salaryRepo.save(data); + await Promise.all([ + this.salaryRepo.save(data), + (history.profileSalaryId = data.id), + this.salaryHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -115,11 +121,16 @@ export class ProfileSalaryEmployeeTempController extends Controller { const history = new ProfileSalaryHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileSalaryId = salaryId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.salaryRepo.save(record), this.salaryHistoryRepo.save(history)]); diff --git a/src/controllers/ProfileTrainingController.ts b/src/controllers/ProfileTrainingController.ts index cbf6735e..930ee815 100644 --- a/src/controllers/ProfileTrainingController.ts +++ b/src/controllers/ProfileTrainingController.ts @@ -160,8 +160,14 @@ export class ProfileTrainingController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileTrainingHistory(); + history.profileTrainingId = data.id; - await this.trainingRepo.save(data); + await Promise.all([ + this.trainingRepo.save(data), + (history.profileTrainingId = data.id), + this.trainingHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -179,11 +185,16 @@ export class ProfileTrainingController extends Controller { const history = new ProfileTrainingHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileTrainingId = trainingId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.trainingRepo.save(record), this.trainingHistoryRepo.save(history)]); diff --git a/src/controllers/ProfileTrainingEmployeeController.ts b/src/controllers/ProfileTrainingEmployeeController.ts index f1334fbf..0b3e0c9e 100644 --- a/src/controllers/ProfileTrainingEmployeeController.ts +++ b/src/controllers/ProfileTrainingEmployeeController.ts @@ -163,8 +163,14 @@ export class ProfileTrainingEmployeeController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileTrainingHistory(); + history.profileTrainingId = data.id; - await this.trainingRepo.save(data); + await Promise.all([ + this.trainingRepo.save(data), + (history.profileTrainingId = data.id), + this.trainingHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -182,11 +188,16 @@ export class ProfileTrainingEmployeeController extends Controller { const history = new ProfileTrainingHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileTrainingId = trainingId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.trainingRepo.save(record), this.trainingHistoryRepo.save(history)]); diff --git a/src/controllers/ProfileTrainingEmployeeTempController.ts b/src/controllers/ProfileTrainingEmployeeTempController.ts index 0eddb100..d2f24117 100644 --- a/src/controllers/ProfileTrainingEmployeeTempController.ts +++ b/src/controllers/ProfileTrainingEmployeeTempController.ts @@ -163,8 +163,14 @@ export class ProfileTrainingEmployeeTempController extends Controller { }; Object.assign(data, { ...body, ...meta }); + const history = new ProfileTrainingHistory(); + history.profileTrainingId = data.id; - await this.trainingRepo.save(data); + await Promise.all([ + this.trainingRepo.save(data), + (history.profileTrainingId = data.id), + this.trainingHistoryRepo.save(history), + ]); return new HttpSuccess(); } @@ -182,11 +188,16 @@ export class ProfileTrainingEmployeeTempController extends Controller { const history = new ProfileTrainingHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); + Object.assign(history, body); + history.profileTrainingId = trainingId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.lastUpdateUserId = req.user.sub; history.lastUpdateFullName = req.user.name; + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; await Promise.all([this.trainingRepo.save(record), this.trainingHistoryRepo.save(history)]);