Checkpoint: Correction de l'erreur lors de l'ajout d'un service dans la liste des services. La fonction initializeDefaultLists vérifie maintenant les doublons avant insertion.
This commit is contained in:
26
server/db.ts
26
server/db.ts
@@ -537,21 +537,31 @@ export async function initializeDefaultLists(userId: number): Promise<void> {
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user