mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
Adds a remote MCP server at POST /mcp and a bearer-authed account API, so an agent can read the account, import statements and write back categorizations without a browser session. - 15 tools across read / write / import scopes; stateless streamable-HTTP JSON-RPC. Tokens are tc_pat_-prefixed and stored as a SHA-256 hash (migration 0027). - Auth is an allow-list, not a widening of authenticateRequest: a bearer token reaches /mcp and /api/v1/* and nothing else, with a regression test pinning 401 on the cookie-authed routes. - File bytes never travel through a tool argument. prepare_import returns a 15-minute HMAC-signed, purpose-bound capability URL and the bytes go over plain HTTP; export_snapshot does the same in reverse. - POST /api/v1/import runs the bank adapters before falling back to generic CSV, so the API accepts the files the Bank Imports tab accepts. The detection/parse core moved to shared/bankImport/ for this; its 42-name public surface is unchanged and 14 of the 20 moved files are byte-identical in logic. Verified against a real 94-row Erste statement locally: parsed by the erste adapter at 0.97 confidence, 100% date and amount parse rates, account created and linked, re-import deduped to 0. 624 worker tests / 73 files, 1680 frontend tests / 137 files, both typechecks and lint clean. Deploys to dev only; prod still requires a v* tag.
348 lines
7.9 KiB
TypeScript
348 lines
7.9 KiB
TypeScript
/**
|
|
* Bank statement import — keyword → category rule engine.
|
|
*
|
|
* A TypeScript port of the Apps Script `mapCategoriesToTransactions`: match a
|
|
* transaction's text (description / counterparty) against an ordered list of
|
|
* keyword rules and assign the first hit. The default rule set below is a small,
|
|
* sensible seed derived from the user's real statements; it is meant to be
|
|
* edited/extended in-app (persistence lives in the rules store, not here).
|
|
*/
|
|
import type { CategoryRuleSet } from './types';
|
|
|
|
/**
|
|
* Return the category for `text`, or null if nothing matches. The **longest**
|
|
* matching keyword wins (most specific), so a rule with `"spar food & fuel"`
|
|
* beats one with `"spar"` even if the latter is listed first; rule order only
|
|
* breaks exact-length ties. Matching is case-insensitive substring.
|
|
*/
|
|
export function matchCategory(text: string, rules: CategoryRuleSet): string | null {
|
|
const s = text.toLowerCase();
|
|
if (!s.trim()) return null;
|
|
let best: string | null = null;
|
|
let bestLen = 0;
|
|
for (const rule of rules) {
|
|
for (const kw of rule.keywords) {
|
|
const k = kw.toLowerCase().trim();
|
|
// Strict `>` keeps the first rule on a length tie (stable, order = priority).
|
|
if (k.length > bestLen && s.includes(k)) {
|
|
best = rule.category;
|
|
bestLen = k.length;
|
|
}
|
|
}
|
|
}
|
|
return best;
|
|
}
|
|
|
|
/**
|
|
* Convenience: match against several text fragments (e.g. description then
|
|
* counterparty), returning the first category found, else `fallback`.
|
|
*/
|
|
export function categorize(
|
|
fragments: Array<string | undefined>,
|
|
rules: CategoryRuleSet,
|
|
fallback = 'Other'
|
|
): string {
|
|
for (const frag of fragments) {
|
|
if (!frag) continue;
|
|
const hit = matchCategory(frag, rules);
|
|
if (hit) return hit;
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
/**
|
|
* Default seed rules. Keep this modest and general — the user refines it in the
|
|
* rules editor. Ordered most-specific-first so e.g. "Google One" → Subscriptions
|
|
* wins over a bare "google" elsewhere.
|
|
*/
|
|
export const DEFAULT_CATEGORY_RULES: CategoryRuleSet = [
|
|
{
|
|
category: 'Groceries',
|
|
keywords: [
|
|
'kaufland',
|
|
'konzum',
|
|
'spar',
|
|
'lidl',
|
|
'plodine',
|
|
'dm drogerie',
|
|
'müller',
|
|
'muller',
|
|
'vrutak',
|
|
'bio&bio',
|
|
'tommy',
|
|
'studenac',
|
|
],
|
|
},
|
|
{
|
|
category: 'Subscriptions',
|
|
keywords: [
|
|
'netflix',
|
|
'spotify',
|
|
'google one',
|
|
'youtube',
|
|
'audible',
|
|
'apple',
|
|
'openai',
|
|
'anthropic',
|
|
'github',
|
|
'cloudflare',
|
|
'patreon',
|
|
],
|
|
},
|
|
{
|
|
// NB: no bare "ina" — as a substring it false-matches Croatian words like
|
|
// "član-ARINA" / "naknad-INA". Use the station forms instead.
|
|
category: 'Car (Gas)',
|
|
keywords: ['ina bp', 'lukoil', 'petrol', 'crodux', 'tifon', 'shell', 'benzinska'],
|
|
},
|
|
{
|
|
category: 'Bank Fees',
|
|
keywords: [
|
|
'naknad',
|
|
'uplata naknade',
|
|
'pripis kamate',
|
|
'pasivne kamate',
|
|
'bank fee',
|
|
'vođenje računa',
|
|
'vodenje racuna',
|
|
],
|
|
},
|
|
{
|
|
category: 'Apartment (Bills)',
|
|
keywords: [
|
|
'pričuva',
|
|
'pricuva',
|
|
'vodne usluge',
|
|
'zbrinjavanje otpada',
|
|
'obračun plina',
|
|
'obracun plina',
|
|
'hep',
|
|
'a1 usluge',
|
|
'a1 hrvatska',
|
|
'plina za',
|
|
'osiguranj',
|
|
],
|
|
},
|
|
{
|
|
category: 'Salary Eur',
|
|
keywords: [
|
|
'isplata plaće',
|
|
'isplata place',
|
|
'isplata potpore',
|
|
'naknada za prijevoz',
|
|
'plaća',
|
|
'salary',
|
|
'payroll',
|
|
],
|
|
},
|
|
{
|
|
category: 'Health',
|
|
keywords: ['ljekarn', 'ljekarne', 'pharmacy', 'poliklinik', 'dom zdravlja'],
|
|
},
|
|
{
|
|
category: 'Restaurant',
|
|
keywords: ['restaurant', 'restoran', 'rougemarin', 'pizzeria', 'caffe', 'bistro'],
|
|
},
|
|
{
|
|
category: 'Shopping',
|
|
keywords: ['about you', 'reserved', 'zara', 'h&m', 'steam', 'amazon'],
|
|
},
|
|
{
|
|
category: 'Transport',
|
|
keywords: ['zet', 'garaža', 'garaza', 'parking', 'hak', 'enc', 'autoceste', 'badminton'],
|
|
},
|
|
];
|
|
|
|
/**
|
|
* A general, English/worldwide seed for users outside Croatia — common global
|
|
* merchants and generic keywords. Deliberately modest; users refine it in the
|
|
* rules editor. Longest-keyword-wins, so "amazon prime" → Subscriptions beats a
|
|
* bare "amazon" → Shopping.
|
|
*/
|
|
export const DEFAULT_CATEGORY_RULES_WORLDWIDE: CategoryRuleSet = [
|
|
{
|
|
category: 'Groceries',
|
|
keywords: [
|
|
'walmart',
|
|
'costco',
|
|
'aldi',
|
|
'lidl',
|
|
'tesco',
|
|
'sainsbury',
|
|
'kroger',
|
|
'carrefour',
|
|
'whole foods',
|
|
'trader joe',
|
|
'supermarket',
|
|
'grocery',
|
|
'safeway',
|
|
],
|
|
},
|
|
{
|
|
category: 'Dining',
|
|
keywords: [
|
|
'mcdonald',
|
|
'burger king',
|
|
'kfc',
|
|
'starbucks',
|
|
'subway',
|
|
'domino',
|
|
'pizza',
|
|
'restaurant',
|
|
'cafe',
|
|
'coffee',
|
|
'uber eats',
|
|
'doordash',
|
|
'deliveroo',
|
|
'grubhub',
|
|
'chipotle',
|
|
],
|
|
},
|
|
{
|
|
category: 'Transport',
|
|
keywords: ['uber', 'lyft', 'bolt', 'taxi', 'transit', 'metro', 'parking', 'toll', 'railway'],
|
|
},
|
|
{
|
|
category: 'Fuel',
|
|
keywords: ['shell', 'esso', 'exxon', 'chevron', 'texaco', 'gas station', 'petrol', 'fuel'],
|
|
},
|
|
{
|
|
category: 'Subscriptions',
|
|
keywords: [
|
|
'netflix',
|
|
'spotify',
|
|
'youtube premium',
|
|
'amazon prime',
|
|
'disney',
|
|
'hbo',
|
|
'hulu',
|
|
'audible',
|
|
'patreon',
|
|
'openai',
|
|
'anthropic',
|
|
'github',
|
|
'google one',
|
|
'icloud',
|
|
'dropbox',
|
|
'adobe',
|
|
'microsoft 365',
|
|
],
|
|
},
|
|
{
|
|
category: 'Shopping',
|
|
keywords: [
|
|
'amazon',
|
|
'ebay',
|
|
'aliexpress',
|
|
'etsy',
|
|
'ikea',
|
|
'zara',
|
|
'h&m',
|
|
'nike',
|
|
'adidas',
|
|
'best buy',
|
|
'shein',
|
|
],
|
|
},
|
|
{
|
|
category: 'Utilities',
|
|
keywords: [
|
|
'electric',
|
|
'electricity',
|
|
'water bill',
|
|
'gas bill',
|
|
'internet',
|
|
'broadband',
|
|
'vodafone',
|
|
'verizon',
|
|
't-mobile',
|
|
'comcast',
|
|
'utility',
|
|
],
|
|
},
|
|
{
|
|
category: 'Health',
|
|
keywords: ['pharmacy', 'cvs', 'walgreens', 'boots', 'clinic', 'hospital', 'dental', 'doctor'],
|
|
},
|
|
{
|
|
category: 'Entertainment',
|
|
keywords: [
|
|
'cinema',
|
|
'movie',
|
|
'theatre',
|
|
'steam',
|
|
'playstation',
|
|
'xbox',
|
|
'nintendo',
|
|
'ticketmaster',
|
|
],
|
|
},
|
|
{
|
|
category: 'Travel',
|
|
keywords: [
|
|
'airbnb',
|
|
'booking.com',
|
|
'expedia',
|
|
'hotel',
|
|
'airlines',
|
|
'ryanair',
|
|
'easyjet',
|
|
'flight',
|
|
'hertz',
|
|
],
|
|
},
|
|
{
|
|
category: 'Bank Fees',
|
|
keywords: [
|
|
'bank fee',
|
|
'service charge',
|
|
'atm fee',
|
|
'overdraft',
|
|
'interest charge',
|
|
'monthly fee',
|
|
],
|
|
},
|
|
{
|
|
category: 'Income',
|
|
keywords: ['salary', 'payroll', 'wages', 'direct deposit', 'refund', 'reimbursement'],
|
|
},
|
|
{
|
|
category: 'Transfers',
|
|
keywords: [
|
|
'transfer',
|
|
'zelle',
|
|
'venmo',
|
|
'paypal',
|
|
'wise',
|
|
'revolut',
|
|
'cash app',
|
|
'wire transfer',
|
|
],
|
|
},
|
|
{
|
|
category: 'Housing',
|
|
// NB: avoid a bare "rent" — as a substring it false-matches "current" (e.g. "Current Account").
|
|
keywords: ['rent payment', 'monthly rent', 'mortgage', 'landlord', 'property management'],
|
|
},
|
|
];
|
|
|
|
/** A named, selectable category-rule group (mapping). */
|
|
export interface RuleGroup {
|
|
id: string;
|
|
label: string;
|
|
rules: CategoryRuleSet;
|
|
}
|
|
|
|
/** Built-in mapping groups the user can pick from in the import UI. */
|
|
export const RULE_GROUPS: RuleGroup[] = [
|
|
{ id: 'croatian', label: 'Croatian', rules: DEFAULT_CATEGORY_RULES },
|
|
{ id: 'worldwide', label: 'Worldwide (English)', rules: DEFAULT_CATEGORY_RULES_WORLDWIDE },
|
|
];
|
|
|
|
export const DEFAULT_RULE_GROUP_ID = 'croatian';
|
|
|
|
/** The seed ruleset for a group id (falls back to the Croatian default). */
|
|
export function rulesForGroup(id: string | null | undefined): CategoryRuleSet {
|
|
return RULE_GROUPS.find((g) => g.id === id)?.rules ?? DEFAULT_CATEGORY_RULES;
|
|
}
|