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,25 +344,33 @@ export class UserController extends Controller {
); );
} }
let list = await getRoles(); let userRole: string | undefined;
if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); if (body.userRole) {
if (Array.isArray(list)) { let list = await getRoles();
list = list.filter(
(a) =>
!["uma_authorization", "offline_access", "default-roles"].some((b) => a.name.includes(b)),
);
}
const currentRole = await getUserRoles(userId);
const role = list.find((v) => v.id === body.userRole); 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 currentRole = await getUserRoles(userId);
const resultAddRole = role && (await addUserRoles(userId, [role])); const role = list.find((v) => v.id === body.userRole);
if (!resultAddRole) { const resultAddRole = role && (await addUserRoles(userId, [role]));
throw new Error("Failed. Cannot set user's role.");
} else { if (!resultAddRole) {
if (Array.isArray(currentRole)) await removeUserRoles(userId, currentRole); throw new Error("Failed. Cannot set user's role.");
} else {
if (Array.isArray(currentRole)) await removeUserRoles(userId, currentRole);
}
userRole = role.name;
} }
const { provinceId, districtId, subDistrictId, ...rest } = body; const { provinceId, districtId, subDistrictId, ...rest } = body;
@ -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")}`) ||