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

+1
View File
@@ -32,6 +32,7 @@ The platform is built with a clear separation of concerns, utilizing a modern fu
- **Weather Integration:** A comprehensive, modern weather interface displays detailed weather conditions sourced directly from the Visual Crossing API with French language support. Features intelligent date comparison using the same day of the week from the previous year for more relevant weather pattern analysis (e.g., Friday August 15, 2025 vs Friday August 16, 2024).
- **Order and Delivery Management:** Full CRUD operations for orders and deliveries, including linking orders to deliveries and managing their statuses.
- **Reconciliation:** Functionality for reconciling Bills of Lading (BL) and invoices with immediate UI updates.
- **Invoice Verification Cache:** Robust caching system for invoice verification results with automatic expiration and conflict resolution. Enhanced with unique constraint handling and graceful error management for production stability (August 2025).
- **User and Permission Management:** Granular access controls ensuring that users (e.g., employees) have appropriate permissions for tasks like creating customer orders.
- **Reporting:** Statistical reporting, including monthly summaries of deliveries based on `deliveredDate`.
+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> {
+1 -1
View File
@@ -160,7 +160,7 @@ export const nocodbConfig = pgTable("nocodb_config", {
// Cache de vérification des factures
export const invoiceVerificationCache = pgTable("invoice_verification_cache", {
id: serial("id").primaryKey(),
cacheKey: varchar("cache_key", { length: 255 }).notNull(),
cacheKey: varchar("cache_key", { length: 255 }).notNull().unique(),
groupId: integer("group_id").notNull(),
invoiceReference: varchar("invoice_reference", { length: 255 }).notNull(),
supplierName: varchar("supplier_name", { length: 255 }),