Skip to content

Commit

Permalink
feat: Add charged currency field to Google Sheets storage (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-hauser authored Jun 25, 2024
1 parent c6eed68 commit f8ac8f9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/storage/sheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
} from "../types.js";
import { TransactionStatuses } from "israeli-bank-scrapers/lib/transactions.js";
import { sendDeprecationMessage } from "../notifier.js";
import { normalizeCurrency } from "../utils/currency.js";

const logger = createLogger("GoogleSheetsStorage");

Expand All @@ -34,6 +35,7 @@ type SheetRow = {
"scraped at": string;
"scraped by": string;
identifier: string;
chargedCurrency: string;
};

export class GoogleSheetsStorage implements TransactionStorage {
Expand All @@ -49,6 +51,7 @@ export class GoogleSheetsStorage implements TransactionStorage {
"scraped at",
"scraped by",
"identifier",
"chargedCurrency",
];

existingTransactionsHashes = new Set<string>();
Expand Down Expand Up @@ -196,6 +199,7 @@ export class GoogleSheetsStorage implements TransactionStorage {
"scraped at": currentDate,
"scraped by": systemName,
identifier: `${tx.identifier ?? ""}`,
chargedCurrency: normalizeCurrency(tx.chargedCurrency),
};
}
}
31 changes: 31 additions & 0 deletions src/utils/currency.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { normalizeCurrency } from "./currency";

describe("normalizeCurrency", () => {
it("should return undefined if currency is undefined", () => {
const result = normalizeCurrency(undefined);
expect(result).toBeUndefined();
});

it.each([
["$", "USD"],
["€", "EUR"],
["₪", "ILS"],
["usd", "USD"],
])(
"should return the normalized currency for %s symbol as %s",
(currency, expected) => {
const result = normalizeCurrency(currency);
expect(result).toBe(expected);
},
);

it("should return the currency as is if it is not a known symbol", () => {
const result = normalizeCurrency("GBP");
expect(result).toBe("GBP");
});

it("should normalize the currency to uppercase", () => {
const result = normalizeCurrency("usd");
expect(result).toBe("USD");
});
});
10 changes: 10 additions & 0 deletions src/utils/currency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const symbolToCurrency = {
$: "USD",
"€": "EUR",
"₪": "ILS",
};

export function normalizeCurrency(currency: string | undefined) {
if (!currency) return undefined;
return (symbolToCurrency[currency] ?? currency).toUpperCase();
}

0 comments on commit f8ac8f9

Please sign in to comment.