Compare commits

..

12 Commits

Author SHA1 Message Date
06cc89f762 💾 Volume for cache, default values for envs in compose
All checks were successful
Deploy application / deploy (push) Successful in 3s
2025-02-09 13:54:35 +01:00
b6daf4268c 🚀 Init and shutdown Actual on job
- Renamed daily function to moveTransactions
- testConnections method to verify connection immediately
2025-02-09 13:34:00 +01:00
db30129c30 ☝ Update dependencies
All checks were successful
Deploy application / deploy (push) Successful in 9s
2025-02-09 13:12:02 +01:00
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
efa9e785f2 🧹 Pino transports API and capture console.log
All checks were successful
Deploy application / deploy (push) Successful in 26s
2025-02-06 19:37:11 +01:00
71e70a2713 🧹 Delete token from db if expired 2025-02-06 19:13:23 +01:00
4f05382fc4 🚀 Daily is now Bank agnostic
- Separated createDir into a new file fs.ts
- Moved mapTransactions into Bank interface
- Take interval as input into importTransactions
2025-02-06 18:56:51 +01:00
066331cca8 🔪🐛 Fix corepack bug, removed unused envs, npm in docker
All checks were successful
Deploy application / deploy (push) Successful in 28s
- Replaced pnpm to npm in docker container because of a bug infestation
- Remove only allow pnpm
- Added missing envs to docker compose
2025-02-05 20:27:08 +01:00
75ad4946d2 🎉 Allow syncing multiple accounts at once
All checks were successful
Deploy application / deploy (push) Successful in 3s
- By defining multiple account keys in the .env, will fetch from all and upload to correct account
- If transaction id is 0, will not be marked as cleared
- Added accountKey to Transaction interface
2025-02-02 12:37:43 +01:00
14257fa058 ❄ Nix shell using flake 2025-02-02 12:34:09 +01:00
9b0391be7c Graceful shutdown on exceptions 2025-02-02 11:22:29 +01:00
34 changed files with 487 additions and 354 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

@ -9,8 +9,6 @@ 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,18 +15,18 @@ 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: ${{ var.ACTUAL_DATA_DIR }} ACTUAL_DATA_DIR: ${{ vars.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 }}
DB_DIRECTORY: ${{ vars.DB_DIRECTORY }} DB_DIRECTORY: ${{ vars.DB_DIRECTORY }}
DB_FILENAME: ${{ vars.DB_FILENAME }} DB_FILENAME: ${{ vars.DB_FILENAME }}
TRANSACTION_RELATIVE_FROM_DATE: ${{ vars.TRANSACTION_RELATIVE_FROM_DATE }}
TRANSACTION_RELATIVE_TO_DATE: ${{ vars.TRANSACTION_RELATIVE_TO_DATE }}
steps: steps:
- name: Check out repository code - name: Check out repository code

View File

