aboutsummaryrefslogtreecommitdiffstats
path: root/cli/src/commands/import.ts
blob: ac39edbf175295283b92e6a250f73aeba2b28fb7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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`)
    }
}