feat: check for existing orders before user deletion and offer to lock instead
Some checks failed
Staging Build / build (push) Has been cancelled
Some checks failed
Staging Build / build (push) Has been cancelled
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user