@ -1,11 +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
ENV PNPM_HOME="/pnpm" ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH" ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable RUN corepack enable pnpm
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile 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
CMD ["pnpm", "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`.

BIN
bun.lockb

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: unless-stopped restart: no
build: build:
context: . context: .
environment: environment:
@ -14,14 +14,16 @@ 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:
- data:/${DB_DIRECTORY} - cache:/${ACTUAL_DATA_DIR:-.cache}
- data:/${DB_DIRECTORY:-data}
volumes: volumes:
cache:
data: data:

8
flake.lock generated
View File

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

View File

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

View File

@ -51,6 +51,8 @@ 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={{brukskontoAccountKey}}&fromDate=2025-01-20&toDate=2025-01-22 GET {{bankingBaseUrl}}/transactions?accountKey={{accountKey1}}&accountKey={{accountKey2}}&
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

@ -2,13 +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": {
"preinstall": "npx only-allow pnpm", "start": "dotenvx run --env-file=.env.local -- node --import=tsx ./src/main.ts | ./packages/common/node_modules/pino-pretty/bin.js",
"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 | ./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}\""
}, },
@ -16,27 +14,24 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@actual-app/api": "^25.1.0", "@actual-app/api": "^25.2.1",
"@dotenvx/dotenvx": "^1.33.0", "@dotenvx/dotenvx": "^1.35.0",
"better-sqlite3": "^11.8.1", "better-sqlite3": "^11.8.1",
"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.5.0",
"prettier": "^3.4.2",
"tsx": "^4.19.2" "tsx": "^4.19.2"
}, },
"devDependencies": { "devDependencies": {
"@jest/globals": "^29.7.0", "@jest/globals": "^29.7.0",
"@types/better-sqlite3": "^7.6.12", "@types/better-sqlite3": "^7.6.12",
"@types/jest": "^29.5.14", "@types/jest": "^29.5.14",
"@types/node": "^22.10.7", "@types/node": "^22.13.1",
"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"
"@types/bun": "latest"
}, },
"prettier": { "prettier": {
"semi": false, "semi": false,

21
packages/common/logger.ts Normal file
View File

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

@ -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>
}

134
pnpm-lock.yaml generated
View File

@ -9,11 +9,11 @@ importers:
.: .:
dependencies: dependencies:
'@actual-app/api': '@actual-app/api':
specifier: ^25.1.0 specifier: ^25.2.1
version: 25.1.0 version: 25.2.1
'@dotenvx/dotenvx': '@dotenvx/dotenvx':
specifier: ^1.33.0 specifier: ^1.35.0
version: 1.33.0 version: 1.35.0
better-sqlite3: better-sqlite3:
specifier: ^11.8.1 specifier: ^11.8.1
version: 11.8.1 version: 11.8.1
@ -26,12 +26,9 @@ 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.5.0
version: 3.4.2 version: 3.5.0
tsx: tsx:
specifier: ^4.19.2 specifier: ^4.19.2
version: 4.19.2 version: 4.19.2
@ -46,28 +43,37 @@ importers:
specifier: ^29.5.14 specifier: ^29.5.14
version: 29.5.14 version: 29.5.14
'@types/node': '@types/node':
specifier: ^22.10.7 specifier: ^22.13.1
version: 22.10.7 version: 22.13.1
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.13.1)(ts-node@10.9.2(@types/node@22.13.1)(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.13.1)(ts-node@10.9.2(@types/node@22.13.1)(typescript@5.7.3)))(typescript@5.7.3)
ts-node: ts-node:
specifier: ^10.9.2 specifier: ^10.9.2
version: 10.9.2(@types/node@22.10.7)(typescript@5.7.3) version: 10.9.2(@types/node@22.13.1)(typescript@5.7.3)
typescript: typescript:
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.2.1':
resolution: {integrity: sha512-sGxcGS170SLKwSsCOFTcs56Z9K8qsdUx+lSEE2Zr8FCKpZaRHlrVf6hPvTpsxWQsopYyfvHoU/MZeLQeHuWXfw==} resolution: {integrity: sha512-3ZtjsPQZotMEBOWI2zpD3PVOxE9DUSdJlHlcNM4pVeEpZyttbC9kTRbJP5IiNz1DE7IFut5ZVCRrblXakXi8Fg==}
engines: {node: '>=18.12.0'} engines: {node: '>=18.12.0'}
'@actual-app/crdt@2.1.0': '@actual-app/crdt@2.1.0':
@ -242,8 +248,8 @@ packages:
resolution: {integrity: sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==} resolution: {integrity: sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==}
engines: {node: '>=12'} engines: {node: '>=12'}
'@dotenvx/dotenvx@1.33.0': '@dotenvx/dotenvx@1.35.0':
resolution: {integrity: sha512-fWVhSrdtObkRJ5SwyNSEUPPm5BHXGlQJAbXeJfrcnonSVdMhKG9pihvJWv86sv8uR0sF/Yd0oI+a9Mj3ISgM3Q==} resolution: {integrity: sha512-MOBRAdEAd9lUKUmd8bfs9KKYEJtZQAqPqa574O6I4K+/9A8JNHQr0dfNSMAA9025TArYNLovKnwIM5f+ejsA/Q==}
hasBin: true hasBin: true
'@ecies/ciphers@0.2.2': '@ecies/ciphers@0.2.2':
@ -561,8 +567,8 @@ packages:
'@types/luxon@3.4.2': '@types/luxon@3.4.2':
resolution: {integrity: sha512-TifLZlFudklWlMBfhubvgqTXRzLDI5pCbGa4P8a3wPyUQSW+1xQ5eDsreP9DWHX3tjq1ke96uYG/nwundroWcA==} resolution: {integrity: sha512-TifLZlFudklWlMBfhubvgqTXRzLDI5pCbGa4P8a3wPyUQSW+1xQ5eDsreP9DWHX3tjq1ke96uYG/nwundroWcA==}
'@types/node@22.10.7': '@types/node@22.13.1':
resolution: {integrity: sha512-V09KvXxFiutGp6B7XkpaDXlNadZxrzajcY50EuoLIpQ6WWYCSvf19lVIazzfIzQvhUN2HjX12spLojTnhuKlGg==} resolution: {integrity: sha512-jK8uzQlrvXqEU91UxiK5J7pKHyzgnI1Qnl0QDHIgVGuolJhRb9EEl28Cj9b3rGR8B2lhFCtvIm5os8lFnO/1Ew==}
'@types/stack-utils@2.0.3': '@types/stack-utils@2.0.3':
resolution: {integrity: sha512-9aEbYZ3TbYMznPdcdr3SmIrLXwC/AKZXQeCf9Pgao5CKb8CyHuEX5jzWPTkvregvhRJHcpRO6BFoGW9ycaOkYw==} resolution: {integrity: sha512-9aEbYZ3TbYMznPdcdr3SmIrLXwC/AKZXQeCf9Pgao5CKb8CyHuEX5jzWPTkvregvhRJHcpRO6BFoGW9ycaOkYw==}
@ -1424,8 +1430,8 @@ packages:
engines: {node: '>=10'} engines: {node: '>=10'}
hasBin: true hasBin: true
prettier@3.4.2: prettier@3.5.0:
resolution: {integrity: sha512-e9MewbtFo+Fevyuxn/4rrcDAaq0IYxPGLvObpQjiZBMAzB9IGmzlnG9RZy3FFas+eBMu2vA0CszMeduow5dIuQ==} resolution: {integrity: sha512-quyMrVt6svPS7CjQ9gKb3GLEX/rl3BCL2oa/QkNcXv4YNVBC9olt3s+H7ukto06q7B1Qz46PbrKLO34PR6vXcA==}
engines: {node: '>=14'} engines: {node: '>=14'}
hasBin: true hasBin: true
@ -1751,7 +1757,7 @@ packages:
snapshots: snapshots:
'@actual-app/api@25.1.0': '@actual-app/api@25.2.1':
dependencies: dependencies:
'@actual-app/crdt': 2.1.0 '@actual-app/crdt': 2.1.0
better-sqlite3: 11.8.1 better-sqlite3: 11.8.1
@ -1961,7 +1967,7 @@ snapshots:
dependencies: dependencies:
'@jridgewell/trace-mapping': 0.3.9 '@jridgewell/trace-mapping': 0.3.9
'@dotenvx/dotenvx@1.33.0': '@dotenvx/dotenvx@1.35.0':
dependencies: dependencies:
commander: 11.1.0 commander: 11.1.0
dotenv: 16.4.7 dotenv: 16.4.7
@ -2062,27 +2068,27 @@ snapshots:
'@jest/console@29.7.0': '@jest/console@29.7.0':
dependencies: dependencies:
'@jest/types': 29.6.3 '@jest/types': 29.6.3
'@types/node': 22.10.7 '@types/node': 22.13.1
chalk: 4.1.2 chalk: 4.1.2
jest-message-util: 29.7.0 jest-message-util: 29.7.0
jest-util: 29.7.0 jest-util: 29.7.0
slash: 3.0.0 slash: 3.0.0
'@jest/core@29.7.0(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3))': '@jest/core@29.7.0(ts-node@10.9.2(@types/node@22.13.1)(typescript@5.7.3))':
dependencies: dependencies:
'@jest/console': 29.7.0 '@jest/console': 29.7.0
'@jest/reporters': 29.7.0 '@jest/reporters': 29.7.0
'@jest/test-result': 29.7.0 '@jest/test-result': 29.7.0
'@jest/transform': 29.7.0 '@jest/transform': 29.7.0
'@jest/types': 29.6.3 '@jest/types': 29.6.3
'@types/node': 22.10.7 '@types/node': 22.13.1
ansi-escapes: 4.3.2 ansi-escapes: 4.3.2
chalk: 4.1.2 chalk: 4.1.2
ci-info: 3.9.0 ci-info: 3.9.0
exit: 0.1.2 exit: 0.1.2
graceful-fs: 4.2.11 graceful-fs: 4.2.11
jest-changed-files: 29.7.0 jest-changed-files: 29.7.0
jest-config: 29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3)) jest-config: 29.7.0(@types/node@22.13.1)(ts-node@10.9.2(@types/node@22.13.1)(typescript@5.7.3))
jest-haste-map: 29.7.0 jest-haste-map: 29.7.0
jest-message-util: 29.7.0 jest-message-util: 29.7.0
jest-regex-util: 29.6.3 jest-regex-util: 29.6.3
@ -2107,7 +2113,7 @@ snapshots:
dependencies: dependencies:
'@jest/fake-timers': 29.7.0 '@jest/fake-timers': 29.7.0
'@jest/types': 29.6.3 '@jest/types': 29.6.3
'@types/node': 22.10.7 '@types/node': 22.13.1
jest-mock: 29.7.0 jest-mock: 29.7.0
'@jest/expect-utils@29.7.0': '@jest/expect-utils@29.7.0':
@ -2125,7 +2131,7 @@ snapshots:
dependencies: dependencies:
'@jest/types': 29.6.3 '@jest/types': 29.6.3
'@sinonjs/fake-timers': 10.3.0 '@sinonjs/fake-timers': 10.3.0
'@types/node': 22.10.7 '@types/node': 22.13.1
jest-message-util: 29.7.0 jest-message-util: 29.7.0
jest-mock: 29.7.0 jest-mock: 29.7.0
jest-util: 29.7.0 jest-util: 29.7.0
@ -2147,7 +2153,7 @@ snapshots:
'@jest/transform': 29.7.0 '@jest/transform': 29.7.0
'@jest/types': 29.6.3 '@jest/types': 29.6.3
'@jridgewell/trace-mapping': 0.3.25 '@jridgewell/trace-mapping': 0.3.25
'@types/node': 22.10.7 '@types/node': 22.13.1
chalk: 4.1.2 chalk: 4.1.2
collect-v8-coverage: 1.0.2 collect-v8-coverage: 1.0.2
exit: 0.1.2 exit: 0.1.2
@ -2217,7 +2223,7 @@ snapshots:
'@jest/schemas': 29.6.3 '@jest/schemas': 29.6.3
'@types/istanbul-lib-coverage': 2.0.6 '@types/istanbul-lib-coverage': 2.0.6
'@types/istanbul-reports': 3.0.4 '@types/istanbul-reports': 3.0.4
'@types/node': 22.10.7 '@types/node': 22.13.1
'@types/yargs': 17.0.33 '@types/yargs': 17.0.33
chalk: 4.1.2 chalk: 4.1.2
@ -2294,11 +2300,11 @@ snapshots:
'@types/better-sqlite3@7.6.12': '@types/better-sqlite3@7.6.12':
dependencies: dependencies:
'@types/node': 22.10.7 '@types/node': 22.13.1
'@types/graceful-fs@4.1.9': '@types/graceful-fs@4.1.9':
dependencies: dependencies:
'@types/node': 22.10.7 '@types/node': 22.13.1
'@types/istanbul-lib-coverage@2.0.6': {} '@types/istanbul-lib-coverage@2.0.6': {}
@ -2317,7 +2323,7 @@ snapshots:
'@types/luxon@3.4.2': {} '@types/luxon@3.4.2': {}
'@types/node@22.10.7': '@types/node@22.13.1':
dependencies: dependencies:
undici-types: 6.20.0 undici-types: 6.20.0
@ -2518,13 +2524,13 @@ snapshots:
convert-source-map@2.0.0: {} convert-source-map@2.0.0: {}
create-jest@29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3)): create-jest@29.7.0(@types/node@22.13.1)(ts-node@10.9.2(@types/node@22.13.1)(typescript@5.7.3)):
dependencies: dependencies:
'@jest/types': 29.6.3 '@jest/types': 29.6.3
chalk: 4.1.2 chalk: 4.1.2
exit: 0.1.2 exit: 0.1.2
graceful-fs: 4.2.11 graceful-fs: 4.2.11
jest-config: 29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3)) jest-config: 29.7.0(@types/node@22.13.1)(ts-node@10.9.2(@types/node@22.13.1)(typescript@5.7.3))
jest-util: 29.7.0 jest-util: 29.7.0
prompts: 2.4.2 prompts: 2.4.2
transitivePeerDependencies: transitivePeerDependencies:
@ -2846,7 +2852,7 @@ snapshots:
'@jest/expect': 29.7.0 '@jest/expect': 29.7.0
'@jest/test-result': 29.7.0 '@jest/test-result': 29.7.0
'@jest/types': 29.6.3 '@jest/types': 29.6.3
'@types/node': 22.10.7 '@types/node': 22.13.1
chalk: 4.1.2 chalk: 4.1.2
co: 4.6.0 co: 4.6.0
dedent: 1.5.3 dedent: 1.5.3
@ -2866,16 +2872,16 @@ snapshots:
- babel-plugin-macros - babel-plugin-macros
- supports-color - supports-color
jest-cli@29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3)): jest-cli@29.7.0(@types/node@22.13.1)(ts-node@10.9.2(@types/node@22.13.1)(typescript@5.7.3)):
dependencies: dependencies:
'@jest/core': 29.7.0(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3)) '@jest/core': 29.7.0(ts-node@10.9.2(@types/node@22.13.1)(typescript@5.7.3))
'@jest/test-result': 29.7.0 '@jest/test-result': 29.7.0
'@jest/types': 29.6.3 '@jest/types': 29.6.3
chalk: 4.1.2 chalk: 4.1.2
create-jest: 29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3)) create-jest: 29.7.0(@types/node@22.13.1)(ts-node@10.9.2(@types/node@22.13.1)(typescript@5.7.3))
exit: 0.1.2 exit: 0.1.2
import-local: 3.2.0 import-local: 3.2.0
jest-config: 29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3)) jest-config: 29.7.0(@types/node@22.13.1)(ts-node@10.9.2(@types/node@22.13.1)(typescript@5.7.3))
jest-util: 29.7.0 jest-util: 29.7.0
jest-validate: 29.7.0 jest-validate: 29.7.0
yargs: 17.7.2 yargs: 17.7.2
@ -2885,7 +2891,7 @@ snapshots:
- supports-color - supports-color
- ts-node - ts-node
jest-config@29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3)): jest-config@29.7.0(@types/node@22.13.1)(ts-node@10.9.2(@types/node@22.13.1)(typescript@5.7.3)):
dependencies: dependencies:
'@babel/core': 7.26.0 '@babel/core': 7.26.0
'@jest/test-sequencer': 29.7.0 '@jest/test-sequencer': 29.7.0
@ -2910,8 +2916,8 @@ snapshots:
slash: 3.0.0 slash: 3.0.0
strip-json-comments: 3.1.1 strip-json-comments: 3.1.1
optionalDependencies: optionalDependencies:
'@types/node': 22.10.7 '@types/node': 22.13.1
ts-node: 10.9.2(@types/node@22.10.7)(typescript@5.7.3) ts-node: 10.9.2(@types/node@22.13.1)(typescript@5.7.3)
transitivePeerDependencies: transitivePeerDependencies:
- babel-plugin-macros - babel-plugin-macros
- supports-color - supports-color
@ -2940,7 +2946,7 @@ snapshots:
'@jest/environment': 29.7.0 '@jest/environment': 29.7.0
'@jest/fake-timers': 29.7.0 '@jest/fake-timers': 29.7.0
'@jest/types': 29.6.3 '@jest/types': 29.6.3
'@types/node': 22.10.7 '@types/node': 22.13.1
jest-mock: 29.7.0 jest-mock: 29.7.0
jest-util: 29.7.0 jest-util: 29.7.0
@ -2950,7 +2956,7 @@ snapshots:
dependencies: dependencies:
'@jest/types': 29.6.3 '@jest/types': 29.6.3
'@types/graceful-fs': 4.1.9 '@types/graceful-fs': 4.1.9
'@types/node': 22.10.7 '@types/node': 22.13.1
anymatch: 3.1.3 anymatch: 3.1.3
fb-watchman: 2.0.2 fb-watchman: 2.0.2
graceful-fs: 4.2.11 graceful-fs: 4.2.11
@ -2989,7 +2995,7 @@ snapshots:
jest-mock@29.7.0: jest-mock@29.7.0:
dependencies: dependencies:
'@jest/types': 29.6.3 '@jest/types': 29.6.3
'@types/node': 22.10.7 '@types/node': 22.13.1
jest-util: 29.7.0 jest-util: 29.7.0
jest-pnp-resolver@1.2.3(jest-resolve@29.7.0): jest-pnp-resolver@1.2.3(jest-resolve@29.7.0):
@ -3024,7 +3030,7 @@ snapshots:
'@jest/test-result': 29.7.0 '@jest/test-result': 29.7.0
'@jest/transform': 29.7.0 '@jest/transform': 29.7.0
'@jest/types': 29.6.3 '@jest/types': 29.6.3
'@types/node': 22.10.7 '@types/node': 22.13.1
chalk: 4.1.2 chalk: 4.1.2
emittery: 0.13.1 emittery: 0.13.1
graceful-fs: 4.2.11 graceful-fs: 4.2.11
@ -3052,7 +3058,7 @@ snapshots:
'@jest/test-result': 29.7.0 '@jest/test-result': 29.7.0
'@jest/transform': 29.7.0 '@jest/transform': 29.7.0
'@jest/types': 29.6.3 '@jest/types': 29.6.3
'@types/node': 22.10.7 '@types/node': 22.13.1
chalk: 4.1.2 chalk: 4.1.2
cjs-module-lexer: 1.4.1 cjs-module-lexer: 1.4.1
collect-v8-coverage: 1.0.2 collect-v8-coverage: 1.0.2
@ -3098,7 +3104,7 @@ snapshots:
jest-util@29.7.0: jest-util@29.7.0:
dependencies: dependencies:
'@jest/types': 29.6.3 '@jest/types': 29.6.3
'@types/node': 22.10.7 '@types/node': 22.13.1
chalk: 4.1.2 chalk: 4.1.2
ci-info: 3.9.0 ci-info: 3.9.0
graceful-fs: 4.2.11 graceful-fs: 4.2.11
@ -3117,7 +3123,7 @@ snapshots:
dependencies: dependencies:
'@jest/test-result': 29.7.0 '@jest/test-result': 29.7.0
'@jest/types': 29.6.3 '@jest/types': 29.6.3
'@types/node': 22.10.7 '@types/node': 22.13.1
ansi-escapes: 4.3.2 ansi-escapes: 4.3.2
chalk: 4.1.2 chalk: 4.1.2
emittery: 0.13.1 emittery: 0.13.1
@ -3126,17 +3132,17 @@ snapshots:
jest-worker@29.7.0: jest-worker@29.7.0:
dependencies: dependencies:
'@types/node': 22.10.7 '@types/node': 22.13.1
jest-util: 29.7.0 jest-util: 29.7.0
merge-stream: 2.0.0 merge-stream: 2.0.0
supports-color: 8.1.1 supports-color: 8.1.1
jest@29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3)): jest@29.7.0(@types/node@22.13.1)(ts-node@10.9.2(@types/node@22.13.1)(typescript@5.7.3)):
dependencies: dependencies:
'@jest/core': 29.7.0(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3)) '@jest/core': 29.7.0(ts-node@10.9.2(@types/node@22.13.1)(typescript@5.7.3))
'@jest/types': 29.6.3 '@jest/types': 29.6.3
import-local: 3.2.0 import-local: 3.2.0
jest-cli: 29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3)) jest-cli: 29.7.0(@types/node@22.13.1)(ts-node@10.9.2(@types/node@22.13.1)(typescript@5.7.3))
transitivePeerDependencies: transitivePeerDependencies:
- '@types/node' - '@types/node'
- babel-plugin-macros - babel-plugin-macros
@ -3343,7 +3349,7 @@ snapshots:
tar-fs: 2.1.1 tar-fs: 2.1.1
tunnel-agent: 0.6.0 tunnel-agent: 0.6.0
prettier@3.4.2: {} prettier@3.5.0: {}
pretty-format@29.7.0: pretty-format@29.7.0:
dependencies: dependencies:
@ -3519,12 +3525,12 @@ snapshots:
dependencies: dependencies:
is-number: 7.0.0 is-number: 7.0.0
ts-jest@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): ts-jest@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.13.1)(ts-node@10.9.2(@types/node@22.13.1)(typescript@5.7.3)))(typescript@5.7.3):
dependencies: dependencies:
bs-logger: 0.2.6 bs-logger: 0.2.6
ejs: 3.1.10 ejs: 3.1.10
fast-json-stable-stringify: 2.1.0 fast-json-stable-stringify: 2.1.0
jest: 29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3)) jest: 29.7.0(@types/node@22.13.1)(ts-node@10.9.2(@types/node@22.13.1)(typescript@5.7.3))
jest-util: 29.7.0 jest-util: 29.7.0
json5: 2.2.3 json5: 2.2.3
lodash.memoize: 4.1.2 lodash.memoize: 4.1.2
@ -3538,14 +3544,14 @@ snapshots:
'@jest/types': 29.6.3 '@jest/types': 29.6.3
babel-jest: 29.7.0(@babel/core@7.26.0) babel-jest: 29.7.0(@babel/core@7.26.0)
ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3): ts-node@10.9.2(@types/node@22.13.1)(typescript@5.7.3):
dependencies: dependencies:
'@cspotcode/source-map-support': 0.8.1 '@cspotcode/source-map-support': 0.8.1
'@tsconfig/node10': 1.0.11 '@tsconfig/node10': 1.0.11
'@tsconfig/node12': 1.0.11 '@tsconfig/node12': 1.0.11
'@tsconfig/node14': 1.0.3 '@tsconfig/node14': 1.0.3
'@tsconfig/node16': 1.0.4 '@tsconfig/node16': 1.0.4
'@types/node': 22.10.7 '@types/node': 22.13.1
acorn: 8.14.0 acorn: 8.14.0
acorn-walk: 8.3.4 acorn-walk: 8.3.4
arg: 4.1.3 arg: 4.1.3

