fix: error when not sent role

This commit is contained in:
Methapon2001 2024-04-18 16:32:38 +07:00
parent 186f13bd6e
commit 4750f1945d

View file

@ -344,13 +344,18 @@ export class UserController extends Controller {
); );
} }
let userRole: string | undefined;
if (body.userRole) {
let list = await getRoles(); let list = await getRoles();
if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server.");
if (Array.isArray(list)) { if (Array.isArray(list)) {
list = list.filter( list = list.filter(
(a) => (a) =>
!["uma_authorization", "offline_access", "default-roles"].some((b) => a.name.includes(b)), !["uma_authorization", "offline_access", "default-roles"].some((b) =>
a.name.includes(b),
),
); );
} }
const currentRole = await getUserRoles(userId); const currentRole = await getUserRoles(userId);
@ -365,6 +370,9 @@ export class UserController extends Controller {
if (Array.isArray(currentRole)) await removeUserRoles(userId, currentRole); if (Array.isArray(currentRole)) await removeUserRoles(userId, currentRole);
} }
userRole = role.name;
}
const { provinceId, districtId, subDistrictId, ...rest } = body; const { provinceId, districtId, subDistrictId, ...rest } = body;
const user = await prisma.user.findFirst({ const user = await prisma.user.findFirst({
@ -391,7 +399,7 @@ export class UserController extends Controller {
include: { province: true, district: true, subDistrict: true }, include: { province: true, district: true, subDistrict: true },
data: { data: {
...rest, ...rest,
userRole: role.name, userRole,
code: code:
(lastUserOfType && (lastUserOfType &&
`${user.code?.slice(0, 3)}${body.userType !== "USER" ? body.userType?.charAt(0) : ""}${(+(lastUserOfType?.code?.slice(-4) || 0) + 1).toString().padStart(4, "0")}`) || `${user.code?.slice(0, 3)}${body.userType !== "USER" ? body.userType?.charAt(0) : ""}${(+(lastUserOfType?.code?.slice(-4) || 0) + 1).toString().padStart(4, "0")}`) ||