Compare commits

..

1 Commits

Author SHA1 Message Date
f5a9e2fc51 Init bun and flake file 2025-02-05 19:25:42 +01:00
14 changed files with 65 additions and 85 deletions

View File

@ -9,6 +9,8 @@ ACTUAL_DATA_DIR=.cache
BANK_INITIAL_REFRESH_TOKEN=initial-valid-refresh-token BANK_INITIAL_REFRESH_TOKEN=initial-valid-refresh-token
BANK_OAUTH_CLIENT_ID=your-client-id BANK_OAUTH_CLIENT_ID=your-client-id
BANK_OAUTH_CLIENT_SECRET=your-client-secret BANK_OAUTH_CLIENT_SECRET=your-client-secret
BANK_OAUTH_STATE=your-state
BANK_OAUTH_REDIRECT_URI=http://your-redirect-uri.com
BANK_ACCOUNT_IDS=your-account-id1,your-account-id2 BANK_ACCOUNT_IDS=your-account-id1,your-account-id2
# Configuration # Configuration
LOG_LEVEL=info# trace | error | warn | info | debug | trace LOG_LEVEL=info# trace | error | warn | info | debug | trace

View File

@ -15,11 +15,13 @@ jobs:
ACTUAL_SERVER_URL: ${{ secrets.ACTUAL_SERVER_URL }} ACTUAL_SERVER_URL: ${{ secrets.ACTUAL_SERVER_URL }}
ACTUAL_PASSWORD: ${{ secrets.ACTUAL_PASSWORD }} ACTUAL_PASSWORD: ${{ secrets.ACTUAL_PASSWORD }}
ACTUAL_ACCOUNT_IDS: ${{ secrets.ACTUAL_ACCOUNT_IDS }} ACTUAL_ACCOUNT_IDS: ${{ secrets.ACTUAL_ACCOUNT_IDS }}
ACTUAL_DATA_DIR: ${{ vars.ACTUAL_DATA_DIR }} ACTUAL_DATA_DIR: ${{ var.ACTUAL_DATA_DIR }}
# Bank # Bank
BANK_INITIAL_REFRESH_TOKEN: ${{ secrets.BANK_INITIAL_REFRESH_TOKEN }} BANK_INITIAL_REFRESH_TOKEN: ${{ secrets.BANK_INITIAL_REFRESH_TOKEN }}
BANK_OAUTH_CLIENT_ID: ${{ secrets.BANK_OAUTH_CLIENT_ID }} BANK_OAUTH_CLIENT_ID: ${{ secrets.BANK_OAUTH_CLIENT_ID }}
BANK_OAUTH_CLIENT_SECRET: ${{ secrets.BANK_OAUTH_CLIENT_SECRET }} BANK_OAUTH_CLIENT_SECRET: ${{ secrets.BANK_OAUTH_CLIENT_SECRET }}
BANK_OAUTH_STATE: ${{ secrets.BANK_OAUTH_STATE }}
BANK_OAUTH_REDIRECT_URI: ${{ secrets.BANK_OAUTH_REDIRECT_URI }}
BANK_ACCOUNT_IDS: ${{ secrets.BANK_ACCOUNT_IDS }} BANK_ACCOUNT_IDS: ${{ secrets.BANK_ACCOUNT_IDS }}
# Configuration # Configuration
LOG_LEVEL: ${{ vars.LOG_LEVEL }} LOG_LEVEL: ${{ vars.LOG_LEVEL }}

View File

@ -3,6 +3,9 @@ LABEL authors="Martin Berg Alstad"
COPY . . COPY . .
RUN --mount=type=cache,id=npm,target=/store npm install --omit=dev --frozen-lockfile ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile
ENTRYPOINT ["npm", "run", "start-prod"] CMD ["pnpm", "start-prod"]

BIN
bun.lockb Executable file

Binary file not shown.

View File

@ -1,7 +1,7 @@
services: services:
server: server:
container_name: actual_sparebank1_cronjob container_name: actual_sparebank1_cronjob
restart: no restart: unless-stopped
build: build:
context: . context: .
environment: environment:
@ -14,14 +14,13 @@ services:
- BANK_INITIAL_REFRESH_TOKEN - BANK_INITIAL_REFRESH_TOKEN
- BANK_OAUTH_CLIENT_ID - BANK_OAUTH_CLIENT_ID
- BANK_OAUTH_CLIENT_SECRET - BANK_OAUTH_CLIENT_SECRET
- BANK_OAUTH_STATE
- BANK_OAUTH_REDIRECT_URI
- BANK_ACCOUNT_IDS - BANK_ACCOUNT_IDS
- LOG_LEVEL - LOG_LEVEL
- DB_DIRECTORY # Required for Docker Compose - DB_DIRECTORY # Required for Docker Compose
- DB_FILENAME - DB_FILENAME
- TRANSACTION_RELATIVE_FROM_DATE
- TRANSACTION_RELATIVE_TO_DATE
volumes: volumes:
# TODO CACHE directory in volume?
- data:/${DB_DIRECTORY} - data:/${DB_DIRECTORY}
volumes: volumes:

