feat: détection doublons par numéro+montant dans les 3 points d'import
This commit is contained in:
22
server/db.ts
22
server/db.ts
@@ -414,23 +414,25 @@ export async function getInvoiceStats(userId: number) {
|
||||
}
|
||||
|
||||
export async function findDuplicateInvoice(
|
||||
supplierName: string | null,
|
||||
invoiceNumber: string | null,
|
||||
invoiceDate: Date | null
|
||||
totalAmount: string | null,
|
||||
userId?: number
|
||||
): Promise<Invoice | undefined> {
|
||||
if (!supplierName || !invoiceNumber || !invoiceDate) return undefined;
|
||||
if (!invoiceNumber || !totalAmount) return undefined;
|
||||
|
||||
const db = await getDb();
|
||||
if (!db) return undefined;
|
||||
|
||||
const conditions = [
|
||||
eq(invoices.invoiceNumber, invoiceNumber),
|
||||
eq(invoices.totalAmount, totalAmount),
|
||||
];
|
||||
if (userId !== undefined) {
|
||||
conditions.push(eq(invoices.userId, userId));
|
||||
}
|
||||
|
||||
const result = await db.select().from(invoices)
|
||||
.where(
|
||||
and(
|
||||
eq(invoices.supplierName, supplierName),
|
||||
eq(invoices.invoiceNumber, invoiceNumber),
|
||||
eq(invoices.invoiceDate, invoiceDate)
|
||||
)
|
||||
)
|
||||
.where(and(...conditions))
|
||||
.limit(1);
|
||||
|
||||
return result[0];
|
||||
|
||||
Reference in New Issue
Block a user