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:
29
server/db.ts
29
server/db.ts
@@ -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 [];
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
updateSourceFile,
|
||||
getUserSettings,
|
||||
findDuplicateInvoice,
|
||||
isInvoiceBlacklisted,
|
||||
createInvoice,
|
||||
createImportLog,
|
||||
} from "./db";
|
||||
@@ -130,6 +131,19 @@ async function processEmailAttachment(
|
||||
processingProgress: `Extraction ${i + 1}/${result.invoiceCount} factures...`,
|
||||
});
|
||||
|
||||
// Vérifier la blacklist (factures supprimées manuellement)
|
||||
const blacklisted = await isInvoiceBlacklisted(invoiceData.invoiceNumber, userId);
|
||||
if (blacklisted) {
|
||||
duplicatesCount++;
|
||||
duplicateDetails.push({
|
||||
supplierName: invoiceData.supplierName,
|
||||
invoiceNumber: invoiceData.invoiceNumber,
|
||||
totalAmount: invoiceData.totalAmount,
|
||||
reason: 'blacklisted',
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for duplicates (numéro de facture + montant)
|
||||
const duplicate = await findDuplicateInvoice(
|
||||
invoiceData.invoiceNumber,
|
||||
|
||||
@@ -34,6 +34,7 @@ import {
|
||||
toggleUserActive,
|
||||
deleteUser,
|
||||
findDuplicateInvoice,
|
||||
isInvoiceBlacklisted,
|
||||
getAllInvoices,
|
||||
getAllImportLogs,
|
||||
getAllBapHistory,
|
||||
@@ -253,6 +254,19 @@ export const appRouter = router({
|
||||
processingProgress: `Extraction ${i + 1}/${result.invoiceCount} factures...`,
|
||||
});
|
||||
|
||||
// Vérifier la blacklist (factures supprimées manuellement)
|
||||
const blacklisted = await isInvoiceBlacklisted(invoiceData.invoiceNumber, userId);
|
||||
if (blacklisted) {
|
||||
duplicatesCount++;
|
||||
duplicateDetails.push({
|
||||
supplierName: invoiceData.supplierName,
|
||||
invoiceNumber: invoiceData.invoiceNumber,
|
||||
totalAmount: invoiceData.totalAmount,
|
||||
reason: 'blacklisted',
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for duplicates (numéro de facture + montant)
|
||||
const duplicate = await findDuplicateInvoice(
|
||||
invoiceData.invoiceNumber,
|
||||
|
||||
Reference in New Issue
Block a user