8
flake.lock generated
View File

@ -2,16 +2,16 @@
"nodes": { "nodes": {
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1738277201, "lastModified": 1738680400,
"narHash": "sha256-6L+WXKCw5mqnUIExvqkD99pJQ41xgyCk6z/H9snClwk=", "narHash": "sha256-ooLh+XW8jfa+91F1nhf9OF7qhuA/y1ChLx6lXDNeY5U=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "666e1b3f09c267afd66addebe80fb05a5ef2b554", "rev": "799ba5bffed04ced7067a91798353d360788b30d",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "NixOS", "owner": "NixOS",
"ref": "nixos-24.11", "ref": "nixos-unstable",
"repo": "nixpkgs", "repo": "nixpkgs",
"type": "github" "type": "github"
} }

View File

@ -1,6 +1,6 @@
{ {
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
}; };
outputs = inputs@{ nixpkgs, ... }: outputs = inputs@{ nixpkgs, ... }:
@ -16,10 +16,7 @@
in in
pkgs.mkShell { pkgs.mkShell {
packages = with pkgs; [ packages = with pkgs; [
git bun
nodejs_22
pnpm
nodePackages.prettier
]; ];
shellHook = "fish"; shellHook = "fish";

View File

@ -51,8 +51,6 @@ GET {{bankingBaseUrl}}/accounts
Authorization: Bearer {{ACCESS_TOKEN}} Authorization: Bearer {{ACCESS_TOKEN}}
### Fetch all transactions of specific days (inclusive) ### Fetch all transactions of specific days (inclusive)
GET {{bankingBaseUrl}}/transactions?accountKey={{accountKey1}}&accountKey={{accountKey2}}& GET {{bankingBaseUrl}}/transactions?accountKey={{brukskontoAccountKey}}&fromDate=2025-01-20&toDate=2025-01-22
fromDate=2025-01-20&
toDate=2025-01-24
Authorization: Bearer {{ACCESS_TOKEN}} Authorization: Bearer {{ACCESS_TOKEN}}
Accept: application/vnd.sparebank1.v1+json; charset=utf-8 Accept: application/vnd.sparebank1.v1+json; charset=utf-8

View File

@ -4,6 +4,7 @@
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"preinstall": "npx only-allow pnpm",
"start": "dotenvx run --env-file=.env.local -- node --import=tsx ./src/main.ts | pino-pretty", "start": "dotenvx run --env-file=.env.local -- node --import=tsx ./src/main.ts | pino-pretty",
"start-prod": "node --import=tsx ./src/main.ts", "start-prod": "node --import=tsx ./src/main.ts",
"run-once": "ONCE=true dotenvx run --env-file=.env.local -- node --import=tsx ./src/main.ts | pino-pretty", "run-once": "ONCE=true dotenvx run --env-file=.env.local -- node --import=tsx ./src/main.ts | pino-pretty",
@ -34,7 +35,8 @@
"pino-pretty": "^13.0.0", "pino-pretty": "^13.0.0",
"ts-jest": "^29.2.5", "ts-jest": "^29.2.5",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
"typescript": "^5.7.3" "typescript": "^5.7.3",
"@types/bun": "latest"
}, },
"prettier": { "prettier": {
"semi": false, "semi": false,

View File

@ -12,14 +12,13 @@ import logger from "@/logger.ts"
export interface Actual { export interface Actual {
importTransactions: ( importTransactions: (
accountId: UUID, accountId: UUID,
transactions: Iterable<ActualTransaction>, transactions: ReadonlyArray<ActualTransaction>,
) => Promise<ImportTransactionsResponse> ) => Promise<ImportTransactionsResponse>
shutdown: () => Promise<void> shutdown: () => Promise<void>
} }
export interface ActualTransaction extends TransactionEntity { export interface ActualTransaction extends TransactionEntity {
account: UUID
payee_name?: string payee_name?: string
} }
@ -52,7 +51,7 @@ export class ActualImpl implements Actual {
async importTransactions( async importTransactions(
accountId: UUID, accountId: UUID,
transactions: Iterable<ActualTransaction>, transactions: ReadonlyArray<ActualTransaction>,
): Promise<ImportTransactionsResponse> { ): Promise<ImportTransactionsResponse> {
return actual.importTransactions(accountId, transactions) return actual.importTransactions(accountId, transactions)
} }

View File

@ -27,12 +27,8 @@ export type BookingStatus = "PENDING" | "BOOKED"
export interface Transaction { export interface Transaction {
id: string id: string
nonUniqueId: string nonUniqueId: string
// The Id of the account date: number // Unix time
accountKey: string amount: number // Amount in NOK
// Unix time
date: number
// Amount in NOK
amount: number
cleanedDescription: string cleanedDescription: string
remoteAccountName: string remoteAccountName: string
bookingStatus: BookingStatus bookingStatus: BookingStatus

View File

@ -1,4 +1,4 @@
import { BANK_OAUTH_CLIENT_ID, BANK_OAUTH_CLIENT_SECRET } from "@/../config.ts" import { BANK_OAUTH_CLIENT_ID, BANK_OAUTH_CLIENT_SECRET } from "../../config.ts"
import type { import type {
OAuthTokenResponse, OAuthTokenResponse,
TransactionResponse, TransactionResponse,
@ -6,7 +6,6 @@ import type {
import logger from "@/logger.ts" import logger from "@/logger.ts"
import { type Dayjs } from "dayjs" import { type Dayjs } from "dayjs"
import { toISODateString } from "@/date.ts" import { toISODateString } from "@/date.ts"
import * as querystring from "node:querystring"
const baseUrl = "https://api.sparebank1.no" const baseUrl = "https://api.sparebank1.no"
@ -14,8 +13,13 @@ type Success<T> = { status: "success"; data: T }
type Failure<T> = { status: "failure"; data: T } type Failure<T> = { status: "failure"; data: T }
type Result<OK, Err> = Success<OK> | Failure<Err> type Result<OK, Err> = Success<OK> | Failure<Err>
const success = <T>(data: T): Success<T> => ({ status: "success", data: data }) function success<T>(data: T): Success<T> {
const failure = <T>(data: T): Failure<T> => ({ status: "failure", data: data }) return { status: "success", data: data }
}
function failure<T>(data: T): Failure<T> {
return { status: "failure", data: data }
}
export async function transactions( export async function transactions(
accessToken: string, accessToken: string,
@ -25,15 +29,16 @@ export async function transactions(
toDate: Dayjs toDate: Dayjs
}, },
): Promise<TransactionResponse> { ): Promise<TransactionResponse> {
const queryString = querystring.stringify({ const queries = new URLSearchParams({
accountKey: accountKeys, // TODO allow multiple accountKeys
accountKey: typeof accountKeys === "string" ? accountKeys : accountKeys[0],
...(timePeriod && { ...(timePeriod && {
fromDate: toISODateString(timePeriod.fromDate), fromDate: toISODateString(timePeriod.fromDate),
toDate: toISODateString(timePeriod.toDate), toDate: toISODateString(timePeriod.toDate),
}), }),
}) })
const url = `${baseUrl}/personal/banking/transactions?${queryString}` const url = `${baseUrl}/personal/banking/transactions?${queries}`
logger.debug(`Sending GET request to '${url}'`) logger.debug(`Sending GET request to '${url}'`)
const response = await fetch(url, { const response = await fetch(url, {
headers: { headers: {
@ -53,7 +58,7 @@ export async function transactions(
export async function refreshToken( export async function refreshToken(
refreshToken: string, refreshToken: string,
): Promise<Result<OAuthTokenResponse, string>> { ): Promise<Result<OAuthTokenResponse, string>> {
const queries = querystring.stringify({ const queries = new URLSearchParams({
client_id: BANK_OAUTH_CLIENT_ID, client_id: BANK_OAUTH_CLIENT_ID,
client_secret: BANK_OAUTH_CLIENT_SECRET, client_secret: BANK_OAUTH_CLIENT_SECRET,
refresh_token: refreshToken, refresh_token: refreshToken,

View File

@ -7,6 +7,7 @@ import {
} from "@/bank/sparebank1.ts" } from "@/bank/sparebank1.ts"
import { bankTransactionIntoActualTransaction } from "@/mappings.ts" import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
import { import {
ACTUAL_ACCOUNT_IDS,
ACTUAL_DATA_DIR, ACTUAL_DATA_DIR,
BANK_ACCOUNT_IDS, BANK_ACCOUNT_IDS,
DB_DIRECTORY, DB_DIRECTORY,
@ -20,6 +21,7 @@ import { CronJob } from "cron"
// TODO Transports api for pino https://github.com/pinojs/pino/blob/HEAD/docs/transports.md // TODO Transports api for pino https://github.com/pinojs/pino/blob/HEAD/docs/transports.md
// 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 verbatimSyntax in tsconfig, conflicts with jest // TODO verbatimSyntax in tsconfig, conflicts with jest
// TODO multi module project. Main | DAL | Sparebank1 impl // 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 store last fetched date in db, and refetch from that date, if app has been offline for some time
@ -29,23 +31,22 @@ export async function daily(actual: Actual, bank: Bank): Promise<void> {
const transactions = await fetchTransactionsFromPastDay(bank) const transactions = await fetchTransactionsFromPastDay(bank)
logger.info(`Fetched ${transactions.length} transactions`) logger.info(`Fetched ${transactions.length} transactions`)
// TODO multiple accounts
const accountId = ACTUAL_ACCOUNT_IDS[0] as UUID
const actualTransactions = transactions.map((transaction) => const actualTransactions = transactions.map((transaction) =>
// TODO move to Bank interface? // TODO move to Bank interface?
bankTransactionIntoActualTransaction(transaction), bankTransactionIntoActualTransaction(transaction, accountId),
) )
const transactionsGroup = Object.groupBy( // TODO Import transactions into Actual
// If multiple accounts, loop over them
// Get account ID from mapper
const response = await actual.importTransactions(
accountId,
actualTransactions, actualTransactions,
(transaction) => transaction.account,
) )
logger.info(`ImportTransactionsResponse=${JSON.stringify(response)}`)
const response = await Promise.all(
Object.entries(transactionsGroup).map(([accountId, transactions]) =>
actual.importTransactions(accountId as UUID, transactions || []),
),
)
logger.debug(response, "Finished importing transactions")
} }
async function fetchTransactionsFromPastDay( async function fetchTransactionsFromPastDay(
@ -81,21 +82,14 @@ async function main(): Promise<void> {
let cronJob: CronJob | undefined let cronJob: CronJob | undefined
if (process.env.ONCE) { if (process.env.ONCE) {
try { await daily(actual, bank)
return await daily(actual, bank)
} finally {
await shutdown() await shutdown()
} return
} }
logger.info("Waiting for CRON job to start") logger.info("Waiting for CRON job to start")
// TODO init and shutdown resources when job runs? // TODO init and shutdown resources when job runs?
try {
cronJob = cronJobDaily(async () => await daily(actual, bank)) cronJob = cronJobDaily(async () => await daily(actual, bank))
} catch (exception) {
logger.error(exception, "Caught exception at daily job, shutting down!")
await shutdown()
}
async function shutdown(): Promise<void> { async function shutdown(): Promise<void> {
logger.info("Shutting down, Bye!") logger.info("Shutting down, Bye!")

View File

@ -3,44 +3,27 @@ import type { UUID } from "node:crypto"
import dayjs from "dayjs" import dayjs from "dayjs"
import { toISODateString } from "@/date.ts" import { toISODateString } from "@/date.ts"
import { type ActualTransaction } from "@/actual.ts" import { type ActualTransaction } from "@/actual.ts"
import { ACTUAL_ACCOUNT_IDS, BANK_ACCOUNT_IDS } from "../config.ts"
import logger from "@/logger.ts"
export function bankTransactionIntoActualTransaction( export function bankTransactionIntoActualTransaction(
transaction: Transaction, transaction: Transaction,
accountId: UUID,
): ActualTransaction { ): ActualTransaction {
return { return {
id: transaction.id, id: transaction.id,
// Transactions with the same id will be ignored // Transactions with the same id will be ignored
imported_id: transaction.nonUniqueId, imported_id: transaction.nonUniqueId,
account: getActualAccountId(transaction), account: accountId,
// The value without decimals // The value without decimals
amount: Math.floor(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 or nonUniqueId is 0, rerun later // TODO if not cleared or nonUniqueId is 0, rerun later
cleared: isCleared(transaction), cleared: transaction.bookingStatus === "BOOKED",
} }
} }
export function isCleared(transaction: Transaction): boolean { // TODO take the account from the bank and match it to the actual account
const id = Number(transaction.nonUniqueId) // Use ENV
return transaction.bookingStatus === "BOOKED" && !Number.isNaN(id) && id > 0 export function bankAccountIntoActualAccount(account: string): string {
} throw new Error("Not implemented")
function getActualAccountId(transcation: Transaction): 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
} }