From 0612abf4151063781cb017ef3d3fa44cecd09b3e Mon Sep 17 00:00:00 2001 From: Manus Date: Mon, 17 Aug 2026 22:35:40 +0200 Subject: [PATCH 1/4] =?UTF-8?q?chore:=20versionner=20le=20manifeste=20de?= =?UTF-8?q?=20d=C3=A9ploiement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.json | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 app.json diff --git a/app.json b/app.json new file mode 100644 index 0000000..f9d753d --- /dev/null +++ b/app.json @@ -0,0 +1,13 @@ +{ + "id": "demat-facturation-dsi", + "name": "Démat. Facturation DSI", + "category": "SANTINOVA", + "urls": { + "recette": "https://demat-facturation.recette.santinova-soft.org", + "prod": "https://demat-facturation.santinova-soft.org" + }, + "containerName": "demat-facturation-app", + "image": "images/demat-facturation-dsi.jpg", + "giteaRepo": "demat-facturation", + "giteaOwner": "manus-admin" +} From a9cea0ecbb2bd5e6d1c829078d1bdf96011f4843 Mon Sep 17 00:00:00 2001 From: Manus Admin Date: Tue, 18 Aug 2026 14:50:31 +0200 Subject: [PATCH 2/4] =?UTF-8?q?fix:=20emp=C3=AAcher=20la=20duplication=20m?= =?UTF-8?q?assive=20de=20fichiers=20lors=20de=20l'import=20email?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Marquer les emails comme lus immédiatement (markSeen: true) - Vérifier si le fichier existe déjà avant de le stocker (findSourceFileByFileName) - Ajouter un verrou anti-concurrence par utilisateur (runningChecks) - Empêche la création de ~570x doublons par facture --- server/db.ts | 15 +++++++++++++++ server/emailImportService.ts | 19 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/server/db.ts b/server/db.ts index c407a22..f869495 100644 --- a/server/db.ts +++ b/server/db.ts @@ -1218,3 +1218,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 d837854..625209a 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 : vérifier si ce fichier a déjà été importé pour cet utilisateur + const existingSourceFile = await findSourceFileByFileName(userId, fileName); + if (existingSourceFile) { + console.log(`[EmailImport] File ${fileName} already imported for user ${userId} (sourceFile #${existingSourceFile.id}), 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 : ne pas lancer si un check est déjà en cours pour cet utilisateur + 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}`); }); From 5c8237d5c6403315f88f80c3a158ccde58e4c1dd Mon Sep 17 00:00:00 2001 From: Manus CI Date: Fri, 21 Aug 2026 10:31:45 +0000 Subject: [PATCH 3/4] =?UTF-8?q?CI=20:=20workflow=20validate.yml,=20manifes?= =?UTF-8?q?te=20ci.required:true,=20garde=20VITEST,=20tests=20isol=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/validate.yml | 45 ++++++++++++++++++++++++++++++++++ app.json | 5 +++- package.json | 5 ++-- server/_core/index.ts | 5 +++- server/db.ts | 2 ++ server/llmFieldsConfig.test.ts | 4 ++- 6 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 .gitea/workflows/validate.yml diff --git a/.gitea/workflows/validate.yml b/.gitea/workflows/validate.yml new file mode 100644 index 0000000..7023c2b --- /dev/null +++ b/.gitea/workflows/validate.yml @@ -0,0 +1,45 @@ +name: Validation applicative + +on: + push: + branches: [main, master] + paths-ignore: + - "**.md" + - "docs/**" + pull_request: + branches: [main, master] + paths-ignore: + - "**.md" + - "docs/**" + workflow_dispatch: + +jobs: + verify: + name: TypeScript, tests et build + # Label dédié : image locale avec Node 22, pnpm verrouillé et Bash déjà installés. + runs-on: ci-node22 + timeout-minutes: 15 + steps: + - name: Récupérer les sources + uses: actions/checkout@v4 + + - name: Calculer la clé de cache pnpm + id: pnpm-cache-key + shell: bash + run: | + echo "store=$(pnpm store path --silent)" >> "$GITHUB_OUTPUT" + echo "lock=$(sha256sum pnpm-lock.yaml | cut -d ' ' -f 1)" >> "$GITHUB_OUTPUT" + + - name: Restaurer le store pnpm + uses: actions/cache@v4 + with: + path: ${{ steps.pnpm-cache-key.outputs.store }} + key: pnpm-${{ runner.os }}-${{ steps.pnpm-cache-key.outputs.lock }} + restore-keys: | + pnpm-${{ runner.os }}- + + - name: Installer les dépendances verrouillées + run: pnpm install --frozen-lockfile --prefer-offline + + - name: Vérifier TypeScript, tests et build + run: pnpm verify diff --git a/app.json b/app.json index f9d753d..c528ebf 100644 --- a/app.json +++ b/app.json @@ -9,5 +9,8 @@ "containerName": "demat-facturation-app", "image": "images/demat-facturation-dsi.jpg", "giteaRepo": "demat-facturation", - "giteaOwner": "manus-admin" + "giteaOwner": "manus-admin", + "ci": { + "required": true + } } diff --git a/package.json b/package.json index bcb1bf0..f69de18 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "check": "tsc --noEmit", "format": "prettier --write .", "test": "vitest run", - "db:push": "drizzle-kit generate && drizzle-kit migrate" + "db:push": "drizzle-kit generate && drizzle-kit migrate", + "verify": "pnpm check && pnpm test && pnpm build" }, "dependencies": { "@aws-sdk/client-s3": "^3.693.0", @@ -137,4 +138,4 @@ "sharp" ] } -} \ No newline at end of file +} diff --git a/server/_core/index.ts b/server/_core/index.ts index 4c2ccc6..f0605ce 100644 --- a/server/_core/index.ts +++ b/server/_core/index.ts @@ -406,4 +406,7 @@ async function startServer() { }, 5000); // Attendre 5s que le serveur soit prêt } -startServer().catch(console.error); +// Vitest ne doit jamais démarrer un serveur HTTP. +if (!process.env.VITEST) { + startServer().catch(console.error); +} diff --git a/server/db.ts b/server/db.ts index f869495..dcb8aa5 100644 --- a/server/db.ts +++ b/server/db.ts @@ -1224,6 +1224,8 @@ export async function updateWebImportSourceStatus( * Used to prevent duplicate file storage during email import */ export async function findSourceFileByFileName(userId: number, fileName: string): Promise { + const db = await getDb(); + if (!db) return null; const result = await db.select() .from(sourceFiles) .where(and( diff --git a/server/llmFieldsConfig.test.ts b/server/llmFieldsConfig.test.ts index d5c9c58..15be5e1 100644 --- a/server/llmFieldsConfig.test.ts +++ b/server/llmFieldsConfig.test.ts @@ -1,11 +1,13 @@ import { describe, it, expect, beforeAll } from "vitest"; + +const describeIntegration = process.env.DATABASE_URL ? describe : describe.skip; import { getLlmFieldsConfigByUser, upsertLlmFieldConfig, initializeDefaultLlmFields } from "./db"; -describe("LLM Fields Configuration", () => { +describeIntegration("LLM Fields Configuration", () => { const testUserId = 99999; // Use a high ID to avoid conflicts beforeAll(async () => { From a1755e181c120fadfcaa6489e9645409dad6c6ef Mon Sep 17 00:00:00 2001 From: manus-admin Date: Fri, 21 Aug 2026 12:37:48 +0200 Subject: [PATCH 4/4] ci: trigger