diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 1c921ae4..1427b388 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -53,7 +53,7 @@ import { PosMasterAct } from "../entities/PosMasterAct"; import { sendToQueue } from "../services/rabbitmq"; import { PosLevel } from "../entities/PosLevel"; import { PosType } from "../entities/PosType"; -import { addUserRoles, createUser, getRoles, deleteUser } from "../keycloak"; +import { addUserRoles, createUser, getRoles, deleteUser, enableStatus } from "../keycloak"; import { ProfileEducation, CreateProfileEducation } from "../entities/ProfileEducation"; import { ProfileEducationHistory } from "../entities/ProfileEducationHistory"; import { CreateProfileCertificate, ProfileCertificate } from "../entities/ProfileCertificate"; @@ -2098,28 +2098,35 @@ export class CommandController extends Controller { } const returnWork = await checkReturnCommandType(String(item.commandId)); if (returnWork && item.isGovernment) { - const userKeycloakId = await createUser(profile.citizenId, profile.citizenId, { - firstName: profile.firstName, - lastName: profile.lastName, - }); - if (typeof userKeycloakId !== "string") { - throw new Error(userKeycloakId.errorMessage); + if (profile.keycloak != null ) { + const enableActive = await enableStatus(profile.keycloak, true); + if (!enableActive) throw new Error("Failed. Cannot change enable status."); } - 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, - })), - ); - if (!result) throw new Error("Failed. Cannot set user's role."); - profile.keycloak = userKeycloakId; - profile.roleKeycloaks = roleKeycloak ? [roleKeycloak] : [] + else { + const userKeycloakId = await createUser(profile.citizenId, profile.citizenId, { + firstName: profile.firstName, + lastName: profile.lastName, + }); + if (typeof userKeycloakId !== "string") { + throw new Error(userKeycloakId.errorMessage); + } + 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, + })), + ); + if (!result) throw new Error("Failed. Cannot set user's role."); + profile.keycloak = userKeycloakId; + } + profile.isActive = true; + profile.roleKeycloaks = profile.roleKeycloaks ?? [roleKeycloak]; } await this.profileRepository.save(profile); }), @@ -2522,12 +2529,17 @@ export class CommandController extends Controller { if (item.isLeave == true && !exceptClear) { await removeProfileInOrganize(_profile.id, "OFFICER"); } + else if (item.isLeave == true && exceptClear && _profile.keycloak != null) { + const enableActive = await enableStatus(_profile.keycloak, false); + if (!enableActive) throw new Error("Failed. Cannot change enable status."); + _profile.isActive = false; + } const clearProfile = await checkCommandType(String(item.commandId)); if (clearProfile) { if (_profile.keycloak != null) { const delUserKeycloak = await deleteUser(_profile.keycloak); if (delUserKeycloak) _profile.keycloak = _null; - profile.roleKeycloaks = []; + _profile.roleKeycloaks = []; } _profile.position = _null; _profile.posTypeId = _null;