From 18a0fbfac8fc822f9b46b84a8b4d08c200a5e1b0 Mon Sep 17 00:00:00 2001 From: Martin Berg Alstad Date: Fri, 31 Jan 2025 16:58:02 +0100 Subject: [PATCH] Relative from and to date in config, changed default to 3 days --- .env.example | 2 ++ config.ts | 15 +++++++++++++++ src/bank/sparebank1.ts | 15 ++++++++++----- src/main.ts | 2 ++ src/mappings.ts | 2 +- 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/.env.example b/.env.example index 4ee9b58..f373d73 100644 --- a/.env.example +++ b/.env.example @@ -16,3 +16,5 @@ BANK_ACCOUNT_IDS=your-account-id1,your-account-id2 LOG_LEVEL=info# trace | error | warn | info | debug | trace DB_DIRECTORY=data# Relative path, must not start or end with / DB_FILENAME=default +TRANSACTION_RELATIVE_FROM_DATE=4 +TRANSACTION_RELATIVE_TO_DATE=3 diff --git a/config.ts b/config.ts index 8f6f0bb..9f4f0d4 100644 --- a/config.ts +++ b/config.ts @@ -22,6 +22,16 @@ export const BANK_ACCOUNT_IDS = getArrayOrThrow("BANK_ACCOUNT_IDS") export const DB_DIRECTORY = getOrDefault("DB_DIRECTORY", "data") export const DB_FILENAME = getOrDefault("DB_FILENAME", "default") export const LOG_LEVEL = getOrDefault("LOG_LEVEL", "info") +// Relative number of days in the past to start fetching transactions from +export const TRANSACTION_RELATIVE_FROM_DATE = getNumberOrDefault( + "TRANSACTION_RELATIVE_FROM_DATE", + 4, +) +// Relative number of days in the past to end fetching transactions from +export const TRANSACTION_RELATIVE_TO_DATE = getNumberOrDefault( + "TRANSACTION_RELATIVE_TO_DATE", + 3, +) // Utility functions function getOrDefault(key: string, def: string): string { @@ -37,3 +47,8 @@ function getOrThrow(key: string): string { function getArrayOrThrow(key: string): ReadonlyArray { return getOrThrow(key).split(",") } + +function getNumberOrDefault(key: string, def: number): number { + const num = Number(process.env[key]) + return Number.isNaN(num) ? def : num +} diff --git a/src/bank/sparebank1.ts b/src/bank/sparebank1.ts index 3e64eea..e6beba8 100644 --- a/src/bank/sparebank1.ts +++ b/src/bank/sparebank1.ts @@ -1,4 +1,8 @@ -import { BANK_INITIAL_REFRESH_TOKEN } from "@/../config.ts" +import { + BANK_INITIAL_REFRESH_TOKEN, + TRANSACTION_RELATIVE_FROM_DATE, + TRANSACTION_RELATIVE_TO_DATE, +} from "@/../config.ts" import logger from "@/logger.ts" import dayjs from "dayjs" import { Database } from "better-sqlite3" @@ -66,7 +70,7 @@ export class Sparebank1Impl implements Bank { private async getRefreshToken(): Promise { const tokenResponse = fetchToken(this.db, "refresh-token") - logger.debug(`Database returned refresh token: '%o'`, tokenResponse) + if (!tokenResponse) { return BANK_INITIAL_REFRESH_TOKEN } else if (this.isValidToken(tokenResponse)) { @@ -95,10 +99,11 @@ export class Sparebank1Impl implements Bank { ...accountKeys: ReadonlyArray ): Promise { const today = dayjs() - const lastDay = today.subtract(1, "day") + const fromDate = today.subtract(TRANSACTION_RELATIVE_FROM_DATE, "days") + const toDate = today.subtract(TRANSACTION_RELATIVE_TO_DATE, "days") return await Api.transactions(await this.getAccessToken(), accountKeys, { - fromDate: lastDay, - toDate: today, + fromDate, + toDate, }) } } diff --git a/src/main.ts b/src/main.ts index a151190..aab35d1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -23,6 +23,8 @@ import { CronJob } from "cron" // TODO move tsx to devDependency. Requires ts support for Node with support for @ alias // TODO global exception handler, log and graceful shutdown // TODO verbatimSyntax in tsconfig, conflicts with jest +// TODO multi module project. Main | DAL | Sparebank1 impl +// TODO store last fetched date in db, and refetch from that date, if app has been offline for some time export async function daily(actual: Actual, bank: Bank): Promise { // Fetch transactions from the bank diff --git a/src/mappings.ts b/src/mappings.ts index 5cf3def..b5eafde 100644 --- a/src/mappings.ts +++ b/src/mappings.ts @@ -17,7 +17,7 @@ export function bankTransactionIntoActualTransaction( amount: transaction.amount * 100, date: toISODateString(dayjs(transaction.date)), payee_name: transaction.cleanedDescription, - // TODO if not cleared, rerun later + // TODO if not cleared or nonUniqueId is 0, rerun later cleared: transaction.bookingStatus === "BOOKED", } }