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
This commit is contained in:
michaelschal committed 2025-08-11 15:31:24 +00:00
1 parent 97f6217dcc
commit 2f6b440579
1 file changed
+69 -27
+69 -27
View File
@@ -233,17 +233,36 @@ export class DatabaseStorage implements IStorage {
} }
async getUserWithGroups(id: string): Promise<UserWithGroups | undefined> { async getUserWithGroups(id: string): Promise<UserWithGroups | undefined> {
const user = await db.query.users.findFirst({ // Get the user first
where: eq(users.id, id), const user = await this.getUser(id);
with: { if (!user) return undefined;
userGroups: {
with: { // Get user's groups through manual join
group: true, const userGroupsData = await db
}, .select({
}, userId: userGroups.userId,
}, groupId: userGroups.groupId,
}); createdAt: userGroups.createdAt,
return user; 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<User[]> { async getUsers(): Promise<User[]> {
@@ -481,7 +500,7 @@ export class DatabaseStorage implements IStorage {
orderBy: [desc(deliveries.createdAt)], orderBy: [desc(deliveries.createdAt)],
}); });
return results.map(result => ({ return results.map((result: any) => ({
...result, ...result,
order: result.order || undefined, order: result.order || undefined,
})); }));
@@ -686,7 +705,7 @@ export class DatabaseStorage implements IStorage {
let averageDeliveryTime = 0; let averageDeliveryTime = 0;
if (deliveredOrders.length > 0) { if (deliveredOrders.length > 0) {
const totalDelayDays = deliveredOrders.reduce((sum, order) => { const totalDelayDays = deliveredOrders.reduce((sum: any, order: any) => {
if (order.deliveredDate && order.plannedDate) { if (order.deliveredDate && order.plannedDate) {
const planned = new Date(order.plannedDate); const planned = new Date(order.plannedDate);
const delivered = new Date(order.deliveredDate); const delivered = new Date(order.deliveredDate);
@@ -741,7 +760,7 @@ export class DatabaseStorage implements IStorage {
const results = await query; const results = await query;
// Récupérer les participations séparément // 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({ const participations = publicityIds.length > 0 ? await db.select({
publicityId: publicityParticipations.publicityId, publicityId: publicityParticipations.publicityId,
groupId: publicityParticipations.groupId, groupId: publicityParticipations.groupId,
@@ -756,11 +775,11 @@ export class DatabaseStorage implements IStorage {
.where(inArray(publicityParticipations.publicityId, publicityIds)) : []; .where(inArray(publicityParticipations.publicityId, publicityIds)) : [];
// Associer les participations aux publicités // Associer les participations aux publicités
return results.map(pub => ({ return results.map((pub: any) => ({
...pub, ...pub,
participations: participations participations: participations
.filter(p => p.publicityId === pub.id) .filter((p: any) => p.publicityId === pub.id)
.map(p => ({ .map((p: any) => ({
publicityId: p.publicityId, publicityId: p.publicityId,
groupId: p.groupId, groupId: p.groupId,
group: p.group, group: p.group,
@@ -811,7 +830,7 @@ export class DatabaseStorage implements IStorage {
return { return {
...publicity[0], ...publicity[0],
participations: participations.map(p => ({ participations: participations.map((p: any) => ({
publicityId: p.publicityId, publicityId: p.publicityId,
groupId: p.groupId, groupId: p.groupId,
group: p.group, group: p.group,
@@ -1437,7 +1456,7 @@ export class DatabaseStorage implements IStorage {
return { return {
...role, ...role,
rolePermissions: rolePerms.map(rp => ({ rolePermissions: rolePerms.map((rp: any) => ({
roleId: rp.id, roleId: rp.id,
permissionId: rp.permissionId, permissionId: rp.permissionId,
createdAt: rp.createdAt, createdAt: rp.createdAt,
@@ -1458,19 +1477,19 @@ export class DatabaseStorage implements IStorage {
} }
// Log des catégories trouvées // 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); console.log('🏷️ DEV getPermissions() - Categories found:', categories);
// Log spécifique pour les permissions tâches // 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); console.log('📋 DEV getPermissions() - Task permissions found:', taskPerms.length);
if (taskPerms.length > 0) { 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}`); console.log(` - ID: ${p.id}, Name: ${p.name}, DisplayName: "${p.displayName}", Category: ${p.category}`);
}); });
} else { } else {
console.log('❌ DEV getPermissions() - NO TASK PERMISSIONS FOUND - This explains the issue!'); 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'); 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<any[]> { async getTasksByDateRange(startDate: string, endDate: string, groupIds?: number[]): Promise<any[]> {
const start = new Date(startDate);
const end = new Date(endDate);
const whereCondition = groupIds const whereCondition = groupIds
? and( ? and(
gte(tasks.dueDate, startDate), gte(tasks.dueDate, start),
lte(tasks.dueDate, endDate), lte(tasks.dueDate, end),
inArray(tasks.groupId, groupIds) inArray(tasks.groupId, groupIds)
) )
: and( : and(
gte(tasks.dueDate, startDate), gte(tasks.dueDate, start),
lte(tasks.dueDate, endDate) lte(tasks.dueDate, end)
); );
return await db.query.tasks.findMany({ return await db.query.tasks.findMany({
@@ -1818,8 +1840,10 @@ class MemStorage implements IStorage {
id: 'admin_dev', id: 'admin_dev',
username: 'admin', username: 'admin',
email: 'admin@dev.local', email: 'admin@dev.local',
name: 'Admin User',
firstName: 'Admin', firstName: 'Admin',
lastName: 'User', lastName: 'User',
profileImageUrl: null,
password: 'admin.salt', // Simple format for development password: 'admin.salt', // Simple format for development
role: 'admin', role: 'admin',
passwordChanged: true, passwordChanged: true,
@@ -1911,6 +1935,24 @@ class MemStorage implements IStorage {
async updateDelivery(id: number, delivery: Partial<InsertDelivery>): Promise<Delivery> { return {} as Delivery; } async updateDelivery(id: number, delivery: Partial<InsertDelivery>): Promise<Delivery> { return {} as Delivery; }
async deleteDelivery(id: number): Promise<void> {} async deleteDelivery(id: number): Promise<void> {}
// Add missing required methods for IStorage interface
async getOrder(id: number): Promise<OrderWithRelations | undefined> { return undefined; }
async getDelivery(id: number): Promise<DeliveryWithRelations | undefined> { return undefined; }
async validateDelivery(id: number, deliveryData: any): Promise<void> {}
async getUserGroups(userId: string): Promise<any[]> { return []; }
async getUserRoles(userId: string): Promise<any[]> { return []; }
async getUserWithRoles(userId: string): Promise<UserWithRoles | undefined> {
const user = await this.getUser(userId);
return user ? { ...user, userRoles: [] } : undefined;
}
async getRoleWithPermissions(roleId: number): Promise<any> { return undefined; }
async getUserPermissions(userId: string): Promise<any[]> { return []; }
async getTasksByDateRange(startDate: string, endDate: string, groupIds?: number[]): Promise<any[]> { return []; }
async getOrderStats(): Promise<any> { return {}; }
async getDeliveryStats(): Promise<any> { return {}; }
async getPublicitiesWithParticipations(year?: number): Promise<any[]> { return []; }
async setPublicityParticipations(publicityId: number, groupIds: number[]): Promise<void> {}
// Add placeholder methods for all other IStorage interface methods // Add placeholder methods for all other IStorage interface methods
async getSavTickets(): Promise<any[]> { return []; } async getSavTickets(): Promise<any[]> { return []; }
async getSavTicket(): Promise<any> { return undefined; } async getSavTicket(): Promise<any> { return undefined; }