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
6 changes: 3 additions & 3 deletions pm-stack/aggregator/scripts/produce-charge-candidates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { writeFile } from "node:fs/promises";
import { homedir } from "node:os";
import { join } from "node:path";
import { promisify } from "node:util";
import type { ChargeCandidate } from "../src/charges-match.ts";
import { isRealCalendarDate, type ChargeCandidate } from "../src/charges-match.ts";

const run = promisify(execFile);
const PLAID_CLI = process.env.PLAID_CLI ?? "/opt/hatch/bin/plaid";
Expand Down Expand Up @@ -100,7 +100,7 @@ async function plaidCandidates(start: string, end: string): Promise<ChargeCandid
if (t.pending || !(t.amount > 0)) continue;
if (EXCLUDED_CATEGORIES.has(t.category?.[0] ?? "")) continue;
const merchant = (t.merchant_name ?? t.name ?? "").trim();
if (!merchant || !/^\d{4}-\d{2}-\d{2}$/.test(t.date)) continue;
if (!merchant || !isRealCalendarDate(t.date)) continue;
const method = methodLabel(accounts.get(t.account_id));
out.push({ source: "plaid", at: t.date, merchant, amountUsd: t.amount, ...(method ? { method } : {}) });
}
Expand Down Expand Up @@ -234,7 +234,7 @@ async function gmailCandidates(
continue;
}
const at = gmailMessageDay(m);
if (!/^\d{4}-\d{2}-\d{2}$/.test(at)) {
if (!isRealCalendarDate(at)) {
skipped++;
continue;
}
Expand Down
7 changes: 5 additions & 2 deletions pm-stack/aggregator/src/charges-collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import {
DEDUPE_WINDOW_DAYS,
isRealCalendarDate,
normalizeMerchantName,
type ChargeCandidate,
type UnmatchedCharge,
Expand Down Expand Up @@ -41,12 +42,14 @@ export function parseChargeCandidates(raw: unknown): ChargeCandidate[] {
if (typeof entry !== "object" || entry === null) continue;
const c = entry as Partial<ChargeCandidate>;
if (c.source !== "plaid" && c.source !== "gmail") continue;
if (typeof c.at !== "string" || !/^\d{4}-\d{2}-\d{2}/.test(c.at)) continue;
if (typeof c.at !== "string") continue;
const at = c.at.slice(0, 10);
if (!isRealCalendarDate(at)) continue;
if (typeof c.merchant !== "string" || !c.merchant.trim()) continue;
if (typeof c.amountUsd !== "number" || !Number.isFinite(c.amountUsd)) continue;
out.push({
source: c.source,
at: c.at.slice(0, 10),
at,
merchant: c.merchant,
amountUsd: c.amountUsd,
...(typeof c.method === "string" && c.method ? { method: c.method } : {}),
Expand Down
15 changes: 15 additions & 0 deletions pm-stack/aggregator/src/charges-match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ export interface ChargeMatchResult {
/** Charges from two sources for the same purchase post within days. */
export const DEDUPE_WINDOW_DAYS = 3;

/** Whether a string is a real YYYY-MM-DD date in the proleptic Gregorian calendar. */
export function isRealCalendarDate(value: string): boolean {
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
if (!match) return false;

const year = Number(match[1]);
const month = Number(match[2]);
const day = Number(match[3]);
if (year < 1 || month < 1 || month > 12 || day < 1) return false;

const leapYear = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
const daysInMonth = [31, leapYear ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
return day <= daysInMonth[month - 1]!;
}

const DROP_TOKENS = new Set([
"US",
"USA",
Expand Down
19 changes: 19 additions & 0 deletions pm-stack/aggregator/test/charges-collect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ describe("parseChargeCandidates", () => {
expect(parseChargeCandidates("nope")).toEqual([]);
expect(parseChargeCandidates({ charges: [] })).toEqual([]);
});

test("rejects impossible calendar dates but keeps leap days and timestamp slicing", () => {
const dates = [
"2026-13-45",
"2026-02-30",
"2026-00-00",
"2026-10-00",
"2026-02-29",
"2026-04-31",
"2024-02-29",
"2026-02-28T14:03:00Z",
];

expect(
parseChargeCandidates(dates.map((at) => ({ source: "plaid", at, merchant: "Example", amountUsd: 1 }))).map(
(candidate) => candidate.at,
),
).toEqual(["2024-02-29", "2026-02-28"]);
});
});

describe("mergeChargeRows canonical-label dedupe", () => {
Expand Down
Loading