From 618ccb5cff4d258a9777a796f36efd7c057a3ebe Mon Sep 17 00:00:00 2001 From: Bright Date: Fri, 31 Jan 2025 14:10:16 +0700 Subject: [PATCH] =?UTF-8?q?fix=20=E0=B8=AD=E0=B8=AD=E0=B8=81=E0=B8=84?= =?UTF-8?q?=E0=B8=B3=E0=B8=AA=E0=B8=B1=E0=B9=88=E0=B8=87=E0=B8=9A=E0=B8=A3?= =?UTF-8?q?=E0=B8=A3=E0=B8=88=E0=B8=B8=E0=B9=81=E0=B8=95=E0=B9=88=E0=B8=87?= =?UTF-8?q?=E0=B8=95=E0=B8=B1=E0=B9=89=E0=B8=87=20=E0=B8=82=E0=B8=A3?= =?UTF-8?q?=E0=B8=81.=E0=B9=80=E0=B8=94=E0=B8=B4=E0=B8=A1=E0=B8=AB?= =?UTF-8?q?=E0=B8=A3=E0=B8=B7=E0=B8=AD=E0=B8=82=E0=B8=A3=E0=B8=81.?= =?UTF-8?q?=E0=B8=AA=E0=B8=96=E0=B8=B2=E0=B8=99=E0=B8=B0=E0=B8=9E=E0=B9=89?= =?UTF-8?q?=E0=B8=99=E0=B8=A3=E0=B8=B2=E0=B8=8A=E0=B8=81=E0=B8=B2=E0=B8=A3?= =?UTF-8?q?=E0=B9=84=E0=B8=A1=E0=B9=88=E0=B8=AD=E0=B8=B1=E0=B8=9E=E0=B9=80?= =?UTF-8?q?=E0=B8=94=E0=B8=9E=E0=B8=AA=E0=B8=96=E0=B8=B2=E0=B8=99=E0=B8=B0?= =?UTF-8?q?=E0=B8=97=E0=B8=94=E0=B8=A5=E0=B8=AD=E0=B8=87=E0=B8=87=E0=B8=B2?= =?UTF-8?q?=E0=B8=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/CommandController.ts | 186 +++++++++++++++++---------- 1 file changed, 115 insertions(+), 71 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 7d5d851c..1fbb3b25 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -61,6 +61,7 @@ import { enableStatus, getUserByUsername, getRoleMappings, + removeUserRoles } from "../keycloak"; import { ProfileEducation, CreateProfileEducation } from "../entities/ProfileEducation"; import { ProfileEducationHistory } from "../entities/ProfileEducationHistory"; @@ -4214,6 +4215,9 @@ export class CommandController extends Controller { const roleKeycloak = await this.roleKeycloakRepo.findOne({ where: { name: Like("USER") }, }); + const list = await getRoles(); + if (!Array.isArray(list)) + throw new Error("Failed. Cannot get role(s) data from the server."); const _null: any = null; await Promise.all( body.data.map(async (item) => { @@ -4242,85 +4246,90 @@ export class CommandController extends Controller { ) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); } + + let registrationProvinceId = await this.provinceRepo.findOneBy({ + id: item.bodyProfile.registrationProvinceId ?? "", + }); + let registrationDistrictId = await this.districtRepo.findOneBy({ + id: item.bodyProfile.registrationDistrictId ?? "", + }); + let registrationSubDistrictId = await this.subDistrictRepo.findOneBy({ + id: item.bodyProfile.registrationSubDistrictId ?? "", + }); + let currentProvinceId = await this.provinceRepo.findOneBy({ + id: item.bodyProfile.currentProvinceId ?? "", + }); + let currentDistrictId = await this.districtRepo.findOneBy({ + id: item.bodyProfile.currentDistrictId ?? "", + }); + let currentSubDistrictId = await this.subDistrictRepo.findOneBy({ + id: item.bodyProfile.currentSubDistrictId ?? "", + }); + + let _dateRetire = item.bodyProfile.birthDate == null + ? _null + : calculateRetireDate(item.bodyProfile.birthDate); + let _dateRetireLaw = item.bodyProfile.birthDate == null + ? _null + : calculateRetireLaw(item.bodyProfile.birthDate); + + let userKeycloakId:any + let result:any + const checkUser = await getUserByUsername(item.bodyProfile.citizenId); + if (checkUser.length == 0) { + userKeycloakId = await createUser(item.bodyProfile.citizenId, item.bodyProfile.citizenId, { + firstName: item.bodyProfile.firstName, + lastName: item.bodyProfile.lastName, + }); + result = await addUserRoles( + userKeycloakId, + list + .filter((v) => v.name === "USER") + .map((x) => ({ + id: x.id, + name: x.name, + })), + ); + } else { + userKeycloakId = checkUser[0].id; + const rolesData = await getRoleMappings(userKeycloakId); + if (rolesData) { + const _delRole = rolesData.map((x:any) => ({ + id : x.id, + name: x.name + })); + await removeUserRoles( + userKeycloakId, + _delRole + ); + } + result = await addUserRoles( + userKeycloakId, + list + .filter((v) => v.name === "USER") + .map((x) => ({ + id: x.id, + name: x.name, + })), + ); + } let profile: any = await this.profileRepository.findOne({ - where: { citizenId: item.bodyProfile.citizenId, isActive: true }, + where: { citizenId: item.bodyProfile.citizenId/*, isActive: true */}, relations: ["roleKeycloaks"], }); + 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 checkUser = await getUserByUsername(profile.citizenId); - if (checkUser.length == 0) { - const userKeycloakId = await createUser(profile.citizenId, profile.citizenId, { - firstName: profile.firstName, - lastName: profile.lastName, - }); - const list = await getRoles(); - if (!Array.isArray(list)) - throw new Error("Failed. Cannot get role(s) data from the server."); - const result = await addUserRoles( - userKeycloakId, - list - .filter((v) => v.name === "USER") - .map((x) => ({ - id: x.id, - name: x.name, - })), - ); - profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : []; - profile.keycloak = - userKeycloakId && typeof userKeycloakId === "string" ? userKeycloakId : ""; - } else { - const rolesData = await getRoleMappings(checkUser[0].id); - if (rolesData) { - const _roleKeycloak = await this.roleKeycloakRepo.find({ - where: { name: In(rolesData.map((x: any) => x.name)) }, - }); - profile.roleKeycloaks = - _roleKeycloak && _roleKeycloak.length > 0 ? _roleKeycloak : []; - } - profile.keycloak = checkUser[0].id; - } - - let registrationProvinceId = await this.provinceRepo.findOneBy({ - id: profile.registrationProvinceId, - }); - profile.registrationProvinceId = registrationProvinceId - ? registrationProvinceId.id - : _null; - let registrationDistrictId = await this.districtRepo.findOneBy({ - id: profile.registrationDistrictId, - }); - profile.registrationDistrictId = registrationDistrictId - ? registrationDistrictId.id - : _null; - let registrationSubDistrictId = await this.subDistrictRepo.findOneBy({ - id: profile.registrationSubDistrictId, - }); - profile.registrationSubDistrictId = registrationSubDistrictId - ? registrationSubDistrictId.id - : _null; - - let currentProvinceId = await this.provinceRepo.findOneBy({ - id: profile.currentProvinceId, - }); + profile.dateRetire = _dateRetire; + profile.dateRetireLaw = _dateRetireLaw + profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : []; + profile.keycloak = userKeycloakId && typeof userKeycloakId === "string" ? userKeycloakId : "" + profile.registrationProvinceId = registrationProvinceId ? registrationProvinceId.id : _null; + profile.registrationDistrictId = registrationDistrictId ? registrationDistrictId.id : _null; + profile.registrationSubDistrictId = registrationSubDistrictId ? registrationSubDistrictId.id : _null; profile.currentProvinceId = currentProvinceId ? currentProvinceId.id : _null; - let currentDistrictId = await this.districtRepo.findOneBy({ - id: profile.currentDistrictId, - }); profile.currentDistrictId = currentDistrictId ? currentDistrictId.id : _null; - let currentSubDistrictId = await this.subDistrictRepo.findOneBy({ - id: profile.currentSubDistrictId, - }); profile.currentSubDistrictId = currentSubDistrictId ? currentSubDistrictId.id : _null; profile.email = item.bodyProfile.email; profile.dateStart = item.bodyProfile.dateStart; @@ -4330,6 +4339,41 @@ export class CommandController extends Controller { await this.profileRepository.save(profile); setLogDataDiff(req, { before, after: profile }); } + //ขรก.ในระบบ หรือ ขรก.ในระบบที่สถานะพ้นจากราชการ + else { + console.log("have keycloak..............") + profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : []; + profile.keycloak = userKeycloakId && typeof userKeycloakId === "string" ? userKeycloakId : ""; + profile.isProbation = item.bodyProfile.isProbation; + profile.isLeave = item.bodyProfile.isLeave; + profile.isRetirement = false; + profile.isActive = true; + profile.dateLeave = _null; + profile.dateRetire = _dateRetire; + profile.dateRetireLaw = _dateRetireLaw + profile.registrationProvinceId = registrationProvinceId ? registrationProvinceId.id : _null; + profile.registrationDistrictId = registrationDistrictId ? registrationDistrictId.id : _null; + profile.registrationSubDistrictId = registrationSubDistrictId ? registrationSubDistrictId.id : _null; + profile.currentProvinceId = currentProvinceId ? currentProvinceId.id : _null; + profile.currentDistrictId = currentDistrictId ? currentDistrictId.id : _null; + profile.currentSubDistrictId = currentSubDistrictId ? currentSubDistrictId.id : _null; + profile.email = item.bodyProfile.email; + profile.dateStart = item.bodyProfile.dateStart; + profile.amount = item.bodyProfile.amount ?? null; + profile.amountSpecial = item.bodyProfile.amountSpecial ?? null; + profile.leaveCommandId = _null; + profile.leaveCommandNo = _null; + profile.leaveRemark = _null; + profile.leaveDate = _null; + profile.leaveType = _null; + profile.lastUpdateUserId = req.user.sub; + profile.lastUpdateFullName = req.user.name; + profile.lastUpdatedAt = new Date(); + profile.refCommandDate = new Date(); + await this.profileRepository.save(profile); + setLogDataDiff(req, { before, after: profile }); + } + if (profile && profile.id) { //Educations if (item.bodyEducations && item.bodyEducations.length > 0) {