Statement import: read a Category column; onboarding counts imported accounts
- A CSV with a Category column (Chase, Amex, Monzo style) now files each row under the matching broke category instead of Other, so budgets suggest per category straight after an import. - Onboarding's bank step counted only linked banks, so after importing a statement it kept sending you back to Link your bank. It now counts imported (manual checking/savings) accounts too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8374fc325b
commit
af66279940
4 files changed
+57
-9
No files matched your search
@@ -1,7 +1,7 @@
|
||||
// node src/lib/statement.test.ts
|
||||
// Samples are shaped like each bank's export (columns, date style, preamble); names and amounts are made up.
|
||||
import assert from "node:assert/strict";
|
||||
import { applyMapping, dateOrder, parseAmount, parseDate, parseStatement, type Row } from "./statement.ts";
|
||||
import { applyMapping, categoryKey, dateOrder, parseAmount, parseDate, parseStatement, type Row } from "./statement.ts";
|
||||
|
||||
const csv = (text: string, locale: string) => {
|
||||
const p = parseStatement(text, "statement.csv", locale);
|
||||
@@ -134,3 +134,15 @@ if (qif.kind === "qif") assert.deepEqual(brief(qif.rows), [["2026-10-02", 42.1,
|
||||
else assert.fail("qif");
|
||||
|
||||
console.log("statement ok");
|
||||
|
||||
// A Category column (Chase-style) comes through on each row and maps to broke's keys.
|
||||
{
|
||||
const c = csv("Date,Description,Category,Amount\n11/08/2026,Comcast,Bills & Utilities,-65.00\n11/07/2026,Whole Foods,Groceries,-72.24\n11/06/2026,Northwind Payroll,Paycheck,2150.00\n", "en-US");
|
||||
assert.deepEqual(c.rows.map((r) => r.category), ["Bills & Utilities", "Groceries", "Paycheck"]);
|
||||
assert.equal(categoryKey("Bills & Utilities"), "RENT_AND_UTILITIES");
|
||||
assert.equal(categoryKey("Food & Drink"), "FOOD_AND_DRINK");
|
||||
assert.equal(categoryKey("Gas"), "TRANSPORTATION");
|
||||
assert.equal(categoryKey("Dining & drinks", { FOOD_AND_DRINK: "Dining & drinks" }), "FOOD_AND_DRINK");
|
||||
assert.equal(categoryKey(""), null);
|
||||
assert.equal(categoryKey("Miscellaneous"), null);
|
||||
}
|
||||
+34
-4
@@ -1,11 +1,11 @@
|
||||
// Bank statement import: CSV, OFX and QIF to transactions. Pure, no "@/" imports, so statement.test.ts runs with plain node.
|
||||
// Output uses the app's sign: positive = money out.
|
||||
|
||||
export type Row = { date: string; amount: number; name: string; key: string };
|
||||
export type Row = { date: string; amount: number; name: string; key: string; category?: string };
|
||||
export type Table = { header: string[]; rows: string[][] };
|
||||
export type DateOrder = "dmy" | "mdy" | "ymd";
|
||||
// Column indexes; -1 = not used. A single signed amount, or separate debit (out) and credit (in) columns.
|
||||
export type Mapping = { date: number; name: number; amount: number; debit: number; credit: number; order: DateOrder; outPositive: boolean };
|
||||
export type Mapping = { date: number; name: number; amount: number; debit: number; credit: number; order: DateOrder; outPositive: boolean; category?: number };
|
||||
export type Parsed = { kind: "csv"; table: Table; mapping: Mapping } | { kind: "ofx" | "qif"; rows: Row[] };
|
||||
|
||||
const MONTHS = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
|
||||
@@ -142,7 +142,8 @@ export function guessMapping(t: Table, locale: string): Mapping {
|
||||
const amount = find(h, [/^amount$/, /^amount\b/, /amount/, /betrag|montant|importe|importo|bedrag|belopp|monto|valor/], /balance|foreign|local|credit|debit|\bcr\b|\bdr\b/);
|
||||
const debit = amount >= 0 ? -1 : find(h, [/withdrawal/, /debit/, /money out/, /paid out/, /^dr$/, /\bout\b/]);
|
||||
const credit = amount >= 0 ? -1 : find(h, [/deposit/, /credit/, /money in/, /paid in/, /^cr$/, /\bin\b/]);
|
||||
return { date, name, amount, debit, credit, order: dateOrder(date >= 0 ? col(date) : [], locale), outPositive: false };
|
||||
const category = find(h, [/^category$/, /categor/]);
|
||||
return { date, name, amount, debit, credit, order: dateOrder(date >= 0 ? col(date) : [], locale), outPositive: false, category };
|
||||
}
|
||||
|
||||
// Stable dedupe keys: the same date, amount and name gets 1, 2, 3... in file order, so re-importing adds nothing.
|
||||
@@ -173,7 +174,10 @@ export function applyMapping(t: Table, m: Mapping, locale: string): { rows: Row[
|
||||
}
|
||||
const name = (m.name >= 0 ? r[m.name] ?? "" : "").replace(/\s+/g, " ").trim();
|
||||
if (!date || amount === null || !amount) skipped++;
|
||||
else out.push({ date, amount: Math.round(amount * 100) / 100, name: name || "Imported transaction" });
|
||||
else {
|
||||
const category = (m.category ?? -1) >= 0 ? (r[m.category!] ?? "").trim() : "";
|
||||
out.push({ date, amount: Math.round(amount * 100) / 100, name: name || "Imported transaction", ...(category && { category }) });
|
||||
}
|
||||
}
|
||||
return { rows: withKeys(out), skipped };
|
||||
}
|
||||
@@ -215,3 +219,29 @@ export function parseStatement(text: string, filename: string, locale: string):
|
||||
const table = readTable(text);
|
||||
return { kind: "csv", table, mapping: guessMapping(table, locale) };
|
||||
}
|
||||
|
||||
// A bank's own category name (Chase, Amex, Monzo exports) to broke's category key. null = leave it unsorted.
|
||||
const CATEGORY_WORDS: [RegExp, string][] = [
|
||||
[/payroll|salary|paycheck|wage|income|deposit|refund/, "INCOME"],
|
||||
[/grocer|supermarket/, "GROCERIES"],
|
||||
[/restaurant|dining|food|drink|coffee|cafe|takeaway|eating/, "FOOD_AND_DRINK"],
|
||||
[/rent|mortgage|bill|utilit|electric|internet|phone|mobile|insurance/, "RENT_AND_UTILITIES"],
|
||||
[/gas|fuel|auto|transport|car|uber|taxi|parking|transit/, "TRANSPORTATION"],
|
||||
[/travel|flight|airline|hotel|holiday/, "TRAVEL"],
|
||||
[/entertain|subscription|stream|movie|music|game/, "ENTERTAINMENT"],
|
||||
[/health|medical|pharmac|doctor|dental|fitness|gym/, "MEDICAL"],
|
||||
[/personal|beauty|hair/, "PERSONAL_CARE"],
|
||||
[/shop|merchandise|retail|clothing/, "GENERAL_MERCHANDISE"],
|
||||
[/home|garden|hardware/, "HOME_IMPROVEMENT"],
|
||||
[/transfer|saving/, "TRANSFER_OUT"],
|
||||
[/fee|interest|charge/, "BANK_FEES"],
|
||||
[/tax|gift|donat|charit/, "GOVERNMENT_AND_NON_PROFIT"],
|
||||
[/loan/, "LOAN_PAYMENTS"],
|
||||
[/service/, "GENERAL_SERVICES"],
|
||||
];
|
||||
export function categoryKey(text: string | undefined, labels: Record<string, string> = {}): string | null {
|
||||
const t = (text ?? "").toLowerCase().trim();
|
||||
if (!t) return null;
|
||||
for (const [key, label] of Object.entries(labels)) if (label.toLowerCase() === t) return key;
|
||||
return CATEGORY_WORDS.find(([re]) => re.test(t))?.[1] ?? null;
|
||||
}
|
||||
Reference in new issue
Block a user