diff --git a/replit.md b/replit.md index 21e38ae..fc0f037 100644 --- a/replit.md +++ b/replit.md @@ -84,7 +84,7 @@ Preferred communication style: Simple, everyday language. - **Root Cause**: User records were referenced by multiple tables (customer_orders, deliveries, DLC products, etc.) - **Solution Implemented**: - Enhanced deleteUser method with database transaction for atomicity - - Dynamic admin user lookup instead of hardcoded fallback user + - Prioritized lookup for `admin_local` (default admin) with fallback to any admin user - Ownership transfer of all user-created records to existing admin before deletion - Comprehensive handling of all foreign key relationships - Graceful error handling when no admin user exists diff --git a/server/storage.ts b/server/storage.ts index e9cd257..a03e74b 100644 --- a/server/storage.ts +++ b/server/storage.ts @@ -356,13 +356,22 @@ export class DatabaseStorage implements IStorage { async deleteUser(id: string): Promise { // Use a transaction to ensure atomicity await db.transaction(async (tx: typeof db) => { - // Find an existing admin user to transfer ownership to - const adminUser = await tx + // Find admin_local or fallback to any admin user to transfer ownership to + let adminUser = await tx .select({ id: users.id }) .from(users) - .where(eq(users.role, "admin")) + .where(eq(users.id, "admin_local")) .limit(1); + // If admin_local doesn't exist, find any admin user + if (adminUser.length === 0) { + adminUser = await tx + .select({ id: users.id }) + .from(users) + .where(eq(users.role, "admin")) + .limit(1); + } + if (adminUser.length === 0) { throw new Error("Cannot delete user: No admin user found to transfer ownership to"); }