From e35c1b2dc2b901d4e923f0a64de68f774678dc0e Mon Sep 17 00:00:00 2001 From: Daniel Hauser Date: Mon, 1 Jan 2024 22:36:31 +0200 Subject: [PATCH 1/2] Fix: message has the whole installments amount and not the charged amount --- src/__snapshots__/messages.test.ts.snap | 27 ++++++++- src/messages.test.ts | 74 ++++++++++++++++++++++++- src/messages.ts | 35 +++++++++--- 3 files changed, 122 insertions(+), 14 deletions(-) diff --git a/src/__snapshots__/messages.test.ts.snap b/src/__snapshots__/messages.test.ts.snap index 15c92855..fe1c1f0d 100644 --- a/src/__snapshots__/messages.test.ts.snap +++ b/src/__snapshots__/messages.test.ts.snap @@ -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: @@ -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. diff --git a/src/messages.test.ts b/src/messages.test.ts index 37d80400..daf7f48c 100644 --- a/src/messages.test.ts +++ b/src/messages.test.ts @@ -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, @@ -18,7 +23,15 @@ describe("messages", () => { accounts: [ { accountNumber: "account1", - txns: [], + txns: [ + transaction({ + chargedAmount: -20, + originalAmount: -100, + description: "ILS", + chargedCurrency: "ILS", + originalCurrency: "USD", + }), + ], }, { accountNumber: "account2", @@ -145,6 +158,63 @@ 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 = [ + { + companyId: CompanyTypes.max, + result: { + success: true, + accounts: [ + { + accountNumber: "account1", + txns: transactions, + }, + ], + }, + }, + ]; + + const stats: Array = [ + { + name: "Storage 1", + table: "TheTable", + total: 1, + added: 2, + pending: 3, + skipped: 4, + existing: 5, + highlightedTransactions: { + SomeGroup: transactions.map((t) => ({ + account: "account1", + companyId: CompanyTypes.max, + hash: "hash1", + ...t, + + })), + }, + }, + ]; + + const summary = getSummaryMessage(results, stats); + + expect(summary).toMatchSnapshot(); + }); }); }); diff --git a/src/messages.ts b/src/messages.ts index b3067dc0..2daeda3e 100644 --- a/src/messages.ts +++ b/src/messages.ts @@ -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, @@ -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, indent = "\t") { From 3899fff96c7d7db0693495c26f866b6f18e0bb1b Mon Sep 17 00:00:00 2001 From: Daniel Hauser Date: Mon, 1 Jan 2024 22:46:31 +0200 Subject: [PATCH 2/2] lint --- src/messages.test.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/messages.test.ts b/src/messages.test.ts index daf7f48c..8d0a0c0e 100644 --- a/src/messages.test.ts +++ b/src/messages.test.ts @@ -165,13 +165,13 @@ describe("messages", () => { type: TransactionTypes.Installments, chargedAmount: 20, originalAmount: 100, - description: "should be +20" + description: "should be +20", }), transaction({ type: TransactionTypes.Installments, chargedAmount: -20, originalAmount: -100, - description: "should be -20" + description: "should be -20", }), ]; @@ -205,7 +205,6 @@ describe("messages", () => { companyId: CompanyTypes.max, hash: "hash1", ...t, - })), }, },