Skip to content

Commit

Permalink
Feature/print added transactions (#205)
Browse files Browse the repository at this point in the history
* Add highlightedTransactions field to SaveStats interface

* Refactor GoogleSheetsStorage to track highlighted transactions

* Add highlighted transactions to summary message
  • Loading branch information
daniel-hauser authored Dec 15, 2023
1 parent f19b152 commit 894337d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 23 deletions.
3 changes: 3 additions & 0 deletions src/__snapshots__/messages.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Saved to:
📝 Storage 2 (TheTable)
7 added
9 skipped (10 existing, 8 pending)
-----
Group1:
description1: +10.00
-------
Pending txns:
Expand Down
17 changes: 17 additions & 0 deletions src/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
],
},
},
];

Expand Down
68 changes: 47 additions & 21 deletions src/messages.ts
Original file line number Diff line number Diff line change
@@ -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<AccountScrapeResult>,
Expand Down Expand Up @@ -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(
Expand All @@ -47,27 +52,29 @@ ${
}`.trim();
}

function getPendingSummary(pending: Array<Transaction>) {
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<Transaction>, 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<AccountScrapeResult>) {
Expand All @@ -89,3 +96,22 @@ function transactionsByStatus(results: Array<AccountScrapeResult>) {
completed: scrapedTxns,
};
}

function highlightedTransactionsString(
groups: Record<string, TransactionRow[]> | 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}`;
})}`
);
}
8 changes: 6 additions & 2 deletions src/storage/sheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,18 @@ export class GoogleSheetsStorage implements TransactionStorage {
const rows: SheetRow[] = [];
await this.init();

const stats: SaveStats = {
const stats = {
name: "Google Sheets",
table: worksheetName,
total: txns.length,
added: 0,
pending: 0,
existing: 0,
skipped: 0,
};
highlightedTransactions: {
Added: [] as Array<TransactionRow>,
},
} satisfies SaveStats;

for (let tx of txns) {
if (this.existingTransactionsHashes.has(tx.hash)) {
Expand All @@ -103,6 +106,7 @@ export class GoogleSheetsStorage implements TransactionStorage {
}

rows.push(this.transactionRow(tx));
stats.highlightedTransactions.Added.push(tx);
}

if (rows.length) {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface SaveStats {
pending: number;
skipped: number;
existing: number;
highlightedTransactions?: Record<string, Array<TransactionRow>>;
}

export interface TransactionStorage {
Expand Down

0 comments on commit 894337d

Please sign in to comment.