diff --git a/App/app/(portal)/admin/users/actions.ts b/App/app/(portal)/admin/users/actions.ts index 19e6d8d..52c7de0 100644 --- a/App/app/(portal)/admin/users/actions.ts +++ b/App/app/(portal)/admin/users/actions.ts @@ -109,11 +109,20 @@ export async function deleteUser(id: string): Promise { if (!session?.user || !hasPermission(session.user.role, "manage_users")) return { error: "Unauthorized" }; if (id === session.user.id) return { error: "Cannot delete your own account." }; - const inUse = await db.purchaseOrder.findFirst({ where: { submitterId: id } }); - if (inUse) return { error: "Cannot delete: user has submitted purchase orders. Deactivate them instead." }; + const hasPos = await db.purchaseOrder.findFirst({ where: { submitterId: id } }); + if (hasPos) return { error: "Cannot delete: user has submitted purchase orders. Deactivate them instead." }; + + const hasActions = await db.pOAction.findFirst({ where: { actorId: id } }); + if (hasActions) return { error: "Cannot delete: user has purchase order activity on record. Deactivate them instead." }; + + const hasConsumption = await db.itemConsumption.findFirst({ where: { recordedById: id } }); + if (hasConsumption) return { error: "Cannot delete: user has inventory consumption records. Deactivate them instead." }; await db.$transaction(async (tx) => { await tx.notification.deleteMany({ where: { userId: id } }); + await tx.superUserRequest.deleteMany({ + where: { OR: [{ userId: id }, { resolvedById: id }] }, + }); await tx.user.delete({ where: { id } }); });