Prioritize default admin for user deletion transfers

Update the user deletion process to prioritize transferring ownership to the `admin_local` user, falling back to any available admin if `admin_local` is not found.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: a8a78c07-e900-425c-a577-5b4c5894379d
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/a8a78c07-e900-425c-a577-5b4c5894379d/vetH0Gp
This commit is contained in:
michaelschal committed 2025-08-11 17:51:53 +00:00
1 parent 2cb363c0b4
commit c24e19da52
2 files changed
+13 -4

No files matched your search

+1 -1
View File
@@ -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.) - **Root Cause**: User records were referenced by multiple tables (customer_orders, deliveries, DLC products, etc.)
- **Solution Implemented**: - **Solution Implemented**:
- Enhanced deleteUser method with database transaction for atomicity - 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 - Ownership transfer of all user-created records to existing admin before deletion
- Comprehensive handling of all foreign key relationships - Comprehensive handling of all foreign key relationships
- Graceful error handling when no admin user exists - Graceful error handling when no admin user exists
+12 -3
View File
@@ -356,13 +356,22 @@ export class DatabaseStorage implements IStorage {
async deleteUser(id: string): Promise<void> { async deleteUser(id: string): Promise<void> {
// Use a transaction to ensure atomicity // Use a transaction to ensure atomicity
await db.transaction(async (tx: typeof db) => { await db.transaction(async (tx: typeof db) => {
// Find an existing admin user to transfer ownership to // Find admin_local or fallback to any admin user to transfer ownership to
const adminUser = await tx let adminUser = await tx
.select({ id: users.id }) .select({ id: users.id })
.from(users) .from(users)
.where(eq(users.role, "admin")) .where(eq(users.id, "admin_local"))
.limit(1); .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) { if (adminUser.length === 0) {
throw new Error("Cannot delete user: No admin user found to transfer ownership to"); throw new Error("Cannot delete user: No admin user found to transfer ownership to");
} }