diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 7ccf2203..4d25fda5 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -2048,12 +2048,6 @@ export class CommandController extends Controller { ) { await Promise.all( body.data.map(async (item) => { - const profile = await this.profileRepository.findOneBy({ - citizenId: item.bodyProfile.citizenId, - }); - if (profile) { - throw new HttpError(HttpStatus.BAD_REQUEST, "พบ profile ซ้ำ"); - } const before = null; const meta = { createdUserId: req.user.sub, @@ -2063,41 +2057,25 @@ export class CommandController extends Controller { createdAt: new Date(), lastUpdatedAt: new Date(), }; + const _null: any = null; if (item.bodyProfile.posLevelId === "") item.bodyProfile.posLevelId = null; if (item.bodyProfile.posTypeId === "") item.bodyProfile.posTypeId = null; - - if ( - item.bodyProfile.posLevelId && - !(await this.posLevelRepo.findOneBy({ id: item.bodyProfile.posLevelId })) - ) { + if (item.bodyProfile.posLevelId && !(await this.posLevelRepo.findOneBy({ id: item.bodyProfile.posLevelId }))) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้"); } - - if ( - item.bodyProfile.posTypeId && - !(await this.posTypeRepo.findOneBy({ id: item.bodyProfile.posTypeId })) - ) { + if (item.bodyProfile.posTypeId && !(await this.posTypeRepo.findOneBy({ id: item.bodyProfile.posTypeId }))) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); } - const profile_: Profile = Object.assign({ ...item.bodyProfile, ...meta }); - const _null: any = null; - profile_.dateRetire = - item.bodyProfile.birthDate == null - ? _null - : calculateRetireDate(item.bodyProfile.birthDate); - profile_.dateRetireLaw = - item.bodyProfile.birthDate == null - ? _null - : calculateRetireLaw(item.bodyProfile.birthDate); - await this.profileRepository.save(profile_); - // setLogDataDiff(req, { before, after: profile_ }); - if (profile_ && profile_.id) { - const userKeycloakId = await createUser(profile_.citizenId, profile_.citizenId, { - //User, Password - firstName: profile_.firstName, - lastName: profile_.lastName, - email: profile_.email, + let profile:any = await this.profileRepository.findOneBy({ citizenId: item.bodyProfile.citizenId }); + if(!profile) { + profile = Object.assign({ ...item.bodyProfile, ...meta }); + profile.dateRetire = item.bodyProfile.birthDate == null ? _null : calculateRetireDate(item.bodyProfile.birthDate); + profile.dateRetireLaw = item.bodyProfile.birthDate == null ? _null : calculateRetireLaw(item.bodyProfile.birthDate); + const userKeycloakId = await createUser(profile.citizenId, profile.citizenId, { + firstName: profile.firstName, + lastName: profile.lastName, + email: profile.email, requiredActions: ["UPDATE_PASSWORD"], }); if (typeof userKeycloakId !== "string") { @@ -2116,7 +2094,12 @@ export class CommandController extends Controller { })), ); if (!result) throw new Error("Failed. Cannot set user's role."); - + profile.keycloak = userKeycloakId; + await this.profileRepository.save(profile); + setLogDataDiff(req, { before, after: profile }); + } + + if(profile && profile.id) { //Educations await Promise.all( item.bodyEducations.map(async (education) => { @@ -2124,14 +2107,13 @@ export class CommandController extends Controller { Object.assign(profileEdu, { ...education, ...meta }); const eduHistory = new ProfileEducationHistory(); Object.assign(eduHistory, { ...profileEdu, id: undefined }); - profileEdu.profileId = profile_.id; + profileEdu.profileId = profile.id; await this.profileEducationRepo.save(profileEdu, { data: req }); setLogDataDiff(req, { before, after: profileEdu }); eduHistory.profileEducationId = profileEdu.id; await this.profileEducationHistoryRepo.save(eduHistory, { data: req }); }), ); - //Certificates await Promise.all( item.bodyCertificates.map(async (cer) => { @@ -2139,40 +2121,38 @@ export class CommandController extends Controller { Object.assign(profileCer, { ...cer, ...meta }); const cerHistory = new ProfileCertificateHistory(); Object.assign(cerHistory, { ...profileCer, id: undefined }); - profileCer.profileId = profile_.id; + profileCer.profileId = profile.id await this.certificateRepo.save(profileCer, { data: req }); setLogDataDiff(req, { before, after: profileCer }); cerHistory.profileCertificateId = profileCer.id; await this.certificateHistoryRepo.save(cerHistory, { data: req }); }), ); - //Salary const dest_item = await this.salaryRepo.findOne({ - where: { profileId: profile_.id }, + where: { profileId: profile.id }, order: { order: "DESC" }, }); const profileSal = new ProfileSalary(); Object.assign(profileSal, { ...item.bodySalarys, ...meta }); const salaryHistory = new ProfileSalaryHistory(); - Object.assign(history, { ...profileSal, id: undefined }); - (profileSal.order = dest_item == null ? 1 : dest_item.order + 1), - (profileSal.profileId = profile_.id); + Object.assign(salaryHistory, { ...profileSal, id: undefined }); + profileSal.order = dest_item == null ? 1 : dest_item.order + 1, + profileSal.profileId = profile.id await this.salaryRepo.save(profileSal, { data: req }); setLogDataDiff(req, { before, after: profileSal }); salaryHistory.profileSalaryId = profileSal.id; await this.salaryHistoryRepo.save(salaryHistory, { data: req }); - //Position const posMaster = await this.posMasterRepository.findOne({ where: { id: item.bodyPosition.posmasterId }, }); - if (posMaster == null) + if (posMaster == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); const posMasterOld = await this.posMasterRepository.findOne({ where: { - current_holderId: profile_.id, + current_holderId: profile.id, orgRevisionId: posMaster.orgRevisionId, }, }); @@ -2203,7 +2183,7 @@ export class CommandController extends Controller { await this.positionRepository.save(clearPosition); } - posMaster.current_holderId = profile_.id; + posMaster.current_holderId = profile.id; if (posMasterOld != null) await this.posMasterRepository.save(posMasterOld); await this.posMasterRepository.save(posMaster); @@ -2215,15 +2195,15 @@ export class CommandController extends Controller { }); if (positionNew != null) { positionNew.positionIsSelected = true; - profile_.posLevelId = positionNew.posLevelId; - profile_.posTypeId = positionNew.posTypeId; - profile_.position = positionNew.positionName; - profile_.keycloak = userKeycloakId; // Update KeyCloak - await this.profileRepository.save(profile_, { data: req }); - setLogDataDiff(req, { before, after: profile_ }); + profile.posLevelId = positionNew.posLevelId; + profile.posTypeId = positionNew.posTypeId; + profile.position = positionNew.positionName; + await this.profileRepository.save(profile, { data: req }); + setLogDataDiff(req, { before, after: profile }); await this.positionRepository.save(positionNew, { data: req }); } } + // console.log(`${item.bodyProfile.citizenId} ==> Success`) }), ); return new HttpSuccess();