refactor: add user to keycloak on create user
This commit is contained in:
parent
6c54796d9d
commit
ee1569e529
1 changed files with 15 additions and 3 deletions
|
|
@ -19,6 +19,7 @@ import minio from "../services/minio";
|
||||||
import { RequestWithUser } from "../interfaces/user";
|
import { RequestWithUser } from "../interfaces/user";
|
||||||
import HttpError from "../interfaces/http-error";
|
import HttpError from "../interfaces/http-error";
|
||||||
import HttpStatus from "../interfaces/http-status";
|
import HttpStatus from "../interfaces/http-status";
|
||||||
|
import { createUser } from "../services/keycloak";
|
||||||
|
|
||||||
if (!process.env.MINIO_BUCKET) {
|
if (!process.env.MINIO_BUCKET) {
|
||||||
throw Error("Require MinIO bucket.");
|
throw Error("Require MinIO bucket.");
|
||||||
|
|
@ -29,11 +30,11 @@ const MINIO_BUCKET = process.env.MINIO_BUCKET;
|
||||||
type UserCreate = {
|
type UserCreate = {
|
||||||
status?: Status;
|
status?: Status;
|
||||||
|
|
||||||
keycloakId: string;
|
|
||||||
|
|
||||||
userType: UserType;
|
userType: UserType;
|
||||||
userRole: string;
|
userRole: string;
|
||||||
|
|
||||||
|
username: string;
|
||||||
|
|
||||||
firstName: string;
|
firstName: string;
|
||||||
firstNameEN: string;
|
firstNameEN: string;
|
||||||
lastName: 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({
|
const record = await prisma.user.create({
|
||||||
include: { province: true, district: true, subDistrict: true },
|
include: { province: true, district: true, subDistrict: true },
|
||||||
data: {
|
data: {
|
||||||
|
id: result,
|
||||||
...rest,
|
...rest,
|
||||||
province: { connect: provinceId ? { id: provinceId } : undefined },
|
province: { connect: provinceId ? { id: provinceId } : undefined },
|
||||||
district: { connect: districtId ? { id: districtId } : undefined },
|
district: { connect: districtId ? { id: districtId } : undefined },
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue