2024-11-17 22:27:29 +01:00
|
|
|
import { type Actual, ActualImpl } from "@/actual.ts"
|
|
|
|
import { cronJobDaily } from "@/cron.ts"
|
2024-12-22 16:06:20 +01:00
|
|
|
import {
|
|
|
|
type Bank,
|
|
|
|
Sparebank1Impl,
|
|
|
|
type Transaction,
|
|
|
|
} from "@/bank/sparebank1.ts"
|
2024-12-01 20:48:42 +01:00
|
|
|
import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
|
2025-01-22 21:00:04 +01:00
|
|
|
import {
|
|
|
|
ACTUAL_ACCOUNT_IDS,
|
|
|
|
ACTUAL_DATA_DIR,
|
|
|
|
BANK_ACCOUNT_IDS,
|
|
|
|
} from "../config.ts"
|
2024-12-25 21:06:42 +01:00
|
|
|
import logger from "@/logger.ts"
|
2024-12-01 19:35:45 +01:00
|
|
|
import type { UUID } from "node:crypto"
|
2025-01-22 21:00:04 +01:00
|
|
|
import { createDb } from "@/bank/db/queries.ts"
|
|
|
|
import * as fs from "node:fs"
|
2024-11-15 22:55:53 +01:00
|
|
|
|
2024-12-01 19:35:45 +01:00
|
|
|
// TODO Transports api for pino https://github.com/pinojs/pino/blob/HEAD/docs/transports.md
|
2024-12-01 20:48:42 +01:00
|
|
|
// TODO create .cache if missing
|
2024-11-15 22:55:53 +01:00
|
|
|
|
2024-12-01 20:48:42 +01:00
|
|
|
export async function daily(actual: Actual, bank: Bank): Promise<void> {
|
2024-12-01 19:35:45 +01:00
|
|
|
// Fetch transactions from the bank
|
|
|
|
const transactions = await fetchTransactionsFromPastDay(bank)
|
2024-12-23 16:47:07 +01:00
|
|
|
logger.info(`Fetched ${transactions.length} transactions`)
|
2024-11-15 22:55:53 +01:00
|
|
|
|
2024-12-01 19:35:45 +01:00
|
|
|
// TODO multiple accounts
|
|
|
|
const accountId = ACTUAL_ACCOUNT_IDS[0] as UUID
|
|
|
|
const actualTransactions = transactions.map((transaction) =>
|
2024-12-01 20:48:42 +01:00
|
|
|
bankTransactionIntoActualTransaction(transaction, accountId),
|
2024-12-01 19:35:45 +01:00
|
|
|
)
|
|
|
|
|
2024-12-25 21:06:42 +01:00
|
|
|
logger.debug(
|
|
|
|
`Mapped ${JSON.stringify(transactions)} to ${JSON.stringify(actualTransactions)} transactions`,
|
|
|
|
)
|
2024-12-23 16:47:07 +01:00
|
|
|
|
2024-12-01 19:35:45 +01:00
|
|
|
// TODO Import transactions into Actual
|
|
|
|
// If multiple accounts, loop over them
|
|
|
|
// Get account ID from mapper
|
2024-12-01 20:48:42 +01:00
|
|
|
|
2024-12-25 21:06:42 +01:00
|
|
|
const response = await actual.importTransactions(
|
|
|
|
accountId,
|
|
|
|
actualTransactions,
|
|
|
|
)
|
2024-12-23 16:47:07 +01:00
|
|
|
logger.info(`ImportTransactionsResponse=${JSON.stringify(response)}`)
|
2024-12-01 19:35:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchTransactionsFromPastDay(
|
|
|
|
bank: Bank,
|
|
|
|
): Promise<ReadonlyArray<Transaction>> {
|
2025-01-25 18:01:47 +01:00
|
|
|
const response = await bank.transactionsPastDay(BANK_ACCOUNT_IDS)
|
|
|
|
return response.transactions
|
2024-11-17 22:27:29 +01:00
|
|
|
}
|
2024-11-15 22:55:53 +01:00
|
|
|
|
2025-01-22 21:00:04 +01:00
|
|
|
function createCacheDirIfMissing(): void {
|
|
|
|
if (!fs.existsSync(ACTUAL_DATA_DIR)) {
|
|
|
|
logger.info(`Missing '${ACTUAL_DATA_DIR}', creating...`)
|
|
|
|
fs.mkdirSync(ACTUAL_DATA_DIR)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-25 18:01:47 +01:00
|
|
|
// TODO add a script to run an immediate job, without cron
|
|
|
|
// TODO catch ^C to stop server
|
2024-11-17 22:27:29 +01:00
|
|
|
async function main(): Promise<void> {
|
2024-12-23 16:47:07 +01:00
|
|
|
logger.info("Starting application")
|
2025-01-22 21:00:04 +01:00
|
|
|
|
|
|
|
createCacheDirIfMissing()
|
|
|
|
|
2024-11-17 22:27:29 +01:00
|
|
|
const actual = await ActualImpl.init()
|
2025-01-25 18:01:47 +01:00
|
|
|
const databaseFileName = "default.sqlite" // TODO move name to env
|
2025-01-22 21:00:04 +01:00
|
|
|
const db = createDb(databaseFileName)
|
2024-12-26 12:49:54 +01:00
|
|
|
logger.info(`Started SQLlite database with filename="${databaseFileName}"`)
|
|
|
|
|
2024-12-23 16:47:07 +01:00
|
|
|
logger.info("Waiting for CRON job to start")
|
2024-12-01 20:48:42 +01:00
|
|
|
|
2025-01-25 18:01:47 +01:00
|
|
|
// cronJobDaily(async () => {
|
|
|
|
logger.info("Running daily job")
|
|
|
|
await daily(actual, new Sparebank1Impl(db))
|
|
|
|
logger.info("Finished daily job")
|
|
|
|
// })
|
2024-12-01 20:48:42 +01:00
|
|
|
|
2025-01-25 18:01:47 +01:00
|
|
|
logger.info("Shutting down")
|
|
|
|
await actual.shutdown()
|
|
|
|
db.close()
|
2024-11-15 22:55:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void main()
|