Add support for submitting the transactions to Actual server

This commit is contained in:
2025-04-01 17:19:50 +02:00
parent 9fa3b7d775
commit 32014f84e8
10 changed files with 155 additions and 18 deletions

View File

@@ -1,18 +1,51 @@
import fs from "fs";
import { openCsv } from "@/csv";
import { createParser } from "@/parser";
import { Actual } from "@/server";
import { Config } from "@/types/config";
export function loadTransactions(file: string, profile: string, config: Config) {
export function loadTransactions(file: string, profile: string, server: string, config: Config) {
const profileConfig = config.profiles[profile];
if (!profileConfig) {
throw new Error(`Unknown profile: ${profile}`);
}
const serverConfig = config.servers[server];
if(!serverConfig) {
throw new Error(`Unknown server: ${server}`);
}
const parser = createParser(profileConfig);
openCsv(file).on('data', async data => {
const transaction = await parser.parseTransaction(profileConfig, data);
console.log(transaction);
});
const actualServer = new Actual(serverConfig);
const skipped: string[] = [];
openCsv(file)
.on('data', async data => {
const transaction = await parser.parseTransaction(profileConfig, data);
if (transaction === undefined) {
skipped.push(`Skipped ==> ${data.join(" ::: ")}`);
return;
}
actualServer.pushTransaction(transaction);
})
.on('close', () => actualServer.submit()
.then(x => {
console.log(`Inserted: ${x.added.length}`);
console.log(`Updated: ${x.updated.length}`);
console.log(`Errors: ${x.errors?.length}`);
console.log(`Skipped: ${skipped.length}`);
const now = new Date();
const date = `${now.getFullYear()}-${(now.getMonth()+1).toString().padStart(2, '0')}-${now.getDate().toString().padStart(2, '0')}`;
const time = `${now.getHours().toString().padStart(2, '0')}-${now.getMinutes().toString().padStart(2, '0')}-${now.getSeconds().toString().padStart(2, '0')}`
const filename = `${serverConfig.data}/import.${profile}.${server}.${date}T${time}.json`.replaceAll(/\/+/g, "/");
const logContent = `${JSON.stringify(x, undefined, 2)}\n\n\n\n${skipped.join("\n")}`
fs.writeFileSync(filename, logContent);
console.log(`Detailed output written to ${filename} file.`);
})
.catch(x => console.error(x))
);
}