fix: error when not sent role
This commit is contained in:
parent
186f13bd6e
commit
4750f1945d
1 changed files with 24 additions and 16 deletions
|
|
@ -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 (Array.isArray(list)) {
|
||||
list = list.filter(
|
||||
(a) =>
|
||||
!["uma_authorization", "offline_access", "default-roles"].some((b) => a.name.includes(b)),
|
||||
);
|
||||
}
|
||||
const currentRole = await getUserRoles(userId);
|
||||
if (body.userRole) {
|
||||
let list = await getRoles();
|
||||
|
||||
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) {
|
||||
throw new Error("Failed. Cannot set user's role.");
|
||||
} else {
|
||||
if (Array.isArray(currentRole)) await removeUserRoles(userId, currentRole);
|
||||
const resultAddRole = role && (await addUserRoles(userId, [role]));
|
||||
|
||||
if (!resultAddRole) {
|
||||
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;
|
||||
|
|
@ -391,7 +399,7 @@ export class UserController extends Controller {
|
|||
include: { province: true, district: true, subDistrict: true },
|
||||
data: {
|
||||
...rest,
|
||||
userRole: role.name,
|
||||
userRole,
|
||||
code:
|
||||
(lastUserOfType &&
|
||||
`${user.code?.slice(0, 3)}${body.userType !== "USER" ? body.userType?.charAt(0) : ""}${(+(lastUserOfType?.code?.slice(-4) || 0) + 1).toString().padStart(4, "0")}`) ||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue