Compare commits

...

2 Commits

Author SHA1 Message Date
3c6cb193eb
🔪🐛 Fixed pnpm in Dockerfile, moved config to src
All checks were successful
Deploy application / deploy (push) Successful in 10s
2025-02-09 13:02:02 +01:00
c5b1ec20d6
🧹 Moved Sb1 API to separate workspace and created common workspace
- Created common workspace
- Create Sparebank1Api workspace
- Moved logger to common
- Moved SB1 types to types.ts
- Logger will avoid duplicating first line when capturing console.logs
- Updated imports and added type keyword
- Added nonUniqueId type
2025-02-09 12:35:08 +01:00
25 changed files with 216 additions and 160 deletions

View File

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

View File

@ -1,8 +1,14 @@
FROM node:22-slim FROM node:22-slim
LABEL authors="Martin Berg Alstad" LABEL authors="Martin Berg Alstad"
COPY . . COPY . ./app
WORKDIR ./app
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 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
ENTRYPOINT ["npm", "run", "start-prod"] ENTRYPOINT ["pnpm", "start-prod"]

View File

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

View File

@ -2,12 +2,11 @@
"name": "sparebank1_actual_budget_integration", "name": "sparebank1_actual_budget_integration",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"main": "index.js",
"scripts": { "scripts": {
"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 | ./packages/common/node_modules/pino-pretty/bin.js",
"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 | ./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 | pino-pretty", "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",
"docker-build": "DB_DIRECTORY=data docker compose --env-file .env.local up -d --build", "docker-build": "DB_DIRECTORY=data docker compose --env-file .env.local up -d --build",
"format": "prettier --write \"./**/*.{js,mjs,ts,md,json}\"" "format": "prettier --write \"./**/*.{js,mjs,ts,md,json}\""
}, },
@ -21,7 +20,6 @@
"cron": "^3.5.0", "cron": "^3.5.0",
"dayjs": "^1.11.13", "dayjs": "^1.11.13",
"dotenv": "^16.4.7", "dotenv": "^16.4.7",
"pino": "^9.6.0",
"prettier": "^3.4.2", "prettier": "^3.4.2",
"tsx": "^4.19.2" "tsx": "^4.19.2"
}, },
@ -31,7 +29,6 @@
"@types/jest": "^29.5.14", "@types/jest": "^29.5.14",
"@types/node": "^22.10.7", "@types/node": "^22.10.7",
"jest": "^29.7.0", "jest": "^29.7.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"

View File

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

View File

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

View File

@ -0,0 +1,12 @@
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

@ -0,0 +1,30 @@
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

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

View File

@ -0,0 +1,35 @@
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

@ -0,0 +1,43 @@
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,9 +26,6 @@ importers:
dotenv: dotenv:
specifier: ^16.4.7 specifier: ^16.4.7
version: 16.4.7 version: 16.4.7
pino:
specifier: ^9.6.0
version: 9.6.0
prettier: prettier:
specifier: ^3.4.2 specifier: ^3.4.2
version: 3.4.2 version: 3.4.2
@ -51,9 +48,6 @@ importers:
jest: jest:
specifier: ^29.7.0 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)) 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: ts-jest:
specifier: ^29.2.5 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) 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)
@ -64,6 +58,18 @@ importers:
specifier: ^5.7.3 specifier: ^5.7.3
version: 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: packages:
'@actual-app/api@25.1.0': '@actual-app/api@25.1.0':

2
pnpm-workspace.yaml Normal file
View File

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

View File

