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"
|
2024-12-01 19:35:45 +01:00
|
|
|
import { ACTUAL_ACCOUNT_IDS, 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"
|
2024-12-26 12:49:54 +01:00
|
|
|
import Database from "better-sqlite3"
|
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>> {
|
2024-12-25 21:06:42 +01:00
|
|
|
return bank.transactionsPastDay(BANK_ACCOUNT_IDS)
|
2024-11-17 22:27:29 +01:00
|
|
|
}
|
2024-11-15 22:55:53 +01:00
|
|
|
|
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")
|
2024-11-17 22:27:29 +01:00
|
|
|
const actual = await ActualImpl.init()
|
2024-12-26 12:49:54 +01:00
|
|
|
const databaseFileName = "default.sqlite"
|
|
|
|
const db = new Database(databaseFileName)
|
2024-12-26 14:08:09 +01:00
|
|
|
db.pragma("journal_mode = WAL")
|
|
|
|
db.exec(
|
|
|
|
"CREATE TABLE IF NOT EXISTS tokens (key VARCHAR PRIMARY KEY, data JSON)",
|
|
|
|
)
|
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
|
|
|
|
2024-12-01 19:35:45 +01:00
|
|
|
cronJobDaily(async () => {
|
2024-12-23 16:47:07 +01:00
|
|
|
logger.info("Running daily job")
|
2024-12-26 14:08:09 +01:00
|
|
|
await daily(actual, new Sparebank1Impl(db))
|
2024-12-23 16:47:07 +01:00
|
|
|
logger.info("Finished daily job")
|
2024-12-01 19:35:45 +01:00
|
|
|
})
|
2024-12-01 20:48:42 +01:00
|
|
|
|
2024-12-23 16:47:07 +01:00
|
|
|
// logger.info("Shutting down")
|
2024-11-17 22:27:29 +01:00
|
|
|
// await actual.shutdown()
|
2024-12-26 12:49:54 +01:00
|
|
|
// db.close()
|
2024-11-15 22:55:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void main()
|