Add support for 2-stage import with web server
This commit is contained in:
@@ -6,13 +6,17 @@ import { Config } from "@/types/config";
|
||||
import { Readable } from "stream";
|
||||
import { Transaction } from "@/types/transaction";
|
||||
|
||||
export type ImportResult = {
|
||||
transactions: Transaction[];
|
||||
result: ActualImportResult;
|
||||
export type PrepareResult = {
|
||||
transactions: Transaction[];
|
||||
skipped: string[][];
|
||||
};
|
||||
|
||||
export const submitTransactions = async (stream: Readable, profile: string, server: string, config: Config, opts: SubmitOptions): Promise<ImportResult> => new Promise((resolve, reject) => {
|
||||
export type ImportResult = PrepareResult & {
|
||||
result: ActualImportResult;
|
||||
};
|
||||
|
||||
|
||||
export const prepareTransactions = async (stream: Readable, profile: string, server: string, config: Config): Promise<PrepareResult> => new Promise((resolve, reject) => {
|
||||
const profileConfig = config.profiles[profile];
|
||||
|
||||
if (!profileConfig) {
|
||||
@@ -25,8 +29,7 @@ export const submitTransactions = async (stream: Readable, profile: string, serv
|
||||
}
|
||||
|
||||
const parser = createParser(profileConfig, serverConfig);
|
||||
|
||||
const actualServer = new Actual(serverConfig);
|
||||
|
||||
const skipped: string[][] = [];
|
||||
|
||||
const handleRow = async (data: string[]) => {
|
||||
@@ -40,18 +43,15 @@ export const submitTransactions = async (stream: Readable, profile: string, serv
|
||||
const handleClose = async () => {
|
||||
try {
|
||||
const transactions = await parser.reconcile();
|
||||
const result = await actualServer.load(transactions, opts);
|
||||
|
||||
|
||||
resolve({
|
||||
transactions,
|
||||
result,
|
||||
transactions,
|
||||
skipped
|
||||
});
|
||||
|
||||
} catch (e) {
|
||||
});
|
||||
} catch (e: unknown) {
|
||||
console.error(e);
|
||||
reject(e);
|
||||
}
|
||||
reject(e);
|
||||
}
|
||||
};
|
||||
|
||||
stream
|
||||
@@ -59,4 +59,26 @@ export const submitTransactions = async (stream: Readable, profile: string, serv
|
||||
.pipe(Papa.parse(Papa.NODE_STREAM_INPUT, profileConfig.csv ?? parser.csvConfig))
|
||||
.on('data', handleRow)
|
||||
.on('close', handleClose);
|
||||
});
|
||||
});
|
||||
|
||||
export const submitTransactions = async (transactions: Transaction[], server: string, config: Config, opts: SubmitOptions): Promise<ActualImportResult> => {
|
||||
const serverConfig = config.servers[server];
|
||||
|
||||
if(!serverConfig) {
|
||||
throw new Error(`Unknown server: ${server}`);
|
||||
}
|
||||
|
||||
const actualServer = new Actual(serverConfig);
|
||||
return await actualServer.load(transactions, opts);
|
||||
};
|
||||
|
||||
export const loadTransactions = async (stream: Readable, profile: string, server: string, config: Config, opts: SubmitOptions): Promise<ImportResult> => {
|
||||
const prepared = await prepareTransactions(stream, profile, server, config)
|
||||
|
||||
const result = await submitTransactions(prepared.transactions, server, config, opts);
|
||||
|
||||
return {
|
||||
...prepared,
|
||||
result
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user