// Pull booked bank transactions into money.transactions (Enable Banking). // // bun --env-file=$HOME/.config/money-sync.env scripts/bank-sync.ts [options] // --dry-run fetch and report, write nothing // --reconcile match against ALL hand-entered rows (account + amount + date ±1 day) // and attach the bank id to them instead of inserting duplicates; // run once after linking (with --dry-run first) // --from YYYY-MM-DD override the start date (default: last synced date - 7 days, or the // account's first transaction on the first run) // --account only this tracker account // // Dedupe: unique (account_id, external_id); re-running is always safe. import postgres from 'postgres'; import { configFromEnv, createEnableBankingClient } from '../src/lib/server/enable-banking'; import { syncBankAccounts } from '../src/lib/server/bank-sync'; const args = process.argv.slice(2); const value = (name: string) => { const i = args.indexOf(name); return i >= 0 ? args[i + 1] : undefined; }; const from = value('--from'); if (from && !/^\d{4}-\d{2}-\d{2}$/.test(from)) throw new Error('--from must be YYYY-MM-DD'); if (!process.env.DB_URL) throw new Error('DB_URL is not set'); const client = createEnableBankingClient(configFromEnv()); const sql = postgres(process.env.DB_URL, { max: 2, connect_timeout: 10, onnotice: () => {} }); const dryRun = args.includes('--dry-run'); try { const results = await syncBankAccounts(sql, client, { dryRun, reconcile: args.includes('--reconcile'), from, accountName: value('--account') }); for (const r of results) { console.log( `${r.account}: fetched ${r.fetched}, booked ${r.booked}, new ${r.inserted}, ` + `matched to existing ${r.matched}, already synced ${r.alreadyKnown}` + (r.balance ? `, bank balance ${r.balance}` : '') + (r.error ? `\n ERROR ${r.error}` : '') ); } if (dryRun) console.log('(dry run: nothing written)'); if (results.some((r) => r.error)) process.exitCode = 1; } finally { await sql.end(); }