2
pnpm-workspace.yaml Normal file
View File

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

View File

@ -4,21 +4,22 @@ 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: (
accountId: UUID, accountId: UUID,
transactions: ReadonlyArray<ActualTransaction>, transactions: Iterable<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
} }
@ -49,9 +50,24 @@ export class ActualImpl implements Actual {
return new ActualImpl() return new ActualImpl()
} }
/**
* Attempts to connect then immediatly shutsdown the connection
* @exception error If connection fails
*/
static async testConnection(): Promise<void> {
let actual: Actual | undefined
logger.info("Testing ActualBudget connection")
try {
actual = await ActualImpl.init()
} finally {
await actual?.shutdown()
logger.info("Finished testing ActualBudget connection")
}
}
async importTransactions( async importTransactions(
accountId: UUID, accountId: UUID,
transactions: ReadonlyArray<ActualTransaction>, transactions: Iterable<ActualTransaction>,
): Promise<ImportTransactionsResponse> { ): Promise<ImportTransactionsResponse> {
return actual.importTransactions(accountId, transactions) return actual.importTransactions(accountId, transactions)
} }

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"
@ -50,7 +48,7 @@ function insertRefreshToken(
db: Database.Database, db: Database.Database,
refreshToken: string, refreshToken: string,
expiresIn: number, expiresIn: number,
) { ): void {
insert(db, "refresh-token", refreshToken, expiresIn) insert(db, "refresh-token", refreshToken, expiresIn)
} }
@ -59,7 +57,7 @@ function insert(
key: TokenKey, key: TokenKey,
token: string, token: string,
expiresIn: number, expiresIn: number,
) { ): void {
db.prepare("INSERT OR REPLACE INTO tokens VALUES (?, ?, ?)").run( db.prepare("INSERT OR REPLACE INTO tokens VALUES (?, ?, ?)").run(
key, key,
token, token,
@ -82,3 +80,10 @@ 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,49 +1,33 @@
import { import {
BANK_INITIAL_REFRESH_TOKEN, BANK_INITIAL_REFRESH_TOKEN,
TRANSACTION_RELATIVE_FROM_DATE, BANK_OAUTH_CLIENT_ID,
TRANSACTION_RELATIVE_TO_DATE, BANK_OAUTH_CLIENT_SECRET,
} from "@/../config.ts" } from "@/config.ts"
import logger from "@/logger.ts" import logger from "@common/logger.ts"
import dayjs from "dayjs" import dayjs, { type Dayjs } from "dayjs"
import { Database } from "better-sqlite3" import type { Database } from "better-sqlite3"
import { import {
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 * as Transactions from "@sb1/transactions.ts"
export interface OAuthTokenResponse { import type { ActualTransaction } from "@/actual.ts"
access_token: string import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
expires_in: number import type { OAuthTokenResponse } from "@sb1/types.ts"
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
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 { export interface Bank {
transactionsPastDay: ( fetchTransactions: (
interval: Interval,
...accountKeys: ReadonlyArray<string> ...accountKeys: ReadonlyArray<string>
) => Promise<TransactionResponse> ) => Promise<ReadonlyArray<ActualTransaction>>
}
export interface Interval {
fromDate: Dayjs
toDate: Dayjs
} }
export class Sparebank1Impl implements Bank { export class Sparebank1Impl implements Bank {
@ -53,6 +37,19 @@ export class Sparebank1Impl implements Bank {
this.db = db 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> { private async getAccessToken(): Promise<string> {
const accessToken = fetchToken(this.db, "access-token") const accessToken = fetchToken(this.db, "access-token")
@ -76,34 +73,25 @@ export class Sparebank1Impl implements Bank {
} else if (this.isValidToken(tokenResponse)) { } else if (this.isValidToken(tokenResponse)) {
return tokenResponse.token return tokenResponse.token
} }
// TODO clear database, if refresh token is invalid, will cause Exceptions on each call logger.warn("Refresh token expired, deleting tokens from database")
clearTokens(this.db)
throw new Error("Refresh token is expired. Create a new one") throw new Error("Refresh token is expired. Create a new one")
} }
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 logger.error({ throw new Error(`Failed to fetch refresh token: '${result.data}'`)
err: new Error(`Failed to fetch refresh token: '${result.data}'`),
})
} }
const oAuthToken = result.data const oAuthToken = result.data
insertTokens(this.db, oAuthToken) insertTokens(this.db, oAuthToken)
return 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,
})
}
} }

View File

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

@ -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.

13
src/fs.ts Normal file
View File

@ -0,0 +1,13 @@
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 })
}
}

