Split preparation and submission of transaction in Actual backend
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import Papa from "papaparse";
|
||||
import iconv from "iconv-lite";
|
||||
import { createParser } from "@/parser";
|
||||
import { Actual, ActualImportResult } from "@/backend";
|
||||
import { Actual, ActualImportResult, SubmitOptions } from "@/backend";
|
||||
import { Config } from "@/types/config";
|
||||
import { Readable } from "stream";
|
||||
import { Transaction } from "@/types/transaction";
|
||||
@@ -12,7 +12,7 @@ export type ImportResult = {
|
||||
skipped: string[][];
|
||||
};
|
||||
|
||||
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) => {
|
||||
export const submitTransactions = async (stream: Readable, profile: string, server: string, config: Config, opts: SubmitOptions): Promise<ImportResult> => new Promise((resolve, reject) => {
|
||||
const profileConfig = config.profiles[profile];
|
||||
|
||||
if (!profileConfig) {
|
||||
@@ -26,7 +26,7 @@ const submitTransactions = (load: (server: Actual, t: Transaction[]) => Promise<
|
||||
|
||||
const parser = createParser(profileConfig, serverConfig);
|
||||
|
||||
const actualServer = new Actual(serverConfig, dryRun);
|
||||
const actualServer = new Actual(serverConfig);
|
||||
const skipped: string[][] = [];
|
||||
|
||||
const handleRow = async (data: string[]) => {
|
||||
@@ -40,7 +40,7 @@ const submitTransactions = (load: (server: Actual, t: Transaction[]) => Promise<
|
||||
const handleClose = async () => {
|
||||
try {
|
||||
const transactions = await parser.reconcile();
|
||||
const result = await load(actualServer, transactions);
|
||||
const result = await actualServer.load(transactions, opts);
|
||||
|
||||
resolve({
|
||||
transactions,
|
||||
@@ -59,9 +59,4 @@ const submitTransactions = (load: (server: Actual, t: Transaction[]) => Promise<
|
||||
.pipe(Papa.parse(Papa.NODE_STREAM_INPUT, profileConfig.csv ?? parser.csvConfig))
|
||||
.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);
|
||||
});
|
||||
Reference in New Issue
Block a user