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

Fix: message has the whole installments amount and not charged amount #215

Merged
merged 2 commits into from
Jan 1, 2024
Merged
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
27 changes: 24 additions & 3 deletions src/__snapshots__/messages.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`messages getSummaryMessage should return a summary message 1`] = `
"6 transactions scraped.
(3 pending, 3 completed)
"7 transactions scraped.
(3 pending, 4 completed)

Accounts updated:
✔️ [max] account1: 0
✔️ [max] account1: 1
✔️ [max] account2: 6

Saved to:
Expand Down Expand Up @@ -44,6 +44,27 @@ Pending txns:
😶 None"
`;

exports[`messages getSummaryMessage should return a summary message with installments 1`] = `
"2 transactions scraped.
(0 pending, 2 completed)

Accounts updated:
✔️ [max] account1: 2

Saved to:
📝 Storage 1 (TheTable)
2 added
4 skipped (5 existing, 3 pending)
-----
SomeGroup:
should be +20: +20.00
should be -20: -20.00

-------
Pending txns:
😶 None"
`;

exports[`messages getSummaryMessage should return a summary message with no results 1`] = `
"0 transactions scraped.

Expand Down
73 changes: 71 additions & 2 deletions src/messages.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { CompanyTypes } from "israeli-bank-scrapers";
import { getSummaryMessage } from "./messages";
import { AccountScrapeResult, SaveStats, Transaction } from "./types";
import {
AccountScrapeResult,
SaveStats,
Transaction,
TransactionRow,
} from "./types";
import {
TransactionStatuses,
TransactionTypes,
Expand All @@ -18,7 +23,15 @@ describe("messages", () => {
accounts: [
{
accountNumber: "account1",
txns: [],
txns: [
transaction({
chargedAmount: -20,
originalAmount: -100,
description: "ILS",
chargedCurrency: "ILS",
originalCurrency: "USD",
}),
],
},
{
accountNumber: "account2",
Expand Down Expand Up @@ -145,6 +158,62 @@ describe("messages", () => {

expect(summary).toMatchSnapshot();
});

it("should return a summary message with installments", () => {
const transactions = [
transaction({
type: TransactionTypes.Installments,
chargedAmount: 20,
originalAmount: 100,
description: "should be +20",
}),
transaction({
type: TransactionTypes.Installments,
chargedAmount: -20,
originalAmount: -100,
description: "should be -20",
}),
];

const results: Array<AccountScrapeResult> = [
{
companyId: CompanyTypes.max,
result: {
success: true,
accounts: [
{
accountNumber: "account1",
txns: transactions,
},
],
},
},
];

const stats: Array<SaveStats> = [
{
name: "Storage 1",
table: "TheTable",
total: 1,
added: 2,
pending: 3,
skipped: 4,
existing: 5,
highlightedTransactions: {
SomeGroup: transactions.map<TransactionRow>((t) => ({
account: "account1",
companyId: CompanyTypes.max,
hash: "hash1",
...t,
})),
},
},
];

const summary = getSummaryMessage(results, stats);

expect(summary).toMatchSnapshot();
});
});
});

Expand Down
35 changes: 26 additions & 9 deletions src/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { TransactionStatuses } from "israeli-bank-scrapers/lib/transactions.js";
import {
TransactionStatuses,
TransactionTypes,
} from "israeli-bank-scrapers/lib/transactions.js";
import {
AccountScrapeResult,
SaveStats,
Expand Down Expand Up @@ -52,15 +55,29 @@ ${
}`.trim();
}

function transactionAmount(t: Transaction): number {
switch (t.type) {
case TransactionTypes.Normal:
switch (t.status) {
case TransactionStatuses.Pending:
return t.originalAmount;
case TransactionStatuses.Completed:
return t.chargedAmount;
}
case TransactionTypes.Installments:
return t.chargedAmount;
}
}

function transactionString(t: Transaction) {
const sign = t.originalAmount < 0 ? "-" : "+";
const originalAmount = Math.abs(t.originalAmount).toFixed(2);
const amount =
t.originalCurrency === "ILS"
? originalAmount
: `${originalAmount} ${t.originalCurrency}`;

return `${t?.description}:\t${sign}${amount}`;
const amount = transactionAmount(t);

const sign = amount < 0 ? "-" : "+";
const absAmount = Math.abs(amount).toFixed(2);

return `${t?.description}:\t${sign}${absAmount}${
t.originalCurrency === "ILS" ? "" : ` ${t.originalCurrency}`
}`;
}

function transactionList(transactions: Array<Transaction>, indent = "\t") {
Expand Down