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 },