From d7c10e4d647c3ce213fa3065bef1003d0cf47c70 Mon Sep 17 00:00:00 2001 From: kittapath Date: Wed, 27 Nov 2024 11:06:05 +0700 Subject: [PATCH] no message --- src/controllers/UserController.ts | 91 ++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 33 deletions(-) diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index b9cb2a68..a2fccac7 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -107,39 +107,6 @@ export class KeycloakController extends Controller { } const list = await getRoles(); if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); - const result = await addUserRoles( - userId, - list.filter((v) => body.roles.includes(v.id)), - ); - - const now = new Date().toISOString(); - const folderData: any = { - pathname: stripLeadingSlash(`${body.username.trim()}/`), - path: "", - name: body.username.trim(), - hidden: false, - permissionGroup: [], - permissionUser: [], - permissionOther: { - create: false, - read: false, - update: false, - delete: false, - perm: false, - }, - favourite: false, - color: "default", - type: "folder", - owner: body.username, - ownerId: userId, - createdAt: now, - createdBy: request.user.preferred_username, - createdByUserId: request.user.sub, - updatedAt: now, - updatedBy: request.user.preferred_username, - updatedByUserId: request.user.sub, - }; - const profile = await this.profileRepo.findOne({ where: { id: body.profileId, @@ -172,6 +139,64 @@ export class KeycloakController extends Controller { return userId; } + @Post("user-emp") + @Security("bearerAuth", ["system", "admin"]) + async createUserEmployee( + @Request() request: { user: { sub: string; preferred_username: string } }, + @Body() + body: { + username: string; + password: string; + firstName?: string; + lastName?: string; + email?: string; + roles: string[]; + profileId?: string; + }, + ) { + const userId = await createUser(body.username, body.password, { + firstName: body.firstName, + lastName: body.lastName, + // email: body.email, + }); + + if (typeof userId !== "string") { + throw new Error(userId.errorMessage); + } + const list = await getRoles(); + if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); + const profile = await this.profileEmpRepo.findOne({ + where: { + id: body.profileId, + }, + }); + + if (profile) { + let _null: any = null; + if (typeof userId === "string") { + profile.keycloak = userId; + } + profile.email = body.email == null ? _null : body.email; + await this.profileEmpRepo.save(profile); + if (body.roles != null && body.roles.length > 0) { + const roleKeycloak = await this.roleKeycloakRepo.find({ + where: { id: In(body.roles) }, + }); + const _profile = await this.profileEmpRepo.findOne({ + where: { keycloak: userId }, + relations: ["roleKeycloaks"], + }); + if (_profile) { + _profile.roleKeycloaks = Array.from( + new Set([..._profile.roleKeycloaks, ...roleKeycloak]), + ); + this.profileEmpRepo.save(_profile); + } + } + } + return userId; + } + @Put("user/{userId}") async editUser( @Path() userId: string,