feat: add field
This commit is contained in:
parent
ec3203e1a8
commit
57f56671b5
1 changed files with 28 additions and 4 deletions
|
|
@ -19,7 +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, deleteUser } from "../services/keycloak";
|
import { addUserRoles, createUser, deleteUser, getRoles } from "../services/keycloak";
|
||||||
|
|
||||||
if (!process.env.MINIO_BUCKET) {
|
if (!process.env.MINIO_BUCKET) {
|
||||||
throw Error("Require MinIO bucket.");
|
throw Error("Require MinIO bucket.");
|
||||||
|
|
@ -41,6 +41,8 @@ type UserCreate = {
|
||||||
lastNameEN: string;
|
lastNameEN: string;
|
||||||
gender: string;
|
gender: string;
|
||||||
|
|
||||||
|
checkpoint?: string | null;
|
||||||
|
checkpointEN?: string | null;
|
||||||
registrationNo?: string | null;
|
registrationNo?: string | null;
|
||||||
startDate?: Date | null;
|
startDate?: Date | null;
|
||||||
retireDate?: Date | null;
|
retireDate?: Date | null;
|
||||||
|
|
@ -77,6 +79,8 @@ type UserUpdate = {
|
||||||
lastNameEN?: string;
|
lastNameEN?: string;
|
||||||
gender?: string;
|
gender?: string;
|
||||||
|
|
||||||
|
checkpoint?: string | null;
|
||||||
|
checkpointEN?: string | null;
|
||||||
registrationNo?: string | null;
|
registrationNo?: string | null;
|
||||||
startDate?: Date | null;
|
startDate?: Date | null;
|
||||||
retireDate?: Date | null;
|
retireDate?: Date | null;
|
||||||
|
|
@ -240,21 +244,41 @@ export class UserController extends Controller {
|
||||||
|
|
||||||
const { provinceId, districtId, subDistrictId, username, ...rest } = body;
|
const { provinceId, districtId, subDistrictId, username, ...rest } = body;
|
||||||
|
|
||||||
const result = await createUser(username, username, {
|
let list = await getRoles();
|
||||||
|
|
||||||
|
if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server.");
|
||||||
|
if (Array.isArray(list)) {
|
||||||
|
list = list.filter(
|
||||||
|
(a) =>
|
||||||
|
!["uma_authorization", "offline_access", "default-roles"].some((b) => a.name.includes(b)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const userId = await createUser(username, username, {
|
||||||
firstName: body.firstName,
|
firstName: body.firstName,
|
||||||
lastName: body.lastName,
|
lastName: body.lastName,
|
||||||
requiredActions: ["UPDATE_PASSWORD"],
|
requiredActions: ["UPDATE_PASSWORD"],
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result || typeof result !== "string") {
|
if (!userId || typeof userId !== "string") {
|
||||||
throw new Error("Cannot create user with keycloak service.");
|
throw new Error("Cannot create user with keycloak service.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const role = list.find((v) => v.id === body.userRole);
|
||||||
|
|
||||||
|
const resultAddRole = role && (await addUserRoles(userId, [role]));
|
||||||
|
|
||||||
|
if (!resultAddRole) {
|
||||||
|
await deleteUser(userId);
|
||||||
|
throw new Error("Failed. Cannot set user's role.");
|
||||||
|
}
|
||||||
|
|
||||||
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,
|
id: userId,
|
||||||
...rest,
|
...rest,
|
||||||
|
userRole: role.name,
|
||||||
province: { connect: provinceId ? { id: provinceId } : undefined },
|
province: { connect: provinceId ? { id: provinceId } : undefined },
|
||||||
district: { connect: districtId ? { id: districtId } : undefined },
|
district: { connect: districtId ? { id: districtId } : undefined },
|
||||||
subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined },
|
subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined },
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue