Compare commits

..

1 Commits

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

View File

@ -4,7 +4,7 @@ httpRequests
.env.*
*.sqlite
jest.config.ts
**/node_modules
node_modules
.git
.gitignore
*.md

View File

@ -9,6 +9,8 @@ ACTUAL_DATA_DIR=.cache
BANK_INITIAL_REFRESH_TOKEN=initial-valid-refresh-token
BANK_OAUTH_CLIENT_ID=your-client-id
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
# Configuration
LOG_LEVEL=info# trace | error | warn | info | debug | trace

View File

@ -15,11 +15,13 @@ jobs:
ACTUAL_SERVER_URL: ${{ secrets.ACTUAL_SERVER_URL }}
ACTUAL_PASSWORD: ${{ secrets.ACTUAL_PASSWORD }}
ACTUAL_ACCOUNT_IDS: ${{ secrets.ACTUAL_ACCOUNT_IDS }}
ACTUAL_DATA_DIR: ${{ vars.ACTUAL_DATA_DIR }}
ACTUAL_DATA_DIR: ${{ var.ACTUAL_DATA_DIR }}
# Bank
BANK_INITIAL_REFRESH_TOKEN: ${{ secrets.BANK_INITIAL_REFRESH_TOKEN }}
BANK_OAUTH_CLIENT_ID: ${{ secrets.BANK_OAUTH_CLIENT_ID }}
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 }}
# Configuration
LOG_LEVEL: ${{ vars.LOG_LEVEL }}

View File

@ -1,14 +1,11 @@
FROM node:22-slim
LABEL authors="Martin Berg Alstad"
COPY . ./app
WORKDIR ./app
COPY . .
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable pnpm
RUN corepack prepare pnpm@9.15.3 --activate
RUN npm i -g corepack@latest
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --recursive --frozen-lockfile
RUN corepack enable
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile
ENTRYPOINT ["pnpm", "start-prod"]
CMD ["pnpm", "start-prod"]

View File

@ -1,8 +1,10 @@
# Sparebank1 ActualBudget Integration
🔧 WIP!
### Setting up the environment
In order to start the application, an `.env.local` file must be present at the root level. The possible and required
In order to start the application, a `.env.local` file must be present at the root level. The possible and required
fields
can be found in the [.env.example](.env.example) file and `config.ts`.

BIN
bun.lockb Executable file

Binary file not shown.

View File

@ -22,7 +22,6 @@ 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")
// TODO default to fetch 1 day
// Relative number of days in the past to start fetching transactions from
export const TRANSACTION_RELATIVE_FROM_DATE = getNumberOrDefault(
"TRANSACTION_RELATIVE_FROM_DATE",

View File

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

8
flake.lock generated
View File

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

View File

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

View File

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

View File

@ -2,11 +2,13 @@
"name": "sparebank1_actual_budget_integration",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "dotenvx run --env-file=.env.local -- node --import=tsx ./src/main.ts | ./packages/common/node_modules/pino-pretty/bin.js",
"preinstall": "npx only-allow pnpm",
"start": "dotenvx run --env-file=.env.local -- node --import=tsx ./src/main.ts | pino-pretty",
"start-prod": "node --import=tsx ./src/main.ts",
"run-once": "ONCE=true dotenvx run --env-file=.env.local -- node --import=tsx ./src/main.ts | ./packages/common/node_modules/pino-pretty/bin.js",
"test": "dotenvx run --env-file=.env.test.local -- node --experimental-vm-modules node_modules/jest/bin/jest.js | ./packages/common/node_modules/pino-pretty/bin.js",
"run-once": "ONCE=true dotenvx run --env-file=.env.local -- node --import=tsx ./src/main.ts | pino-pretty",
"test": "dotenvx run --env-file=.env.test.local -- node --experimental-vm-modules node_modules/jest/bin/jest.js | pino-pretty",
"docker-build": "DB_DIRECTORY=data docker compose --env-file .env.local up -d --build",
"format": "prettier --write \"./**/*.{js,mjs,ts,md,json}\""
},
@ -20,6 +22,7 @@
"cron": "^3.5.0",
"dayjs": "^1.11.13",
"dotenv": "^16.4.7",
"pino": "^9.6.0",
"prettier": "^3.4.2",
"tsx": "^4.19.2"
},
@ -29,9 +32,11 @@
"@types/jest": "^29.5.14",
"@types/node": "^22.10.7",
"jest": "^29.7.0",
"pino-pretty": "^13.0.0",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.7.3"
"typescript": "^5.7.3",
"@types/bun": "latest"
},
"prettier": {
"semi": false,

View File

@ -1,21 +0,0 @@
import pino from "pino"
import { LOG_LEVEL } from "@/config.ts"
/**
* / Returns a logging instance with the default log-level "info"
*/
const logger = pino(
pino.destination({
level: LOG_LEVEL,
}),
)
console.log = function (...args): void {
if (args.length > 1) {
logger.info(args?.slice(1), args?.[0])
} else {
logger.info(args?.[0])
}
}
export default logger

View File

@ -1,12 +0,0 @@
{
"name": "common",
"version": "1.0.0",
"description": "",
"license": "ISC",
"dependencies": {
"pino": "^9.6.0"
},
"devDependencies": {
"pino-pretty": "^13.0.0"
}
}

View File

@ -1,12 +0,0 @@
import type { Failure, Success } from "./types"
export const baseUrl = "https://api.sparebank1.no"
export const success = <T>(data: T): Success<T> => ({
status: "success",
data: data,
})
export const failure = <T>(data: T): Failure<T> => ({
status: "failure",
data: data,
})

View File

@ -1,30 +0,0 @@
import { OAuthTokenResponse, Result } from "./types"
import * as querystring from "node:querystring"
import { baseUrl, failure, success } from "./common"
import logger from "@common/logger"
export async function refreshToken(
clientId: string,
clientSecret: string,
refreshToken: string,
): Promise<Result<OAuthTokenResponse, string>> {
const queries = querystring.stringify({
client_id: clientId,
client_secret: clientSecret,
refresh_token: refreshToken,
grant_type: "refresh_token",
})
const url = `${baseUrl}/oauth/token?${queries}`
logger.debug(`Sending POST request to url: '${url}'`)
const response = await fetch(url, {
method: "post",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
})
logger.debug(`Received response with status '${response.status}'`)
if (!response.ok) {
return failure(await response.text())
}
return success(await response.json())
}

View File

@ -1,6 +0,0 @@
{
"name": "packages",
"version": "1.0.0",
"description": "",
"license": "ISC"
}

View File

@ -1,35 +0,0 @@
import type { Interval, TransactionResponse } from "./types"
import * as querystring from "node:querystring"
import { toISODateString } from "@common/date"
import logger from "@common/logger"
import { baseUrl } from "./common"
export async function list(
accessToken: string,
accountKeys: string | ReadonlyArray<string>,
interval?: Interval,
): Promise<TransactionResponse> {
const queryString = querystring.stringify({
accountKey: accountKeys,
...(interval && {
fromDate: toISODateString(interval.fromDate),
toDate: toISODateString(interval.toDate),
}),
})
const url = `${baseUrl}/personal/banking/transactions?${queryString}`
logger.debug(`Sending GET request to '${url}'`)
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/vnd.sparebank1.v1+json;charset=utf-8",
},
})
logger.debug(`Received response with status '${response.status}'`)
if (response.ok) {
return response.json()
} else {
logger.warn(await response.json())
return { transactions: [] }
}
}

View File

