Rollen-Fix
This commit is contained in:
@@ -21,6 +21,33 @@ const updateUserSchema = z.object({
|
||||
approvalPermissions: z.array(approvalPermissionSchema).default([])
|
||||
});
|
||||
|
||||
function serializeManagedUser(user: {
|
||||
id: string;
|
||||
name: string;
|
||||
username: string;
|
||||
role: "ADMIN" | "FINANCE" | "MEMBER";
|
||||
workingGroupId: string | null;
|
||||
workingGroup: { name: string } | null;
|
||||
approvalPreference: "CHAIR_A" | "CHAIR_B" | "FINANCE" | null;
|
||||
approvalPermissions: ("CHAIR_A" | "CHAIR_B" | "FINANCE")[];
|
||||
_count: {
|
||||
createdExpenses: number;
|
||||
approvals: number;
|
||||
};
|
||||
}) {
|
||||
return {
|
||||
id: user.id,
|
||||
name: user.username,
|
||||
username: user.username,
|
||||
role: user.role,
|
||||
workingGroupId: user.workingGroupId,
|
||||
workingGroupName: user.workingGroup?.name ?? null,
|
||||
approvalPermissions: normalizeApprovalPermissions(user.role, user.approvalPermissions, user.approvalPreference),
|
||||
createdExpensesCount: user._count.createdExpenses,
|
||||
approvalsCount: user._count.approvals
|
||||
};
|
||||
}
|
||||
|
||||
type Context = {
|
||||
params: {
|
||||
id: string;
|
||||
@@ -91,7 +118,26 @@ export async function PATCH(request: Request, { params }: Context) {
|
||||
role: parsed.data.role,
|
||||
workingGroupId,
|
||||
approvalPreference,
|
||||
approvalPermissions
|
||||
approvalPermissions: {
|
||||
set: approvalPermissions
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const refreshedUser = await prisma.user.findUniqueOrThrow({
|
||||
where: { id: updatedUser.id },
|
||||
include: {
|
||||
workingGroup: {
|
||||
select: {
|
||||
name: true
|
||||
}
|
||||
},
|
||||
_count: {
|
||||
select: {
|
||||
approvals: true,
|
||||
createdExpenses: true
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -113,7 +159,7 @@ export async function PATCH(request: Request, { params }: Context) {
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
return NextResponse.json({ user: serializeManagedUser(refreshedUser) });
|
||||
}
|
||||
|
||||
export async function DELETE(_: Request, { params }: Context) {
|
||||
|
||||
Reference in New Issue
Block a user