Add support for adding raw transactions
This commit is contained in:
@@ -93,7 +93,21 @@ export class Actual {
|
||||
return actualTransaction;
|
||||
}
|
||||
|
||||
async submit(transactions: Transaction[]): Promise<ActualImportResult> {
|
||||
async import(transactions: Transaction[]): Promise<ActualImportResult> {
|
||||
return this.#submit(transactions, t => api.importTransactions(t.account, [t]));
|
||||
}
|
||||
|
||||
async add(transactions: Transaction[], learnCategories: boolean, runTransfers: boolean): Promise<ActualImportResult> {
|
||||
return this.#submit(transactions, async t => {
|
||||
|
||||
const result = await api.addTransactions(t.account, [t], { learnCategories, runTransfers });
|
||||
return result === "ok"
|
||||
? { added: [t], updated: [], errors: [] }
|
||||
: { added: [], updated: [], errors: [{ message: `Cannot add transaction: ${t}` }] };
|
||||
});
|
||||
}
|
||||
|
||||
async #submit(transactions: Transaction[], add: (t: ActualTransaction) => Promise<ActualImportResult>): Promise<ActualImportResult> {
|
||||
try {
|
||||
await mkdir(this.#config.data, { recursive: true });
|
||||
} catch(e) {}
|
||||
@@ -119,7 +133,7 @@ export class Actual {
|
||||
}
|
||||
|
||||
else for (const transaction of toImport) {
|
||||
const result = await api.importTransactions(transaction.account, [transaction]);
|
||||
const result = await add(transaction);
|
||||
|
||||
output.added.push(...result.added);
|
||||
output.updated.push(...result.updated);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import fs from "fs";
|
||||
import { program } from "commander";
|
||||
import { ImportOptions } from "@/types/cli";
|
||||
import { AddOptions, ImportOptions } from "@/types/cli";
|
||||
import { loadConfig } from "./config";
|
||||
import { importTransactions } from "@/runner";
|
||||
import { addTransactions, importTransactions } from "@/runner";
|
||||
import { serve } from "@/server";
|
||||
|
||||
export function run(...args: string[]) {
|
||||
@@ -10,6 +10,17 @@ export function run(...args: string[]) {
|
||||
.name("actual-importer")
|
||||
.version("0.0.1")
|
||||
|
||||
program
|
||||
.command("add <csv-file>")
|
||||
.requiredOption("-c, --config <file>", "sets the path to the YAML file with configuration")
|
||||
.option("-d, --dry-run", "simulates the import by printing out what will be imported")
|
||||
.option("-p, --profile <name>", "sets the desired profile to invoke")
|
||||
.option("-s, --server <name>", "sets the desired server to upload transactions to")
|
||||
.option("-t, --transfers", "whether transfer transactions should be resolved")
|
||||
.option("-l, --learn", "whether new rules for category assignment should be created")
|
||||
.option("-x, --set <arg>", "overrides the config option for this specific run (arg: <key>=<name>, i.e. profiles.myprofile.parser=pl.ing", (v: string, prev: string[]) => prev.concat([v]), [])
|
||||
.action(doAdd)
|
||||
|
||||
program
|
||||
.command("import <csv-file>")
|
||||
.requiredOption("-c, --config <file>", "sets the path to the YAML file with configuration")
|
||||
@@ -28,6 +39,23 @@ export function run(...args: string[]) {
|
||||
program.parse(args);
|
||||
}
|
||||
|
||||
async function doAdd(file: string, options: AddOptions) {
|
||||
const config = parseConfig(options.config, options.set);
|
||||
|
||||
if (Object.keys(config?.profiles ?? {}).length === 0) {
|
||||
throw new Error(`No profiles defined in the ${options.config}`);
|
||||
}
|
||||
|
||||
if (Object.keys(config?.servers ?? {}).length === 0) {
|
||||
throw new Error(`No servers defined in the ${options.config}`);
|
||||
}
|
||||
|
||||
const profile = options.profile ?? config.defaultProfile ?? Object.keys(config.profiles)[0];
|
||||
const server = options.server ?? config.defaultServer ?? Object.keys(config.servers)[0];
|
||||
|
||||
const result = await addTransactions(fs.createReadStream(file), profile, server, config, options.dryRun, options.learn, options.transfers);
|
||||
}
|
||||
|
||||
async function doImport(file: string, options: ImportOptions) {
|
||||
const config = parseConfig(options.config, options.set);
|
||||
|
||||
@@ -45,7 +73,6 @@ async function doImport(file: string, options: ImportOptions) {
|
||||
const result = await importTransactions(fs.createReadStream(file), profile, server, config, options.dryRun);
|
||||
}
|
||||
|
||||
|
||||
function doServe(port: string, options: any) {
|
||||
const config = parseConfig(options.config, options.set);
|
||||
|
||||
|
||||
@@ -12,53 +12,56 @@ export type ImportResult = {
|
||||
skipped: string[][];
|
||||
};
|
||||
|
||||
export async function importTransactions(stream: Readable, profile: string, server: string, config: Config, dryRun?: boolean): Promise<ImportResult> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const profileConfig = config.profiles[profile];
|
||||
const submitTransactions = (load: (server: Actual, t: Transaction[]) => Promise<ActualImportResult>) => async (stream: Readable, profile: string, server: string, config: Config, dryRun?: boolean): Promise<ImportResult> => new Promise((resolve, reject) => {
|
||||
const profileConfig = config.profiles[profile];
|
||||
|
||||
if (!profileConfig) {
|
||||
throw new Error(`Unknown profile: ${profile}`);
|
||||
}
|
||||
if (!profileConfig) {
|
||||
throw new Error(`Unknown profile: ${profile}`);
|
||||
}
|
||||
|
||||
const serverConfig = config.servers[server];
|
||||
if(!serverConfig) {
|
||||
throw new Error(`Unknown server: ${server}`);
|
||||
}
|
||||
const serverConfig = config.servers[server];
|
||||
if(!serverConfig) {
|
||||
throw new Error(`Unknown server: ${server}`);
|
||||
}
|
||||
|
||||
const parser = createParser(profileConfig, serverConfig);
|
||||
const parser = createParser(profileConfig, serverConfig);
|
||||
|
||||
const actualServer = new Actual(serverConfig, dryRun);
|
||||
const skipped: string[][] = [];
|
||||
const actualServer = new Actual(serverConfig, dryRun);
|
||||
const skipped: string[][] = [];
|
||||
|
||||
const handleRow = async (data: string[]) => {
|
||||
const pushed = await parser.pushTransaction(data);
|
||||
|
||||
if (!pushed) {
|
||||
skipped.push(data);
|
||||
}
|
||||
};
|
||||
|
||||
const handleClose = async () => {
|
||||
try {
|
||||
const transactions = await parser.reconcile();
|
||||
const result = await actualServer.submit(transactions);
|
||||
|
||||
resolve({
|
||||
transactions,
|
||||
result,
|
||||
skipped
|
||||
});
|
||||
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
reject(e);
|
||||
}
|
||||
};
|
||||
const handleRow = async (data: string[]) => {
|
||||
const pushed = await parser.pushTransaction(data);
|
||||
|
||||
stream
|
||||
.pipe(iconv.decodeStream(profileConfig.encoding ?? "utf8"))
|
||||
.pipe(Papa.parse(Papa.NODE_STREAM_INPUT))
|
||||
.on('data', handleRow)
|
||||
.on('close', handleClose);
|
||||
});
|
||||
};
|
||||
if (!pushed) {
|
||||
skipped.push(data);
|
||||
}
|
||||
};
|
||||
|
||||
const handleClose = async () => {
|
||||
try {
|
||||
const transactions = await parser.reconcile();
|
||||
const result = await load(actualServer, transactions);
|
||||
|
||||
resolve({
|
||||
transactions,
|
||||
result,
|
||||
skipped
|
||||
});
|
||||
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
reject(e);
|
||||
}
|
||||
};
|
||||
|
||||
stream
|
||||
.pipe(iconv.decodeStream(profileConfig.encoding ?? "utf8"))
|
||||
.pipe(Papa.parse(Papa.NODE_STREAM_INPUT))
|
||||
.on('data', handleRow)
|
||||
.on('close', handleClose);
|
||||
});
|
||||
|
||||
export const importTransactions = submitTransactions((s, t) => s.import(t));
|
||||
|
||||
export const addTransactions = async (stream: Readable, profile: string, server: string, config: Config, dryRun?: boolean, learnCategories?: boolean, runTransfers?: boolean): Promise<ImportResult> =>
|
||||
submitTransactions((s, t) => s.add(t, !!learnCategories, !!runTransfers))(stream, profile, server, config, dryRun);
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ImportResult, importTransactions } from "@/runner";
|
||||
import { addTransactions, ImportResult, importTransactions } from "@/runner";
|
||||
import { Config } from "@/types/config";
|
||||
import express from "express";
|
||||
import multer from "multer";
|
||||
@@ -30,6 +30,21 @@ export function serve(config: Config, port: number) {
|
||||
${Object.keys(config.servers).map(s => `<option${s === config.defaultServer ? " selected": ""}>${s}</option>`)}
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label for="mode">Import mode</label>
|
||||
<select id="mode" name="mode">
|
||||
<option value="add" selected>Add</option>
|
||||
<option value="import">Import</option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label for="learn">Learn categories (only for <u>add</u> mode)</label>
|
||||
<input type="checkbox" id="learn" name="learn" />
|
||||
</p>
|
||||
<p>
|
||||
<label for="transfers">Run transfers (only for <u>add</u> mode)</label>
|
||||
<input type="checkbox" id="transfers" name="transfers" checked />
|
||||
</p>
|
||||
<p>
|
||||
<button type="submit">Import</button>
|
||||
</p>
|
||||
@@ -44,13 +59,15 @@ export function serve(config: Config, port: number) {
|
||||
throw new Error("No file to upload");
|
||||
}
|
||||
|
||||
const { profile, server } = req.body;
|
||||
const { profile, server, learn, transfers, mode } = req.body;
|
||||
|
||||
const stream = new Readable();
|
||||
stream.push(req.file.buffer);
|
||||
stream.push(null);
|
||||
|
||||
const result = await importTransactions(stream, profile, server, config);
|
||||
const result = mode === "add"
|
||||
? await addTransactions(stream, profile, server, config, false, readCheckbox(learn), readCheckbox(transfers))
|
||||
: await importTransactions(stream, profile, server, config);
|
||||
res.send(formatResult(result));
|
||||
});
|
||||
|
||||
@@ -59,6 +76,10 @@ export function serve(config: Config, port: number) {
|
||||
});
|
||||
}
|
||||
|
||||
function readCheckbox(value: string|undefined): boolean {
|
||||
return ["on", "true"].includes(value?.toLowerCase() ?? "");
|
||||
}
|
||||
|
||||
function formatResult(result: ImportResult): string {
|
||||
return `
|
||||
<html>
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
export type AddOptions = ImportOptions & {
|
||||
learn?: boolean;
|
||||
transfers?: boolean;
|
||||
};
|
||||
|
||||
export type ImportOptions = {
|
||||
config: string;
|
||||
dryRun?: boolean;
|
||||
|
||||
Reference in New Issue
Block a user