@ -4,10 +4,10 @@ import {
ACTUAL_PASSWORD, ACTUAL_PASSWORD,
ACTUAL_SERVER_URL, ACTUAL_SERVER_URL,
ACTUAL_SYNC_ID, ACTUAL_SYNC_ID,
} from "../config.ts" } from "@/config.ts"
import type { TransactionEntity } from "@actual-app/api/@types/loot-core/types/models" import type { TransactionEntity } from "@actual-app/api/@types/loot-core/types/models"
import { type UUID } from "node:crypto" import { type UUID } from "node:crypto"
import logger from "@/logger.ts" import logger from "@common/logger.ts"
export interface Actual { export interface Actual {
importTransactions: ( importTransactions: (

View File

@ -1,7 +1,7 @@
import Database from "better-sqlite3" import Database from "better-sqlite3"
import { type OAuthTokenResponse } from "@/bank/sparebank1.ts"
import dayjs, { type Dayjs } from "dayjs" import dayjs, { type Dayjs } from "dayjs"
import type { OAuthTokenResponse } from "@sb1/types.ts"
export type TokenResponse = { export type TokenResponse = {
key: TokenKey key: TokenKey
@ -9,10 +9,8 @@ export type TokenResponse = {
expires_at: Dayjs expires_at: Dayjs
} }
export type TokenResponseRaw = { type TokenResponseRaw = {
key: TokenResponse["key"] [K in keyof TokenResponse]: K extends "expires_at" ? string : TokenResponse[K]
token: TokenResponse["token"]
expires_at: string
} }
export type TokenKey = "access-token" | "refresh-token" export type TokenKey = "access-token" | "refresh-token"

View File

@ -1,47 +1,22 @@
import { BANK_INITIAL_REFRESH_TOKEN } from "@/../config.ts" import {
import logger from "@/logger.ts" BANK_INITIAL_REFRESH_TOKEN,
import dayjs, { Dayjs } from "dayjs" BANK_OAUTH_CLIENT_ID,
import { Database } from "better-sqlite3" 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 { import {
clearTokens, clearTokens,
fetchToken, fetchToken,
insertTokens, insertTokens,
type TokenResponse, type TokenResponse,
} from "@/bank/db/queries.ts" } from "@/bank/db/queries.ts"
import * as Api from "./sparebank1Api.ts" import * as Oauth from "@sb1/oauth.ts"
import { ActualTransaction } from "@/actual.ts" import * as Transactions from "@sb1/transactions.ts"
import type { ActualTransaction } from "@/actual.ts"
import { bankTransactionIntoActualTransaction } from "@/mappings.ts" import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
import type { OAuthTokenResponse } from "@sb1/types.ts"
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 interface Transaction {
id: string
nonUniqueId: string
// 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<Transaction>
}
export interface Bank { export interface Bank {
fetchTransactions: ( fetchTransactions: (
@ -66,7 +41,7 @@ export class Sparebank1Impl implements Bank {
interval: Interval, interval: Interval,
...accountKeys: ReadonlyArray<string> ...accountKeys: ReadonlyArray<string>
): Promise<ReadonlyArray<ActualTransaction>> { ): Promise<ReadonlyArray<ActualTransaction>> {
const response = await Api.transactions( const response = await Transactions.list(
await this.getAccessToken(), await this.getAccessToken(),
accountKeys, accountKeys,
interval, interval,
@ -105,7 +80,11 @@ export class Sparebank1Impl implements Bank {
private async fetchNewTokens(): Promise<OAuthTokenResponse> { private async fetchNewTokens(): Promise<OAuthTokenResponse> {
const refreshToken = await this.getRefreshToken() const refreshToken = await this.getRefreshToken()
const result = await Api.refreshToken(refreshToken) const result = await Oauth.refreshToken(
BANK_OAUTH_CLIENT_ID,
BANK_OAUTH_CLIENT_SECRET,
refreshToken,
)
if (result.status === "failure") { if (result.status === "failure") {
throw new Error(`Failed to fetch refresh token: '${result.data}'`) throw new Error(`Failed to fetch refresh token: '${result.data}'`)

View File

@ -1,72 +0,0 @@
import { BANK_OAUTH_CLIENT_ID, BANK_OAUTH_CLIENT_SECRET } from "@/../config.ts"
import type {
Interval,
OAuthTokenResponse,
TransactionResponse,
} from "@/bank/sparebank1.ts"
import logger from "@/logger.ts"
import { toISODateString } from "@/date.ts"
import * as querystring from "node:querystring"
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>
const success = <T>(data: T): Success<T> => ({ status: "success", data: data })
const failure = <T>(data: T): Failure<T> => ({ status: "failure", data: data })
export async function transactions(
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: [] }
}
}
export async function refreshToken(
refreshToken: string,
): Promise<Result<OAuthTokenResponse, string>> {
const queries = querystring.stringify({
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

@ -22,6 +22,7 @@ export const BANK_ACCOUNT_IDS = getArrayOrThrow("BANK_ACCOUNT_IDS")
export const DB_DIRECTORY = getOrDefault("DB_DIRECTORY", "data") export const DB_DIRECTORY = getOrDefault("DB_DIRECTORY", "data")
export const DB_FILENAME = getOrDefault("DB_FILENAME", "default") export const DB_FILENAME = getOrDefault("DB_FILENAME", "default")
export const LOG_LEVEL = getOrDefault("LOG_LEVEL", "info") 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 // Relative number of days in the past to start fetching transactions from
export const TRANSACTION_RELATIVE_FROM_DATE = getNumberOrDefault( export const TRANSACTION_RELATIVE_FROM_DATE = getNumberOrDefault(
"TRANSACTION_RELATIVE_FROM_DATE", "TRANSACTION_RELATIVE_FROM_DATE",

View File

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

View File

@ -1,5 +1,5 @@
import * as fs from "node:fs" import * as fs from "node:fs"
import logger from "./logger" import logger from "@common/logger"
export function createDirsIfMissing(...directories: string[]): void { export function createDirsIfMissing(...directories: string[]): void {
directories.forEach(createDirIfMissing) directories.forEach(createDirIfMissing)

View File

@ -8,8 +8,8 @@ import {
DB_FILENAME, DB_FILENAME,
TRANSACTION_RELATIVE_FROM_DATE, TRANSACTION_RELATIVE_FROM_DATE,
TRANSACTION_RELATIVE_TO_DATE, TRANSACTION_RELATIVE_TO_DATE,
} from "../config.ts" } from "@/config.ts"
import logger from "@/logger.ts" import logger from "@common/logger.ts"
import type { UUID } from "node:crypto" import type { UUID } from "node:crypto"
import { createDb } from "@/bank/db/queries.ts" import { createDb } from "@/bank/db/queries.ts"
import { CronJob } from "cron" import { CronJob } from "cron"

View File

@ -1,13 +1,13 @@
import type { Transaction } from "@/bank/sparebank1.ts"
import type { UUID } from "node:crypto" import type { UUID } from "node:crypto"
import dayjs from "dayjs" import dayjs from "dayjs"
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 { ACTUAL_ACCOUNT_IDS, BANK_ACCOUNT_IDS } from "@/config.ts"
import logger from "@/logger.ts" import logger from "@common/logger.ts"
import { toISODateString } from "@common/date.ts"
import type { SB1Transaction } from "@sb1/types.ts"
export function bankTransactionIntoActualTransaction( export function bankTransactionIntoActualTransaction(
transaction: Transaction, transaction: SB1Transaction,
): ActualTransaction { ): ActualTransaction {
return { return {
id: transaction.id, id: transaction.id,
@ -23,12 +23,12 @@ export function bankTransactionIntoActualTransaction(
} }
} }
export function isCleared(transaction: Transaction): boolean { export function isCleared(transaction: SB1Transaction): boolean {
const id = Number(transaction.nonUniqueId) const id = Number(transaction.nonUniqueId)
return transaction.bookingStatus === "BOOKED" && !Number.isNaN(id) && id > 0 return transaction.bookingStatus === "BOOKED" && !Number.isNaN(id) && id > 0
} }
function getActualAccountId(transcation: Transaction): UUID { function getActualAccountId(transcation: SB1Transaction): UUID {
for ( for (
let i = 0; let i = 0;
i < Math.min(ACTUAL_ACCOUNT_IDS.length, BANK_ACCOUNT_IDS.length); i < Math.min(ACTUAL_ACCOUNT_IDS.length, BANK_ACCOUNT_IDS.length);

View File

@ -1,12 +1,8 @@
import type { import type { Bank, Interval } from "@/bank/sparebank1.ts"
Bank,
BookingStatus,
Interval,
Transaction,
} from "@/bank/sparebank1.ts"
import dayjs from "dayjs" import dayjs from "dayjs"
import { ActualTransaction } from "@/actual.ts" import type { ActualTransaction } from "@/actual.ts"
import { bankTransactionIntoActualTransaction } from "@/mappings.ts" import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
import type { BookingStatus, SB1Transaction } from "@sb1/types.ts"
export class BankStub implements Bank { export class BankStub implements Bank {
async fetchTransactions( async fetchTransactions(
@ -20,7 +16,7 @@ export class BankStub implements Bank {
bookingStatus: "BOOKED" as BookingStatus, bookingStatus: "BOOKED" as BookingStatus,
accountKey: "1", accountKey: "1",
} }
const bankTransactions: ReadonlyArray<Transaction> = [ const bankTransactions: ReadonlyArray<SB1Transaction> = [
{ {
id: "1", id: "1",
nonUniqueId: "1", nonUniqueId: "1",

View File

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