From 37fc689ecc6be025e6c80fe7413a48a8b8d2718d Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Tue, 5 Nov 2024 17:26:42 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=AA=E0=B8=A3=E0=B9=89=E0=B8=B2=E0=B8=87?= =?UTF-8?q?=20user=20=E0=B8=9B=E0=B8=A3=E0=B8=B0=E0=B8=88=E0=B8=B3?= =?UTF-8?q?=E0=B8=82=E0=B8=AD=E0=B8=87=20keycloak=20#759?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/CommandController.ts | 49 ++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 7bf671ed..fa26d3d1 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -1604,24 +1604,26 @@ export class CommandController extends Controller { commandRecive = Object.assign(new CommandRecive(), item); commandRecive.order = order; let salaryData = null_; - if(item.profileId){ + if (item.profileId) { salaryData = await this.profileRepository.findOne({ where: { - id: item.profileId - } + id: item.profileId, + }, }); let null_: any = 0; if (!salaryData) { - salaryData = await this.profileEmployeeRepository.findOne({ + salaryData = await this.profileEmployeeRepository.findOne({ where: { - id: item.profileId - } - }); + id: item.profileId, + }, + }); } commandRecive.amount = salaryData ? salaryData.amount : null_; - commandRecive.positionSalaryAmount = salaryData ? salaryData.positionSalaryAmount : null_; - commandRecive.mouthSalaryAmount = salaryData ? salaryData.mouthSalaryAmount : null_; - }else{ + commandRecive.positionSalaryAmount = salaryData + ? salaryData.positionSalaryAmount + : null_; + commandRecive.mouthSalaryAmount = salaryData ? salaryData.mouthSalaryAmount : null_; + } else { commandRecive.amount = null_; commandRecive.positionSalaryAmount = null_; commandRecive.mouthSalaryAmount = null_; @@ -2984,7 +2986,34 @@ export class CommandController extends Controller { posMasterId: profile.posmasterIdTemp, }, }); + if (positionNew != null) { + + // Create Keycloak + 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); + } + 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; + // End Create Keycloak + positionNew.positionIsSelected = true; profile.posLevelId = positionNew.posLevelId; profile.posTypeId = positionNew.posTypeId;