Compare commits
20 Commits
9ed0a19393
...
bun-rewrit
Author | SHA1 | Date | |
---|---|---|---|
f5a9e2fc51
|
|||
41cb92cce5
|
|||
18a0fbfac8
|
|||
752bdbceb4
|
|||
61f0153319
|
|||
d618438b14
|
|||
9850017e3a
|
|||
aaa85e99cb
|
|||
2e73baf98b
|
|||
4977e7ad6a
|
|||
b61903f5c8
|
|||
9a00592a7a
|
|||
4a773e4b43
|
|||
3bf354b4bf
|
|||
8854a22b40
|
|||
6650e2cd2b
|
|||
480c0356f9
|
|||
29b394baf4
|
|||
966c5f50b3
|
|||
c43bff3e15 |
10
.dockerignore
Normal file
10
.dockerignore
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
.cache
|
||||||
|
.idea
|
||||||
|
httpRequests
|
||||||
|
.env.*
|
||||||
|
*.sqlite
|
||||||
|
jest.config.ts
|
||||||
|
node_modules
|
||||||
|
.git
|
||||||
|
.gitignore
|
||||||
|
*.md
|
16
.env.example
16
.env.example
@ -1,10 +1,20 @@
|
|||||||
|
# Actual Budget
|
||||||
ACTUAL_BUDGET_ID=your-budget-id
|
ACTUAL_BUDGET_ID=your-budget-id
|
||||||
ACTUAL_SYNC_ID=your-sync-id
|
ACTUAL_SYNC_ID=your-sync-id
|
||||||
ACTUAL_SERVER_URL=your-server-url
|
ACTUAL_SERVER_URL=http://your-server-url:5006
|
||||||
ACTUAL_PASSWORD=your-password
|
ACTUAL_PASSWORD=your-password
|
||||||
|
ACTUAL_ACCOUNT_IDS=your-account-id1,your-account-id2
|
||||||
|
ACTUAL_DATA_DIR=.cache
|
||||||
# Bank
|
# Bank
|
||||||
|
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_STATE=your-state
|
||||||
BANK_OAUTH_REDIRECT_URI=your-redirect-uri
|
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
|
||||||
|
LOG_LEVEL=info# trace | error | warn | info | debug | trace
|
||||||
|
DB_DIRECTORY=data# Relative path, must not start or end with /
|
||||||
|
DB_FILENAME=default
|
||||||
|
TRANSACTION_RELATIVE_FROM_DATE=4
|
||||||
|
TRANSACTION_RELATIVE_TO_DATE=3
|
||||||
|
35
.gitea/workflows/deploy.yml
Normal file
35
.gitea/workflows/deploy.yml
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
name: Deploy application
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
deploy:
|
||||||
|
runs-on: host
|
||||||
|
|
||||||
|
env:
|
||||||
|
# Actual budget
|
||||||
|
ACTUAL_BUDGET_ID: ${{ secrets.ACTUAL_BUDGET_ID }}
|
||||||
|
ACTUAL_SYNC_ID: ${{ secrets.ACTUAL_SYNC_ID }}
|
||||||
|
ACTUAL_SERVER_URL: ${{ secrets.ACTUAL_SERVER_URL }}
|
||||||
|
ACTUAL_PASSWORD: ${{ secrets.ACTUAL_PASSWORD }}
|
||||||
|
ACTUAL_ACCOUNT_IDS: ${{ secrets.ACTUAL_ACCOUNT_IDS }}
|
||||||
|
ACTUAL_DATA_DIR: ${{ var.ACTUAL_DATA_DIR }}
|
||||||
|
# Bank
|
||||||
|
BANK_INITIAL_REFRESH_TOKEN: ${{ secrets.BANK_INITIAL_REFRESH_TOKEN }}
|
||||||
|
BANK_OAUTH_CLIENT_ID: ${{ secrets.BANK_OAUTH_CLIENT_ID }}
|
||||||
|
BANK_OAUTH_CLIENT_SECRET: ${{ secrets.BANK_OAUTH_CLIENT_SECRET }}
|
||||||
|
BANK_OAUTH_STATE: ${{ secrets.BANK_OAUTH_STATE }}
|
||||||
|
BANK_OAUTH_REDIRECT_URI: ${{ secrets.BANK_OAUTH_REDIRECT_URI }}
|
||||||
|
BANK_ACCOUNT_IDS: ${{ secrets.BANK_ACCOUNT_IDS }}
|
||||||
|
# Configuration
|
||||||
|
LOG_LEVEL: ${{ vars.LOG_LEVEL }}
|
||||||
|
DB_DIRECTORY: ${{ vars.DB_DIRECTORY }}
|
||||||
|
DB_FILENAME: ${{ vars.DB_FILENAME }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Check out repository code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: Run docker-compose
|
||||||
|
run: docker compose up -d --build
|
5
.gitignore
vendored
5
.gitignore
vendored
@ -174,3 +174,8 @@ dist
|
|||||||
|
|
||||||
# Finder (MacOS) folder config
|
# Finder (MacOS) folder config
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
|
# SQLite
|
||||||
|
*.sqlite
|
||||||
|
*.sqlite-shm
|
||||||
|
*.sqlite-wal
|
||||||
|
11
Dockerfile
Normal file
11
Dockerfile
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
FROM node:22-slim
|
||||||
|
LABEL authors="Martin Berg Alstad"
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
ENV PNPM_HOME="/pnpm"
|
||||||
|
ENV PATH="$PNPM_HOME:$PATH"
|
||||||
|
RUN corepack enable
|
||||||
|
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile
|
||||||
|
|
||||||
|
CMD ["pnpm", "start-prod"]
|
25
README.md
25
README.md
@ -1,3 +1,28 @@
|
|||||||
# Sparebank1 ActualBudget Integration
|
# Sparebank1 ActualBudget Integration
|
||||||
|
|
||||||
🔧 WIP!
|
🔧 WIP!
|
||||||
|
|
||||||
|
### 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
|
||||||
|
fields
|
||||||
|
can be found in the [.env.example](.env.example) file and `config.ts`.
|
||||||
|
|
||||||
|
For running integration tests, the `.env.test.local` file must be present at the root level, with Actual fields present.
|
||||||
|
|
||||||
|
HTTP requests can be used from an IDE via the .http files. Secrets must be placed in a file called
|
||||||
|
`http-client.private.env.json` in the [httpRequests](httpRequests) directory. See the .http files for required values.
|
||||||
|
|
||||||
|
### Running the application
|
||||||
|
|
||||||
|
Start the application using a CronJob that runs at a given time. Can be stopped using an interrupt (^C)
|
||||||
|
|
||||||
|
```shell
|
||||||
|
pnpm start
|
||||||
|
```
|
||||||
|
|
||||||
|
Start the application without a CronJob, it will run once, then shutdown.
|
||||||
|
|
||||||
|
```shell
|
||||||
|
pnpm run-once
|
||||||
|
```
|
||||||
|
35
config.ts
35
config.ts
@ -3,19 +3,41 @@ import dotenv from "dotenv"
|
|||||||
|
|
||||||
dotenv.config()
|
dotenv.config()
|
||||||
|
|
||||||
export const ACTUAL_BUDGET_ID = getOrThrow("ACTUAL_BUDGET_ID")
|
// Actual
|
||||||
export const ACTUAL_SYNC_ID = getOrThrow("ACTUAL_SYNC_ID")
|
export const ACTUAL_SYNC_ID = getOrThrow("ACTUAL_SYNC_ID")
|
||||||
export const ACTUAL_SERVER_URL = getOrThrow("ACTUAL_SERVER_URL")
|
export const ACTUAL_SERVER_URL = getOrThrow("ACTUAL_SERVER_URL")
|
||||||
export const ACTUAL_PASSWORD = getOrThrow("ACTUAL_PASSWORD")
|
export const ACTUAL_PASSWORD = getOrThrow("ACTUAL_PASSWORD")
|
||||||
export const ACTUAL_ACCOUNT_IDS = getArrayOrThrow("ACTUAL_ACCOUNT_IDS")
|
export const ACTUAL_ACCOUNT_IDS = getArrayOrThrow("ACTUAL_ACCOUNT_IDS")
|
||||||
export const ACTUAL_DATA_DIR = ".cache"
|
export const ACTUAL_DATA_DIR = getOrDefault("ACTUAL_DATA_DIR", ".cache")
|
||||||
|
|
||||||
|
// Bank
|
||||||
|
export const BANK_INITIAL_REFRESH_TOKEN = getOrThrow(
|
||||||
|
"BANK_INITIAL_REFRESH_TOKEN",
|
||||||
|
)
|
||||||
export const BANK_OAUTH_CLIENT_ID = getOrThrow("BANK_OAUTH_CLIENT_ID")
|
export const BANK_OAUTH_CLIENT_ID = getOrThrow("BANK_OAUTH_CLIENT_ID")
|
||||||
export const BANK_OAUTH_CLIENT_SECRET = getOrThrow("BANK_OAUTH_CLIENT_SECRET")
|
export const BANK_OAUTH_CLIENT_SECRET = getOrThrow("BANK_OAUTH_CLIENT_SECRET")
|
||||||
export const BANK_OAUTH_REDIRECT_URI = getOrThrow("BANK_OAUTH_REDIRECT_URI")
|
|
||||||
export const BANK_OAUTH_STATE = getOrThrow("BANK_OAUTH_STATE")
|
|
||||||
export const BANK_ACCOUNT_IDS = getArrayOrThrow("BANK_ACCOUNT_IDS")
|
export const BANK_ACCOUNT_IDS = getArrayOrThrow("BANK_ACCOUNT_IDS")
|
||||||
|
|
||||||
|
// Configuration
|
||||||
|
export const DB_DIRECTORY = getOrDefault("DB_DIRECTORY", "data")
|
||||||
|
export const DB_FILENAME = getOrDefault("DB_FILENAME", "default")
|
||||||
|
export const LOG_LEVEL = getOrDefault("LOG_LEVEL", "info")
|
||||||
|
// Relative number of days in the past to start fetching transactions from
|
||||||
|
export const TRANSACTION_RELATIVE_FROM_DATE = getNumberOrDefault(
|
||||||
|
"TRANSACTION_RELATIVE_FROM_DATE",
|
||||||
|
4,
|
||||||
|
)
|
||||||
|
// Relative number of days in the past to end fetching transactions from
|
||||||
|
export const TRANSACTION_RELATIVE_TO_DATE = getNumberOrDefault(
|
||||||
|
"TRANSACTION_RELATIVE_TO_DATE",
|
||||||
|
3,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Utility functions
|
||||||
|
function getOrDefault(key: string, def: string): string {
|
||||||
|
return process.env[key] || def
|
||||||
|
}
|
||||||
|
|
||||||
function getOrThrow(key: string): string {
|
function getOrThrow(key: string): string {
|
||||||
const value = process.env[key]
|
const value = process.env[key]
|
||||||
assert(value, `Missing environment variable: ${key}`)
|
assert(value, `Missing environment variable: ${key}`)
|
||||||
@ -25,3 +47,8 @@ function getOrThrow(key: string): string {
|
|||||||
function getArrayOrThrow(key: string): ReadonlyArray<string> {
|
function getArrayOrThrow(key: string): ReadonlyArray<string> {
|
||||||
return getOrThrow(key).split(",")
|
return getOrThrow(key).split(",")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getNumberOrDefault(key: string, def: number): number {
|
||||||
|
const num = Number(process.env[key])
|
||||||
|
return Number.isNaN(num) ? def : num
|
||||||
|
}
|
||||||
|
27
docker-compose.yml
Normal file
27
docker-compose.yml
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
services:
|
||||||
|
server:
|
||||||
|
container_name: actual_sparebank1_cronjob
|
||||||
|
restart: unless-stopped
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
environment:
|
||||||
|
- ACTUAL_BUDGET_ID
|
||||||
|
- ACTUAL_SYNC_ID
|
||||||
|
- ACTUAL_SERVER_URL
|
||||||
|
- ACTUAL_PASSWORD
|
||||||
|
- ACTUAL_ACCOUNT_IDS
|
||||||
|
- ACTUAL_DATA_DIR
|
||||||
|
- BANK_INITIAL_REFRESH_TOKEN
|
||||||
|
- BANK_OAUTH_CLIENT_ID
|
||||||
|
- BANK_OAUTH_CLIENT_SECRET
|
||||||
|
- BANK_OAUTH_STATE
|
||||||
|
- BANK_OAUTH_REDIRECT_URI
|
||||||
|
- BANK_ACCOUNT_IDS
|
||||||
|
- LOG_LEVEL
|
||||||
|
- DB_DIRECTORY # Required for Docker Compose
|
||||||
|
- DB_FILENAME
|
||||||
|
volumes:
|
||||||
|
- data:/${DB_DIRECTORY}
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
data:
|
27
flake.lock
generated
Normal file
27
flake.lock
generated
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1738680400,
|
||||||
|
"narHash": "sha256-ooLh+XW8jfa+91F1nhf9OF7qhuA/y1ChLx6lXDNeY5U=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "799ba5bffed04ced7067a91798353d360788b30d",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
25
flake.nix
Normal file
25
flake.nix
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
{
|
||||||
|
inputs = {
|
||||||
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs = inputs@{ nixpkgs, ... }:
|
||||||
|
let
|
||||||
|
system = "x86_64-linux";
|
||||||
|
in
|
||||||
|
{
|
||||||
|
devShells.${system}.default =
|
||||||
|
let
|
||||||
|
pkgs = import nixpkgs {
|
||||||
|
inherit system;
|
||||||
|
};
|
||||||
|
in
|
||||||
|
pkgs.mkShell {
|
||||||
|
packages = with pkgs; [
|
||||||
|
bun
|
||||||
|
];
|
||||||
|
|
||||||
|
shellHook = "fish";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
@ -11,13 +11,12 @@ GET {{oauthBaseUrl}}/authorize?client_id={{sparebank1OauthClientId}}&
|
|||||||
### OAuth2 Access Token Request
|
### OAuth2 Access Token Request
|
||||||
# Refresh token is valid for 365 days
|
# Refresh token is valid for 365 days
|
||||||
# Access token is valid for 10 minutes
|
# Access token is valid for 10 minutes
|
||||||
@authenticationCode=<insert code here>
|
|
||||||
POST {{oauthBaseUrl}}/token
|
POST {{oauthBaseUrl}}/token
|
||||||
Content-Type: application/x-www-form-urlencoded
|
Content-Type: application/x-www-form-urlencoded
|
||||||
|
|
||||||
client_id = {{sparebank1OauthClientId}} &
|
client_id = {{sparebank1OauthClientId}} &
|
||||||
client_secret = {{sparebank1OauthClientSecret}} &
|
client_secret = {{sparebank1OauthClientSecret}} &
|
||||||
code = {{authenticationCode}} &
|
code = {{sparebank1OauthAuthCode}} &
|
||||||
grant_type = authorization_code &
|
grant_type = authorization_code &
|
||||||
state = {{sparebank1OauthState}} &
|
state = {{sparebank1OauthState}} &
|
||||||
redirect_uri = {{sparebank1OauthRedirectUri}}
|
redirect_uri = {{sparebank1OauthRedirectUri}}
|
||||||
@ -43,7 +42,6 @@ grant_type = refresh_token
|
|||||||
%}
|
%}
|
||||||
|
|
||||||
### Hello World from Sparebank1
|
### Hello World from Sparebank1
|
||||||
|
|
||||||
GET https://api.sparebank1.no/common/helloworld
|
GET https://api.sparebank1.no/common/helloworld
|
||||||
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
|
||||||
@ -52,9 +50,7 @@ Accept: application/vnd.sparebank1.v1+json; charset=utf-8
|
|||||||
GET {{bankingBaseUrl}}/accounts
|
GET {{bankingBaseUrl}}/accounts
|
||||||
Authorization: Bearer {{ACCESS_TOKEN}}
|
Authorization: Bearer {{ACCESS_TOKEN}}
|
||||||
|
|
||||||
### Fetch all transactions of the previous day
|
### Fetch all transactions of specific days (inclusive)
|
||||||
# TODO date search not working?
|
GET {{bankingBaseUrl}}/transactions?accountKey={{brukskontoAccountKey}}&fromDate=2025-01-20&toDate=2025-01-22
|
||||||
GET {{bankingBaseUrl}}/transactions?accountKey={{brukskontoAccountKey}}&fromDate=2024-11-14&
|
|
||||||
toDate=2024-11-15
|
|
||||||
Authorization: Bearer {{ACCESS_TOKEN}}
|
Authorization: Bearer {{ACCESS_TOKEN}}
|
||||||
|
Accept: application/vnd.sparebank1.v1+json; charset=utf-8
|
||||||
|
22
jest.config.ts
Normal file
22
jest.config.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import type { JestConfigWithTsJest } from "ts-jest"
|
||||||
|
|
||||||
|
const config: JestConfigWithTsJest = {
|
||||||
|
verbose: true,
|
||||||
|
transform: {
|
||||||
|
"^.+\\.ts?$": [
|
||||||
|
"ts-jest",
|
||||||
|
{
|
||||||
|
useESM: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
extensionsToTreatAsEsm: [".ts"],
|
||||||
|
moduleNameMapper: {
|
||||||
|
"^(\\.{1,2}/.*)\\.js$": "$1",
|
||||||
|
// Resolve @/ module paths
|
||||||
|
"@/(.*)": "<rootDir>/src/$1",
|
||||||
|
},
|
||||||
|
setupFiles: ["<rootDir>/config.ts"],
|
||||||
|
}
|
||||||
|
|
||||||
|
export default config
|
1
modules.d.ts
vendored
1
modules.d.ts
vendored
@ -1 +0,0 @@
|
|||||||
// TODO
|
|
37
package.json
37
package.json
@ -4,24 +4,39 @@
|
|||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node --import=tsx ./src/main.ts | pino-pretty",
|
"preinstall": "npx only-allow pnpm",
|
||||||
"test": "node --test --experimental-strip-types ./tests/**",
|
"start": "dotenvx run --env-file=.env.local -- node --import=tsx ./src/main.ts | pino-pretty",
|
||||||
|
"start-prod": "node --import=tsx ./src/main.ts",
|
||||||
|
"run-once": "ONCE=true dotenvx run --env-file=.env.local -- node --import=tsx ./src/main.ts | pino-pretty",
|
||||||
|
"test": "dotenvx run --env-file=.env.test.local -- node --experimental-vm-modules node_modules/jest/bin/jest.js | pino-pretty",
|
||||||
|
"docker-build": "DB_DIRECTORY=data docker compose --env-file .env.local up -d --build",
|
||||||
"format": "prettier --write \"./**/*.{js,mjs,ts,md,json}\""
|
"format": "prettier --write \"./**/*.{js,mjs,ts,md,json}\""
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@actual-app/api": "^24.11.0",
|
"@actual-app/api": "^25.1.0",
|
||||||
"cron": "^3.2.1",
|
"@dotenvx/dotenvx": "^1.33.0",
|
||||||
"dotenv": "^16.4.5",
|
"better-sqlite3": "^11.8.1",
|
||||||
"pino": "^9.5.0",
|
"cron": "^3.5.0",
|
||||||
"prettier": "^3.3.3"
|
"dayjs": "^1.11.13",
|
||||||
|
"dotenv": "^16.4.7",
|
||||||
|
"pino": "^9.6.0",
|
||||||
|
"prettier": "^3.4.2",
|
||||||
|
"tsx": "^4.19.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^22.9.0",
|
"@jest/globals": "^29.7.0",
|
||||||
"tsx": "^4.19.2",
|
"@types/better-sqlite3": "^7.6.12",
|
||||||
"typescript": "^5.6.3"
|
"@types/jest": "^29.5.14",
|
||||||
|
"@types/node": "^22.10.7",
|
||||||
|
"jest": "^29.7.0",
|
||||||
|
"pino-pretty": "^13.0.0",
|
||||||
|
"ts-jest": "^29.2.5",
|
||||||
|
"ts-node": "^10.9.2",
|
||||||
|
"typescript": "^5.7.3",
|
||||||
|
"@types/bun": "latest"
|
||||||
},
|
},
|
||||||
"prettier": {
|
"prettier": {
|
||||||
"semi": false,
|
"semi": false,
|
||||||
@ -29,4 +44,4 @@
|
|||||||
"tabWidth": 2,
|
"tabWidth": 2,
|
||||||
"trailingComma": "all"
|
"trailingComma": "all"
|
||||||
}
|
}
|
||||||
}
|
}
|
2916
pnpm-lock.yaml
generated
2916
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -3,19 +3,25 @@ import {
|
|||||||
ACTUAL_DATA_DIR,
|
ACTUAL_DATA_DIR,
|
||||||
ACTUAL_PASSWORD,
|
ACTUAL_PASSWORD,
|
||||||
ACTUAL_SERVER_URL,
|
ACTUAL_SERVER_URL,
|
||||||
|
ACTUAL_SYNC_ID,
|
||||||
} from "../config.ts"
|
} from "../config.ts"
|
||||||
import type { TransactionEntity } from "@actual-app/api/@types/loot-core/types/models"
|
import type { 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"
|
||||||
|
|
||||||
export interface Actual {
|
export interface Actual {
|
||||||
importTransactions: (
|
importTransactions: (
|
||||||
accountId: UUID,
|
accountId: UUID,
|
||||||
transactions: ReadonlyArray<TransactionEntity>,
|
transactions: ReadonlyArray<ActualTransaction>,
|
||||||
) => Promise<ImportTransactionsResponse>
|
) => Promise<ImportTransactionsResponse>
|
||||||
|
|
||||||
shutdown: () => Promise<void>
|
shutdown: () => Promise<void>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ActualTransaction extends TransactionEntity {
|
||||||
|
payee_name?: string
|
||||||
|
}
|
||||||
|
|
||||||
export interface Message {
|
export interface Message {
|
||||||
message: string
|
message: string
|
||||||
}
|
}
|
||||||
@ -38,17 +44,24 @@ export class ActualImpl implements Actual {
|
|||||||
// This is the password you use to log into the server
|
// This is the password you use to log into the server
|
||||||
password: ACTUAL_PASSWORD,
|
password: ACTUAL_PASSWORD,
|
||||||
})
|
})
|
||||||
|
logger.info(`Initialized ActualBudget API for ${ACTUAL_SERVER_URL}`)
|
||||||
|
await this.downloadBudget()
|
||||||
return new ActualImpl()
|
return new ActualImpl()
|
||||||
}
|
}
|
||||||
|
|
||||||
async importTransactions(
|
async importTransactions(
|
||||||
accountId: UUID,
|
accountId: UUID,
|
||||||
transactions: ReadonlyArray<TransactionEntity>,
|
transactions: ReadonlyArray<ActualTransaction>,
|
||||||
): Promise<ImportTransactionsResponse> {
|
): Promise<ImportTransactionsResponse> {
|
||||||
return await actual.importTransactions(accountId, transactions)
|
return actual.importTransactions(accountId, transactions)
|
||||||
}
|
}
|
||||||
|
|
||||||
async shutdown() {
|
async shutdown(): Promise<void> {
|
||||||
return await actual.shutdown()
|
return actual.shutdown()
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async downloadBudget(): Promise<void> {
|
||||||
|
await actual.downloadBudget(ACTUAL_SYNC_ID)
|
||||||
|
logger.info(`Downloaded budget`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
84
src/bank/db/queries.ts
Normal file
84
src/bank/db/queries.ts
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
import Database from "better-sqlite3"
|
||||||
|
|
||||||
|
import { type OAuthTokenResponse } from "@/bank/sparebank1.ts"
|
||||||
|
import dayjs, { type Dayjs } from "dayjs"
|
||||||
|
|
||||||
|
export type TokenResponse = {
|
||||||
|
key: TokenKey
|
||||||
|
token: string
|
||||||
|
expires_at: Dayjs
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TokenResponseRaw = {
|
||||||
|
key: TokenResponse["key"]
|
||||||
|
token: TokenResponse["token"]
|
||||||
|
expires_at: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TokenKey = "access-token" | "refresh-token"
|
||||||
|
|
||||||
|
export function createDb(filepath: string) {
|
||||||
|
const db = new Database(filepath)
|
||||||
|
db.pragma("journal_mode = WAL")
|
||||||
|
db.exec(
|
||||||
|
"CREATE TABLE IF NOT EXISTS tokens ('key' VARCHAR PRIMARY KEY, token VARCHAR NOT NULL, expires_at DATETIME NOT NULL)",
|
||||||
|
)
|
||||||
|
return db
|
||||||
|
}
|
||||||
|
|
||||||
|
export function insertTokens(
|
||||||
|
db: Database.Database,
|
||||||
|
oAuthToken: OAuthTokenResponse,
|
||||||
|
): void {
|
||||||
|
insertAccessToken(db, oAuthToken.access_token, oAuthToken.expires_in)
|
||||||
|
insertRefreshToken(
|
||||||
|
db,
|
||||||
|
oAuthToken.refresh_token,
|
||||||
|
oAuthToken.refresh_token_absolute_expires_in,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function insertAccessToken(
|
||||||
|
db: Database.Database,
|
||||||
|
accessToken: string,
|
||||||
|
expiresIn: number,
|
||||||
|
) {
|
||||||
|
insert(db, "access-token", accessToken, expiresIn)
|
||||||
|
}
|
||||||
|
|
||||||
|
function insertRefreshToken(
|
||||||
|
db: Database.Database,
|
||||||
|
refreshToken: string,
|
||||||
|
expiresIn: number,
|
||||||
|
) {
|
||||||
|
insert(db, "refresh-token", refreshToken, expiresIn)
|
||||||
|
}
|
||||||
|
|
||||||
|
function insert(
|
||||||
|
db: Database.Database,
|
||||||
|
key: TokenKey,
|
||||||
|
token: string,
|
||||||
|
expiresIn: number,
|
||||||
|
) {
|
||||||
|
db.prepare("INSERT OR REPLACE INTO tokens VALUES (?, ?, ?)").run(
|
||||||
|
key,
|
||||||
|
token,
|
||||||
|
dayjs().add(expiresIn, "seconds").toISOString(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function fetchToken(
|
||||||
|
db: Database.Database,
|
||||||
|
tokenKey: TokenKey,
|
||||||
|
): TokenResponse | null {
|
||||||
|
const response = db
|
||||||
|
.prepare("SELECT * FROM tokens WHERE key = ?")
|
||||||
|
.get(tokenKey) as TokenResponseRaw | null
|
||||||
|
|
||||||
|
return (
|
||||||
|
response && {
|
||||||
|
...response,
|
||||||
|
expires_at: dayjs(response.expires_at),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
109
src/bank/sparebank1.ts
Normal file
109
src/bank/sparebank1.ts
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
80
src/bank/sparebank1Api.ts
Normal file
80
src/bank/sparebank1Api.ts
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
import { BANK_OAUTH_CLIENT_ID, BANK_OAUTH_CLIENT_SECRET } from "../../config.ts"
|
||||||
|
import type {
|
||||||
|
OAuthTokenResponse,
|
||||||
|
TransactionResponse,
|
||||||
|
} from "@/bank/sparebank1.ts"
|
||||||
|
import logger from "@/logger.ts"
|
||||||
|
import { type Dayjs } from "dayjs"
|
||||||
|
import { toISODateString } from "@/date.ts"
|
||||||
|
|
||||||
|
const baseUrl = "https://api.sparebank1.no"
|
||||||
|
|
||||||
|
type Success<T> = { status: "success"; data: T }
|
||||||
|
type Failure<T> = { status: "failure"; data: T }
|
||||||
|
type Result<OK, Err> = Success<OK> | Failure<Err>
|
||||||
|
|
||||||
|
function success<T>(data: T): Success<T> {
|
||||||
|
return { status: "success", data: data }
|
||||||
|
}
|
||||||
|
|
||||||
|
function failure<T>(data: T): Failure<T> {
|
||||||
|
return { status: "failure", data: data }
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function transactions(
|
||||||
|
accessToken: string,
|
||||||
|
accountKeys: string | ReadonlyArray<string>,
|
||||||
|
timePeriod?: {
|
||||||
|
fromDate: Dayjs
|
||||||
|
toDate: Dayjs
|
||||||
|
},
|
||||||
|
): Promise<TransactionResponse> {
|
||||||
|
const queries = new URLSearchParams({
|
||||||
|
// TODO allow multiple accountKeys
|
||||||
|
accountKey: typeof accountKeys === "string" ? accountKeys : accountKeys[0],
|
||||||
|
...(timePeriod && {
|
||||||
|
fromDate: toISODateString(timePeriod.fromDate),
|
||||||
|
toDate: toISODateString(timePeriod.toDate),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
const url = `${baseUrl}/personal/banking/transactions?${queries}`
|
||||||
|
logger.debug(`Sending GET request to '${url}'`)
|
||||||
|
const response = await fetch(url, {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${accessToken}`,
|
||||||
|
Accept: "application/vnd.sparebank1.v1+json;charset=utf-8",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
logger.debug(`Received response with status '${response.status}'`)
|
||||||
|
if (response.ok) {
|
||||||
|
return response.json()
|
||||||
|
} else {
|
||||||
|
logger.warn(await response.json())
|
||||||
|
return { transactions: [] }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function refreshToken(
|
||||||
|
refreshToken: string,
|
||||||
|
): Promise<Result<OAuthTokenResponse, string>> {
|
||||||
|
const queries = new URLSearchParams({
|
||||||
|
client_id: BANK_OAUTH_CLIENT_ID,
|
||||||
|
client_secret: BANK_OAUTH_CLIENT_SECRET,
|
||||||
|
refresh_token: refreshToken,
|
||||||
|
grant_type: "refresh_token",
|
||||||
|
})
|
||||||
|
const url = `${baseUrl}/oauth/token?${queries}`
|
||||||
|
logger.debug(`Sending POST request to url: '${url}'`)
|
||||||
|
const response = await fetch(url, {
|
||||||
|
method: "post",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
logger.debug(`Received response with status '${response.status}'`)
|
||||||
|
if (!response.ok) {
|
||||||
|
return failure(await response.text())
|
||||||
|
}
|
||||||
|
return success(await response.json())
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
import { CronJob } from "cron"
|
import { CronJob } from "cron"
|
||||||
|
import logger from "@/logger.ts"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run a function every day at 1 AM, Oslo time.
|
* Run a function every day at 1 AM, Oslo time.
|
||||||
@ -8,7 +9,11 @@ import { CronJob } from "cron"
|
|||||||
export function cronJobDaily(onTick: () => Promise<void>): CronJob {
|
export function cronJobDaily(onTick: () => Promise<void>): CronJob {
|
||||||
return CronJob.from({
|
return CronJob.from({
|
||||||
cronTime: "0 0 1 * * *",
|
cronTime: "0 0 1 * * *",
|
||||||
onTick,
|
onTick: async () => {
|
||||||
|
logger.info("Starting daily job")
|
||||||
|
await onTick()
|
||||||
|
logger.info("Finished daily job")
|
||||||
|
},
|
||||||
start: true,
|
start: true,
|
||||||
timeZone: "Europe/Oslo",
|
timeZone: "Europe/Oslo",
|
||||||
})
|
})
|
||||||
|
3
src/date.ts
Normal file
3
src/date.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import { type Dayjs } from "dayjs"
|
||||||
|
|
||||||
|
export const toISODateString = (day: Dayjs): string => day.format("YYYY-MM-DD")
|
9
src/logger.ts
Normal file
9
src/logger.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import pino from "pino"
|
||||||
|
import { LOG_LEVEL } from "../config.ts"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* / Returns a logging instance with the default log-level "info"
|
||||||
|
*/
|
||||||
|
export default pino({
|
||||||
|
level: LOG_LEVEL,
|
||||||
|
})
|
86
src/main.ts
86
src/main.ts
@ -1,22 +1,40 @@
|
|||||||
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, Sparebank1Impl, type Transaction } from "@/sparebank1.ts"
|
import {
|
||||||
|
type Bank,
|
||||||
|
Sparebank1Impl,
|
||||||
|
type Transaction,
|
||||||
|
} from "@/bank/sparebank1.ts"
|
||||||
import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
|
import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
|
||||||
import { ACTUAL_ACCOUNT_IDS, BANK_ACCOUNT_IDS } from "../config.ts"
|
import {
|
||||||
import logger from "pino"
|
ACTUAL_ACCOUNT_IDS,
|
||||||
|
ACTUAL_DATA_DIR,
|
||||||
|
BANK_ACCOUNT_IDS,
|
||||||
|
DB_DIRECTORY,
|
||||||
|
DB_FILENAME,
|
||||||
|
} from "../config.ts"
|
||||||
|
import logger from "@/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"
|
||||||
|
|
||||||
// TODO Transports api for pino https://github.com/pinojs/pino/blob/HEAD/docs/transports.md
|
// TODO Transports api for pino https://github.com/pinojs/pino/blob/HEAD/docs/transports.md
|
||||||
// TODO create .cache if missing
|
// TODO move tsx to devDependency. Requires ts support for Node with support for @ alias
|
||||||
|
// TODO global exception handler, log and graceful shutdown
|
||||||
|
// TODO verbatimSyntax in tsconfig, conflicts with jest
|
||||||
|
// TODO multi module project. Main | DAL | Sparebank1 impl
|
||||||
|
// TODO store last fetched date in db, and refetch from that date, if app has been offline for some time
|
||||||
|
|
||||||
export async function daily(actual: Actual, bank: Bank): Promise<void> {
|
export async function daily(actual: Actual, bank: Bank): Promise<void> {
|
||||||
// Fetch transactions from the bank
|
// Fetch transactions from the bank
|
||||||
const transactions = await fetchTransactionsFromPastDay(bank)
|
const transactions = await fetchTransactionsFromPastDay(bank)
|
||||||
logger().info(`Fetched ${transactions.length} transactions`)
|
logger.info(`Fetched ${transactions.length} transactions`)
|
||||||
|
|
||||||
// TODO multiple accounts
|
// TODO multiple accounts
|
||||||
const accountId = ACTUAL_ACCOUNT_IDS[0] as UUID
|
const accountId = ACTUAL_ACCOUNT_IDS[0] as UUID
|
||||||
const actualTransactions = transactions.map((transaction) =>
|
const actualTransactions = transactions.map((transaction) =>
|
||||||
|
// TODO move to Bank interface?
|
||||||
bankTransactionIntoActualTransaction(transaction, accountId),
|
bankTransactionIntoActualTransaction(transaction, accountId),
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,31 +42,61 @@ export async function daily(actual: Actual, bank: Bank): Promise<void> {
|
|||||||
// If multiple accounts, loop over them
|
// If multiple accounts, loop over them
|
||||||
// Get account ID from mapper
|
// Get account ID from mapper
|
||||||
|
|
||||||
// TODO TypeError: Cannot read properties of undefined (reading 'timestamp')
|
const response = await actual.importTransactions(
|
||||||
await actual.importTransactions(accountId, actualTransactions)
|
accountId,
|
||||||
|
actualTransactions,
|
||||||
|
)
|
||||||
|
logger.info(`ImportTransactionsResponse=${JSON.stringify(response)}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchTransactionsFromPastDay(
|
async function fetchTransactionsFromPastDay(
|
||||||
bank: Bank,
|
bank: Bank,
|
||||||
): Promise<ReadonlyArray<Transaction>> {
|
): Promise<ReadonlyArray<Transaction>> {
|
||||||
// TODO refresh token
|
const response = await bank.transactionsPastDay(...BANK_ACCOUNT_IDS)
|
||||||
const { access_token } = await bank.refreshToken("my_refresh_token")
|
return response.transactions
|
||||||
return bank.transactionsPastDay(BANK_ACCOUNT_IDS, access_token)
|
}
|
||||||
|
|
||||||
|
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 actual = await ActualImpl.init()
|
|
||||||
logger().info("Initialized Actual Budget API")
|
|
||||||
|
|
||||||
cronJobDaily(async () => {
|
createDirIfMissing(ACTUAL_DATA_DIR)
|
||||||
logger().info("Running daily job")
|
createDirIfMissing(DB_DIRECTORY)
|
||||||
await daily(actual, new Sparebank1Impl())
|
|
||||||
logger().info("Finished daily job")
|
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()
|
||||||
})
|
})
|
||||||
|
|
||||||
// logger().info("Shutting down")
|
let cronJob: CronJob | undefined
|
||||||
// await actual.shutdown()
|
if (process.env.ONCE) {
|
||||||
|
await daily(actual, bank)
|
||||||
|
await shutdown()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("Waiting for CRON job to start")
|
||||||
|
// TODO init and shutdown resources when job runs?
|
||||||
|
cronJob = cronJobDaily(async () => await daily(actual, bank))
|
||||||
|
|
||||||
|
async function shutdown(): Promise<void> {
|
||||||
|
logger.info("Shutting down, Bye!")
|
||||||
|
await actual.shutdown()
|
||||||
|
db.close()
|
||||||
|
cronJob?.stop()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
|
@ -1,21 +1,24 @@
|
|||||||
import type { Transaction } from "@/sparebank1.ts"
|
import type { Transaction } from "@/bank/sparebank1.ts"
|
||||||
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 dayjs from "dayjs"
|
||||||
|
import { toISODateString } from "@/date.ts"
|
||||||
|
import { type ActualTransaction } from "@/actual.ts"
|
||||||
|
|
||||||
// TODO more fields / correct fields?
|
|
||||||
export function bankTransactionIntoActualTransaction(
|
export function bankTransactionIntoActualTransaction(
|
||||||
transaction: Transaction,
|
transaction: Transaction,
|
||||||
accountId: UUID,
|
accountId: UUID,
|
||||||
): TransactionEntity {
|
): 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.id,
|
imported_id: transaction.nonUniqueId,
|
||||||
account: accountId,
|
account: accountId,
|
||||||
// The value without decimals
|
// The value without decimals
|
||||||
amount: transaction.amount * 100,
|
amount: transaction.amount * 100,
|
||||||
date: transaction.date,
|
date: toISODateString(dayjs(transaction.date)),
|
||||||
payee: transaction.description,
|
payee_name: transaction.cleanedDescription,
|
||||||
|
// TODO if not cleared or nonUniqueId is 0, rerun later
|
||||||
|
cleared: transaction.bookingStatus === "BOOKED",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,57 +0,0 @@
|
|||||||
// TODO move types
|
|
||||||
export interface OAuthTokenResponse {
|
|
||||||
access_token: string
|
|
||||||
expires_in: number
|
|
||||||
refresh_token_expires_in: number
|
|
||||||
refresh_token_absolute_expires_in: number
|
|
||||||
token_type: "Bearer"
|
|
||||||
refresh_token: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Transaction {
|
|
||||||
id: string
|
|
||||||
date: string
|
|
||||||
amount: number
|
|
||||||
description: string
|
|
||||||
cleanedDescription: string
|
|
||||||
remoteAccountName: string
|
|
||||||
|
|
||||||
[key: string]: string | number | boolean | unknown
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Bank = Sparebank1
|
|
||||||
|
|
||||||
export interface Sparebank1 {
|
|
||||||
accessToken: () => Promise<OAuthTokenResponse>
|
|
||||||
refreshToken: (refreshToken: string) => Promise<OAuthTokenResponse>
|
|
||||||
|
|
||||||
transactionsPastDay: (
|
|
||||||
accountKeys: ReadonlyArray<string> | string,
|
|
||||||
accessToken: string,
|
|
||||||
) => Promise<ReadonlyArray<Transaction>>
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Sparebank1Impl implements Sparebank1 {
|
|
||||||
private baseUrl = "https://api.sparebank1.no"
|
|
||||||
|
|
||||||
// TODO remove?
|
|
||||||
async accessToken(): Promise<OAuthTokenResponse> {
|
|
||||||
throw new Error("Not implemented")
|
|
||||||
|
|
||||||
// if (response.ok) {
|
|
||||||
// return await response.json()
|
|
||||||
// }
|
|
||||||
// throw new Error(`Failed to get access token. ${response.statusText}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
async refreshToken(refreshToken: string): Promise<OAuthTokenResponse> {
|
|
||||||
throw new Error("Not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
async transactionsPastDay(
|
|
||||||
accountKeys: ReadonlyArray<string> | string,
|
|
||||||
accessToken: string,
|
|
||||||
): Promise<ReadonlyArray<Transaction>> {
|
|
||||||
throw new Error("Not implemented")
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +1,16 @@
|
|||||||
import { describe, it } from "node:test"
|
import { describe, it } from "@jest/globals"
|
||||||
|
|
||||||
import { daily } from "@/main.ts"
|
import { daily } 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"
|
||||||
import assert from "node:assert"
|
|
||||||
|
// TODO testcontainers with Actual?
|
||||||
|
// TODO tests don't stop after completing
|
||||||
|
|
||||||
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 () => {
|
||||||
await daily(await ActualImpl.init(), new BankStub())
|
const actual = await ActualImpl.init()
|
||||||
assert.ok(true)
|
await daily(actual, new BankStub())
|
||||||
|
await actual.shutdown()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,49 +1,41 @@
|
|||||||
import type { Bank, OAuthTokenResponse, Transaction } from "@/sparebank1.ts"
|
import type {
|
||||||
|
Bank,
|
||||||
const tokenResponse: OAuthTokenResponse = {
|
BookingStatus,
|
||||||
access_token: "my_access_token",
|
TransactionResponse,
|
||||||
token_type: "Bearer",
|
} from "@/bank/sparebank1.ts"
|
||||||
expires_in: 3600,
|
import dayjs from "dayjs"
|
||||||
refresh_token: "my_refresh_token",
|
|
||||||
refresh_token_expires_in: 3600,
|
|
||||||
refresh_token_absolute_expires_in: 3600,
|
|
||||||
}
|
|
||||||
|
|
||||||
export class BankStub implements Bank {
|
export class BankStub implements Bank {
|
||||||
async accessToken(): Promise<OAuthTokenResponse> {
|
|
||||||
return tokenResponse
|
|
||||||
}
|
|
||||||
|
|
||||||
async refreshToken(_unused: string): Promise<OAuthTokenResponse> {
|
|
||||||
return tokenResponse
|
|
||||||
}
|
|
||||||
|
|
||||||
async transactionsPastDay(
|
async transactionsPastDay(
|
||||||
_accountIds: ReadonlyArray<string> | string,
|
_accountIds: ReadonlyArray<string> | string,
|
||||||
_accessToken: string,
|
): Promise<TransactionResponse> {
|
||||||
): Promise<ReadonlyArray<Transaction>> {
|
|
||||||
const someFields = {
|
const someFields = {
|
||||||
date: new Date().toDateString(),
|
date: dayjs("2019-08-20").unix(),
|
||||||
description: "Test transaction",
|
|
||||||
cleanedDescription: "Test transaction",
|
cleanedDescription: "Test transaction",
|
||||||
remoteAccountName: "Test account",
|
remoteAccountName: "Test account",
|
||||||
|
bookingStatus: "BOOKED" as BookingStatus,
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
transactions: [
|
||||||
|
{
|
||||||
|
id: "1",
|
||||||
|
nonUniqueId: "1",
|
||||||
|
amount: 100,
|
||||||
|
...someFields,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "2",
|
||||||
|
nonUniqueId: "2",
|
||||||
|
amount: 200,
|
||||||
|
...someFields,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "3",
|
||||||
|
nonUniqueId: "3",
|
||||||
|
amount: -50,
|
||||||
|
...someFields,
|
||||||
|
},
|
||||||
|
],
|
||||||
}
|
}
|
||||||
return [
|
|
||||||
{
|
|
||||||
id: "1",
|
|
||||||
amount: 100,
|
|
||||||
...someFields,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "2",
|
|
||||||
amount: 200,
|
|
||||||
...someFields,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "3",
|
|
||||||
amount: -50,
|
|
||||||
...someFields,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
|
"include": ["./src/**/*.ts", "./tests/**/*.ts"],
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "esnext",
|
"target": "esnext",
|
||||||
"module": "ESNext",
|
"module": "ESNext",
|
||||||
@ -8,11 +9,9 @@
|
|||||||
"strict": true,
|
"strict": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"allowImportingTsExtensions": true,
|
"allowImportingTsExtensions": true,
|
||||||
"verbatimModuleSyntax": true,
|
|
||||||
"paths": {
|
"paths": {
|
||||||
"@/*": [
|
"@/*": ["./src/*"]
|
||||||
"./src/*"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
"exclude": ["node_modules", "./*.ts", "__test__"]
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user