Relative from and to date in config, changed default to 3 days
All checks were successful
Deploy application / deploy (push) Successful in 7s

This commit is contained in:
Martin Berg Alstad 2025-01-31 16:58:02 +01:00
parent 752bdbceb4
commit 18a0fbfac8
Signed by: martials
GPG Key ID: A3824877B269F2E2
5 changed files with 30 additions and 6 deletions

View File

@ -16,3 +16,5 @@ BANK_ACCOUNT_IDS=your-account-id1,your-account-id2
LOG_LEVEL=info# trace | error | warn | info | debug | trace LOG_LEVEL=info# trace | error | warn | info | debug | trace
DB_DIRECTORY=data# Relative path, must not start or end with / DB_DIRECTORY=data# Relative path, must not start or end with /
DB_FILENAME=default DB_FILENAME=default
TRANSACTION_RELATIVE_FROM_DATE=4
TRANSACTION_RELATIVE_TO_DATE=3

View File

@ -22,6 +22,16 @@ export const BANK_ACCOUNT_IDS = getArrayOrThrow("BANK_ACCOUNT_IDS")
export const DB_DIRECTORY = getOrDefault("DB_DIRECTORY", "data") export const DB_DIRECTORY = getOrDefault("DB_DIRECTORY", "data")
export const DB_FILENAME = getOrDefault("DB_FILENAME", "default") export const DB_FILENAME = getOrDefault("DB_FILENAME", "default")
export const LOG_LEVEL = getOrDefault("LOG_LEVEL", "info") 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 // Utility functions
function getOrDefault(key: string, def: string): string { function getOrDefault(key: string, def: string): string {
@ -37,3 +47,8 @@ function getOrThrow(key: string): string {
function getArrayOrThrow(key: string): ReadonlyArray<string> { function getArrayOrThrow(key: string): ReadonlyArray<string> {
return getOrThrow(key).split(",") return getOrThrow(key).split(",")
} }
function getNumberOrDefault(key: string, def: number): number {
const num = Number(process.env[key])
return Number.isNaN(num) ? def : num
}

View File

@ -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 logger from "@/logger.ts"
import dayjs from "dayjs" import dayjs from "dayjs"
import { Database } from "better-sqlite3" import { Database } from "better-sqlite3"
@ -66,7 +70,7 @@ export class Sparebank1Impl implements Bank {
private async getRefreshToken(): Promise<string> { private async getRefreshToken(): Promise<string> {
const tokenResponse = fetchToken(this.db, "refresh-token") const tokenResponse = fetchToken(this.db, "refresh-token")
logger.debug(`Database returned refresh token: '%o'`, tokenResponse)
if (!tokenResponse) { if (!tokenResponse) {
return BANK_INITIAL_REFRESH_TOKEN return BANK_INITIAL_REFRESH_TOKEN
} else if (this.isValidToken(tokenResponse)) { } else if (this.isValidToken(tokenResponse)) {
@ -95,10 +99,11 @@ export class Sparebank1Impl implements Bank {
...accountKeys: ReadonlyArray<string> ...accountKeys: ReadonlyArray<string>
): Promise<TransactionResponse> { ): Promise<TransactionResponse> {
const today = dayjs() 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, { return await Api.transactions(await this.getAccessToken(), accountKeys, {
fromDate: lastDay, fromDate,
toDate: today, toDate,
}) })
} }
} }

View File

@ -23,6 +23,8 @@ import { CronJob } from "cron"
// TODO move tsx to devDependency. Requires ts support for Node with support for @ alias // TODO move tsx to devDependency. Requires ts support for Node with support for @ alias
// TODO global exception handler, log and graceful shutdown // TODO global exception handler, log and graceful shutdown
// TODO verbatimSyntax in tsconfig, conflicts with jest // 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<void> { export async function daily(actual: Actual, bank: Bank): Promise<void> {
// Fetch transactions from the bank // Fetch transactions from the bank

View File

@ -17,7 +17,7 @@ export function bankTransactionIntoActualTransaction(
amount: transaction.amount * 100, amount: transaction.amount * 100,
date: toISODateString(dayjs(transaction.date)), date: toISODateString(dayjs(transaction.date)),
payee_name: transaction.cleanedDescription, payee_name: transaction.cleanedDescription,
// TODO if not cleared, rerun later // TODO if not cleared or nonUniqueId is 0, rerun later
cleared: transaction.bookingStatus === "BOOKED", cleared: transaction.bookingStatus === "BOOKED",
} }
} }