From 2f6b44057955b6672c659e53cfd19281545614b8 Mon Sep 17 00:00:00 2001 From: michaelschal <35957947-michaelschal@users.noreply.replit.com> Date: Mon, 11 Aug 2025 15:31:24 +0000 Subject: [PATCH] Update user group retrieval and data handling for improved efficiency Refactor `getUserWithGroups` to use a manual join for user groups and update type assertions for mapping results to ensure correct data handling. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 2743061c-c65e-41ed-bf21-8da6d73315e6 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/2743061c-c65e-41ed-bf21-8da6d73315e6/mVgDcja --- server/storage.ts | 96 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 27 deletions(-) diff --git a/server/storage.ts b/server/storage.ts index 9d84018..972efba 100644 --- a/server/storage.ts +++ b/server/storage.ts @@ -233,17 +233,36 @@ export class DatabaseStorage implements IStorage { } async getUserWithGroups(id: string): Promise { - const user = await db.query.users.findFirst({ - where: eq(users.id, id), - with: { - userGroups: { - with: { - group: true, - }, - }, - }, - }); - return user; + // Get the user first + const user = await this.getUser(id); + if (!user) return undefined; + + // Get user's groups through manual join + const userGroupsData = await db + .select({ + userId: userGroups.userId, + groupId: userGroups.groupId, + createdAt: userGroups.createdAt, + group: { + id: groups.id, + name: groups.name, + color: groups.color, + nocodbConfigId: groups.nocodbConfigId, + nocodbTableId: groups.nocodbTableId, + nocodbTableName: groups.nocodbTableName, + invoiceColumnName: groups.invoiceColumnName, + createdAt: groups.createdAt, + updatedAt: groups.updatedAt, + } + }) + .from(userGroups) + .innerJoin(groups, eq(userGroups.groupId, groups.id)) + .where(eq(userGroups.userId, id)); + + return { + ...user, + userGroups: userGroupsData + }; } async getUsers(): Promise { @@ -481,7 +500,7 @@ export class DatabaseStorage implements IStorage { orderBy: [desc(deliveries.createdAt)], }); - return results.map(result => ({ + return results.map((result: any) => ({ ...result, order: result.order || undefined, })); @@ -686,7 +705,7 @@ export class DatabaseStorage implements IStorage { let averageDeliveryTime = 0; if (deliveredOrders.length > 0) { - const totalDelayDays = deliveredOrders.reduce((sum, order) => { + const totalDelayDays = deliveredOrders.reduce((sum: any, order: any) => { if (order.deliveredDate && order.plannedDate) { const planned = new Date(order.plannedDate); const delivered = new Date(order.deliveredDate); @@ -741,7 +760,7 @@ export class DatabaseStorage implements IStorage { const results = await query; // Récupérer les participations séparément - const publicityIds = results.map(pub => pub.id); + const publicityIds = results.map((pub: any) => pub.id); const participations = publicityIds.length > 0 ? await db.select({ publicityId: publicityParticipations.publicityId, groupId: publicityParticipations.groupId, @@ -756,11 +775,11 @@ export class DatabaseStorage implements IStorage { .where(inArray(publicityParticipations.publicityId, publicityIds)) : []; // Associer les participations aux publicités - return results.map(pub => ({ + return results.map((pub: any) => ({ ...pub, participations: participations - .filter(p => p.publicityId === pub.id) - .map(p => ({ + .filter((p: any) => p.publicityId === pub.id) + .map((p: any) => ({ publicityId: p.publicityId, groupId: p.groupId, group: p.group, @@ -811,7 +830,7 @@ export class DatabaseStorage implements IStorage { return { ...publicity[0], - participations: participations.map(p => ({ + participations: participations.map((p: any) => ({ publicityId: p.publicityId, groupId: p.groupId, group: p.group, @@ -1437,7 +1456,7 @@ export class DatabaseStorage implements IStorage { return { ...role, - rolePermissions: rolePerms.map(rp => ({ + rolePermissions: rolePerms.map((rp: any) => ({ roleId: rp.id, permissionId: rp.permissionId, createdAt: rp.createdAt, @@ -1458,19 +1477,19 @@ export class DatabaseStorage implements IStorage { } // Log des catégories trouvées - const categories = [...new Set(result.map(p => p.category))]; + const categories = [...new Set(result.map((p: any) => p.category))]; console.log('🏷️ DEV getPermissions() - Categories found:', categories); // Log spécifique pour les permissions tâches - const taskPerms = result.filter(p => p.category === 'gestion_taches'); + const taskPerms = result.filter((p: any) => p.category === 'gestion_taches'); console.log('📋 DEV getPermissions() - Task permissions found:', taskPerms.length); if (taskPerms.length > 0) { - taskPerms.forEach(p => { + taskPerms.forEach((p: any) => { console.log(` - ID: ${p.id}, Name: ${p.name}, DisplayName: "${p.displayName}", Category: ${p.category}`); }); } else { console.log('❌ DEV getPermissions() - NO TASK PERMISSIONS FOUND - This explains the issue!'); - console.log('🔍 DEV getPermissions() - Sample permissions:', result.slice(0, 3).map(p => ({ id: p.id, name: p.name, category: p.category }))); + console.log('🔍 DEV getPermissions() - Sample permissions:', result.slice(0, 3).map((p: any) => ({ id: p.id, name: p.name, category: p.category }))); } console.log('✅ DEV getPermissions() - Returning', result.length, 'permissions'); @@ -1739,15 +1758,18 @@ export class DatabaseStorage implements IStorage { } async getTasksByDateRange(startDate: string, endDate: string, groupIds?: number[]): Promise { + const start = new Date(startDate); + const end = new Date(endDate); + const whereCondition = groupIds ? and( - gte(tasks.dueDate, startDate), - lte(tasks.dueDate, endDate), + gte(tasks.dueDate, start), + lte(tasks.dueDate, end), inArray(tasks.groupId, groupIds) ) : and( - gte(tasks.dueDate, startDate), - lte(tasks.dueDate, endDate) + gte(tasks.dueDate, start), + lte(tasks.dueDate, end) ); return await db.query.tasks.findMany({ @@ -1818,8 +1840,10 @@ class MemStorage implements IStorage { id: 'admin_dev', username: 'admin', email: 'admin@dev.local', + name: 'Admin User', firstName: 'Admin', lastName: 'User', + profileImageUrl: null, password: 'admin.salt', // Simple format for development role: 'admin', passwordChanged: true, @@ -1911,6 +1935,24 @@ class MemStorage implements IStorage { async updateDelivery(id: number, delivery: Partial): Promise { return {} as Delivery; } async deleteDelivery(id: number): Promise {} + // Add missing required methods for IStorage interface + async getOrder(id: number): Promise { return undefined; } + async getDelivery(id: number): Promise { return undefined; } + async validateDelivery(id: number, deliveryData: any): Promise {} + async getUserGroups(userId: string): Promise { return []; } + async getUserRoles(userId: string): Promise { return []; } + async getUserWithRoles(userId: string): Promise { + const user = await this.getUser(userId); + return user ? { ...user, userRoles: [] } : undefined; + } + async getRoleWithPermissions(roleId: number): Promise { return undefined; } + async getUserPermissions(userId: string): Promise { return []; } + async getTasksByDateRange(startDate: string, endDate: string, groupIds?: number[]): Promise { return []; } + async getOrderStats(): Promise { return {}; } + async getDeliveryStats(): Promise { return {}; } + async getPublicitiesWithParticipations(year?: number): Promise { return []; } + async setPublicityParticipations(publicityId: number, groupIds: number[]): Promise {} + // Add placeholder methods for all other IStorage interface methods async getSavTickets(): Promise { return []; } async getSavTicket(): Promise { return undefined; }