feat: check for existing orders before user deletion and offer to lock instead
Some checks failed
Staging Build / build (push) Has been cancelled

This commit is contained in:
DanielS
2026-06-25 01:33:38 +02:00
parent 86d71b2441
commit 24929494a6
2 changed files with 22 additions and 1 deletions

View File

@@ -92,6 +92,21 @@ export async function createUser(data: { email: string; role?: 'admin' | 'partne
export async function deleteUser(id: string) {
try {
const admin = createAdminClient();
// Check if user has orders (documents/belege)
const { count, error: countError } = await admin
.from('orders')
.select('*', { count: 'exact', head: true })
.eq('user_id', id);
if (countError) {
console.error('Failed to count user orders:', countError);
}
if (count && count > 0) {
throw new Error('Auf diesen User gibt es bereits Belege. Sperren möglich.');
}
// Delete from public.users first to satisfy foreign key constraints
const { error: dbError } = await admin.from('users').delete().eq('id', id);
if (dbError) {