Checkpoint: Ajout de la table deletedInvoices (blacklist) : quand une facture est supprimée, son numéro est enregistré et l'import email/fichier ne la réimporte plus.

This commit is contained in:
Manus
2026-07-30 09:48:00 +00:00
parent aebc607e8a
commit 5d18dfa468
9 changed files with 2323 additions and 7 deletions

View File

@@ -44,7 +44,10 @@ import {
BapHistory,
invoiceLearnings,
InsertInvoiceLearning,
InvoiceLearning
InvoiceLearning,
deletedInvoices,
InsertDeletedInvoice,
DeletedInvoice
} from "../drizzle/schema";
import { ENV } from './_core/env';
@@ -265,9 +268,33 @@ export async function updateInvoice(id: number, data: Partial<Invoice>) {
export async function deleteInvoice(id: number) {
const db = await getDb();
if (!db) throw new Error("Database not available");
// Enregistrer dans la blacklist avant suppression
const invoice = await db.select().from(invoices).where(eq(invoices.id, id)).limit(1);
if (invoice[0] && invoice[0].invoiceNumber) {
await db.insert(deletedInvoices).values({
userId: invoice[0].userId,
invoiceNumber: invoice[0].invoiceNumber,
totalAmount: invoice[0].totalAmount ?? undefined,
supplierName: invoice[0].supplierName ?? undefined,
}).onDuplicateKeyUpdate({ set: { deletedAt: new Date() } });
}
await db.delete(invoices).where(eq(invoices.id, id));
}
/** Vérifie si une facture est dans la blacklist (supprimée manuellement) */
export async function isInvoiceBlacklisted(
invoiceNumber: string | null,
userId: number
): Promise<boolean> {
if (!invoiceNumber) return false;
const db = await getDb();
if (!db) return false;
const result = await db.select().from(deletedInvoices)
.where(and(eq(deletedInvoices.userId, userId), eq(deletedInvoices.invoiceNumber, invoiceNumber)))
.limit(1);
return result.length > 0;
}
export async function searchInvoices(userId: number | null, query: string): Promise<Invoice[]> {
const db = await getDb();
if (!db) return [];