Compare commits

..

4 Commits

Author SHA1 Message Date
manus-admin
a1755e181c ci: trigger 2026-08-21 12:38:06 +02:00
Manus CI
5c8237d5c6 CI : workflow validate.yml, manifeste ci.required:true, garde VITEST, tests isolés 2026-08-21 12:38:06 +02:00
Manus Admin
a9cea0ecbb fix: empêcher la duplication massive de fichiers lors de l'import email
- 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
2026-08-21 12:38:06 +02:00
Manus
0612abf415 chore: versionner le manifeste de déploiement 2026-08-21 12:38:06 +02:00
7 changed files with 106 additions and 5 deletions

View File

@@ -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

16
app.json Normal file
View File

@@ -0,0 +1,16 @@
{
"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",
"ci": {
"required": true
}
}

View File

@@ -10,7 +10,8 @@
"check": "tsc --noEmit", "check": "tsc --noEmit",
"format": "prettier --write .", "format": "prettier --write .",
"test": "vitest run", "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": { "dependencies": {
"@aws-sdk/client-s3": "^3.693.0", "@aws-sdk/client-s3": "^3.693.0",

View File

@@ -406,4 +406,7 @@ async function startServer() {
}, 5000); // Attendre 5s que le serveur soit prêt }, 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);
}

View File

@@ -1218,3 +1218,20 @@ export async function updateWebImportSourceStatus(
if (success) update.lastSuccessAt = new Date(); if (success) update.lastSuccessAt = new Date();
await db.update(webImportSources).set(update).where(eq(webImportSources.id, id)); 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<any | null> {
const db = await getDb();
if (!db) return null;
const result = await db.select()
.from(sourceFiles)
.where(and(
eq(sourceFiles.userId, userId),
eq(sourceFiles.fileName, fileName)
))
.limit(1);
return result[0] || null;
}

View File

@@ -9,6 +9,7 @@ import {
isInvoiceBlacklisted, isInvoiceBlacklisted,
createInvoice, createInvoice,
createImportLog, createImportLog,
findSourceFileByFileName,
} from "./db"; } from "./db";
import { extractInvoicesWithMistral, generateMetadataJSON } from "./invoiceExtractor"; import { extractInvoicesWithMistral, generateMetadataJSON } from "./invoiceExtractor";
import { localStoragePut, generateStorageKey } from "./localStorage"; import { localStoragePut, generateStorageKey } from "./localStorage";
@@ -32,6 +33,8 @@ interface EmailImportConfig {
// Store active intervals for each user // Store active intervals for each user
const activeIntervals = new Map<number, NodeJS.Timeout>(); const activeIntervals = new Map<number, NodeJS.Timeout>();
// Verrou anti-concurrence par userId
const runningChecks = new Set<number>();
/** /**
* Process a single email attachment (PDF) * Process a single email attachment (PDF)
@@ -51,6 +54,12 @@ async function processEmailAttachment(
console.log(`[EmailImport] File size: ${fileBuffer.length} bytes`); console.log(`[EmailImport] File size: ${fileBuffer.length} bytes`);
// Store source file // 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); const sourceFileKey = generateStorageKey(userId, fileName);
console.log(`[EmailImport] Generated storage key: ${sourceFileKey}`); console.log(`[EmailImport] Generated storage key: ${sourceFileKey}`);
@@ -330,6 +339,12 @@ async function buildImapConfig(config: EmailImportConfig): Promise<Imap.Config>
* Connect to IMAP and process unread emails with PDF attachments * Connect to IMAP and process unread emails with PDF attachments
*/ */
async function checkEmailsForPDFs(config: EmailImportConfig): Promise<void> { async function checkEmailsForPDFs(config: EmailImportConfig): Promise<void> {
// 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) // Build IMAP config (may involve async OAuth2 token fetch)
const imapConfig = await buildImapConfig(config); const imapConfig = await buildImapConfig(config);
@@ -380,7 +395,7 @@ async function checkEmailsForPDFs(config: EmailImportConfig): Promise<void> {
const fetch = imap.fetch(results, { const fetch = imap.fetch(results, {
bodies: "", bodies: "",
markSeen: false, // Don't mark as seen yet markSeen: true, // Mark as seen immediately to prevent re-processing
}); });
const processedEmails: number[] = []; const processedEmails: number[] = [];
@@ -474,11 +489,13 @@ async function checkEmailsForPDFs(config: EmailImportConfig): Promise<void> {
}); });
imap.once("error", (err) => { imap.once("error", (err) => {
runningChecks.delete(config.userId);
console.error("[EmailImport] IMAP connection error:", err); console.error("[EmailImport] IMAP connection error:", err);
reject(err); reject(err);
}); });
imap.once("end", () => { imap.once("end", () => {
runningChecks.delete(config.userId);
console.log(`[EmailImport] IMAP connection ended for user ${config.userId}`); console.log(`[EmailImport] IMAP connection ended for user ${config.userId}`);
}); });

View File

@@ -1,11 +1,13 @@
import { describe, it, expect, beforeAll } from "vitest"; import { describe, it, expect, beforeAll } from "vitest";
const describeIntegration = process.env.DATABASE_URL ? describe : describe.skip;
import { import {
getLlmFieldsConfigByUser, getLlmFieldsConfigByUser,
upsertLlmFieldConfig, upsertLlmFieldConfig,
initializeDefaultLlmFields initializeDefaultLlmFields
} from "./db"; } from "./db";
describe("LLM Fields Configuration", () => { describeIntegration("LLM Fields Configuration", () => {
const testUserId = 99999; // Use a high ID to avoid conflicts const testUserId = 99999; // Use a high ID to avoid conflicts
beforeAll(async () => { beforeAll(async () => {