View File

@ -1,9 +0,0 @@
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,75 +1,71 @@
import { type Actual, ActualImpl } from "@/actual.ts" import { type Actual, ActualImpl } from "@/actual.ts"
import { cronJobDaily } from "@/cron.ts" import { cronJobDaily } from "@/cron.ts"
import { type Bank, type Interval, Sparebank1Impl } from "@/bank/sparebank1.ts"
import { import {
type Bank,
Sparebank1Impl,
type Transaction,
} from "@/bank/sparebank1.ts"
import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
import {
ACTUAL_ACCOUNT_IDS,
ACTUAL_DATA_DIR, ACTUAL_DATA_DIR,
BANK_ACCOUNT_IDS, BANK_ACCOUNT_IDS,
DB_DIRECTORY, DB_DIRECTORY,
DB_FILENAME, DB_FILENAME,
} from "../config.ts" TRANSACTION_RELATIVE_FROM_DATE,
import logger from "@/logger.ts" TRANSACTION_RELATIVE_TO_DATE,
} from "@/config.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 * as fs from "node:fs"
import { CronJob } from "cron" 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 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
// TODO do not fetch if saturday or sunday
export async function daily(actual: Actual, bank: Bank): Promise<void> { export async function moveTransactions(
// Fetch transactions from the bank actual: Actual,
const transactions = await fetchTransactionsFromPastDay(bank)
logger.info(`Fetched ${transactions.length} transactions`)
// 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,
)
logger.info(`ImportTransactionsResponse=${JSON.stringify(response)}`)
}
async function fetchTransactionsFromPastDay(
bank: Bank, bank: Bank,
): Promise<ReadonlyArray<Transaction>> { ): Promise<void> {
const response = await bank.transactionsPastDay(...BANK_ACCOUNT_IDS) // Fetch transactions from the bank
return response.transactions const actualTransactions = await bank.fetchTransactions(
relativeInterval(),
...BANK_ACCOUNT_IDS,
)
logger.info(`Fetched ${actualTransactions.length} transactions`)
const transactionsByAccount = Object.groupBy(
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",
)
} }
function createDirIfMissing(directory: string): void { function relativeInterval(): Interval {
if (!fs.existsSync(directory)) { const today = dayjs()
logger.info(`Missing '${directory}', creating...`) return {
fs.mkdirSync(directory, { recursive: true }) fromDate: today.subtract(TRANSACTION_RELATIVE_FROM_DATE, "days"),
toDate: today.subtract(TRANSACTION_RELATIVE_TO_DATE, "days"),
} }
} }
async function main(): Promise<void> { async function main(): Promise<void> {
logger.info("Starting application") logger.info("Starting application")
createDirIfMissing(ACTUAL_DATA_DIR) createDirsIfMissing(ACTUAL_DATA_DIR, DB_DIRECTORY)
createDirIfMissing(DB_DIRECTORY)
const actual = await ActualImpl.init()
const databaseFilePath = `${DB_DIRECTORY}/${DB_FILENAME}.sqlite` const databaseFilePath = `${DB_DIRECTORY}/${DB_FILENAME}.sqlite`
const db = createDb(databaseFilePath) const db = createDb(databaseFilePath)
logger.info(`Started Sqlite database at '${databaseFilePath}'`) logger.info(`Started Sqlite database at '${databaseFilePath}'`)
@ -82,18 +78,33 @@ async function main(): Promise<void> {
let cronJob: CronJob | undefined let cronJob: CronJob | undefined
if (process.env.ONCE) { if (process.env.ONCE) {
await daily(actual, bank) const actual = await ActualImpl.init()
try {
return await moveTransactions(actual, bank)
} finally {
await actual.shutdown()
await shutdown() await shutdown()
return }
} else {
await ActualImpl.testConnection()
} }
logger.info("Waiting for CRON job to start") logger.info("Waiting for CronJob to start")
// TODO init and shutdown resources when job runs? let actual: Actual | undefined
cronJob = cronJobDaily(async () => await daily(actual, bank)) try {
cronJob = cronJobDaily(async () => {
actual = await ActualImpl.init()
await moveTransactions(actual, bank)
})
} catch (exception) {
logger.error(exception, "Caught exception at CronJob, shutting down!")
await shutdown()
} finally {
await actual?.shutdown()
}
async function shutdown(): Promise<void> { async function shutdown(): Promise<void> {
logger.info("Shutting down, Bye!") logger.info("Shutting down, Bye!")
await actual.shutdown()
db.close() db.close()
cronJob?.stop() cronJob?.stop()
} }

View File

@ -1,29 +1,46 @@
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 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,
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: accountId, account: getActualAccountId(transaction),
// The value without decimals // The value without decimals
amount: transaction.amount * 100, amount: Math.floor(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: transaction.bookingStatus === "BOOKED", cleared: isCleared(transaction),
} }
} }
// TODO take the account from the bank and match it to the actual account export function isCleared(transaction: SB1Transaction): boolean {
// Use ENV const id = Number(transaction.nonUniqueId)
export function bankAccountIntoActualAccount(account: string): string { return transaction.bookingStatus === "BOOKED" && !Number.isNaN(id) && id > 0
throw new Error("Not implemented") }
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
} }

View File

@ -1,6 +1,6 @@
import { describe, it } from "@jest/globals" import { describe, it } from "@jest/globals"
import { daily } from "@/main.ts" import { moveTransactions } from "@/main.ts"
import { ActualImpl } from "@/actual.ts" import { ActualImpl } from "@/actual.ts"
import { BankStub } from "./stubs/bankStub.ts" import { BankStub } from "./stubs/bankStub.ts"
@ -10,7 +10,7 @@ import { BankStub } from "./stubs/bankStub.ts"
describe("Main logic of the application", () => { describe("Main logic of the application", () => {
it("should import the transactions to Actual Budget", async () => { it("should import the transactions to Actual Budget", async () => {
const actual = await ActualImpl.init() const actual = await ActualImpl.init()
await daily(actual, new BankStub()) await moveTransactions(actual, new BankStub())
await actual.shutdown() await actual.shutdown()
}) })
}) })

View File

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

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__"]