Improve invoice verification caching with conflict resolution and error handling

Implement unique constraint on invoice verification cache key and handle duplicate entries gracefully in the backend.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: d43bd811-9372-45a7-8ac9-4a954c0538e1
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/d43bd811-9372-45a7-8ac9-4a954c0538e1/g5c0JYM
This commit is contained in:
michaelschal committed 2025-08-15 09:48:29 +00:00
1 parent 77e3214491
commit 80ca426a33
4 files changed
+29 -3

No files matched your search

+5
View File
@@ -64,6 +64,11 @@ export class InvoiceVerificationService {
console.log('💾 Résultat sauvé en cache:', { invoiceReference, groupId, exists: result.exists });
} catch (error) {
// Gérer spécifiquement les erreurs de contrainte unique (duplicate key)
if (error && typeof error === 'object' && 'code' in error && error.code === '23505') {
console.log('🔄 Cache déjà existant pour cette clé, ignoré:', { invoiceReference, groupId });
return;
}
console.error('❌ Erreur sauvegarde cache:', error);
}
}
+22 -2
View File
@@ -1287,8 +1287,28 @@ export class DatabaseStorage implements IStorage {
}
async createInvoiceVerificationCache(cacheData: InsertInvoiceVerificationCache): Promise<InvoiceVerificationCache> {
const [cache] = await db.insert(invoiceVerificationCache).values(cacheData).returning();
return cache;
try {
// Utiliser UPSERT pour gérer les conflits de clés
const [cache] = await db
.insert(invoiceVerificationCache)
.values(cacheData)
.onConflictDoUpdate({
target: invoiceVerificationCache.cacheKey,
set: {
exists: cacheData.exists,
matchType: cacheData.matchType,
errorMessage: cacheData.errorMessage,
cacheHit: cacheData.cacheHit,
apiCallTime: cacheData.apiCallTime,
updatedAt: new Date()
}
})
.returning();
return cache;
} catch (error) {
console.error('❌ Erreur création cache:', error);
throw error;
}
}
async saveInvoiceVerificationCache(cacheData: InsertInvoiceVerificationCache): Promise<InvoiceVerificationCache> {