@ -1,43 +0,0 @@
import type { Dayjs } from "dayjs"
export type Success<T> = { status: "success"; data: T }
export type Failure<T> = { status: "failure"; data: T }
export type Result<OK, Err> = Success<OK> | Failure<Err>
export interface Interval {
fromDate: Dayjs
toDate: Dayjs
}
export interface OAuthTokenResponse {
access_token: string
expires_in: number
refresh_token_expires_in: number
refresh_token_absolute_expires_in: number
token_type: "Bearer"
refresh_token: string
}
export type BookingStatus = "PENDING" | "BOOKED"
export type NonUniqueId = "000000000000000000" | `${number}`
export interface SB1Transaction {
id: string
nonUniqueId: NonUniqueId
// The Id of the account
accountKey: string
// Unix time
date: number
// Amount in NOK
amount: number
cleanedDescription: string
remoteAccountName: string
bookingStatus: BookingStatus
[key: string]: string | number | boolean | unknown
}
export interface TransactionResponse {
transactions: ReadonlyArray<SB1Transaction>
}

18
pnpm-lock.yaml generated
View File

@ -26,6 +26,9 @@ importers:
dotenv:
specifier: ^16.4.7
version: 16.4.7
pino:
specifier: ^9.6.0
version: 9.6.0
prettier:
specifier: ^3.4.2
version: 3.4.2
@ -48,6 +51,9 @@ importers:
jest:
specifier: ^29.7.0
version: 29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3))
pino-pretty:
specifier: ^13.0.0
version: 13.0.0
ts-jest:
specifier: ^29.2.5
version: 29.2.5(@babel/core@7.26.0)(@jest/transform@29.7.0)(@jest/types@29.6.3)(babel-jest@29.7.0(@babel/core@7.26.0))(jest@29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3)))(typescript@5.7.3)
@ -58,18 +64,6 @@ importers:
specifier: ^5.7.3
version: 5.7.3
packages/common:
dependencies:
pino:
specifier: ^9.6.0
version: 9.6.0
devDependencies:
pino-pretty:
specifier: ^13.0.0
version: 13.0.0
packages/sparebank1Api: {}
packages:
'@actual-app/api@25.1.0':

View File

@ -1,2 +0,0 @@
packages:
- "packages/*"

View File

