Checkpoint: Correction robuste des doublons de factures : empreinte SHA-256 unique des PDF sources, détection globale avant import manuel/email/dossier/web, verrou contre les vérifications IMAP concurrentes, attente réelle de fin de traitement avant marquage lu, migration et tests de non-régression.
This commit is contained in:
@@ -4,6 +4,7 @@ import path from "path";
|
||||
import {
|
||||
getImportSettingsByUser,
|
||||
createSourceFile,
|
||||
getSourceFileByContentHash,
|
||||
updateSourceFile,
|
||||
getUserSettings,
|
||||
findDuplicateInvoice,
|
||||
@@ -11,7 +12,8 @@ import {
|
||||
createImportLog,
|
||||
} from "./db";
|
||||
import { extractInvoicesWithMistral, generateMetadataJSON } from "./invoiceExtractor";
|
||||
import { localStoragePut, generateStorageKey } from "./localStorage";
|
||||
import { localStorageDelete, localStoragePut, generateStorageKey } from "./localStorage";
|
||||
import { calculateFileSha256 } from "./fileFingerprint";
|
||||
import { sendImportNotification } from "./notificationService";
|
||||
|
||||
interface FolderImportConfig {
|
||||
@@ -41,6 +43,13 @@ async function processFolderFile(
|
||||
// Read the file
|
||||
const fileBuffer = await fs.readFile(filePath);
|
||||
console.log(`[FolderImport] File size: ${fileBuffer.length} bytes`);
|
||||
|
||||
const contentHash = calculateFileSha256(fileBuffer);
|
||||
const existingSource = await getSourceFileByContentHash(contentHash);
|
||||
if (existingSource) {
|
||||
console.log(`[FolderImport] PDF déjà importé, fichier ignoré: ${fileName}`);
|
||||
return { success: true, imported: 0, duplicates: 1, errors: 0 };
|
||||
}
|
||||
|
||||
// Store source file
|
||||
const sourceFileKey = generateStorageKey(userId, fileName);
|
||||
@@ -57,13 +66,23 @@ async function processFolderFile(
|
||||
}
|
||||
|
||||
// Create source file record
|
||||
const sourceFile = await createSourceFile({
|
||||
userId,
|
||||
fileName,
|
||||
fileKey: sourceFileKey,
|
||||
fileUrl: sourceFileUrl,
|
||||
processingStatus: "processing",
|
||||
});
|
||||
let sourceFile;
|
||||
try {
|
||||
sourceFile = await createSourceFile({
|
||||
userId,
|
||||
fileName,
|
||||
fileKey: sourceFileKey,
|
||||
fileUrl: sourceFileUrl,
|
||||
contentHash,
|
||||
processingStatus: "processing",
|
||||
});
|
||||
} catch (error: any) {
|
||||
if (error?.code === "ER_DUP_ENTRY" || error?.errno === 1062) {
|
||||
await localStorageDelete(sourceFileKey).catch(() => undefined);
|
||||
return { success: true, imported: 0, duplicates: 1, errors: 0 };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
console.log(`[FolderImport] Source file record created with ID: ${sourceFile.id}`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user