diff options
Diffstat (limited to 'cli/src/commands/accounts.ts')
| -rw-r--r-- | cli/src/commands/accounts.ts | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/cli/src/commands/accounts.ts b/cli/src/commands/accounts.ts new file mode 100644 index 0000000..4b7e980 --- /dev/null +++ b/cli/src/commands/accounts.ts @@ -0,0 +1,46 @@ +import * as p from "@clack/prompts" +import { loadConfig, saveConfig } from "../config" +import { createSb1Client } from "../sb1" +import { getAccounts } from "../actual" + +export async function accounts() { + const config = loadConfig() + const sb1 = createSb1Client(config.sb1) + + const spinner = p.spinner() + spinner.start("Fetching accounts...") + const [sb1Accounts, actualAccounts] = await Promise.all([ + sb1.getAccounts(), + getAccounts(config.actual) + ]) + spinner.stop("Accounts loaded.") + + const openActualAccounts = actualAccounts.filter(a => !a.closed) + + p.intro("Account mappings") + + const mappings = [] + for (const sb1Account of sb1Accounts) { + const existing = config.mappings.find(m => m.sb1Id === sb1Account.key) + + const actualId = await p.select({ + message: `${sb1Account.name} (${sb1Account.balance} ${sb1Account.currencyCode})`, + options: [ + { value: null, label: "Skip" }, + ...openActualAccounts.map(a => ({ value: a.id, label: a.name })) + ], + initialValue: existing?.actualId ?? null, + }) + + if (p.isCancel(actualId)) { + p.cancel("Cancelled.") + process.exit(0) + } + + if (actualId) mappings.push({ sb1Id: sb1Account.key, actualId, label: sb1Account.name }) + } + + saveConfig({ ...config, mappings }) + + p.outro(`Saved ${mappings.length} mapping(s).`) +} |
