diff --git a/src/controllers/ProfileChangeNameController.ts b/src/controllers/ProfileChangeNameController.ts index a120052b..c9ca7158 100644 --- a/src/controllers/ProfileChangeNameController.ts +++ b/src/controllers/ProfileChangeNameController.ts @@ -24,6 +24,8 @@ import { ProfileChangeName, UpdateProfileChangeName, } from "../entities/ProfileChangeName"; +import CallAPI from "../interfaces/call-api"; +import { updateName } from "../keycloak"; @Route("api/v1/org/profile/changeName") @Tags("ProfileChangeName") @@ -145,6 +147,16 @@ export class ProfileChangeNameController extends Controller { profile.prefix = body.prefix ?? profile.prefix; await this.profileRepository.save(profile); + // if (profile != null) { + // const result = await updateName(profile.id, profile.firstName, profile.lastName,req); + + // const _mapData = { + // data: mappedData, + // total: total, + // }; + + // } + return new HttpSuccess(data.id); } diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 99f6ce42..2f8fed6f 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -3150,6 +3150,7 @@ export class ProfileController extends Controller { case "citizenId": [findProfile, total] = await this.profileRepo.findAndCount({ where: { + keycloak: IsNull(), citizenId: Like(`%${body.keyword}%`), }, relations: ["posType", "posLevel", "current_holders", "profileSalary"], @@ -3289,6 +3290,6 @@ export class ProfileController extends Controller { }), ); - return new HttpSuccess({data: mapDataProfile , total}); + return new HttpSuccess({ data: mapDataProfile, total }); } } diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index 92f8bb14..764b920a 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -2931,7 +2931,7 @@ export class ProfileEmployeeController extends Controller { const skip = (page - 1) * pageSize; const take = pageSize; switch (body.fieldName) { - case "idcard": + case "citizenId": [findProfile, total] = await this.profileRepo.findAndCount({ where: { keycloak: IsNull(), @@ -3049,7 +3049,7 @@ export class ProfileEmployeeController extends Controller { firstName: item.firstName, lastName: item.lastName, position: item.position, - idcard: item.citizenId, + citizenId: item.citizenId, email: item.email, phone: item.phone, name: fullName, diff --git a/src/keycloak/index.ts b/src/keycloak/index.ts index b32eaf6e..240ff947 100644 --- a/src/keycloak/index.ts +++ b/src/keycloak/index.ts @@ -203,6 +203,45 @@ export async function editUser(userId: string, opts: Record) { const id = path?.split("/").at(-1); return id || true; } +/** + * Update keycloak user by uuid + * + * Client must have permission to manage realm's user + * + * @returns user uuid or true if success, false otherwise. + */ +export async function updateName( + userId: string, + firstName: string, + lastName: string, + opts: Record, +) { + const { password, ...rest } = opts; + + const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/users/${userId}`, { + // prettier-ignore + headers: { + "authorization": `Bearer ${await getToken()}`, + "content-type": `application/json`, + }, + method: "PUT", + body: JSON.stringify({ + enabled: true, + credentials: (password && [{ type: "password", value: opts?.password }]) || undefined, + ...rest, + }), + }).catch((e) => console.log("Keycloak Error: ", e)); + + if (!res) return false; + if (!res.ok) { + // return Boolean(console.error("Keycloak Error Response: ", await res.json())); + return await res.json(); + } + + const path = res.headers.get("Location"); + const id = path?.split("/").at(-1); + return id || true; +} /** * Delete keycloak user by uuid @@ -588,4 +627,6 @@ export async function removeUserGroup(userId: string, groupId: string) { } return true; + + }