From 894337dc2322ed38519fbf1bddb8cce50a2c7d79 Mon Sep 17 00:00:00 2001 From: Daniel Hauser <36034483+daniel-hauser@users.noreply.github.com> Date: Fri, 15 Dec 2023 21:49:55 +0200 Subject: [PATCH] Feature/print added transactions (#205) * Add highlightedTransactions field to SaveStats interface * Refactor GoogleSheetsStorage to track highlighted transactions * Add highlighted transactions to summary message --- src/__snapshots__/messages.test.ts.snap | 3 ++ src/messages.test.ts | 17 +++++++ src/messages.ts | 68 +++++++++++++++++-------- src/storage/sheets.ts | 8 ++- src/types.ts | 1 + 5 files changed, 74 insertions(+), 23 deletions(-) diff --git a/src/__snapshots__/messages.test.ts.snap b/src/__snapshots__/messages.test.ts.snap index cfb93109..15c92855 100644 --- a/src/__snapshots__/messages.test.ts.snap +++ b/src/__snapshots__/messages.test.ts.snap @@ -15,6 +15,9 @@ Saved to: šŸ“ Storage 2 (TheTable) 7 added 9 skipped (10 existing, 8 pending) + ----- + Group1: + description1: +10.00 ------- Pending txns: diff --git a/src/messages.test.ts b/src/messages.test.ts index a1d9919f..37d80400 100644 --- a/src/messages.test.ts +++ b/src/messages.test.ts @@ -73,6 +73,23 @@ describe("messages", () => { pending: 8, skipped: 9, existing: 10, + highlightedTransactions: { + Group1: [ + { + account: "account1", + companyId: CompanyTypes.max, + hash: "hash1", + type: TransactionTypes.Normal, + date: new Date().toISOString(), + processedDate: new Date().toISOString(), + description: "description1", + originalAmount: 10, + originalCurrency: "ILS", + chargedAmount: 10, + status: TransactionStatuses.Completed, + }, + ], + }, }, ]; diff --git a/src/messages.ts b/src/messages.ts index f5359b23..b3067dc0 100644 --- a/src/messages.ts +++ b/src/messages.ts @@ -1,5 +1,10 @@ import { TransactionStatuses } from "israeli-bank-scrapers/lib/transactions.js"; -import { AccountScrapeResult, SaveStats, Transaction } from "./types"; +import { + AccountScrapeResult, + SaveStats, + Transaction, + TransactionRow, +} from "./types"; export function getSummaryMessage( results: Array, @@ -30,8 +35,8 @@ ${stats.map((s) => statsString(s)).join("\n") || "\tšŸ˜¶ None"} ------- Pending txns: -${getPendingSummary(pending) || "\tšŸ˜¶ None"} - `.trim(); +${transactionList(pending) || "\tšŸ˜¶ None"} +`.trim(); } function transactionsString( @@ -47,27 +52,29 @@ ${ }`.trim(); } -function getPendingSummary(pending: Array) { - return pending - .map((t) => { - const sign = t.originalAmount < 0 ? "-" : "+"; - const originalAmount = Math.abs(t.originalAmount).toFixed(2); - const amount = - t.originalCurrency === "ILS" - ? originalAmount - : `${originalAmount} ${t.originalCurrency}`; - - return `\t${t?.description}:\t${sign}${amount}`; - }) - .join("\n"); +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}`; +} + +function transactionList(transactions: Array, indent = "\t") { + return transactions.map((t) => `${indent}${transactionString(t)}`).join("\n"); } -function statsString(starts: SaveStats): string { +function statsString(stats: SaveStats): string { return ` -šŸ“ ${starts.name} (${starts.table}) -\t${starts.added} added -\t${starts.skipped} skipped (${starts.existing} existing, ${starts.pending} pending) - `.trim(); +šŸ“ ${stats.name} (${stats.table}) +\t${stats.added} added +\t${stats.skipped} skipped (${stats.existing} existing, ${ + stats.pending + } pending) +${highlightedTransactionsString(stats.highlightedTransactions, 1)}`.trim(); } function transactionsByStatus(results: Array) { @@ -89,3 +96,22 @@ function transactionsByStatus(results: Array) { completed: scrapedTxns, }; } + +function highlightedTransactionsString( + groups: Record | undefined, + indent = 0, +) { + if (!groups || Object.keys(groups).length === 0) { + return ""; + } + + const indentString = "\t".repeat(indent); + + return ( + `${indentString}${"-".repeat(5)}\n` + + `${Object.entries(groups).map(([name, txns]) => { + const transactionsString = transactionList(txns, `${indentString}\t`); + return `${indentString}${name}:\n${transactionsString}`; + })}` + ); +} diff --git a/src/storage/sheets.ts b/src/storage/sheets.ts index 098c5929..074211ab 100644 --- a/src/storage/sheets.ts +++ b/src/storage/sheets.ts @@ -79,7 +79,7 @@ export class GoogleSheetsStorage implements TransactionStorage { const rows: SheetRow[] = []; await this.init(); - const stats: SaveStats = { + const stats = { name: "Google Sheets", table: worksheetName, total: txns.length, @@ -87,7 +87,10 @@ export class GoogleSheetsStorage implements TransactionStorage { pending: 0, existing: 0, skipped: 0, - }; + highlightedTransactions: { + Added: [] as Array, + }, + } satisfies SaveStats; for (let tx of txns) { if (this.existingTransactionsHashes.has(tx.hash)) { @@ -103,6 +106,7 @@ export class GoogleSheetsStorage implements TransactionStorage { } rows.push(this.transactionRow(tx)); + stats.highlightedTransactions.Added.push(tx); } if (rows.length) { diff --git a/src/types.ts b/src/types.ts index a6076ae9..dc64c3fb 100644 --- a/src/types.ts +++ b/src/types.ts @@ -36,6 +36,7 @@ export interface SaveStats { pending: number; skipped: number; existing: number; + highlightedTransactions?: Record>; } export interface TransactionStorage {