Relative from and to date in config, changed default to 3 days
All checks were successful
Deploy application / deploy (push) Successful in 7s
All checks were successful
Deploy application / deploy (push) Successful in 7s
This commit is contained in:
parent
752bdbceb4
commit
18a0fbfac8
@ -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
|
||||
|
15
config.ts
15
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<string> {
|
||||
return getOrThrow(key).split(",")
|
||||
}
|
||||
|
||||
function getNumberOrDefault(key: string, def: number): number {
|
||||
const num = Number(process.env[key])
|
||||
return Number.isNaN(num) ? def : num
|
||||
}
|
||||
|
@ -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<string> {
|
||||
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<string>
|
||||
): Promise<TransactionResponse> {
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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<void> {
|
||||
// Fetch transactions from the bank
|
||||
|
@ -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",
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user