diff options
| author | ivar <i@oiee.no> | 2026-03-09 23:05:38 +0100 |
|---|---|---|
| committer | ivar <i@oiee.no> | 2026-03-09 23:05:38 +0100 |
| commit | 69448e29a85cad3a94b3be3ad33efbc52764528f (patch) | |
| tree | c32b8c817322fdf26edbbb3fa75b9505a7020ae8 /cli/src/commands/import.ts | |
| parent | b35302fa020ec82a9d67a6cb34379d42983d3cfc (diff) | |
| download | sparebank1-actualbudget-master.tar.xz sparebank1-actualbudget-master.zip | |
Diffstat (limited to 'cli/src/commands/import.ts')
| -rw-r--r-- | cli/src/commands/import.ts | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/cli/src/commands/import.ts b/cli/src/commands/import.ts new file mode 100644 index 0000000..ac39edb --- /dev/null +++ b/cli/src/commands/import.ts @@ -0,0 +1,45 @@ +import { Temporal } from "temporal-polyfill" +import { loadConfig } from "../config" +import { createSb1Client } from "../sb1" +import { importTransactions } from "../actual" + +export async function runImport(args: string[]) { + const dryRun = args.includes("--dry-run") + const since = args.find(a => a.startsWith("--since="))?.split("=")[1] + + if (since) { + try { + Temporal.PlainDate.from(since, { overflow: "reject" }) + } catch { + throw new Error(`Invalid --since date "${since}". Expected a valid date in YYYY-MM-DD format.`) + } + } + + const config = loadConfig() + + if (!config.mappings.length) { + throw new Error("No account mappings configured. Run `sb1-actual accounts` to see available accounts, then add mappings to config.json.") + } + + const sb1 = createSb1Client(config.sb1) + + if (dryRun) console.log("Dry run — no transactions will be written.\n") + if (since) console.log(`Fetching transactions since ${since}\n`) + + for (const mapping of config.mappings) { + const label = mapping.label ?? mapping.sb1Id + console.log(`Fetching transactions for ${label}...`) + + const transactions = await sb1.getTransactions(mapping.sb1Id, since) + if (!transactions.length) { + console.log(` No transactions found.\n`) + continue + } + + const booked = transactions.filter(t => t.bookingStatus === "BOOKED") + console.log(` ${booked.length} booked transaction(s) found.`) + + const result = await importTransactions(config.actual, mapping.actualId, transactions, dryRun) + console.log(` Imported: ${result.added?.length ?? 0} added, ${result.updated?.length ?? 0} updated\n`) + } +} |
