import { users, groups, suppliers, orders, deliveries, userGroups, publicities, publicityParticipations, customerOrders, dlcProducts, tasks, nocodbConfig, invoiceVerificationCache, type User, type UpsertUser, type Group, type InsertGroup, type Supplier, type InsertSupplier, type Order, type InsertOrder, type Delivery, type InsertDelivery, type UserGroup, type InsertUserGroup, type OrderWithRelations, type DeliveryWithRelations, type UserWithGroups, type Publicity, type InsertPublicity, type PublicityParticipation, type InsertPublicityParticipation, type PublicityWithRelations, type CustomerOrder, type InsertCustomerOrder, type CustomerOrderWithRelations, type DlcProduct, type InsertDlcProduct, type DlcProductFrontend, type InsertDlcProductFrontend, type DlcProductWithRelations, type Task, type InsertTask, type TaskWithRelations, type NocodbConfig, type InsertNocodbConfig, type InvoiceVerificationCache, type InsertInvoiceVerificationCache, } from "@shared/schema"; import { db } from "./db"; import { eq, and, inArray, desc, sql, gte, lte, lt, or, isNull, isNotNull, asc } from "drizzle-orm"; export interface IStorage { // User operations getUser(id: string): Promise; getUserByEmail(email: string): Promise; getUserByUsername(username: string): Promise; upsertUser(user: UpsertUser): Promise; getUserWithGroups(id: string): Promise; getUsers(): Promise; createUser(user: UpsertUser): Promise; updateUser(id: string, user: Partial): Promise; deleteUser(id: string): Promise; // Group operations getGroups(): Promise; getGroup(id: number): Promise; createGroup(group: InsertGroup): Promise; updateGroup(id: number, group: Partial): Promise; deleteGroup(id: number): Promise; // Supplier operations getSuppliers(): Promise; createSupplier(supplier: InsertSupplier): Promise; updateSupplier(id: number, supplier: Partial): Promise; deleteSupplier(id: number): Promise; // Order operations getOrders(groupIds?: number[]): Promise; getOrdersByDateRange(startDate: string, endDate: string, groupIds?: number[]): Promise; getOrder(id: number): Promise; createOrder(order: InsertOrder): Promise; updateOrder(id: number, order: Partial): Promise; deleteOrder(id: number): Promise; // Delivery operations getDeliveries(groupIds?: number[]): Promise; getDeliveriesByDateRange(startDate: string, endDate: string, groupIds?: number[]): Promise; getDelivery(id: number): Promise; createDelivery(delivery: InsertDelivery): Promise; updateDelivery(id: number, delivery: Partial): Promise; deleteDelivery(id: number): Promise; validateDelivery(id: number, blData?: { blNumber: string; blAmount: number }): Promise; // User-Group operations getUserGroups(userId: string): Promise; assignUserToGroup(userGroup: InsertUserGroup): Promise; removeUserFromGroup(userId: string, groupId: number): Promise; // Statistics getMonthlyStats(year: number, month: number, groupIds?: number[]): Promise<{ ordersCount: number; deliveriesCount: number; pendingOrdersCount: number; averageDeliveryTime: number; totalPalettes: number; totalPackages: number; }>; // Publicity operations getPublicities(year?: number, groupIds?: number[]): Promise; getPublicity(id: number): Promise; createPublicity(publicity: InsertPublicity): Promise; updatePublicity(id: number, publicity: Partial): Promise; deletePublicity(id: number): Promise; // Publicity participation operations getPublicityParticipations(publicityId: number): Promise; setPublicityParticipations(publicityId: number, groupIds: number[]): Promise; // NocoDB Configuration operations getNocodbConfigs(): Promise; getNocodbConfig(id: number): Promise; getActiveNocodbConfig(): Promise; createNocodbConfig(config: InsertNocodbConfig): Promise; updateNocodbConfig(id: number, config: Partial): Promise; deleteNocodbConfig(id: number): Promise; // Invoice Verification Cache operations getInvoiceVerificationCache(cacheKey: string): Promise; createInvoiceVerificationCache(cache: InsertInvoiceVerificationCache): Promise; clearExpiredCache(): Promise; // Customer Order operations getCustomerOrders(groupIds?: number[]): Promise; getCustomerOrder(id: number): Promise; createCustomerOrder(customerOrder: InsertCustomerOrder): Promise; updateCustomerOrder(id: number, customerOrder: Partial): Promise; deleteCustomerOrder(id: number): Promise; // DLC Product operations getDlcProducts(groupIds?: number[], filters?: { status?: string; supplierId?: number; }): Promise; getDlcProduct(id: number): Promise; createDlcProduct(dlcProduct: InsertDlcProduct): Promise; updateDlcProduct(id: number, dlcProduct: Partial): Promise; deleteDlcProduct(id: number): Promise; validateDlcProduct(id: number, validatedBy: string): Promise; getDlcStats(groupIds?: number[]): Promise<{ active: number; expiringSoon: number; expired: number; }>; // Task operations getTasks(groupIds?: number[]): Promise; getTask(id: number): Promise; createTask(task: InsertTask): Promise; updateTask(id: number, task: Partial): Promise; deleteTask(id: number): Promise; completeTask(id: number, completedBy?: string): Promise; } export class DatabaseStorage implements IStorage { // User operations async getUser(id: string): Promise { const [user] = await db.select().from(users).where(eq(users.id, id)); return user; } async getUserByEmail(email: string): Promise { const [user] = await db.select().from(users).where(eq(users.email, email)); return user; } async getUserByUsername(username: string): Promise { const [user] = await db.select().from(users).where(eq(users.username, username)); return user; } async upsertUser(userData: UpsertUser): Promise { const [user] = await db .insert(users) .values(userData) .onConflictDoUpdate({ target: users.id, set: { ...userData, updatedAt: new Date(), }, }) .returning(); return user; } async getUserWithGroups(id: string): Promise { const user = await this.getUser(id); if (!user) return undefined; try { const result = await db.execute(sql` SELECT ug.user_id, ug.group_id, g.id as group_id_ref, g.name as group_name, g.color as group_color, g.created_at as group_created_at, g.updated_at as group_updated_at FROM user_groups ug INNER JOIN groups g ON ug.group_id = g.id WHERE ug.user_id = ${id} `); const userGroups = result.rows.map((row: any) => ({ userId: row.user_id, groupId: row.group_id, group: { id: row.group_id_ref, name: row.group_name, color: row.group_color, createdAt: row.group_created_at, updatedAt: row.group_updated_at, } })); return { ...user, userGroups }; } catch (error) { console.error('Error fetching user with groups:', error); return { ...user, userGroups: [] }; } } async getUsers(): Promise { return await db.select().from(users); } async createUser(userData: UpsertUser): Promise { const [user] = await db.insert(users).values(userData).returning(); return user; } async updateUser(id: string, userData: Partial): Promise { const [user] = await db .update(users) .set({ ...userData, updatedAt: new Date() }) .where(eq(users.id, id)) .returning(); return user; } async deleteUser(id: string): Promise { await db.delete(users).where(eq(users.id, id)); } // Group operations async getGroups(): Promise { return await db.select().from(groups).orderBy(groups.name); } async getGroup(id: number): Promise { const [group] = await db.select().from(groups).where(eq(groups.id, id)); return group; } async createGroup(groupData: InsertGroup): Promise { const [group] = await db.insert(groups).values(groupData).returning(); return group; } async updateGroup(id: number, groupData: Partial): Promise { const [group] = await db .update(groups) .set({ ...groupData, updatedAt: new Date() }) .where(eq(groups.id, id)) .returning(); return group; } async deleteGroup(id: number): Promise { await db.delete(groups).where(eq(groups.id, id)); } // Supplier operations async getSuppliers(): Promise { return await db.select().from(suppliers).orderBy(suppliers.name); } async createSupplier(supplierData: InsertSupplier): Promise { const [supplier] = await db.insert(suppliers).values(supplierData).returning(); return supplier; } async updateSupplier(id: number, supplierData: Partial): Promise { const [supplier] = await db .update(suppliers) .set({ ...supplierData, updatedAt: new Date() }) .where(eq(suppliers.id, id)) .returning(); return supplier; } async deleteSupplier(id: number): Promise { await db.delete(suppliers).where(eq(suppliers.id, id)); } // Order operations async getOrders(groupIds?: number[]): Promise { let query = db .select({ id: orders.id, supplierId: orders.supplierId, groupId: orders.groupId, plannedDate: orders.plannedDate, status: orders.status, quantity: orders.quantity, unit: orders.unit, notes: orders.notes, createdBy: orders.createdBy, createdAt: orders.createdAt, updatedAt: orders.updatedAt, supplier: suppliers, group: groups, }) .from(orders) .leftJoin(suppliers, eq(orders.supplierId, suppliers.id)) .leftJoin(groups, eq(orders.groupId, groups.id)); if (groupIds && groupIds.length > 0) { query = query.where(inArray(orders.groupId, groupIds)); } return await query.orderBy(desc(orders.createdAt)); } async getOrdersByDateRange(startDate: string, endDate: string, groupIds?: number[]): Promise { let query = db .select({ id: orders.id, supplierId: orders.supplierId, groupId: orders.groupId, plannedDate: orders.plannedDate, status: orders.status, quantity: orders.quantity, unit: orders.unit, notes: orders.notes, createdBy: orders.createdBy, createdAt: orders.createdAt, updatedAt: orders.updatedAt, supplier: suppliers, group: groups, }) .from(orders) .leftJoin(suppliers, eq(orders.supplierId, suppliers.id)) .leftJoin(groups, eq(orders.groupId, groups.id)) .where( and( gte(orders.plannedDate, startDate), lte(orders.plannedDate, endDate) ) ); if (groupIds && groupIds.length > 0) { query = query.where( and( gte(orders.plannedDate, startDate), lte(orders.plannedDate, endDate), inArray(orders.groupId, groupIds) ) ); } return await query.orderBy(desc(orders.plannedDate)); } async getOrder(id: number): Promise { const [order] = await db .select({ id: orders.id, supplierId: orders.supplierId, groupId: orders.groupId, plannedDate: orders.plannedDate, status: orders.status, quantity: orders.quantity, unit: orders.unit, notes: orders.notes, createdBy: orders.createdBy, createdAt: orders.createdAt, updatedAt: orders.updatedAt, supplier: suppliers, group: groups, }) .from(orders) .leftJoin(suppliers, eq(orders.supplierId, suppliers.id)) .leftJoin(groups, eq(orders.groupId, groups.id)) .where(eq(orders.id, id)); return order; } async createOrder(orderData: InsertOrder): Promise { const [order] = await db.insert(orders).values(orderData).returning(); return order; } async updateOrder(id: number, orderData: Partial): Promise { const [order] = await db .update(orders) .set({ ...orderData, updatedAt: new Date() }) .where(eq(orders.id, id)) .returning(); return order; } async deleteOrder(id: number): Promise { await db.delete(orders).where(eq(orders.id, id)); } // Delivery operations async getDeliveries(groupIds?: number[]): Promise { let query = db .select({ id: deliveries.id, orderId: deliveries.orderId, supplierId: deliveries.supplierId, groupId: deliveries.groupId, scheduledDate: deliveries.scheduledDate, deliveredDate: deliveries.deliveredDate, status: deliveries.status, quantity: deliveries.quantity, unit: deliveries.unit, blNumber: deliveries.blNumber, blAmount: deliveries.blAmount, invoiceReference: deliveries.invoiceReference, invoiceAmount: deliveries.invoiceAmount, reconciled: deliveries.reconciled, validatedAt: deliveries.validatedAt, notes: deliveries.notes, createdBy: deliveries.createdBy, createdAt: deliveries.createdAt, updatedAt: deliveries.updatedAt, supplier: suppliers, group: groups, }) .from(deliveries) .leftJoin(suppliers, eq(deliveries.supplierId, suppliers.id)) .leftJoin(groups, eq(deliveries.groupId, groups.id)); if (groupIds && groupIds.length > 0) { query = query.where(inArray(deliveries.groupId, groupIds)); } return await query.orderBy(desc(deliveries.createdAt)); } async getDeliveriesByDateRange(startDate: string, endDate: string, groupIds?: number[]): Promise { let query = db .select({ id: deliveries.id, orderId: deliveries.orderId, supplierId: deliveries.supplierId, groupId: deliveries.groupId, scheduledDate: deliveries.scheduledDate, deliveredDate: deliveries.deliveredDate, status: deliveries.status, quantity: deliveries.quantity, unit: deliveries.unit, blNumber: deliveries.blNumber, blAmount: deliveries.blAmount, invoiceReference: deliveries.invoiceReference, invoiceAmount: deliveries.invoiceAmount, reconciled: deliveries.reconciled, validatedAt: deliveries.validatedAt, notes: deliveries.notes, createdBy: deliveries.createdBy, createdAt: deliveries.createdAt, updatedAt: deliveries.updatedAt, supplier: suppliers, group: groups, }) .from(deliveries) .leftJoin(suppliers, eq(deliveries.supplierId, suppliers.id)) .leftJoin(groups, eq(deliveries.groupId, groups.id)) .where( and( gte(deliveries.scheduledDate, startDate), lte(deliveries.scheduledDate, endDate) ) ); if (groupIds && groupIds.length > 0) { query = query.where( and( gte(deliveries.scheduledDate, startDate), lte(deliveries.scheduledDate, endDate), inArray(deliveries.groupId, groupIds) ) ); } return await query.orderBy(desc(deliveries.scheduledDate)); } async getDelivery(id: number): Promise { const [delivery] = await db .select({ id: deliveries.id, orderId: deliveries.orderId, supplierId: deliveries.supplierId, groupId: deliveries.groupId, scheduledDate: deliveries.scheduledDate, deliveredDate: deliveries.deliveredDate, status: deliveries.status, quantity: deliveries.quantity, unit: deliveries.unit, blNumber: deliveries.blNumber, blAmount: deliveries.blAmount, invoiceReference: deliveries.invoiceReference, invoiceAmount: deliveries.invoiceAmount, reconciled: deliveries.reconciled, validatedAt: deliveries.validatedAt, notes: deliveries.notes, createdBy: deliveries.createdBy, createdAt: deliveries.createdAt, updatedAt: deliveries.updatedAt, supplier: suppliers, group: groups, }) .from(deliveries) .leftJoin(suppliers, eq(deliveries.supplierId, suppliers.id)) .leftJoin(groups, eq(deliveries.groupId, groups.id)) .where(eq(deliveries.id, id)); return delivery; } async createDelivery(deliveryData: InsertDelivery): Promise { const [delivery] = await db.insert(deliveries).values(deliveryData).returning(); return delivery; } async updateDelivery(id: number, deliveryData: Partial): Promise { const [delivery] = await db .update(deliveries) .set({ ...deliveryData, updatedAt: new Date() }) .where(eq(deliveries.id, id)) .returning(); return delivery; } async deleteDelivery(id: number): Promise { await db.delete(deliveries).where(eq(deliveries.id, id)); } async validateDelivery(id: number, blData?: { blNumber: string; blAmount: number }): Promise { const updateData: any = { status: 'delivered', deliveredDate: new Date().toISOString(), updatedAt: new Date(), }; if (blData) { updateData.blNumber = blData.blNumber; if (blData.blAmount !== undefined) { updateData.blAmount = blData.blAmount.toString(); } } await db.update(deliveries).set(updateData).where(eq(deliveries.id, id)); } // User-Group operations async getUserGroups(userId: string): Promise { return await db.select().from(userGroups).where(eq(userGroups.userId, userId)); } async assignUserToGroup(userGroupData: InsertUserGroup): Promise { const [userGroup] = await db.insert(userGroups).values(userGroupData).returning(); return userGroup; } async removeUserFromGroup(userId: string, groupId: number): Promise { await db.delete(userGroups).where( and(eq(userGroups.userId, userId), eq(userGroups.groupId, groupId)) ); } // Statistics async getMonthlyStats(year: number, month: number, groupIds?: number[]): Promise<{ ordersCount: number; deliveriesCount: number; pendingOrdersCount: number; averageDeliveryTime: number; totalPalettes: number; totalPackages: number; }> { // En mode développement avec MemStorage, retourner des statistiques simulées if (process.env.NODE_ENV === 'development') { console.log('📊 Mode développement - statistiques simulées'); return { ordersCount: 12, deliveriesCount: 8, pendingOrdersCount: 3, averageDeliveryTime: 2.5, totalPalettes: 45, totalPackages: 156, }; } // En production, calculer les vraies statistiques const startDate = `${year}-${month.toString().padStart(2, '0')}-01`; const endDate = month === 12 ? `${year + 1}-01-01` : `${year}-${(month + 1).toString().padStart(2, '0')}-01`; try { console.log('📊 Calcul statistiques mensuelles:', { year, month, startDate, endDate, groupIds }); // Construire les conditions pour le filtrage par groupe let ordersWhereCondition = and( gte(orders.plannedDate, startDate), lt(orders.plannedDate, endDate) ); let deliveriesWhereCondition = and( gte(deliveries.scheduledDate, startDate), lt(deliveries.scheduledDate, endDate) ); // Ajouter le filtre par groupe si spécifié if (groupIds && groupIds.length > 0) { ordersWhereCondition = and(ordersWhereCondition, inArray(orders.groupId, groupIds)); deliveriesWhereCondition = and(deliveriesWhereCondition, inArray(deliveries.groupId, groupIds)); } // Compter les commandes du mois const ordersResult = await db .select({ count: sql`count(*)` }) .from(orders) .where(ordersWhereCondition); // Compter les livraisons du mois const deliveriesResult = await db .select({ count: sql`count(*)` }) .from(deliveries) .where(deliveriesWhereCondition); // Commandes en attente (sans date de livraison ou pas encore livrées) let pendingWhereCondition = eq(orders.status, 'pending'); if (groupIds && groupIds.length > 0) { pendingWhereCondition = and(pendingWhereCondition, inArray(orders.groupId, groupIds)) as any; } const pendingResult = await db .select({ count: sql`count(*)` }) .from(orders) .where(pendingWhereCondition); // Calculer les totaux de palettes et colis du mois const deliveriesStatsResult = await db .select({ totalPalettes: sql`COALESCE(SUM(CAST(${deliveries.quantity} as INTEGER)), 0)`, totalPackages: sql`COALESCE(COUNT(*), 0)`, avgDelay: sql`COALESCE(AVG(EXTRACT(DAY FROM (${deliveries.deliveredDate} - ${deliveries.scheduledDate}))), 0)` }) .from(deliveries) .where(and( deliveriesWhereCondition, isNotNull(deliveries.deliveredDate) )); const ordersCount = Number(ordersResult[0]?.count || 0); const deliveriesCount = Number(deliveriesResult[0]?.count || 0); const pendingOrdersCount = Number(pendingResult[0]?.count || 0); const totalPalettes = Number(deliveriesStatsResult[0]?.totalPalettes || 0); const totalPackages = Number(deliveriesStatsResult[0]?.totalPackages || 0); const averageDeliveryTime = Number(deliveriesStatsResult[0]?.avgDelay || 0); console.log('📊 Statistiques calculées:', { ordersCount, deliveriesCount, pendingOrdersCount, averageDeliveryTime, totalPalettes, totalPackages, }); return { ordersCount, deliveriesCount, pendingOrdersCount, averageDeliveryTime, totalPalettes, totalPackages, }; } catch (error) { console.error('❌ Erreur calcul statistiques:', error); // Retourner des zéros en cas d'erreur pour éviter un crash return { ordersCount: 0, deliveriesCount: 0, pendingOrdersCount: 0, averageDeliveryTime: 0, totalPalettes: 0, totalPackages: 0, }; } } // Publicity operations async getPublicities(year?: number, groupIds?: number[]): Promise { let query = db .select({ id: publicities.id, pubNumber: publicities.pubNumber, designation: publicities.designation, startDate: publicities.startDate, endDate: publicities.endDate, year: publicities.year, createdBy: publicities.createdBy, createdAt: publicities.createdAt, updatedAt: publicities.updatedAt, }) .from(publicities); if (year) { query = query.where(eq(publicities.year, year)); } const results = await query.orderBy(desc(publicities.createdAt)); const publicityIds = results.map((p: any) => p.id); const participations = publicityIds.length > 0 ? await db .select({ publicityId: publicityParticipations.publicityId, groupId: publicityParticipations.groupId, group: groups, }) .from(publicityParticipations) .leftJoin(groups, eq(publicityParticipations.groupId, groups.id)) .where(inArray(publicityParticipations.publicityId, publicityIds)) : []; return results.map((publicity: any) => ({ ...publicity, participations: participations .filter((p: any) => p.publicityId === publicity.id) .map((p: any) => ({ publicityId: p.publicityId, groupId: p.groupId, group: p.group!, })), })); } async getPublicity(id: number): Promise { const [publicity] = await db.select().from(publicities).where(eq(publicities.id, id)); if (!publicity) return undefined; const participations = await db .select({ publicityId: publicityParticipations.publicityId, groupId: publicityParticipations.groupId, group: groups, }) .from(publicityParticipations) .leftJoin(groups, eq(publicityParticipations.groupId, groups.id)) .where(eq(publicityParticipations.publicityId, id)); return { ...publicity, participations: participations.map((p: any) => ({ publicityId: p.publicityId, groupId: p.groupId, group: p.group!, })), }; } async createPublicity(publicityData: InsertPublicity): Promise { const [publicity] = await db.insert(publicities).values(publicityData).returning(); return publicity; } async updatePublicity(id: number, publicityData: Partial): Promise { const [publicity] = await db .update(publicities) .set({ ...publicityData, updatedAt: new Date() }) .where(eq(publicities.id, id)) .returning(); return publicity; } async deletePublicity(id: number): Promise { await db.delete(publicityParticipations).where(eq(publicityParticipations.publicityId, id)); await db.delete(publicities).where(eq(publicities.id, id)); } async getPublicityParticipations(publicityId: number): Promise { return await db .select() .from(publicityParticipations) .where(eq(publicityParticipations.publicityId, publicityId)); } async setPublicityParticipations(publicityId: number, groupIds: number[]): Promise { await db.delete(publicityParticipations).where(eq(publicityParticipations.publicityId, publicityId)); if (groupIds.length > 0) { const participationsData = groupIds.map(groupId => ({ publicityId, groupId, })); await db.insert(publicityParticipations).values(participationsData); } } // NocoDB Configuration operations async getNocodbConfigs(): Promise { return await db.select().from(nocodbConfig).orderBy(desc(nocodbConfig.createdAt)); } async getNocodbConfig(id: number): Promise { const [config] = await db.select().from(nocodbConfig).where(eq(nocodbConfig.id, id)); return config; } async getActiveNocodbConfig(): Promise { try { const [config] = await db .select() .from(nocodbConfig) .where(eq(nocodbConfig.isActive, true)) .limit(1); console.log('🔧 Configuration NocoDB active récupérée:', config); return config; } catch (error) { console.error('❌ Erreur récupération config NocoDB:', error); return undefined; } } async createNocodbConfig(configData: InsertNocodbConfig): Promise { const [config] = await db.insert(nocodbConfig).values(configData).returning(); return config; } async updateNocodbConfig(id: number, configData: Partial): Promise { const [config] = await db .update(nocodbConfig) .set({ ...configData, updatedAt: new Date() }) .where(eq(nocodbConfig.id, id)) .returning(); return config; } async deleteNocodbConfig(id: number): Promise { await db.delete(nocodbConfig).where(eq(nocodbConfig.id, id)); } // Invoice Verification Cache operations async getInvoiceVerificationCache(cacheKey: string): Promise { const [cache] = await db .select() .from(invoiceVerificationCache) .where(eq(invoiceVerificationCache.cacheKey, cacheKey)); return cache; } async createInvoiceVerificationCache(cacheData: InsertInvoiceVerificationCache): Promise { const [cache] = await db.insert(invoiceVerificationCache).values(cacheData).returning(); return cache; } async clearExpiredCache(): Promise { await db.delete(invoiceVerificationCache).where( sql`expires_at < NOW()` ); } // Real implementations for production operations async getCustomerOrders(groupIds?: number[]): Promise { let query = db .select({ customerOrder: customerOrders, supplier: suppliers, group: groups }) .from(customerOrders) .leftJoin(suppliers, eq(customerOrders.supplierId, suppliers.id)) .leftJoin(groups, eq(customerOrders.groupId, groups.id)); if (groupIds && groupIds.length > 0) { query = query.where(inArray(customerOrders.groupId, groupIds)); } const results = await query.orderBy(desc(customerOrders.createdAt)); return results.map((row: any) => ({ ...row.customerOrder, supplier: row.supplier, group: row.group, creator: row.creator || { id: row.customerOrder.createdBy, username: 'unknown' } })); } async getCustomerOrder(id: number): Promise { const [result] = await db .select({ customerOrder: customerOrders, supplier: suppliers, group: groups }) .from(customerOrders) .leftJoin(suppliers, eq(customerOrders.supplierId, suppliers.id)) .leftJoin(groups, eq(customerOrders.groupId, groups.id)) .where(eq(customerOrders.id, id)); if (!result) return undefined; return { ...result.customerOrder, supplier: result.supplier, group: result.group, creator: result.creator || { id: result.customerOrder.createdBy, username: 'unknown' } }; } async createCustomerOrder(orderData: InsertCustomerOrder): Promise { const [customerOrder] = await db.insert(customerOrders).values(orderData).returning(); return customerOrder; } async updateCustomerOrder(id: number, orderData: Partial): Promise { const [customerOrder] = await db .update(customerOrders) .set({ ...orderData, updatedAt: new Date() }) .where(eq(customerOrders.id, id)) .returning(); return customerOrder; } async deleteCustomerOrder(id: number): Promise { await db.delete(customerOrders).where(eq(customerOrders.id, id)); } async getDlcProducts(groupIds?: number[]): Promise { let query = db .select({ dlcProduct: dlcProducts, supplier: suppliers, group: groups }) .from(dlcProducts) .leftJoin(suppliers, eq(dlcProducts.supplierId, suppliers.id)) .leftJoin(groups, eq(dlcProducts.groupId, groups.id)); if (groupIds && groupIds.length > 0) { query = query.where(inArray(dlcProducts.groupId, groupIds)); } const results = await query.orderBy(asc(dlcProducts.expiryDate)); return results.map((row: any) => ({ ...row.dlcProduct, supplier: row.supplier, group: row.group, creator: row.creator || { id: row.dlcProduct.createdBy, username: 'unknown' } })); } async getDlcProduct(id: number): Promise { const [result] = await db .select({ dlcProduct: dlcProducts, supplier: suppliers, group: groups }) .from(dlcProducts) .leftJoin(suppliers, eq(dlcProducts.supplierId, suppliers.id)) .leftJoin(groups, eq(dlcProducts.groupId, groups.id)) .where(eq(dlcProducts.id, id)); if (!result) return undefined; return { ...result.dlcProduct, supplier: result.supplier, group: result.group, creator: result.creator || { id: result.dlcProduct.createdBy, username: 'unknown' } }; } async createDlcProduct(productData: InsertDlcProduct): Promise { const [dlcProduct] = await db.insert(dlcProducts).values(productData).returning(); return { ...dlcProduct, dlcDate: new Date(dlcProduct.expiryDate) } as DlcProductFrontend; } async updateDlcProduct(id: number, productData: Partial): Promise { const [dlcProduct] = await db .update(dlcProducts) .set({ ...productData, updatedAt: new Date() }) .where(eq(dlcProducts.id, id)) .returning(); return dlcProduct as DlcProductFrontend; } async deleteDlcProduct(id: number): Promise { await db.delete(dlcProducts).where(eq(dlcProducts.id, id)); } async validateDlcProduct(id: number, validatedBy: string): Promise { const [dlcProduct] = await db .update(dlcProducts) .set({ validatedBy, validatedAt: new Date(), updatedAt: new Date() }) .where(eq(dlcProducts.id, id)) .returning(); return dlcProduct as DlcProductFrontend; } async getDlcStats(groupIds?: number[]): Promise<{ active: number; expiringSoon: number; expired: number; }> { const today = new Date(); const alertDate = new Date(); alertDate.setDate(today.getDate() + 15); let whereCondition = sql`1 = 1`; if (groupIds && groupIds.length > 0) { whereCondition = inArray(dlcProducts.groupId, groupIds); } const [stats] = await db .select({ active: sql`COUNT(CASE WHEN ${dlcProducts.expiryDate} > ${today.toISOString().split('T')[0]} THEN 1 END)`, expiringSoon: sql`COUNT(CASE WHEN ${dlcProducts.expiryDate} BETWEEN ${today.toISOString().split('T')[0]} AND ${alertDate.toISOString().split('T')[0]} THEN 1 END)`, expired: sql`COUNT(CASE WHEN ${dlcProducts.expiryDate} <= ${today.toISOString().split('T')[0]} THEN 1 END)` }) .from(dlcProducts) .where(whereCondition); return { active: stats.active || 0, expiringSoon: stats.expiringSoon || 0, expired: stats.expired || 0 }; } async getTasks(groupIds?: number[]): Promise { let query = db .select({ task: tasks, group: groups }) .from(tasks) .leftJoin(groups, eq(tasks.groupId, groups.id)); if (groupIds && groupIds.length > 0) { query = query.where(inArray(tasks.groupId, groupIds)); } const results = await query.orderBy(desc(tasks.createdAt)); return results.map((row: any) => ({ ...row.task, group: row.group })); } async getTask(id: number): Promise { const [result] = await db .select({ task: tasks, group: groups }) .from(tasks) .leftJoin(groups, eq(tasks.groupId, groups.id)) .where(eq(tasks.id, id)); if (!result) return undefined; return { ...result.task, group: result.group }; } async createTask(taskData: InsertTask): Promise { const [task] = await db.insert(tasks).values(taskData).returning(); return task; } async updateTask(id: number, taskData: Partial): Promise { const [task] = await db .update(tasks) .set({ ...taskData, updatedAt: new Date() }) .where(eq(tasks.id, id)) .returning(); return task; } async deleteTask(id: number): Promise { await db.delete(tasks).where(eq(tasks.id, id)); } async completeTask(id: number, completedBy: string): Promise { await db .update(tasks) .set({ status: 'completed', completedAt: new Date(), completedBy, updatedAt: new Date() }) .where(eq(tasks.id, id)); } } // MemStorage class for development export class MemStorage implements IStorage { private users = new Map(); private groups = new Map(); private suppliers = new Map(); private orders = new Map(); private deliveries = new Map(); private userGroups = new Map(); private publicities = new Map(); private publicityParticipations = new Map(); private customerOrders = new Map(); private dlcProducts = new Map(); private tasks = new Map(); private idCounters = { group: 1, supplier: 1, order: 1, delivery: 1, publicity: 1, customerOrder: 1, dlcProduct: 1, task: 1, }; constructor() { this.initializeTestData(); this.initializeExtraTestData(); } private initializeTestData() { // Create admin user const adminUser: User = { id: 'admin', username: 'admin', email: 'admin@example.com', name: 'Admin', firstName: 'Admin', lastName: 'User', profileImageUrl: null, password: 'admin', // In real app, this would be hashed role: 'admin', passwordChanged: false, createdAt: new Date(), updatedAt: new Date(), }; this.users.set(adminUser.id, adminUser); // Create test groups const testGroup: Group = { id: 1, name: 'Magasin Test', color: '#3B82F6', nocodbConfigId: null, nocodbTableName: null, invoiceColumnName: null, nocodbBlColumnName: null, nocodbAmountColumnName: null, nocodbSupplierColumnName: null, webhookUrl: null, createdAt: new Date(), updatedAt: new Date(), }; this.groups.set(testGroup.id, testGroup); this.idCounters.group = 2; // Create test supplier const testSupplier: Supplier = { id: 1, name: 'Fournisseur Test', contact: 'test@supplier.com', phone: '0123456789', hasDlc: false, automaticReconciliation: false, createdAt: new Date(), updatedAt: new Date(), }; this.suppliers.set(testSupplier.id, testSupplier); this.idCounters.supplier = 2; console.log('✅ MemStorage initialized with admin user (admin/admin)'); } // User operations async getUser(id: string): Promise { return this.users.get(id); } async getUserByEmail(email: string): Promise { for (const user of this.users.values()) { if (user.email === email) return user; } return undefined; } async getUserByUsername(username: string): Promise { for (const user of this.users.values()) { if (user.username === username) return user; } return undefined; } async upsertUser(userData: UpsertUser): Promise { const existingUser = this.users.get(userData.id); const user: User = { id: userData.id, username: userData.username, email: userData.email || null, name: userData.name || null, firstName: userData.firstName || null, lastName: userData.lastName || null, profileImageUrl: userData.profileImageUrl || null, password: userData.password || null, role: userData.role || 'user', passwordChanged: userData.passwordChanged || false, createdAt: existingUser?.createdAt || new Date(), updatedAt: new Date(), }; this.users.set(user.id, user); return user; } async getUserWithGroups(id: string): Promise { const user = await this.getUser(id); if (!user) return undefined; const userGroupsList = this.userGroups.get(id) || []; const userGroupsWithGroups = userGroupsList.map(ug => ({ ...ug, group: this.groups.get(ug.groupId)!, })); return { ...user, userGroups: userGroupsWithGroups }; } async getUsers(): Promise { return Array.from(this.users.values()); } async createUser(userData: UpsertUser): Promise { return this.upsertUser(userData); } async updateUser(id: string, userData: Partial): Promise { const existingUser = this.users.get(id); if (!existingUser) throw new Error('User not found'); const updatedUser = { ...existingUser, ...userData, updatedAt: new Date() }; this.users.set(id, updatedUser); return updatedUser; } async deleteUser(id: string): Promise { this.users.delete(id); this.userGroups.delete(id); } // Group operations async getGroups(): Promise { return Array.from(this.groups.values()); } async getGroup(id: number): Promise { return this.groups.get(id); } async createGroup(groupData: InsertGroup): Promise { const id = this.idCounters.group++; const group: Group = { id, name: groupData.name, color: groupData.color || null, nocodbConfigId: groupData.nocodbConfigId || null, nocodbTableName: groupData.nocodbTableName || null, invoiceColumnName: groupData.invoiceColumnName || null, nocodbBlColumnName: groupData.nocodbBlColumnName || null, nocodbAmountColumnName: groupData.nocodbAmountColumnName || null, nocodbSupplierColumnName: groupData.nocodbSupplierColumnName || null, webhookUrl: groupData.webhookUrl || null, createdAt: new Date(), updatedAt: new Date(), }; this.groups.set(id, group); return group; } async updateGroup(id: number, groupData: Partial): Promise { const existingGroup = this.groups.get(id); if (!existingGroup) throw new Error('Group not found'); const updatedGroup = { ...existingGroup, ...groupData, updatedAt: new Date() }; this.groups.set(id, updatedGroup); return updatedGroup; } async deleteGroup(id: number): Promise { this.groups.delete(id); } // Supplier operations async getSuppliers(): Promise { return Array.from(this.suppliers.values()); } async createSupplier(supplierData: InsertSupplier): Promise { const id = this.idCounters.supplier++; const supplier: Supplier = { id, name: supplierData.name, contact: supplierData.contact || null, phone: supplierData.phone || null, hasDlc: supplierData.hasDlc || null, automaticReconciliation: supplierData.automaticReconciliation || null, createdAt: new Date(), updatedAt: new Date(), }; this.suppliers.set(id, supplier); return supplier; } async updateSupplier(id: number, supplierData: Partial): Promise { const existingSupplier = this.suppliers.get(id); if (!existingSupplier) throw new Error('Supplier not found'); const updatedSupplier = { ...existingSupplier, ...supplierData, updatedAt: new Date() }; this.suppliers.set(id, updatedSupplier); return updatedSupplier; } async deleteSupplier(id: number): Promise { this.suppliers.delete(id); } // NocoDB Configuration methods async getNocodbConfigs(): Promise { return []; // Empty array for development } async getNocodbConfig(id: number): Promise { return undefined; // Not found in development } async getActiveNocodbConfig(): Promise { // En développement, créer une config de test const testConfig: NocodbConfig = { id: 1, name: 'Configuration Test NocoDB', baseUrl: 'https://nocodb-test.example.com', projectId: 'test-project-id', apiToken: 'test-api-token-123', description: 'Configuration de test pour vérification des factures', isActive: true, createdBy: 'admin', createdAt: new Date(), updatedAt: new Date(), }; console.log('🔧 Retour config NocoDB test:', testConfig); return testConfig; } async createNocodbConfig(config: InsertNocodbConfig): Promise { const newConfig: NocodbConfig = { id: 1, name: config.name, baseUrl: config.baseUrl, projectId: config.projectId, apiToken: config.apiToken, description: config.description || null, isActive: config.isActive ?? true, createdBy: config.createdBy || null, createdAt: new Date(), updatedAt: new Date(), }; return newConfig; } async updateNocodbConfig(id: number, config: Partial): Promise { const updatedConfig: NocodbConfig = { id, name: config.name || 'Test Config', baseUrl: config.baseUrl || 'https://nocodb.example.com', projectId: config.projectId || 'test-project', apiToken: config.apiToken || 'test-token', description: config.description || null, isActive: config.isActive ?? true, createdBy: config.createdBy || null, createdAt: new Date(), updatedAt: new Date(), }; return updatedConfig; } async deleteNocodbConfig(id: number): Promise { // No-op in development } async getInvoiceVerificationCache(cacheKey: string): Promise { return undefined; // Pas de cache en développement } async createInvoiceVerificationCache(cache: InsertInvoiceVerificationCache): Promise { const newCache: InvoiceVerificationCache = { id: 1, cacheKey: cache.cacheKey, groupId: cache.groupId, invoiceReference: cache.invoiceReference, supplierName: cache.supplierName || null, exists: cache.exists, matchType: cache.matchType, errorMessage: cache.errorMessage || null, cacheHit: cache.cacheHit || false, apiCallTime: cache.apiCallTime || null, expiresAt: cache.expiresAt, createdAt: new Date(), updatedAt: new Date(), }; return newCache; } async clearExpiredCache(): Promise { // No-op en développement } // Placeholder implementations for remaining methods async getOrders(): Promise { return []; } async getOrdersByDateRange(): Promise { return []; } async getOrder(): Promise { return undefined; } async createOrder(): Promise { return {} as Order; } async updateOrder(): Promise { return {} as Order; } async deleteOrder(): Promise {} async getDeliveries(): Promise { return []; } async getDeliveriesByDateRange(): Promise { return []; } async getDelivery(): Promise { return undefined; } async createDelivery(): Promise { return {} as Delivery; } async updateDelivery(): Promise { return {} as Delivery; } async deleteDelivery(): Promise {} async validateDelivery(): Promise {} async getUserGroups(): Promise { return []; } async assignUserToGroup(): Promise { return {} as UserGroup; } async removeUserFromGroup(): Promise {} async getMonthlyStats(): Promise { return {}; } async getPublicities(): Promise { return []; } async getPublicity(): Promise { return undefined; } async createPublicity(): Promise { return {} as Publicity; } async updatePublicity(): Promise { return {} as Publicity; } async deletePublicity(): Promise {} async getPublicityParticipations(): Promise { return []; } async setPublicityParticipations(): Promise {} async getCustomerOrders(groupIds?: number[]): Promise { let orders = Array.from(this.customerOrders.values()); if (groupIds && groupIds.length > 0) { orders = orders.filter(order => groupIds.includes(order.groupId)); } return orders.map(order => ({ ...order, supplier: this.suppliers.get(order.supplierId)!, group: this.groups.get(order.groupId)!, creator: this.users.get(order.createdBy)! })); } async getCustomerOrder(id: number): Promise { const order = this.customerOrders.get(id); if (!order) return undefined; return { ...order, supplier: this.suppliers.get(order.supplierId)!, group: this.groups.get(order.groupId)!, creator: this.users.get(order.createdBy)! }; } async createCustomerOrder(orderData: InsertCustomerOrder): Promise { const id = this.idCounters.customerOrder++; const order: CustomerOrder = { id, ...orderData, status: orderData.status || 'En attente de Commande', quantity: orderData.quantity || 1, deposit: orderData.deposit || "0.00" as any, isPromotionalPrice: orderData.isPromotionalPrice || false, customerNotified: orderData.customerNotified || false, createdAt: new Date(), updatedAt: new Date(), }; this.customerOrders.set(id, order); return order; } async updateCustomerOrder(id: number, orderData: Partial): Promise { const existingOrder = this.customerOrders.get(id); if (!existingOrder) throw new Error('Customer order not found'); const updatedOrder = { ...existingOrder, ...orderData, updatedAt: new Date() }; this.customerOrders.set(id, updatedOrder); return updatedOrder; } async deleteCustomerOrder(id: number): Promise { this.customerOrders.delete(id); } async getDlcProducts(groupIds?: number[], filters?: { status?: string; supplierId?: number; }): Promise { let products = Array.from(this.dlcProducts.values()); if (groupIds && groupIds.length > 0) { products = products.filter(product => groupIds.includes(product.groupId)); } if (filters) { if (filters.status) { products = products.filter(product => product.status === filters.status); } if (filters.supplierId) { products = products.filter(product => product.supplierId === filters.supplierId); } } return products.map(product => ({ ...product, supplier: this.suppliers.get(product.supplierId)!, group: this.groups.get(product.groupId)!, creator: this.users.get(product.createdBy)! })); } async getDlcProduct(id: number): Promise { const product = this.dlcProducts.get(id); if (!product) return undefined; return { ...product, supplier: this.suppliers.get(product.supplierId)!, group: this.groups.get(product.groupId)!, creator: this.users.get(product.createdBy)! }; } async createDlcProduct(productData: InsertDlcProduct): Promise { const id = this.idCounters.dlcProduct++; const product: DlcProduct = { id, ...productData, status: productData.status || 'en_cours', quantity: productData.quantity || 1, unit: productData.unit || 'unité', location: productData.location || 'Magasin', alertThreshold: productData.alertThreshold || 15, createdAt: new Date(), updatedAt: new Date(), }; this.dlcProducts.set(id, product); return { ...product, dlcDate: new Date(product.expiryDate) } as DlcProductFrontend; } async updateDlcProduct(id: number, productData: Partial): Promise { const existingProduct = this.dlcProducts.get(id); if (!existingProduct) throw new Error('DLC Product not found'); const updatedProduct = { ...existingProduct, ...productData, updatedAt: new Date() }; this.dlcProducts.set(id, updatedProduct); return { ...updatedProduct, dlcDate: new Date(updatedProduct.expiryDate) } as DlcProductFrontend; } async deleteDlcProduct(id: number): Promise { this.dlcProducts.delete(id); } async validateDlcProduct(id: number, validatedBy: string): Promise { const existingProduct = this.dlcProducts.get(id); if (!existingProduct) throw new Error('DLC Product not found'); const updatedProduct = { ...existingProduct, validatedBy, validatedAt: new Date(), updatedAt: new Date() }; this.dlcProducts.set(id, updatedProduct); return { ...updatedProduct, dlcDate: new Date(updatedProduct.expiryDate) } as DlcProductFrontend; } async getDlcStats(groupIds?: number[]): Promise<{ active: number; expiringSoon: number; expired: number; }> { const today = new Date(); const alertDate = new Date(); alertDate.setDate(today.getDate() + 15); let products = Array.from(this.dlcProducts.values()); if (groupIds && groupIds.length > 0) { products = products.filter(product => groupIds.includes(product.groupId)); } return { active: products.filter(p => new Date(p.expiryDate) > today).length, expiringSoon: products.filter(p => { const expiry = new Date(p.expiryDate); return expiry >= today && expiry <= alertDate; }).length, expired: products.filter(p => new Date(p.expiryDate) <= today).length }; } async getTasks(groupIds?: number[]): Promise { let tasks = Array.from(this.tasks.values()); if (groupIds && groupIds.length > 0) { tasks = tasks.filter(task => groupIds.includes(task.groupId)); } return tasks.map(task => ({ ...task, group: this.groups.get(task.groupId)!, creator: this.users.get(task.createdBy)! })); } async getTask(id: number): Promise { const task = this.tasks.get(id); if (!task) return undefined; return { ...task, group: this.groups.get(task.groupId)!, creator: this.users.get(task.createdBy)! }; } async createTask(taskData: InsertTask): Promise { const id = this.idCounters.task++; const task: Task = { id, ...taskData, status: taskData.status || 'pending', priority: taskData.priority || 'medium', completedAt: null, completedBy: null, createdAt: new Date(), updatedAt: new Date(), }; this.tasks.set(id, task); return task; } private initializeExtraTestData() { // Add test customer orders const testOrder1: CustomerOrder = { id: 1, supplierId: 1, groupId: 1, createdBy: 'admin', orderTaker: 'Admin', customerName: 'Marie Dupont', customerPhone: '0123456789', productDesignation: 'Table de jardin', quantity: 2, deposit: '150.00' as any, status: 'En attente de Commande', isPromotionalPrice: false, customerNotified: false, notes: 'Livraison urgente demandée', createdAt: new Date('2025-01-15'), updatedAt: new Date('2025-01-15'), }; const testOrder2: CustomerOrder = { id: 2, supplierId: 1, groupId: 1, createdBy: 'admin', orderTaker: 'Admin', customerName: 'Pierre Martin', customerPhone: '0987654321', productDesignation: 'Chaises pliantes x4', quantity: 1, deposit: '80.00' as any, status: 'Commande passée', isPromotionalPrice: true, customerNotified: true, notes: null, createdAt: new Date('2025-01-10'), updatedAt: new Date('2025-01-12'), }; this.customerOrders.set(1, testOrder1); this.customerOrders.set(2, testOrder2); this.idCounters.customerOrder = 3; // Add test DLC products const testDlc1: DlcProduct = { id: 1, supplierId: 1, groupId: 1, createdBy: 'admin', productName: 'Yaourts bio', expiryDate: '2025-08-20', status: 'en_cours', quantity: 24, unit: 'pièces', notes: 'À vendre rapidement', location: 'Frigo principal', alertThreshold: 5, validatedBy: null, validatedAt: null, createdAt: new Date('2025-08-10'), updatedAt: new Date('2025-08-10'), }; const testDlc2: DlcProduct = { id: 2, supplierId: 1, groupId: 1, createdBy: 'admin', productName: 'Pain de mie complet', expiryDate: '2025-08-16', status: 'alerte', quantity: 8, unit: 'unités', notes: 'DLC proche, promotion conseillée', location: 'Rayonnage A3', alertThreshold: 3, validatedBy: null, validatedAt: null, createdAt: new Date('2025-08-08'), updatedAt: new Date('2025-08-13'), }; this.dlcProducts.set(1, testDlc1); this.dlcProducts.set(2, testDlc2); this.idCounters.dlcProduct = 3; // Add test tasks const testTask1: Task = { id: 1, groupId: 1, createdBy: 'admin', title: 'Vérifier stock yaourts', description: 'Contrôler les DLC des yaourts et organiser la rotation', status: 'pending', priority: 'high', assignedTo: 'admin', dueDate: new Date('2025-08-15'), completedAt: null, completedBy: null, createdAt: new Date('2025-08-13'), updatedAt: new Date('2025-08-13'), }; const testTask2: Task = { id: 2, groupId: 1, createdBy: 'admin', title: 'Contacter fournisseur', description: 'Appeler le fournisseur pour confirmer la livraison de demain', status: 'in_progress', priority: 'medium', assignedTo: 'admin', dueDate: new Date('2025-08-14'), completedAt: null, completedBy: null, createdAt: new Date('2025-08-12'), updatedAt: new Date('2025-08-13'), }; this.tasks.set(1, testTask1); this.tasks.set(2, testTask2); this.idCounters.task = 3; } async updateTask(id: number, taskData: Partial): Promise { const existingTask = this.tasks.get(id); if (!existingTask) throw new Error('Task not found'); const updatedTask = { ...existingTask, ...taskData, updatedAt: new Date() }; this.tasks.set(id, updatedTask); return updatedTask; } async deleteTask(id: number): Promise { this.tasks.delete(id); } async completeTask(id: number, completedBy: string): Promise { const existingTask = this.tasks.get(id); if (!existingTask) throw new Error('Task not found'); const updatedTask = { ...existingTask, status: 'completed' as const, completedAt: new Date(), completedBy, updatedAt: new Date() }; this.tasks.set(id, updatedTask); } } // FORCE: Use DatabaseStorage to see real production data console.log('🔗 Database initialization:', { NODE_ENV: process.env.NODE_ENV, isProduction: process.env.NODE_ENV === 'production', hasDbUrl: !!process.env.DATABASE_URL, dbHost: process.env.DATABASE_URL?.split('@')[1]?.split('/')[0] || 'unknown' }); // Force DatabaseStorage for real data access export const storage: IStorage = new DatabaseStorage(); console.log('✅ Using DatabaseStorage (real PostgreSQL data)');