refactor: add user to keycloak on create user

This commit is contained in:
Methapon2001 2024-04-17 11:21:57 +07:00
parent 6c54796d9d
commit ee1569e529

View file

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