From ee1569e5294f8d81b254260a22cea0310cc9505a Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Wed, 17 Apr 2024 11:21:57 +0700 Subject: [PATCH] refactor: add user to keycloak on create user --- src/controllers/user-controller.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/controllers/user-controller.ts b/src/controllers/user-controller.ts index e0d3956..99e5e96 100644 --- a/src/controllers/user-controller.ts +++ b/src/controllers/user-controller.ts @@ -19,6 +19,7 @@ import minio from "../services/minio"; import { RequestWithUser } from "../interfaces/user"; import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; +import { createUser } from "../services/keycloak"; if (!process.env.MINIO_BUCKET) { throw Error("Require MinIO bucket."); @@ -29,11 +30,11 @@ const MINIO_BUCKET = process.env.MINIO_BUCKET; type UserCreate = { status?: Status; - keycloakId: string; - userType: UserType; userRole: string; + username: string; + firstName: string; firstNameEN: string; lastName: string; @@ -237,11 +238,22 @@ export class UserController extends Controller { } } - const { provinceId, districtId, subDistrictId, ...rest } = body; + const { provinceId, districtId, subDistrictId, username, ...rest } = body; + + const result = await createUser(username, username, { + firstName: body.firstName, + lastName: body.lastName, + requiredActions: ["UPDATE_PASSWORD"], + }); + + if (!result || typeof result !== "string") { + throw new Error("Cannot create user with keycloak service."); + } const record = await prisma.user.create({ include: { province: true, district: true, subDistrict: true }, data: { + id: result, ...rest, province: { connect: provinceId ? { id: provinceId } : undefined }, district: { connect: districtId ? { id: districtId } : undefined },