Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add currency_rate setting; Rename field payee_name to payee #35

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ describe("parser", () => {
const result = runParser(csvFixtures.payeeField, parseCfg);
expect(result.transactions).toHaveLength(3);
result.transactions.forEach((tx) => {
expect(tx.payee_name).toBeDefined();
expect(tx.payee_name!.length).toBeGreaterThan(0);
expect(tx.payee).toBeDefined();
expect(tx.payee!.length).toBeGreaterThan(0);
});
});

Expand Down
18 changes: 9 additions & 9 deletions src/lib/parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BankFile, ParsedBankFile, Parser, Transaction } from "../types";
import { BankFile, BankFilePattern, ParsedBankFile, Parser, Transaction } from "../types";
import parseCsv from "csv-parse/lib/sync";
import { Options as parseOptions } from "csv-parse";
import { DateTime } from "luxon";
Expand All @@ -23,22 +23,22 @@ export function parseBankFile(source: BankFile, parsers: Parser[]) {
const endRow = records.length - parser.footer_rows;
records = records.slice(startRow, endRow);

const transactions = records.map((tx) => buildTransaction(tx, parser));
const transactions = records.map((tx) => buildTransaction(tx, parser, source.matchedPattern));
logResult(transactions.length, source.path);
return {
transactions,
source,
} as ParsedBankFile;
}

export function buildTransaction(record: any, parser: Parser): Transaction {
export function buildTransaction(record: any, parser: Parser, pattern?: BankFilePattern): Transaction {
const tx: Transaction = {
amount: parseAmount(record, parser),
date: parseDate(record, parser.date_format),
payee: record.payee?.trim(),
memo: mergeMemoFields(record),
payee_name: record.payee?.trim(),
amount: parseAmount(record, parser, pattern?.currency_rate),
};
if (!tx.payee_name) delete tx.payee_name;
if (!tx.payee) delete tx.payee;
return tx;
}

Expand All @@ -63,7 +63,7 @@ function parseDate(record: any, dateFormat: string) {
throw "PARSING ERROR";
}

function parseAmount(record: any, parser: Parser): number {
function parseAmount(record: any, parser: Parser, currencyRate: number = 1): number {
const { thousand_separator, decimal_separator, outflow_indicator } = parser;
const { inflow, outflow, amount, in_out_flag } = record;
let value = inflow || outflow || amount;
Expand Down Expand Up @@ -91,11 +91,11 @@ function parseAmount(record: any, parser: Parser): number {
// If the outflow column exists, OR
// If the in_out_flag column exists AND it contains the outflow indicator
// invert the value of the amount
if (outflow !== undefined || in_out_flag?.startsWith(outflow_indicator)) {
if (outflow || in_out_flag?.startsWith(outflow_indicator)) {
value = -value; // 420.69 ==> -420.69
}

return value;
return Math.round((value * currencyRate) * 100) / 100;
}

function logResult(txCount: number, sourcePath: string) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/uploader.spec.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const parsedBankFile: ParsedBankFile = {
amount: 420,
date: new Date("1990-02-27"),
memo: "tx1",
payee_name: "payee1",
payee: "payee1",
},
{
amount: 420,
Expand Down Expand Up @@ -50,7 +50,7 @@ export const expectedTransactions = [
cleared: "cleared",
account_id: "testAccountId",
flag_color: "purple",
payee_name: "payee1",
payee: "payee1",
occurrence: 1,
},
{
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type BankFilePattern = {
upload: boolean;
save_parsed_file: boolean;
delete_original_file: boolean;
currency_rate?: number;
};

export type BankFile = {
Expand All @@ -43,7 +44,7 @@ export type Transaction = {
date: Date;
account_id?: string;
flag_color?: string;
payee_name?: string;
payee?: string;
};

export type Parser = {
Expand Down