Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pm-stack/aggregator/src/charges-match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,15 @@ export function matchCharges(candidates: ChargeCandidate[], services: ChargeServ
);
for (const candidate of ordered) {
const normalized = normalizeMerchantName(candidate.merchant);
// Only pair observations from different sources. When normalization
// removes every token, also require the raw descriptor to agree so
// unrelated numeric merchant IDs cannot collapse into one charge.
const existing = clusters.find(
(cl) =>
cl.amountUsd === candidate.amountUsd &&
cl.normalized === normalized &&
(candidate.source === "plaid" ? !cl.plaid : !cl.gmail) &&
(normalized !== "" || cl.first.merchant.trim().toUpperCase() === candidate.merchant.trim().toUpperCase()) &&
daysBetween(cl.first.at, candidate.at) <= DEDUPE_WINDOW_DAYS,
);
if (existing) {
Expand Down
24 changes: 24 additions & 0 deletions pm-stack/aggregator/test/charges-match.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ describe("matchCharges", () => {
expect(matchCharges(candidates, SERVICES).rows).toHaveLength(2);
});

test("same-source Plaid charges are not deduped", () => {
const candidates: ChargeCandidate[] = [
{ source: "plaid", at: "2026-10-28", merchant: "OPENAI", amountUsd: 20 },
{ source: "plaid", at: "2026-10-29", merchant: "OPENAI", amountUsd: 20 },
];
expect(matchCharges(candidates, SERVICES).rows).toHaveLength(2);
});

test("same-source Gmail charges are not deduped", () => {
const candidates: ChargeCandidate[] = [
{ source: "gmail", at: "2026-10-28", merchant: "OPENAI", amountUsd: 20 },
{ source: "gmail", at: "2026-10-29", merchant: "OPENAI", amountUsd: 20 },
];
expect(matchCharges(candidates, SERVICES).rows).toHaveLength(2);
});

test("distinct empty-normalized merchants are not deduped", () => {
const candidates: ChargeCandidate[] = [
{ source: "plaid", at: "2026-10-28", merchant: "1234567", amountUsd: 20 },
{ source: "gmail", at: "2026-10-29", merchant: "9876543", amountUsd: 20 },
];
expect(matchCharges(candidates, SERVICES).rows).toHaveLength(2);
});

test("matched charges take the service name as their label", () => {
const candidates: ChargeCandidate[] = [
{ source: "plaid", at: "2026-10-11", merchant: "ANTHROPIC PBC", amountUsd: 100 },
Expand Down
Loading