diff --git a/attached_assets/image_1754934559795.png b/attached_assets/image_1754934559795.png new file mode 100644 index 0000000..fc3ce2d Binary files /dev/null and b/attached_assets/image_1754934559795.png differ diff --git a/replit.md b/replit.md index fccbac2..21e38ae 100644 --- a/replit.md +++ b/replit.md @@ -75,4 +75,18 @@ Preferred communication style: Simple, everyday language. - **Development Server**: Vite dev server ### Other Integrations -- **NocoDB Integration**: Configurable for invoice verification and data synchronization. \ No newline at end of file +- **NocoDB Integration**: Configurable for invoice verification and data synchronization. + +## Recent Changes + +### August 11, 2025 - User Deletion Fix +- **Issue**: Foreign key constraint errors when deleting users who created customer orders or other records +- **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 + - 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 +- **Tables Handled**: customer_orders, orders, deliveries, publicities, dlcProducts, tasks, nocodbConfig, userGroups, userRoles +- **Result**: Safe user deletion while preserving business data integrity \ No newline at end of file diff --git a/server/storage.ts b/server/storage.ts index f08cf65..e9cd257 100644 --- a/server/storage.ts +++ b/server/storage.ts @@ -356,8 +356,18 @@ export class DatabaseStorage implements IStorage { async deleteUser(id: string): Promise { // Use a transaction to ensure atomicity await db.transaction(async (tx: typeof db) => { - // Transfer ownership of all records created by this user to admin_dev - const adminUserId = "admin_dev"; + // Find an existing admin user to transfer ownership to + const 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"); + } + + const adminUserId = adminUser[0].id; // Update customer orders await tx