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..8d0a0c0e 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,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 = [ + { + 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") {