Compare commits
19 Commits
bun-rewrit
...
main
Author | SHA1 | Date | |
---|---|---|---|
7ea55567da | |||
080b65854d | |||
a0cefcdfa1 | |||
a2c6c55430 | |||
0e83172558 | |||
95ddcbaf13 | |||
4367c24fb0 | |||
06cc89f762 | |||
b6daf4268c | |||
db30129c30 | |||
3c6cb193eb | |||
c5b1ec20d6 | |||
efa9e785f2 | |||
71e70a2713 | |||
4f05382fc4 | |||
066331cca8 | |||
75ad4946d2 | |||
14257fa058 | |||
9b0391be7c |
@ -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
|
@ -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
|
||||||
|
@ -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 || 'N/A' }}
|
||||||
|
TRANSACTION_RELATIVE_TO_DATE: ${{ vars.TRANSACTION_RELATIVE_TO_DATE || 'N/A' }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Check out repository code
|
- name: Check out repository code
|
||||||
|
11
Dockerfile
11
Dockerfile
@ -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"]
|
@ -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`.
|
||||||
|
|
||||||
|
@ -14,14 +14,17 @@ 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
|
||||||
- DB_FILENAME
|
- DB_FILENAME
|
||||||
|
- TRANSACTION_RELATIVE_FROM_DATE
|
||||||
|
- TRANSACTION_RELATIVE_TO_DATE
|
||||||
volumes:
|
volumes:
|
||||||
- data:/${DB_DIRECTORY}
|
- cache:/app/${ACTUAL_DATA_DIR:-.cache}
|
||||||
|
- data:/app/${DB_DIRECTORY:-data}
|
||||||
|
|
||||||
|
# TODO change volume name from hostexecutor-*
|
||||||
volumes:
|
volumes:
|
||||||
|
cache:
|
||||||
data:
|
data:
|
||||||
|
8
flake.lock
generated
8
flake.lock
generated
@ -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"
|
||||||
}
|
}
|
||||||
|
@ -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";
|
||||||
|
@ -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
|
||||||
|
29
package.json
29
package.json
@ -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.3.0",
|
||||||
"@dotenvx/dotenvx": "^1.33.0",
|
"@dotenvx/dotenvx": "^1.38.3",
|
||||||
"better-sqlite3": "^11.8.1",
|
"better-sqlite3": "^11.8.1",
|
||||||
"cron": "^3.5.0",
|
"cron": "^4.1.0",
|
||||||
"dayjs": "^1.11.13",
|
"dayjs": "^1.11.13",
|
||||||
"dotenv": "^16.4.7",
|
"dotenv": "^16.4.7",
|
||||||
"pino": "^9.6.0",
|
"prettier": "^3.5.2",
|
||||||
"prettier": "^3.4.2",
|
"tsx": "^4.19.3"
|
||||||
"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.8",
|
||||||
"jest": "^29.7.0",
|
"jest": "^29.7.0",
|
||||||
"pino-pretty": "^13.0.0",
|
"ts-jest": "^29.2.6",
|
||||||
"ts-jest": "^29.2.5",
|
|
||||||
"ts-node": "^10.9.2",
|
"ts-node": "^10.9.2",
|
||||||
"typescript": "^5.7.3",
|
"typescript": "^5.8.2"
|
||||||
"@types/bun": "latest"
|
|
||||||
},
|
},
|
||||||
"prettier": {
|
"prettier": {
|
||||||
"semi": false,
|
"semi": false,
|
||||||
@ -44,4 +39,4 @@
|
|||||||
"tabWidth": 2,
|
"tabWidth": 2,
|
||||||
"trailingComma": "all"
|
"trailingComma": "all"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
21
packages/common/logger.ts
Normal file
21
packages/common/logger.ts
Normal 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
|
12
packages/common/package.json
Normal file
12
packages/common/package.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
66
packages/common/types.ts
Normal file
66
packages/common/types.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import type { Dayjs } from "dayjs"
|
||||||
|
import type { UUID } from "node:crypto"
|
||||||
|
import type { TransactionEntity } from "@actual-app/api/@types/loot-core/types/models"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines how to interact with the bank
|
||||||
|
*/
|
||||||
|
export interface Bank {
|
||||||
|
/**
|
||||||
|
* Fetch all transactions in the specified days, from the given accounts
|
||||||
|
* @param interval Which days to fetch transactions for
|
||||||
|
* @param accountKeys The id of the accounts to fetch transactions from
|
||||||
|
* @returns An array of all transactions
|
||||||
|
*/
|
||||||
|
fetchTransactions: (
|
||||||
|
interval: Interval,
|
||||||
|
...accountKeys: ReadonlyArray<string>
|
||||||
|
) => Promise<ReadonlyArray<ActualTransaction>>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shutdown resources
|
||||||
|
*/
|
||||||
|
shutdown: () => Promise<void> | void
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Interval {
|
||||||
|
fromDate: Dayjs
|
||||||
|
toDate: Dayjs
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Describes how to interact with ActualBudget
|
||||||
|
*/
|
||||||
|
export interface Actual {
|
||||||
|
/**
|
||||||
|
* Import transactions following the rules defined in the ActualBudget instance
|
||||||
|
* If the transactions exists, it will be updated, or no change should be done.
|
||||||
|
* @param accountId The ActualBudget id to upload to
|
||||||
|
* @param transactions The transactions to import
|
||||||
|
* @returns An object describing what changed
|
||||||
|
*/
|
||||||
|
importTransactions: (
|
||||||
|
accountId: UUID,
|
||||||
|
transactions: Iterable<ActualTransaction>,
|
||||||
|
) => Promise<ImportTransactionsResponse>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disconnect from ActualBudget and release resources
|
||||||
|
*/
|
||||||
|
shutdown: () => Promise<void>
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ActualTransaction extends TransactionEntity {
|
||||||
|
account: UUID
|
||||||
|
payee_name?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ImportTransactionsResponse {
|
||||||
|
errors?: Message[]
|
||||||
|
added: number
|
||||||
|
updated: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Message {
|
||||||
|
message: string
|
||||||
|
}
|
@ -1,24 +1,16 @@
|
|||||||
import Database from "better-sqlite3"
|
import Database from "better-sqlite3"
|
||||||
|
import dayjs from "dayjs"
|
||||||
import { type OAuthTokenResponse } from "@/bank/sparebank1.ts"
|
import type { OAuthTokenResponse } from "@sb1/types.ts"
|
||||||
import dayjs, { type Dayjs } from "dayjs"
|
import type {
|
||||||
|
TokenKey,
|
||||||
export type TokenResponse = {
|
TokenResponse,
|
||||||
key: TokenKey
|
TokenResponseRaw,
|
||||||
token: string
|
} from "@sb1impl/db/types.ts"
|
||||||
expires_at: Dayjs
|
import logger from "@common/logger.ts"
|
||||||
}
|
|
||||||
|
|
||||||
export type TokenResponseRaw = {
|
|
||||||
key: TokenResponse["key"]
|
|
||||||
token: TokenResponse["token"]
|
|
||||||
expires_at: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export type TokenKey = "access-token" | "refresh-token"
|
|
||||||
|
|
||||||
export function createDb(filepath: string) {
|
export function createDb(filepath: string) {
|
||||||
const db = new Database(filepath)
|
const db = new Database(filepath)
|
||||||
|
logger.info(`Started Sqlite database at '${filepath}'`)
|
||||||
db.pragma("journal_mode = WAL")
|
db.pragma("journal_mode = WAL")
|
||||||
db.exec(
|
db.exec(
|
||||||
"CREATE TABLE IF NOT EXISTS tokens ('key' VARCHAR PRIMARY KEY, token VARCHAR NOT NULL, expires_at DATETIME NOT NULL)",
|
"CREATE TABLE IF NOT EXISTS tokens ('key' VARCHAR PRIMARY KEY, token VARCHAR NOT NULL, expires_at DATETIME NOT NULL)",
|
||||||
@ -50,7 +42,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 +51,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 +74,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[])
|
||||||
|
}
|
13
packages/sparebank1/db/types.ts
Normal file
13
packages/sparebank1/db/types.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import type { Dayjs } from "dayjs"
|
||||||
|
|
||||||
|
export type TokenResponse = {
|
||||||
|
key: TokenKey
|
||||||
|
token: string
|
||||||
|
expires_at: Dayjs
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TokenResponseRaw = {
|
||||||
|
[K in keyof TokenResponse]: K extends "expires_at" ? string : TokenResponse[K]
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TokenKey = "access-token" | "refresh-token"
|
43
packages/sparebank1/mappings.ts
Normal file
43
packages/sparebank1/mappings.ts
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import { ACTUAL_ACCOUNT_IDS, BANK_ACCOUNT_IDS } from "@/config.ts"
|
||||||
|
import { toISODateString } from "@common/date.ts"
|
||||||
|
import dayjs from "dayjs"
|
||||||
|
|
||||||
|
import type { ActualTransaction } from "@common/types.ts"
|
||||||
|
import type { SB1Transaction } from "@sb1/types.ts"
|
||||||
|
import type { UUID } from "node:crypto"
|
||||||
|
|
||||||
|
export function bankTransactionIntoActualTransaction(
|
||||||
|
transaction: SB1Transaction,
|
||||||
|
): ActualTransaction {
|
||||||
|
return {
|
||||||
|
id: transaction.id,
|
||||||
|
// Transactions with the same id will be ignored
|
||||||
|
imported_id: transaction.nonUniqueId,
|
||||||
|
account: getActualAccountId(transaction),
|
||||||
|
// The value without decimals
|
||||||
|
amount: Math.floor(transaction.amount * 100),
|
||||||
|
date: toISODateString(dayjs(transaction.date)),
|
||||||
|
payee_name: transaction.cleanedDescription,
|
||||||
|
cleared: isCleared(transaction),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new Error(
|
||||||
|
"Failed to find ActualAccountId, length of BANK_ACCOUNT_IDS and ACTUAL_ACCOUNT_IDS must match",
|
||||||
|
)
|
||||||
|
}
|
104
packages/sparebank1/sparebank1.ts
Normal file
104
packages/sparebank1/sparebank1.ts
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
import * as Transactions from "@sb1/transactions.ts"
|
||||||
|
import * as Oauth from "@sb1/oauth.ts"
|
||||||
|
import {
|
||||||
|
BANK_INITIAL_REFRESH_TOKEN,
|
||||||
|
BANK_OAUTH_CLIENT_ID,
|
||||||
|
BANK_OAUTH_CLIENT_SECRET,
|
||||||
|
DB_DIRECTORY,
|
||||||
|
DB_FILENAME,
|
||||||
|
} from "@/config.ts"
|
||||||
|
import {
|
||||||
|
clearTokens,
|
||||||
|
createDb,
|
||||||
|
fetchToken,
|
||||||
|
insertTokens,
|
||||||
|
} from "@sb1impl/db/queries.ts"
|
||||||
|
import { bankTransactionIntoActualTransaction } from "./mappings.ts"
|
||||||
|
import { createDirIfMissing } from "@/fs.ts"
|
||||||
|
import logger from "@common/logger.ts"
|
||||||
|
import dayjs from "dayjs"
|
||||||
|
|
||||||
|
import type { ActualTransaction, Bank, Interval } from "@common/types.ts"
|
||||||
|
import type { TokenResponse } from "@sb1impl/db/types.ts"
|
||||||
|
import type { OAuthTokenResponse } from "@sb1/types.ts"
|
||||||
|
import type { Database } from "better-sqlite3"
|
||||||
|
|
||||||
|
export class Sparebank1Impl implements Bank {
|
||||||
|
private readonly db: Database
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
createDirIfMissing(DB_DIRECTORY)
|
||||||
|
const databaseFilePath = `${DB_DIRECTORY}/${DB_FILENAME}.sqlite`
|
||||||
|
this.db = createDb(databaseFilePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO if not cleared rerun later
|
||||||
|
async fetchTransactions(
|
||||||
|
interval: Interval,
|
||||||
|
...accountKeys: ReadonlyArray<string>
|
||||||
|
): Promise<ReadonlyArray<ActualTransaction>> {
|
||||||
|
const response = await Transactions.list(
|
||||||
|
await this.getAccessToken(),
|
||||||
|
accountKeys,
|
||||||
|
interval,
|
||||||
|
)
|
||||||
|
|
||||||
|
return response.transactions
|
||||||
|
.map((transaction) => {
|
||||||
|
const actualTransaction =
|
||||||
|
bankTransactionIntoActualTransaction(transaction)
|
||||||
|
|
||||||
|
return actualTransaction.cleared === true ? actualTransaction : null
|
||||||
|
})
|
||||||
|
.filter((transaction) => transaction !== null)
|
||||||
|
}
|
||||||
|
|
||||||
|
shutdown(): void {
|
||||||
|
this.db.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getAccessToken(): Promise<string> {
|
||||||
|
const accessToken = fetchToken(this.db, "access-token")
|
||||||
|
|
||||||
|
if (accessToken && this.isValidToken(accessToken)) {
|
||||||
|
return accessToken.token
|
||||||
|
}
|
||||||
|
const response = await this.fetchNewTokens()
|
||||||
|
return response.access_token
|
||||||
|
}
|
||||||
|
|
||||||
|
private isValidToken(tokenResponse: TokenResponse): boolean {
|
||||||
|
// TODO make sure the same timezone is used. Db uses UTC
|
||||||
|
return dayjs().isBefore(tokenResponse.expires_at)
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getRefreshToken(): Promise<string> {
|
||||||
|
const tokenResponse = fetchToken(this.db, "refresh-token")
|
||||||
|
|
||||||
|
if (!tokenResponse) {
|
||||||
|
return BANK_INITIAL_REFRESH_TOKEN
|
||||||
|
} else if (this.isValidToken(tokenResponse)) {
|
||||||
|
return tokenResponse.token
|
||||||
|
}
|
||||||
|
logger.warn("Refresh token expired, deleting tokens from database")
|
||||||
|
clearTokens(this.db)
|
||||||
|
throw new Error("Refresh token is expired. Create a new one")
|
||||||
|
}
|
||||||
|
|
||||||
|
private async fetchNewTokens(): Promise<OAuthTokenResponse> {
|
||||||
|
const refreshToken = await this.getRefreshToken()
|
||||||
|
const result = await Oauth.refreshToken(
|
||||||
|
BANK_OAUTH_CLIENT_ID,
|
||||||
|
BANK_OAUTH_CLIENT_SECRET,
|
||||||
|
refreshToken,
|
||||||
|
)
|
||||||
|
|
||||||
|
if (result.status === "failure") {
|
||||||
|
throw new Error(`Failed to fetch refresh token: '${result.data}'`)
|
||||||
|
}
|
||||||
|
const oAuthToken = result.data
|
||||||
|
|
||||||
|
insertTokens(this.db, oAuthToken)
|
||||||
|
return oAuthToken
|
||||||
|
}
|
||||||
|
}
|
12
packages/sparebank1Api/common.ts
Normal file
12
packages/sparebank1Api/common.ts
Normal 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,
|
||||||
|
})
|
30
packages/sparebank1Api/oauth.ts
Normal file
30
packages/sparebank1Api/oauth.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import type { 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())
|
||||||
|
}
|
6
packages/sparebank1Api/package.json
Normal file
6
packages/sparebank1Api/package.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"name": "packages",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"license": "ISC"
|
||||||
|
}
|
36
packages/sparebank1Api/transactions.ts
Normal file
36
packages/sparebank1Api/transactions.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import type { TransactionResponse } from "./types"
|
||||||
|
import * as querystring from "node:querystring"
|
||||||
|
import { toISODateString } from "@common/date"
|
||||||
|
import logger from "@common/logger"
|
||||||
|
import { baseUrl } from "./common"
|
||||||
|
import type { Interval } from "@common/types.ts"
|
||||||
|
|
||||||
|
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.info(`GET '${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: [] }
|
||||||
|
}
|
||||||
|
}
|
40
packages/sparebank1Api/types.ts
Normal file
40
packages/sparebank1Api/types.ts
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
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 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"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 18-character unique ID used to identify a transaction
|
||||||
|
* The value is "000000000000000000" until the transaction is booked, and might be set a few days later
|
||||||
|
*/
|
||||||
|
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>
|
||||||
|
}
|
845
pnpm-lock.yaml
generated
845
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
2
pnpm-workspace.yaml
Normal file
2
pnpm-workspace.yaml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
packages:
|
||||||
|
- "packages/*"
|
@ -4,38 +4,21 @@ 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 logger from "@common/logger.ts"
|
||||||
import { type UUID } from "node:crypto"
|
import type { UUID } from "node:crypto"
|
||||||
import logger from "@/logger.ts"
|
import type {
|
||||||
|
Actual,
|
||||||
export interface Actual {
|
ActualTransaction,
|
||||||
importTransactions: (
|
ImportTransactionsResponse,
|
||||||
accountId: UUID,
|
} from "@common/types.ts"
|
||||||
transactions: ReadonlyArray<ActualTransaction>,
|
import { createDirIfMissing } from "@/fs.ts"
|
||||||
) => Promise<ImportTransactionsResponse>
|
|
||||||
|
|
||||||
shutdown: () => Promise<void>
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ActualTransaction extends TransactionEntity {
|
|
||||||
payee_name?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Message {
|
|
||||||
message: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ImportTransactionsResponse {
|
|
||||||
errors?: Message[]
|
|
||||||
added: number
|
|
||||||
updated: number
|
|
||||||
}
|
|
||||||
|
|
||||||
export class ActualImpl implements Actual {
|
export class ActualImpl implements Actual {
|
||||||
private constructor() {}
|
private constructor() {}
|
||||||
|
|
||||||
static async init(): Promise<Actual> {
|
static async init(): Promise<Actual> {
|
||||||
|
createDirIfMissing(ACTUAL_DATA_DIR)
|
||||||
await actual.init({
|
await actual.init({
|
||||||
// Budget data will be cached locally here, in subdirectories for each file.
|
// Budget data will be cached locally here, in subdirectories for each file.
|
||||||
dataDir: ACTUAL_DATA_DIR,
|
dataDir: ACTUAL_DATA_DIR,
|
||||||
@ -49,14 +32,30 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
async shutdown(): Promise<void> {
|
async shutdown(): Promise<void> {
|
||||||
|
logger.info(`Shutting down ActualBudget API for ${ACTUAL_SERVER_URL}`)
|
||||||
return actual.shutdown()
|
return actual.shutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,109 +0,0 @@
|
|||||||
import {
|
|
||||||
BANK_INITIAL_REFRESH_TOKEN,
|
|
||||||
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 {
|
|
||||||
fetchToken,
|
|
||||||
insertTokens,
|
|
||||||
type TokenResponse,
|
|
||||||
} from "@/bank/db/queries.ts"
|
|
||||||
import * as Api from "./sparebank1Api.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
|
|
||||||
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 {
|
|
||||||
private readonly db: Database
|
|
||||||
|
|
||||||
constructor(db: Database) {
|
|
||||||
this.db = db
|
|
||||||
}
|
|
||||||
|
|
||||||
private async getAccessToken(): Promise<string> {
|
|
||||||
const accessToken = fetchToken(this.db, "access-token")
|
|
||||||
|
|
||||||
if (accessToken && this.isValidToken(accessToken)) {
|
|
||||||
return accessToken.token
|
|
||||||
}
|
|
||||||
const response = await this.fetchNewTokens()
|
|
||||||
return response.access_token
|
|
||||||
}
|
|
||||||
|
|
||||||
private isValidToken(tokenResponse: TokenResponse): boolean {
|
|
||||||
// TODO make sure the same timezone is used. Db uses UTC
|
|
||||||
return dayjs().isBefore(tokenResponse.expires_at)
|
|
||||||
}
|
|
||||||
|
|
||||||
private async getRefreshToken(): Promise<string> {
|
|
||||||
const tokenResponse = fetchToken(this.db, "refresh-token")
|
|
||||||
|
|
||||||
if (!tokenResponse) {
|
|
||||||
return BANK_INITIAL_REFRESH_TOKEN
|
|
||||||
} else if (this.isValidToken(tokenResponse)) {
|
|
||||||
return tokenResponse.token
|
|
||||||
}
|
|
||||||
// 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")
|
|
||||||
}
|
|
||||||
|
|
||||||
async fetchNewTokens(): Promise<OAuthTokenResponse> {
|
|
||||||
const refreshToken = await this.getRefreshToken()
|
|
||||||
const result = await Api.refreshToken(refreshToken)
|
|
||||||
|
|
||||||
if (result.status === "failure") {
|
|
||||||
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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
@ -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())
|
|
||||||
}
|
|
@ -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",
|
@ -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.
|
||||||
|
10
src/fs.ts
Normal file
10
src/fs.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import * as fs from "node:fs"
|
||||||
|
import logger from "@common/logger"
|
||||||
|
|
||||||
|
// TODO move to common
|
||||||
|
export function createDirIfMissing(directory: string): void {
|
||||||
|
if (!fs.existsSync(directory)) {
|
||||||
|
logger.info(`Missing '${directory}', creating...`)
|
||||||
|
fs.mkdirSync(directory, { recursive: true })
|
||||||
|
}
|
||||||
|
}
|
@ -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,
|
|
||||||
})
|
|
185
src/main.ts
185
src/main.ts
@ -1,102 +1,127 @@
|
|||||||
import { type Actual, ActualImpl } from "@/actual.ts"
|
import { ActualImpl } from "@/actual.ts"
|
||||||
import { cronJobDaily } from "@/cron.ts"
|
import { cronJobDaily } from "@/cron.ts"
|
||||||
import {
|
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,
|
BANK_ACCOUNT_IDS,
|
||||||
DB_DIRECTORY,
|
TRANSACTION_RELATIVE_FROM_DATE,
|
||||||
DB_FILENAME,
|
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 * as fs from "node:fs"
|
|
||||||
import { CronJob } from "cron"
|
import { CronJob } from "cron"
|
||||||
|
import dayjs from "dayjs"
|
||||||
|
import type { Actual, Bank, Interval } from "@common/types.ts"
|
||||||
|
import { Sparebank1Impl } from "@sb1impl/sparebank1.ts"
|
||||||
|
|
||||||
// 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 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
|
||||||
|
|
||||||
export async function daily(actual: Actual, bank: Bank): Promise<void> {
|
|
||||||
// Fetch transactions from the bank
|
|
||||||
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,
|
|
||||||
): 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> {
|
async function main(): Promise<void> {
|
||||||
logger.info("Starting application")
|
logger.info("Starting application")
|
||||||
|
const bank = new Sparebank1Impl()
|
||||||
|
|
||||||
createDirIfMissing(ACTUAL_DATA_DIR)
|
|
||||||
createDirIfMissing(DB_DIRECTORY)
|
|
||||||
|
|
||||||
const actual = await ActualImpl.init()
|
|
||||||
const databaseFilePath = `${DB_DIRECTORY}/${DB_FILENAME}.sqlite`
|
|
||||||
const db = createDb(databaseFilePath)
|
|
||||||
logger.info(`Started Sqlite database at '${databaseFilePath}'`)
|
|
||||||
const bank = new Sparebank1Impl(db)
|
|
||||||
|
|
||||||
process.on("SIGINT", async () => {
|
|
||||||
logger.info("Caught interrupt signal")
|
|
||||||
await shutdown()
|
|
||||||
})
|
|
||||||
|
|
||||||
let cronJob: CronJob | undefined
|
|
||||||
if (process.env.ONCE) {
|
if (process.env.ONCE) {
|
||||||
await daily(actual, bank)
|
return await runOnce(bank)
|
||||||
await shutdown()
|
}
|
||||||
|
await ActualImpl.testConnection()
|
||||||
|
await runCronJob(bank)
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function moveTransactions(
|
||||||
|
actual: Actual,
|
||||||
|
bank: Bank,
|
||||||
|
): Promise<void> {
|
||||||
|
const actualTransactions = await bank.fetchTransactions(
|
||||||
|
relativeInterval(),
|
||||||
|
...BANK_ACCOUNT_IDS,
|
||||||
|
)
|
||||||
|
logger.info(`Fetched ${actualTransactions.length} transactions`)
|
||||||
|
|
||||||
|
if (actualTransactions.length === 0) {
|
||||||
|
logger.debug("No transactions to import, skipping")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Waiting for CRON job to start")
|
const transactionsByAccount = Object.groupBy(
|
||||||
// TODO init and shutdown resources when job runs?
|
actualTransactions,
|
||||||
cronJob = cronJobDaily(async () => await daily(actual, bank))
|
(transaction) => transaction.account,
|
||||||
|
)
|
||||||
|
|
||||||
async function shutdown(): Promise<void> {
|
const responses = await Promise.all(
|
||||||
logger.info("Shutting down, Bye!")
|
Object.entries(transactionsByAccount).map(([accountId, transactions]) =>
|
||||||
await actual.shutdown()
|
actual.importTransactions(accountId as UUID, transactions || []),
|
||||||
db.close()
|
),
|
||||||
cronJob?.stop()
|
)
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
responses.map((response) => ({
|
||||||
|
added: response.added,
|
||||||
|
updated: response.updated,
|
||||||
|
})),
|
||||||
|
"Finished importing transactions",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 runOnce(bank: Bank) {
|
||||||
|
const actual = await ActualImpl.init()
|
||||||
|
|
||||||
|
registerInterrupt(bank)
|
||||||
|
|
||||||
|
try {
|
||||||
|
return await moveTransactions(actual, bank)
|
||||||
|
} finally {
|
||||||
|
await actual.shutdown()
|
||||||
|
await shutdown(bank)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runCronJob(bank: Bank): Promise<void> {
|
||||||
|
logger.info("Waiting for CronJob to start")
|
||||||
|
|
||||||
|
const cronJob = cronJobDaily(async () => {
|
||||||
|
let actual: Actual | undefined
|
||||||
|
try {
|
||||||
|
actual = await ActualImpl.init()
|
||||||
|
await moveTransactions(actual, bank)
|
||||||
|
} catch (exception) {
|
||||||
|
logger.error(exception, "Caught exception at CronJob, shutting down!")
|
||||||
|
await shutdown(bank, cronJob)
|
||||||
|
} finally {
|
||||||
|
await actual?.shutdown()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
registerInterrupt(bank, cronJob)
|
||||||
|
}
|
||||||
|
|
||||||
|
let isShuttingDown = false
|
||||||
|
|
||||||
|
function registerInterrupt(
|
||||||
|
bank: Bank,
|
||||||
|
cronJob: CronJob | undefined = undefined,
|
||||||
|
): void {
|
||||||
|
process.on("SIGINT", async () => {
|
||||||
|
if (isShuttingDown) return
|
||||||
|
isShuttingDown = true
|
||||||
|
logger.info("Caught interrupt signal")
|
||||||
|
await shutdown(bank, cronJob)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async function shutdown(
|
||||||
|
bank: Bank,
|
||||||
|
cronJob: CronJob | undefined = undefined,
|
||||||
|
): Promise<void> {
|
||||||
|
logger.info("Shutting down, Bye!")
|
||||||
|
await bank.shutdown()
|
||||||
|
cronJob?.stop()
|
||||||
|
}
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
|
@ -1,29 +0,0 @@
|
|||||||
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"
|
|
||||||
|
|
||||||
export function bankTransactionIntoActualTransaction(
|
|
||||||
transaction: Transaction,
|
|
||||||
accountId: UUID,
|
|
||||||
): ActualTransaction {
|
|
||||||
return {
|
|
||||||
id: transaction.id,
|
|
||||||
// Transactions with the same id will be ignored
|
|
||||||
imported_id: transaction.nonUniqueId,
|
|
||||||
account: accountId,
|
|
||||||
// The value without decimals
|
|
||||||
amount: transaction.amount * 100,
|
|
||||||
date: toISODateString(dayjs(transaction.date)),
|
|
||||||
payee_name: transaction.cleanedDescription,
|
|
||||||
// TODO if not cleared or nonUniqueId is 0, rerun later
|
|
||||||
cleared: transaction.bookingStatus === "BOOKED",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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")
|
|
||||||
}
|
|
@ -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()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,41 +1,41 @@
|
|||||||
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",
|
amount: 100,
|
||||||
amount: 100,
|
...someFields,
|
||||||
...someFields,
|
},
|
||||||
},
|
{
|
||||||
{
|
id: "2",
|
||||||
id: "2",
|
nonUniqueId: "2",
|
||||||
nonUniqueId: "2",
|
amount: 200,
|
||||||
amount: 200,
|
...someFields,
|
||||||
...someFields,
|
},
|
||||||
},
|
{
|
||||||
{
|
id: "3",
|
||||||
id: "3",
|
nonUniqueId: "3",
|
||||||
nonUniqueId: "3",
|
amount: -50,
|
||||||
amount: -50,
|
...someFields,
|
||||||
...someFields,
|
},
|
||||||
},
|
]
|
||||||
],
|
return bankTransactions.map(bankTransactionIntoActualTransaction)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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,12 @@
|
|||||||
"strict": true,
|
"strict": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"allowImportingTsExtensions": true,
|
"allowImportingTsExtensions": true,
|
||||||
|
"noEmit": true,
|
||||||
"paths": {
|
"paths": {
|
||||||
"@/*": ["./src/*"]
|
"@/*": ["./src/*"],
|
||||||
|
"@common/*": ["./packages/common/*"],
|
||||||
|
"@sb1/*": ["./packages/sparebank1Api/*"],
|
||||||
|
"@sb1impl/*": ["./packages/sparebank1/*"]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"exclude": ["node_modules", "./*.ts", "__test__"]
|
"exclude": ["node_modules", "./*.ts", "__test__"]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user