diff --git a/server/db.ts b/server/db.ts index da76716..b703efa 100644 --- a/server/db.ts +++ b/server/db.ts @@ -1221,3 +1221,18 @@ export async function updateWebImportSourceStatus( if (success) update.lastSuccessAt = new Date(); await db.update(webImportSources).set(update).where(eq(webImportSources.id, id)); } + +/** + * Check if a source file with the same fileName already exists for this user + * Used to prevent duplicate file storage during email import + */ +export async function findSourceFileByFileName(userId: number, fileName: string): Promise { + const result = await db.select() + .from(sourceFiles) + .where(and( + eq(sourceFiles.userId, userId), + eq(sourceFiles.fileName, fileName) + )) + .limit(1); + return result[0] || null; +} diff --git a/server/emailImportService.ts b/server/emailImportService.ts index dada7ce..0774dc8 100644 --- a/server/emailImportService.ts +++ b/server/emailImportService.ts @@ -9,6 +9,7 @@ import { isInvoiceBlacklisted, createInvoice, createImportLog, + findSourceFileByFileName, } from "./db"; import { extractInvoicesWithMistral, generateMetadataJSON } from "./invoiceExtractor"; import { localStoragePut, generateStorageKey } from "./localStorage"; @@ -32,6 +33,8 @@ interface EmailImportConfig { // Store active intervals for each user const activeIntervals = new Map(); +// Verrou anti-concurrence par userId +const runningChecks = new Set(); /** * Process a single email attachment (PDF) @@ -51,6 +54,12 @@ async function processEmailAttachment( console.log(`[EmailImport] File size: ${fileBuffer.length} bytes`); // Store source file + // ANTI-DUPLICATION + const existingSourceFile = await findSourceFileByFileName(userId, fileName); + if (existingSourceFile) { + console.log(`[EmailImport] File ${fileName} already imported for user ${userId}, skipping`); + return { success: true, totalInvoices: 0, imported: 0, duplicates: 1, errors: 0 }; + } const sourceFileKey = generateStorageKey(userId, fileName); console.log(`[EmailImport] Generated storage key: ${sourceFileKey}`); @@ -330,6 +339,12 @@ async function buildImapConfig(config: EmailImportConfig): Promise * Connect to IMAP and process unread emails with PDF attachments */ async function checkEmailsForPDFs(config: EmailImportConfig): Promise { + // Anti-concurrence + if (runningChecks.has(config.userId)) { + console.log(`[EmailImport] Check already running for user ${config.userId}, skipping`); + return; + } + runningChecks.add(config.userId); // Build IMAP config (may involve async OAuth2 token fetch) const imapConfig = await buildImapConfig(config); @@ -380,7 +395,7 @@ async function checkEmailsForPDFs(config: EmailImportConfig): Promise { const fetch = imap.fetch(results, { bodies: "", - markSeen: false, // Don't mark as seen yet + markSeen: true, // Mark as seen immediately to prevent re-processing }); const processedEmails: number[] = []; @@ -474,11 +489,13 @@ async function checkEmailsForPDFs(config: EmailImportConfig): Promise { }); imap.once("error", (err) => { + runningChecks.delete(config.userId); console.error("[EmailImport] IMAP connection error:", err); reject(err); }); imap.once("end", () => { + runningChecks.delete(config.userId); console.log(`[EmailImport] IMAP connection ended for user ${config.userId}`); });