72 lines
2.3 KiB
TypeScript
72 lines
2.3 KiB
TypeScript
|
|
/**
|
||
|
|
* Expense duplicate detection — heuristic match on
|
||
|
|
* (port + vendor + amount + date ± 3d). PR1 ships the function shape;
|
||
|
|
* PR8 wires the BullMQ trigger and the merge service.
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { and, between, eq, ne, sql } from 'drizzle-orm';
|
||
|
|
|
||
|
|
import { db } from '@/lib/db';
|
||
|
|
import { expenses } from '@/lib/db/schema/financial';
|
||
|
|
|
||
|
|
const DEDUP_WINDOW_DAYS = 3;
|
||
|
|
|
||
|
|
export interface DedupCandidate {
|
||
|
|
/** Existing expense that the new one likely duplicates. */
|
||
|
|
candidateId: string;
|
||
|
|
/** 0..1 confidence; 1.0 = exact vendor + amount + same day. */
|
||
|
|
confidence: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function scanForDuplicates(expenseId: string): Promise<DedupCandidate[]> {
|
||
|
|
const target = await db.query.expenses.findFirst({ where: eq(expenses.id, expenseId) });
|
||
|
|
if (!target) return [];
|
||
|
|
|
||
|
|
const { portId, establishmentName, amount, expenseDate } = target;
|
||
|
|
if (!establishmentName || !amount || !expenseDate) return [];
|
||
|
|
|
||
|
|
const lo = new Date(expenseDate);
|
||
|
|
lo.setDate(lo.getDate() - DEDUP_WINDOW_DAYS);
|
||
|
|
const hi = new Date(expenseDate);
|
||
|
|
hi.setDate(hi.getDate() + DEDUP_WINDOW_DAYS);
|
||
|
|
|
||
|
|
const matches = await db.query.expenses.findMany({
|
||
|
|
where: and(
|
||
|
|
eq(expenses.portId, portId),
|
||
|
|
sql`lower(${expenses.establishmentName}) = lower(${establishmentName})`,
|
||
|
|
eq(expenses.amount, amount),
|
||
|
|
between(expenses.expenseDate, lo, hi),
|
||
|
|
ne(expenses.id, expenseId),
|
||
|
|
),
|
||
|
|
limit: 5,
|
||
|
|
});
|
||
|
|
|
||
|
|
return matches.map((m) => ({
|
||
|
|
candidateId: m.id,
|
||
|
|
confidence: dayDiff(m.expenseDate, expenseDate) === 0 ? 1.0 : 0.85,
|
||
|
|
}));
|
||
|
|
}
|
||
|
|
|
||
|
|
function dayDiff(a: Date, b: Date): number {
|
||
|
|
const ms = Math.abs(a.getTime() - b.getTime());
|
||
|
|
return Math.round(ms / 86_400_000);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Mark an expense as a duplicate of the candidate with the highest score. */
|
||
|
|
export async function markBestDuplicate(expenseId: string): Promise<string | null> {
|
||
|
|
const candidates = await scanForDuplicates(expenseId);
|
||
|
|
if (candidates.length === 0) {
|
||
|
|
await db
|
||
|
|
.update(expenses)
|
||
|
|
.set({ dedupScannedAt: sql`now()` })
|
||
|
|
.where(eq(expenses.id, expenseId));
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
const best = candidates.reduce((a, b) => (a.confidence >= b.confidence ? a : b));
|
||
|
|
await db
|
||
|
|
.update(expenses)
|
||
|
|
.set({ duplicateOf: best.candidateId, dedupScannedAt: sql`now()` })
|
||
|
|
.where(eq(expenses.id, expenseId));
|
||
|
|
return best.candidateId;
|
||
|
|
}
|