diff --git a/server/db.ts b/server/db.ts index 5ec1d8c..a76877d 100644 --- a/server/db.ts +++ b/server/db.ts @@ -537,21 +537,31 @@ export async function initializeDefaultLists(userId: number): Promise { // Initialize default departments const defaultDepartments = ["DSI", "TRAVAUX", "AUTRE"]; + const existingDepts = await db.select().from(departmentList).where(eq(departmentList.userId, userId)); + const existingDeptNames = new Set(existingDepts.map(d => d.name)); + for (const name of defaultDepartments) { - try { - await db.insert(departmentList).values({ userId, name }).onDuplicateKeyUpdate({ set: { name } }); - } catch (error) { - // Ignore duplicate errors + if (!existingDeptNames.has(name)) { + try { + await db.insert(departmentList).values({ userId, name }); + } catch (error) { + // Ignore errors + } } } // Initialize default accounting allocations const defaultAllocations = ["TOUS", "PA", "HEP", "SANITAIRE", "AUTRE"]; + const existingAllocs = await db.select().from(accountingAllocationList).where(eq(accountingAllocationList.userId, userId)); + const existingAllocNames = new Set(existingAllocs.map(a => a.name)); + for (const name of defaultAllocations) { - try { - await db.insert(accountingAllocationList).values({ userId, name }).onDuplicateKeyUpdate({ set: { name } }); - } catch (error) { - // Ignore duplicate errors + if (!existingAllocNames.has(name)) { + try { + await db.insert(accountingAllocationList).values({ userId, name }); + } catch (error) { + // Ignore errors + } } } } diff --git a/todo.md b/todo.md index 5116b57..cdc5b66 100644 --- a/todo.md +++ b/todo.md @@ -206,3 +206,7 @@ - [x] Ajouter les colonnes dans la liste des factures (Invoices.tsx) - [x] Initialiser les valeurs par défaut dans les tables (initializeDefaultLists) - [x] Ajouter le lien "Administration des listes" dans le menu de navigation + +## Bug : Erreur lors de l'ajout d'un service +- [x] Corriger la fonction initializeDefaultLists pour éviter les erreurs de duplication +- [x] Vérifier que l'ajout de services et ventilations fonctionne correctement