🌟 Refactor

- Moved mappings into Sb1 impl
- Moved Actual types to @common
- Moved createDirIfMissing to respective functions
- Refactored main into multiple functions
- Moved create db into Sb1impl and close
- Log requests on info
This commit is contained in:
2025-02-13 21:07:30 +01:00
parent 06cc89f762
commit 4367c24fb0
13 changed files with 178 additions and 118 deletions

View File

@ -5,38 +5,20 @@ import {
ACTUAL_SERVER_URL,
ACTUAL_SYNC_ID,
} from "@/config.ts"
import type { TransactionEntity } from "@actual-app/api/@types/loot-core/types/models"
import { type UUID } from "node:crypto"
import logger from "@common/logger.ts"
export interface Actual {
importTransactions: (
accountId: UUID,
transactions: Iterable<ActualTransaction>,
) => Promise<ImportTransactionsResponse>
shutdown: () => Promise<void>
}
export interface ActualTransaction extends TransactionEntity {
account: UUID
payee_name?: string
}
export interface Message {
message: string
}
export interface ImportTransactionsResponse {
errors?: Message[]
added: number
updated: number
}
import type { UUID } from "node:crypto"
import type {
Actual,
ActualTransaction,
ImportTransactionsResponse,
} from "@common/types.ts"
import { createDirIfMissing } from "@/fs.ts"
export class ActualImpl implements Actual {
private constructor() {}
static async init(): Promise<Actual> {
createDirIfMissing(ACTUAL_DATA_DIR)
await actual.init({
// Budget data will be cached locally here, in subdirectories for each file.
dataDir: ACTUAL_DATA_DIR,
@ -73,6 +55,7 @@ export class ActualImpl implements Actual {
}
async shutdown(): Promise<void> {
logger.info(`Shutting down ActualBudget API for ${ACTUAL_SERVER_URL}`)
return actual.shutdown()
}

View File

@ -1,89 +0,0 @@
import Database from "better-sqlite3"
import dayjs, { type Dayjs } from "dayjs"
import type { OAuthTokenResponse } from "@sb1/types.ts"
export type TokenResponse = {
key: TokenKey
token: string
expires_at: Dayjs
}
type TokenResponseRaw = {
[K in keyof TokenResponse]: K extends "expires_at" ? string : TokenResponse[K]
}
export type TokenKey = "access-token" | "refresh-token"
export function createDb(filepath: string) {
const db = new Database(filepath)
db.pragma("journal_mode = WAL")
db.exec(
"CREATE TABLE IF NOT EXISTS tokens ('key' VARCHAR PRIMARY KEY, token VARCHAR NOT NULL, expires_at DATETIME NOT NULL)",
)
return db
}
export function insertTokens(
db: Database.Database,
oAuthToken: OAuthTokenResponse,
): void {
insertAccessToken(db, oAuthToken.access_token, oAuthToken.expires_in)
insertRefreshToken(
db,
oAuthToken.refresh_token,
oAuthToken.refresh_token_absolute_expires_in,
)
}
function insertAccessToken(
db: Database.Database,
accessToken: string,
expiresIn: number,
) {
insert(db, "access-token", accessToken, expiresIn)
}
function insertRefreshToken(
db: Database.Database,
refreshToken: string,
expiresIn: number,
): void {
insert(db, "refresh-token", refreshToken, expiresIn)
}
function insert(
db: Database.Database,
key: TokenKey,
token: string,
expiresIn: number,
): void {
db.prepare("INSERT OR REPLACE INTO tokens VALUES (?, ?, ?)").run(
key,
token,
dayjs().add(expiresIn, "seconds").toISOString(),
)
}
export function fetchToken(
db: Database.Database,
tokenKey: TokenKey,
): TokenResponse | null {
const response = db
.prepare("SELECT * FROM tokens WHERE key = ?")
.get(tokenKey) as TokenResponseRaw | null
return (
response && {
...response,
expires_at: dayjs(response.expires_at),
}
)
}
export function clearTokens(db: Database.Database): void {
db.prepare("DELETE FROM tokens WHERE key in ( ?, ? )").run([
"access-token",
"refresh-token",
] as TokenKey[])
}

View File

@ -1,97 +0,0 @@
import {
BANK_INITIAL_REFRESH_TOKEN,
BANK_OAUTH_CLIENT_ID,
BANK_OAUTH_CLIENT_SECRET,
} from "@/config.ts"
import logger from "@common/logger.ts"
import dayjs, { type Dayjs } from "dayjs"
import type { Database } from "better-sqlite3"
import {
clearTokens,
fetchToken,
insertTokens,
type TokenResponse,
} from "@/bank/db/queries.ts"
import * as Oauth from "@sb1/oauth.ts"
import * as Transactions from "@sb1/transactions.ts"
import type { ActualTransaction } from "@/actual.ts"
import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
import type { OAuthTokenResponse } from "@sb1/types.ts"
export interface Bank {
fetchTransactions: (
interval: Interval,
...accountKeys: ReadonlyArray<string>
) => Promise<ReadonlyArray<ActualTransaction>>
}
export interface Interval {
fromDate: Dayjs
toDate: Dayjs
}
export class Sparebank1Impl implements Bank {
private readonly db: Database
constructor(db: Database) {
this.db = db
}
async fetchTransactions(
interval: Interval,
...accountKeys: ReadonlyArray<string>
): Promise<ReadonlyArray<ActualTransaction>> {
const response = await Transactions.list(
await this.getAccessToken(),
accountKeys,
interval,
)
const sparebankTransactions = response.transactions
return sparebankTransactions.map(bankTransactionIntoActualTransaction)
}
private async getAccessToken(): Promise<string> {
const accessToken = fetchToken(this.db, "access-token")
if (accessToken && this.isValidToken(accessToken)) {
return accessToken.token
}
const response = await this.fetchNewTokens()
return response.access_token
}
private isValidToken(tokenResponse: TokenResponse): boolean {
// TODO make sure the same timezone is used. Db uses UTC
return dayjs().isBefore(tokenResponse.expires_at)
}
private async getRefreshToken(): Promise<string> {
const tokenResponse = fetchToken(this.db, "refresh-token")
if (!tokenResponse) {
return BANK_INITIAL_REFRESH_TOKEN
} else if (this.isValidToken(tokenResponse)) {
return tokenResponse.token
}
logger.warn("Refresh token expired, deleting tokens from database")
clearTokens(this.db)
throw new Error("Refresh token is expired. Create a new one")
}
private async fetchNewTokens(): Promise<OAuthTokenResponse> {
const refreshToken = await this.getRefreshToken()
const result = await Oauth.refreshToken(
BANK_OAUTH_CLIENT_ID,
BANK_OAUTH_CLIENT_SECRET,
refreshToken,
)
if (result.status === "failure") {
throw new Error(`Failed to fetch refresh token: '${result.data}'`)
}
const oAuthToken = result.data
insertTokens(this.db, oAuthToken)
return oAuthToken
}
}

View File

@ -1,10 +1,6 @@
import * as fs from "node:fs"
import logger from "@common/logger"
export function createDirsIfMissing(...directories: string[]): void {
directories.forEach(createDirIfMissing)
}
export function createDirIfMissing(directory: string): void {
if (!fs.existsSync(directory)) {
logger.info(`Missing '${directory}', creating...`)

View File

@ -1,32 +1,38 @@
import { type Actual, ActualImpl } from "@/actual.ts"
import { ActualImpl } from "@/actual.ts"
import { cronJobDaily } from "@/cron.ts"
import { type Bank, type Interval, Sparebank1Impl } from "@/bank/sparebank1.ts"
import {
ACTUAL_DATA_DIR,
BANK_ACCOUNT_IDS,
DB_DIRECTORY,
DB_FILENAME,
TRANSACTION_RELATIVE_FROM_DATE,
TRANSACTION_RELATIVE_TO_DATE,
} from "@/config.ts"
import logger from "@common/logger.ts"
import type { UUID } from "node:crypto"
import { createDb } from "@/bank/db/queries.ts"
import { CronJob } from "cron"
import { createDirsIfMissing } from "@/fs.ts"
import dayjs from "dayjs"
import type { Actual, Bank, Interval } from "@common/types.ts"
import { Sparebank1Impl } from "@sb1impl/sparebank1.ts"
// TODO move tsx to devDependency. Requires ts support for Node with support for @ alias
// 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
// TODO do not fetch if saturday or sunday
async function main(): Promise<void> {
logger.info("Starting application")
const bank = new Sparebank1Impl()
if (process.env.ONCE) {
return await runOnce(bank)
}
await ActualImpl.testConnection()
await runCronJob(bank)
}
// TODO log the days the transactions are fetched
export async function moveTransactions(
actual: Actual,
bank: Bank,
): Promise<void> {
// Fetch transactions from the bank
const actualTransactions = await bank.fetchTransactions(
relativeInterval(),
...BANK_ACCOUNT_IDS,
@ -61,53 +67,57 @@ function relativeInterval(): Interval {
}
}
async function main(): Promise<void> {
logger.info("Starting application")
async function runOnce(bank: Bank) {
const actual = await ActualImpl.init()
createDirsIfMissing(ACTUAL_DATA_DIR, DB_DIRECTORY)
registerInterrupt(bank)
const databaseFilePath = `${DB_DIRECTORY}/${DB_FILENAME}.sqlite`
const db = createDb(databaseFilePath)
logger.info(`Started Sqlite database at '${databaseFilePath}'`)
const bank = new Sparebank1Impl(db)
process.on("SIGINT", async () => {
logger.info("Caught interrupt signal")
await shutdown()
})
let cronJob: CronJob | undefined
if (process.env.ONCE) {
const actual = await ActualImpl.init()
try {
return await moveTransactions(actual, bank)
} finally {
await actual.shutdown()
await shutdown()
}
} else {
await ActualImpl.testConnection()
try {
return await moveTransactions(actual, bank)
} finally {
await actual.shutdown()
await shutdown(bank)
}
}
async function runCronJob(bank: Bank): Promise<void> {
let actual: Actual | undefined
let cronJob: CronJob | undefined
logger.info("Waiting for CronJob to start")
let actual: Actual | undefined
try {
// TODO move try-catch inside closure?
cronJob = cronJobDaily(async () => {
actual = await ActualImpl.init()
await moveTransactions(actual, bank)
})
registerInterrupt(bank, cronJob)
} catch (exception) {
logger.error(exception, "Caught exception at CronJob, shutting down!")
await shutdown()
await shutdown(bank, cronJob)
} finally {
// TODO shuts down immediatly, move into closure
await actual?.shutdown()
}
async function shutdown(): Promise<void> {
logger.info("Shutting down, Bye!")
db.close()
cronJob?.stop()
}
}
function registerInterrupt(
bank: Bank,
cronJob: CronJob | undefined = undefined,
): void {
process.on("SIGINT", async () => {
logger.info("Caught interrupt signal")
await shutdown(bank, cronJob)
})
}
async function shutdown(
bank: Bank,
cronJob: CronJob | undefined = undefined,
): Promise<void> {
logger.info("Shutting down, Bye!")
await bank.shutdown()
cronJob?.stop()
}
void main()

View File

@ -1,46 +0,0 @@
import type { UUID } from "node:crypto"
import dayjs from "dayjs"
import { type ActualTransaction } from "@/actual.ts"
import { ACTUAL_ACCOUNT_IDS, BANK_ACCOUNT_IDS } from "@/config.ts"
import logger from "@common/logger.ts"
import { toISODateString } from "@common/date.ts"
import type { SB1Transaction } from "@sb1/types.ts"
export function bankTransactionIntoActualTransaction(
transaction: SB1Transaction,
): ActualTransaction {
return {
id: transaction.id,
// Transactions with the same id will be ignored
imported_id: transaction.nonUniqueId,
account: getActualAccountId(transaction),
// The value without decimals
amount: Math.floor(transaction.amount * 100),
date: toISODateString(dayjs(transaction.date)),
payee_name: transaction.cleanedDescription,
// TODO if not cleared or nonUniqueId is 0, rerun later
cleared: isCleared(transaction),
}
}
export function isCleared(transaction: SB1Transaction): boolean {
const id = Number(transaction.nonUniqueId)
return transaction.bookingStatus === "BOOKED" && !Number.isNaN(id) && id > 0
}
function getActualAccountId(transcation: SB1Transaction): UUID {
for (
let i = 0;
i < Math.min(ACTUAL_ACCOUNT_IDS.length, BANK_ACCOUNT_IDS.length);
i++
) {
if (BANK_ACCOUNT_IDS[i] === transcation.accountKey) {
return ACTUAL_ACCOUNT_IDS[i] as UUID
}
}
const error = new Error(
"Failed to find ActualAccountId, length of BANK_ACCOUNT_IDS and ACTUAL_ACCOUNT_IDS must match",
)
logger.error(error)
throw error
}