@ -4,22 +4,21 @@ import {
ACTUAL_PASSWORD,
ACTUAL_SERVER_URL,
ACTUAL_SYNC_ID,
} from "@/config.ts"
} 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"
import logger from "@/logger.ts"
export interface Actual {
importTransactions: (
accountId: UUID,
transactions: Iterable<ActualTransaction>,
transactions: ReadonlyArray<ActualTransaction>,
) => Promise<ImportTransactionsResponse>
shutdown: () => Promise<void>
}
export interface ActualTransaction extends TransactionEntity {
account: UUID
payee_name?: string
}
@ -52,7 +51,7 @@ export class ActualImpl implements Actual {
async importTransactions(
accountId: UUID,
transactions: Iterable<ActualTransaction>,
transactions: ReadonlyArray<ActualTransaction>,
): Promise<ImportTransactionsResponse> {
return actual.importTransactions(accountId, transactions)
}

View File

@ -1,7 +1,7 @@
import Database from "better-sqlite3"
import { type OAuthTokenResponse } from "@/bank/sparebank1.ts"
import dayjs, { type Dayjs } from "dayjs"
import type { OAuthTokenResponse } from "@sb1/types.ts"
export type TokenResponse = {
key: TokenKey
@ -9,8 +9,10 @@ export type TokenResponse = {
expires_at: Dayjs
}
type TokenResponseRaw = {
[K in keyof TokenResponse]: K extends "expires_at" ? string : TokenResponse[K]
export type TokenResponseRaw = {
key: TokenResponse["key"]
token: TokenResponse["token"]
expires_at: string
}
export type TokenKey = "access-token" | "refresh-token"
@ -48,7 +50,7 @@ function insertRefreshToken(
db: Database.Database,
refreshToken: string,
expiresIn: number,
): void {
) {
insert(db, "refresh-token", refreshToken, expiresIn)
}
@ -57,7 +59,7 @@ function insert(
key: TokenKey,
token: string,
expiresIn: number,
): void {
) {
db.prepare("INSERT OR REPLACE INTO tokens VALUES (?, ?, ?)").run(
key,
token,
@ -80,10 +82,3 @@ export function fetchToken(
}
)
}
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,33 +1,49 @@
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"
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"
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"
import * as Api from "./sparebank1Api.ts"
export interface Bank {
fetchTransactions: (
interval: Interval,
...accountKeys: ReadonlyArray<string>
) => Promise<ReadonlyArray<ActualTransaction>>
export interface OAuthTokenResponse {
access_token: string
expires_in: number
refresh_token_expires_in: number
refresh_token_absolute_expires_in: number
token_type: "Bearer"
refresh_token: string
}
export interface Interval {
fromDate: Dayjs
toDate: Dayjs
export type BookingStatus = "PENDING" | "BOOKED"
export interface Transaction {
id: string
nonUniqueId: string
date: number // Unix time
amount: number // Amount in NOK
cleanedDescription: string
remoteAccountName: string
bookingStatus: BookingStatus
[key: string]: string | number | boolean | unknown
}
export interface TransactionResponse {
transactions: ReadonlyArray<Transaction>
}
export interface Bank {
transactionsPastDay: (
...accountKeys: ReadonlyArray<string>
) => Promise<TransactionResponse>
}
export class Sparebank1Impl implements Bank {
@ -37,19 +53,6 @@ export class Sparebank1Impl implements Bank {
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")
@ -73,25 +76,34 @@ export class Sparebank1Impl implements Bank {
} else if (this.isValidToken(tokenResponse)) {
return tokenResponse.token
}
logger.warn("Refresh token expired, deleting tokens from database")
clearTokens(this.db)
// TODO clear database, if refresh token is invalid, will cause Exceptions on each call
throw new Error("Refresh token is expired. Create a new one")
}
private async fetchNewTokens(): Promise<OAuthTokenResponse> {
async fetchNewTokens(): Promise<OAuthTokenResponse> {
const refreshToken = await this.getRefreshToken()
const result = await Oauth.refreshToken(
BANK_OAUTH_CLIENT_ID,
BANK_OAUTH_CLIENT_SECRET,
refreshToken,
)
const result = await Api.refreshToken(refreshToken)
if (result.status === "failure") {
throw new Error(`Failed to fetch refresh token: '${result.data}'`)
throw logger.error({
err: new Error(`Failed to fetch refresh token: '${result.data}'`),
})
}
const oAuthToken = result.data
insertTokens(this.db, oAuthToken)
return oAuthToken
}
async transactionsPastDay(
...accountKeys: ReadonlyArray<string>
): Promise<TransactionResponse> {
const today = dayjs()
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,
toDate,
})
}
}

80
src/bank/sparebank1Api.ts Normal file
View File

@ -0,0 +1,80 @@
import { BANK_OAUTH_CLIENT_ID, BANK_OAUTH_CLIENT_SECRET } from "../../config.ts"
import type {
OAuthTokenResponse,
TransactionResponse,
} from "@/bank/sparebank1.ts"
import logger from "@/logger.ts"
import { type Dayjs } from "dayjs"
import { toISODateString } from "@/date.ts"
const baseUrl = "https://api.sparebank1.no"
type Success<T> = { status: "success"; data: T }
type Failure<T> = { status: "failure"; data: T }
type Result<OK, Err> = Success<OK> | Failure<Err>
function success<T>(data: T): Success<T> {
return { status: "success", data: data }
}
function failure<T>(data: T): Failure<T> {
return { status: "failure", data: data }
}
export async function transactions(
accessToken: string,
accountKeys: string | ReadonlyArray<string>,
timePeriod?: {
fromDate: Dayjs
toDate: Dayjs
},
): Promise<TransactionResponse> {
const queries = new URLSearchParams({
// TODO allow multiple accountKeys
accountKey: typeof accountKeys === "string" ? accountKeys : accountKeys[0],
...(timePeriod && {
fromDate: toISODateString(timePeriod.fromDate),
toDate: toISODateString(timePeriod.toDate),
}),
})
const url = `${baseUrl}/personal/banking/transactions?${queries}`
logger.debug(`Sending GET request to '${url}'`)
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/vnd.sparebank1.v1+json;charset=utf-8",
},
})
logger.debug(`Received response with status '${response.status}'`)
if (response.ok) {
return response.json()
} else {
logger.warn(await response.json())
return { transactions: [] }
}
}
export async function refreshToken(
refreshToken: string,
): Promise<Result<OAuthTokenResponse, string>> {
const queries = new URLSearchParams({
client_id: BANK_OAUTH_CLIENT_ID,
client_secret: BANK_OAUTH_CLIENT_SECRET,
refresh_token: refreshToken,
grant_type: "refresh_token",
})
const url = `${baseUrl}/oauth/token?${queries}`
logger.debug(`Sending POST request to url: '${url}'`)
const response = await fetch(url, {
method: "post",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
})
logger.debug(`Received response with status '${response.status}'`)
if (!response.ok) {
return failure(await response.text())
}
return success(await response.json())
}

View File

@ -1,5 +1,5 @@
import { CronJob } from "cron"
import logger from "@common/logger.ts"
import logger from "@/logger.ts"
/**
* Run a function every day at 1 AM, Oslo time.

View File

@ -1,13 +0,0 @@
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...`)
fs.mkdirSync(directory, { recursive: true })
}
}

9
src/logger.ts Normal file
View File

@ -0,0 +1,9 @@
import pino from "pino"
import { LOG_LEVEL } from "../config.ts"
/**
* / Returns a logging instance with the default log-level "info"
*/
export default pino({
level: LOG_LEVEL,
})

View File

@ -1,67 +1,73 @@
import { type Actual, ActualImpl } from "@/actual.ts"
import { cronJobDaily } from "@/cron.ts"
import { type Bank, type Interval, Sparebank1Impl } from "@/bank/sparebank1.ts"
import {
type Bank,
Sparebank1Impl,
type Transaction,
} from "@/bank/sparebank1.ts"
import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
import {
ACTUAL_ACCOUNT_IDS,
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"
} from "../config.ts"
import logger from "@/logger.ts"
import type { UUID } from "node:crypto"
import { createDb } from "@/bank/db/queries.ts"
import * as fs from "node:fs"
import { CronJob } from "cron"
import { createDirsIfMissing } from "@/fs.ts"
import dayjs from "dayjs"
// 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 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
// TODO do not fetch if saturday or sunday
export async function daily(actual: Actual, bank: Bank): Promise<void> {
// Fetch transactions from the bank
const actualTransactions = await bank.fetchTransactions(
relativeInterval(),
...BANK_ACCOUNT_IDS,
)
logger.info(`Fetched ${actualTransactions.length} transactions`)
const transactions = await fetchTransactionsFromPastDay(bank)
logger.info(`Fetched ${transactions.length} transactions`)
const transactionsByAccount = Object.groupBy(
// TODO multiple accounts
const accountId = ACTUAL_ACCOUNT_IDS[0] as UUID
const actualTransactions = transactions.map((transaction) =>
// TODO move to Bank interface?
bankTransactionIntoActualTransaction(transaction, accountId),
)
// TODO Import transactions into Actual
// If multiple accounts, loop over them
// Get account ID from mapper
const response = await actual.importTransactions(
accountId,
actualTransactions,
(transaction) => transaction.account,
)
const responses = await Promise.all(
Object.entries(transactionsByAccount).map(([accountId, transactions]) =>
actual.importTransactions(accountId as UUID, transactions || []),
),
)
logger.debug(
responses.map((response) => ({
added: response.added,
updated: response.updated,
})),
"Finished importing transactions",
)
logger.info(`ImportTransactionsResponse=${JSON.stringify(response)}`)
}
function relativeInterval(): Interval {
const today = dayjs()
return {
fromDate: today.subtract(TRANSACTION_RELATIVE_FROM_DATE, "days"),
toDate: today.subtract(TRANSACTION_RELATIVE_TO_DATE, "days"),
async function fetchTransactionsFromPastDay(
bank: Bank,
): Promise<ReadonlyArray<Transaction>> {
const response = await bank.transactionsPastDay(...BANK_ACCOUNT_IDS)
return response.transactions
}
function createDirIfMissing(directory: string): void {
if (!fs.existsSync(directory)) {
logger.info(`Missing '${directory}', creating...`)
fs.mkdirSync(directory, { recursive: true })
}
}
async function main(): Promise<void> {
logger.info("Starting application")
createDirsIfMissing(ACTUAL_DATA_DIR, DB_DIRECTORY)
createDirIfMissing(ACTUAL_DATA_DIR)
createDirIfMissing(DB_DIRECTORY)
const actual = await ActualImpl.init()
const databaseFilePath = `${DB_DIRECTORY}/${DB_FILENAME}.sqlite`
@ -76,21 +82,14 @@ async function main(): Promise<void> {
let cronJob: CronJob | undefined
if (process.env.ONCE) {
try {
return await daily(actual, bank)
} finally {
await daily(actual, bank)
await shutdown()
}
return
}
logger.info("Waiting for CRON job to start")
// TODO init and shutdown resources when job runs?
try {
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> {
logger.info("Shutting down, Bye!")

View File

@ -1,46 +1,29 @@
import type { Transaction } from "@/bank/sparebank1.ts"
import type { UUID } from "node:crypto"
import dayjs from "dayjs"
import { toISODateString } from "@/date.ts"
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,
transaction: Transaction,
accountId: UUID,
): ActualTransaction {
return {
id: transaction.id,
// Transactions with the same id will be ignored
imported_id: transaction.nonUniqueId,
account: getActualAccountId(transaction),
account: accountId,
// The value without decimals
amount: Math.floor(transaction.amount * 100),
amount: 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),
cleared: transaction.bookingStatus === "BOOKED",
}
}
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
// TODO take the account from the bank and match it to the actual account
// Use ENV
export function bankAccountIntoActualAccount(account: string): string {
throw new Error("Not implemented")
}

View File

@ -1,22 +1,22 @@
import type { Bank, Interval } from "@/bank/sparebank1.ts"
import type {
Bank,
BookingStatus,
TransactionResponse,
} from "@/bank/sparebank1.ts"
import dayjs from "dayjs"
import type { ActualTransaction } from "@/actual.ts"
import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
import type { BookingStatus, SB1Transaction } from "@sb1/types.ts"
export class BankStub implements Bank {
async fetchTransactions(
_interval: Interval,
async transactionsPastDay(
_accountIds: ReadonlyArray<string> | string,
): Promise<ReadonlyArray<ActualTransaction>> {
): Promise<TransactionResponse> {
const someFields = {
date: dayjs("2019-08-20").unix(),
cleanedDescription: "Test transaction",
remoteAccountName: "Test account",
bookingStatus: "BOOKED" as BookingStatus,
accountKey: "1",
}
const bankTransactions: ReadonlyArray<SB1Transaction> = [
return {
transactions: [
{
id: "1",
nonUniqueId: "1",
@ -35,7 +35,7 @@ export class BankStub implements Bank {
amount: -50,
...someFields,
},
]
return bankTransactions.map(bankTransactionIntoActualTransaction)
],
}
}
}

View File

@ -1,5 +1,5 @@
{
"include": ["./src/**/*.ts", "./packages/**/*.ts", "./tests/**/*.ts"],
"include": ["./src/**/*.ts", "./tests/**/*.ts"],
"compilerOptions": {
"target": "esnext",
"module": "ESNext",
@ -9,11 +9,8 @@
"strict": true,
"skipLibCheck": true,
"allowImportingTsExtensions": true,
"noEmit": true,
"paths": {
"@/*": ["./src/*"],
"@common/*": ["./packages/common/*"],
"@sb1/*": ["./packages/sparebank1Api/*"]
"@/*": ["./src/*"]
}
},
"exclude": ["node_modules", "./*.ts", "__test__"]