diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index d0c83267..cb1e97c6 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -18,7 +18,7 @@ import HttpSuccess from "../interfaces/http-success"; import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { Command } from "../entities/Command"; -import { Brackets, LessThan, MoreThan, Double, In, Between, IsNull, Not } from "typeorm"; +import { Brackets, LessThan, MoreThan, Double, In, Between, IsNull, Not, Like } from "typeorm"; import { CommandType } from "../entities/CommandType"; import { CommandSend } from "../entities/CommandSend"; import { Profile, CreateProfileAllFields } from "../entities/Profile"; @@ -60,6 +60,7 @@ import { CreateProfileCertificate, ProfileCertificate } from "../entities/Profil import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory"; import permission from "../interfaces/permission"; import { CommandSign } from "../entities/CommandSign"; +import { RoleKeycloak } from "../entities/RoleKeycloak"; @Route("api/v1/org/command") @Tags("Command") @@ -95,6 +96,7 @@ export class CommandController extends Controller { private certificateHistoryRepo = AppDataSource.getRepository(ProfileCertificateHistory); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); private commandSignRepository = AppDataSource.getRepository(CommandSign); + private roleKeycloakRepo = AppDataSource.getRepository(RoleKeycloak); /** * API list รายการคำสั่ง @@ -2033,9 +2035,15 @@ export class CommandController extends Controller { }[]; }, ) { + const roleKeycloak = await this.roleKeycloakRepo.findOne({ + where: { name: Like("USER") }, + }); await Promise.all( body.data.map(async (item) => { - const profile = await this.profileRepository.findOneBy({ id: item.profileId }); + const profile = await this.profileRepository.findOne({ + where: { id: item.profileId }, + relations: ["roleKeycloaks"] + }) if (!profile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); } @@ -2082,6 +2090,7 @@ export class CommandController extends Controller { if (profile.keycloak != null) { const delUserKeycloak = await deleteUser(profile.keycloak); if (delUserKeycloak) profile.keycloak = _null; + profile.roleKeycloaks = []; } profile.position = _null; profile.posTypeId = _null; @@ -2110,6 +2119,7 @@ export class CommandController extends Controller { ); if (!result) throw new Error("Failed. Cannot set user's role."); profile.keycloak = userKeycloakId; + profile.roleKeycloaks = roleKeycloak ? [roleKeycloak] : [] } await this.profileRepository.save(profile); }), @@ -2145,7 +2155,10 @@ export class CommandController extends Controller { ) { await Promise.all( body.data.map(async (item) => { - const profile = await this.profileEmployeeRepository.findOneBy({ id: item.profileId }); + const profile = await this.profileEmployeeRepository.findOne({ + where: {id: item.profileId}, + relations: ["roleKeycloaks"] + }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } @@ -2196,6 +2209,7 @@ export class CommandController extends Controller { if (profile.keycloak != null) { const delUserKeycloak = await deleteUser(profile.keycloak); if (delUserKeycloak) profile.keycloak = _null; + profile.roleKeycloaks = []; } profile.position = _null; profile.posTypeId = _null; @@ -2237,7 +2251,10 @@ export class CommandController extends Controller { ) { await Promise.all( body.data.map(async (item) => { - const profile = await this.profileRepository.findOneBy({ id: item.profileId }); + const profile = await this.profileRepository.findOne({ + where: { id: item.profileId }, + relations: ["roleKeycloaks"] + }); if (!profile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); } @@ -2267,6 +2284,7 @@ export class CommandController extends Controller { if (profile.keycloak != null) { const delUserKeycloak = await deleteUser(profile.keycloak); if (delUserKeycloak) profile.keycloak = _null; + profile.roleKeycloaks = []; } profile.position = _null; profile.posTypeId = _null; @@ -2380,7 +2398,7 @@ export class CommandController extends Controller { await Promise.all( body.data.map(async (item) => { const profile = await this.profileRepository.findOne({ - relations: ["profileSalary","posLevel","posType","current_holders"], + relations: ["profileSalary","posLevel","posType","current_holders", "roleKeycloaks"], where: { id: item.profileId }, order: { profileSalary: { @@ -2509,6 +2527,7 @@ export class CommandController extends Controller { if (_profile.keycloak != null) { const delUserKeycloak = await deleteUser(_profile.keycloak); if (delUserKeycloak) _profile.keycloak = _null; + profile.roleKeycloaks = []; } _profile.position = _null; _profile.posTypeId = _null; @@ -2676,6 +2695,7 @@ export class CommandController extends Controller { } const _profile = await this.profileRepository.findOne({ where: { id: item.profileId }, + relations: ["roleKeycloaks"] }); if (!_profile) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์"); @@ -2759,6 +2779,7 @@ export class CommandController extends Controller { if (_profile.keycloak != null) { const delUserKeycloak = await deleteUser(_profile.keycloak); if (delUserKeycloak) _profile.keycloak = _null; + _profile.roleKeycloaks = []; } _profile.position = _null; _profile.posTypeId = _null; @@ -2795,6 +2816,9 @@ export class CommandController extends Controller { }[]; }, ) { + const roleKeycloak = await this.roleKeycloakRepo.findOne({ + where: { name: Like("USER") }, + }); await Promise.all( body.data.map(async (item) => { const before = null; @@ -2822,8 +2846,9 @@ export class CommandController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); } - let profile: any = await this.profileRepository.findOneBy({ - citizenId: item.bodyProfile.citizenId, + let profile: any = await this.profileRepository.findOne({ + where: { citizenId: item.bodyProfile.citizenId }, + relations: ["roleKeycloaks"] }); if (!profile) { profile = Object.assign({ ...item.bodyProfile, ...meta }); @@ -2838,7 +2863,6 @@ export class CommandController extends Controller { const userKeycloakId = await createUser(profile.citizenId, profile.citizenId, { firstName: profile.firstName, lastName: profile.lastName, - // email: profile.email, }); if (typeof userKeycloakId !== "string") { throw new Error(userKeycloakId.errorMessage); @@ -2857,6 +2881,7 @@ export class CommandController extends Controller { ); if (!result) throw new Error("Failed. Cannot set user's role."); profile.keycloak = userKeycloakId; + profile.roleKeycloaks = roleKeycloak ? [roleKeycloak] : [] profile.email = item.bodyProfile.email; await this.profileRepository.save(profile); setLogDataDiff(req, { before, after: profile }); @@ -3050,9 +3075,15 @@ export class CommandController extends Controller { }[]; }, ) { + const roleKeycloak = await this.roleKeycloakRepo.findOne({ + where: { name: Like("USER") }, + }); await Promise.all( body.refIds.map(async (item) => { - const profile = await this.profileEmployeeRepository.findOneBy({ id: item.refId }); + const profile = await this.profileEmployeeRepository.findOne({ + where: { id: item.refId }, + relations: ["roleKeycloaks"] + }) if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } @@ -3173,6 +3204,7 @@ export class CommandController extends Controller { ); if (!result) throw new Error("Failed. Cannot set user's role."); profile.keycloak = userKeycloakId; + profile.roleKeycloaks = roleKeycloak ? [roleKeycloak] : [] // End Create Keycloak positionNew.positionIsSelected = true;