Compare commits
No commits in common. "3c6cb193eb7f5b561acf9ec7a16312d8d02ee76f" and "efa9e785f233ab628a40522ed2e624071755e9ed" have entirely different histories.
3c6cb193eb
...
efa9e785f2
@ -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
|
12
Dockerfile
12
Dockerfile
@ -1,14 +1,8 @@
|
|||||||
FROM node:22-slim
|
FROM node:22-slim
|
||||||
LABEL authors="Martin Berg Alstad"
|
LABEL authors="Martin Berg Alstad"
|
||||||
|
|
||||||
COPY . ./app
|
COPY . .
|
||||||
WORKDIR ./app
|
|
||||||
|
|
||||||
ENV PNPM_HOME="/pnpm"
|
RUN --mount=type=cache,id=npm,target=/store npm install --omit=dev --frozen-lockfile
|
||||||
ENV PATH="$PNPM_HOME:$PATH"
|
|
||||||
RUN corepack enable pnpm
|
|
||||||
RUN corepack prepare pnpm@9.15.3 --activate
|
|
||||||
RUN npm i -g corepack@latest
|
|
||||||
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --recursive --frozen-lockfile
|
|
||||||
|
|
||||||
ENTRYPOINT ["pnpm", "start-prod"]
|
ENTRYPOINT ["npm", "run", "start-prod"]
|
@ -1,8 +1,10 @@
|
|||||||
# Sparebank1 ActualBudget Integration
|
# Sparebank1 ActualBudget Integration
|
||||||
|
|
||||||
|
🔧 WIP!
|
||||||
|
|
||||||
### Setting up the environment
|
### Setting up the environment
|
||||||
|
|
||||||
In order to start the application, an `.env.local` file must be present at the root level. The possible and required
|
In order to start the application, a `.env.local` file must be present at the root level. The possible and required
|
||||||
fields
|
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`.
|
||||||
|
|
||||||
|
@ -22,7 +22,6 @@ 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",
|
@ -2,11 +2,12 @@
|
|||||||
"name": "sparebank1_actual_budget_integration",
|
"name": "sparebank1_actual_budget_integration",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "dotenvx run --env-file=.env.local -- node --import=tsx ./src/main.ts | ./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 | ./packages/common/node_modules/pino-pretty/bin.js",
|
"run-once": "ONCE=true dotenvx run --env-file=.env.local -- node --import=tsx ./src/main.ts | pino-pretty",
|
||||||
"test": "dotenvx run --env-file=.env.test.local -- node --experimental-vm-modules node_modules/jest/bin/jest.js | ./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",
|
||||||
"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}\""
|
||||||
},
|
},
|
||||||
@ -20,6 +21,7 @@
|
|||||||
"cron": "^3.5.0",
|
"cron": "^3.5.0",
|
||||||
"dayjs": "^1.11.13",
|
"dayjs": "^1.11.13",
|
||||||
"dotenv": "^16.4.7",
|
"dotenv": "^16.4.7",
|
||||||
|
"pino": "^9.6.0",
|
||||||
"prettier": "^3.4.2",
|
"prettier": "^3.4.2",
|
||||||
"tsx": "^4.19.2"
|
"tsx": "^4.19.2"
|
||||||
},
|
},
|
||||||
@ -29,6 +31,7 @@
|
|||||||
"@types/jest": "^29.5.14",
|
"@types/jest": "^29.5.14",
|
||||||
"@types/node": "^22.10.7",
|
"@types/node": "^22.10.7",
|
||||||
"jest": "^29.7.0",
|
"jest": "^29.7.0",
|
||||||
|
"pino-pretty": "^13.0.0",
|
||||||
"ts-jest": "^29.2.5",
|
"ts-jest": "^29.2.5",
|
||||||
"ts-node": "^10.9.2",
|
"ts-node": "^10.9.2",
|
||||||
"typescript": "^5.7.3"
|
"typescript": "^5.7.3"
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "common",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "",
|
|
||||||
"license": "ISC",
|
|
||||||
"dependencies": {
|
|
||||||
"pino": "^9.6.0"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"pino-pretty": "^13.0.0"
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
import type { Failure, Success } from "./types"
|
|
||||||
|
|
||||||
export const baseUrl = "https://api.sparebank1.no"
|
|
||||||
|
|
||||||
export const success = <T>(data: T): Success<T> => ({
|
|
||||||
status: "success",
|
|
||||||
data: data,
|
|
||||||
})
|
|
||||||
export const failure = <T>(data: T): Failure<T> => ({
|
|
||||||
status: "failure",
|
|
||||||
data: data,
|
|
||||||
})
|
|
@ -1,30 +0,0 @@
|
|||||||
import { OAuthTokenResponse, Result } from "./types"
|
|
||||||
import * as querystring from "node:querystring"
|
|
||||||
import { baseUrl, failure, success } from "./common"
|
|
||||||
import logger from "@common/logger"
|
|
||||||
|
|
||||||
export async function refreshToken(
|
|
||||||
clientId: string,
|
|
||||||
clientSecret: string,
|
|
||||||
refreshToken: string,
|
|
||||||
): Promise<Result<OAuthTokenResponse, string>> {
|
|
||||||
const queries = querystring.stringify({
|
|
||||||
client_id: clientId,
|
|
||||||
client_secret: clientSecret,
|
|
||||||
refresh_token: refreshToken,
|
|
||||||
grant_type: "refresh_token",
|
|
||||||
})
|
|
||||||
const url = `${baseUrl}/oauth/token?${queries}`
|
|
||||||
logger.debug(`Sending POST request to url: '${url}'`)
|
|
||||||
const response = await fetch(url, {
|
|
||||||
method: "post",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/x-www-form-urlencoded",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
logger.debug(`Received response with status '${response.status}'`)
|
|
||||||
if (!response.ok) {
|
|
||||||
return failure(await response.text())
|
|
||||||
}
|
|
||||||
return success(await response.json())
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "packages",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "",
|
|
||||||
"license": "ISC"
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
import type { Interval, TransactionResponse } from "./types"
|
|
||||||
import * as querystring from "node:querystring"
|
|
||||||
import { toISODateString } from "@common/date"
|
|
||||||
import logger from "@common/logger"
|
|
||||||
import { baseUrl } from "./common"
|
|
||||||
|
|
||||||
export async function list(
|
|
||||||
accessToken: string,
|
|
||||||
accountKeys: string | ReadonlyArray<string>,
|
|
||||||
interval?: Interval,
|
|
||||||
): Promise<TransactionResponse> {
|
|
||||||
const queryString = querystring.stringify({
|
|
||||||
accountKey: accountKeys,
|
|
||||||
...(interval && {
|
|
||||||
fromDate: toISODateString(interval.fromDate),
|
|
||||||
toDate: toISODateString(interval.toDate),
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
|
|
||||||
const url = `${baseUrl}/personal/banking/transactions?${queryString}`
|
|
||||||
logger.debug(`Sending GET request to '${url}'`)
|
|
||||||
const response = await fetch(url, {
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${accessToken}`,
|
|
||||||
Accept: "application/vnd.sparebank1.v1+json;charset=utf-8",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
logger.debug(`Received response with status '${response.status}'`)
|
|
||||||
if (response.ok) {
|
|
||||||
return response.json()
|
|
||||||
} else {
|
|
||||||
logger.warn(await response.json())
|
|
||||||
return { transactions: [] }
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
import type { Dayjs } from "dayjs"
|
|
||||||
|
|
||||||
export type Success<T> = { status: "success"; data: T }
|
|
||||||
export type Failure<T> = { status: "failure"; data: T }
|
|
||||||
export type Result<OK, Err> = Success<OK> | Failure<Err>
|
|
||||||
|
|
||||||
export interface Interval {
|
|
||||||
fromDate: Dayjs
|
|
||||||
toDate: Dayjs
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface OAuthTokenResponse {
|
|
||||||
access_token: string
|
|
||||||
expires_in: number
|
|
||||||
refresh_token_expires_in: number
|
|
||||||
refresh_token_absolute_expires_in: number
|
|
||||||
token_type: "Bearer"
|
|
||||||
refresh_token: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export type BookingStatus = "PENDING" | "BOOKED"
|
|
||||||
|
|
||||||
export type NonUniqueId = "000000000000000000" | `${number}`
|
|
||||||
|
|
||||||
export interface SB1Transaction {
|
|
||||||
id: string
|
|
||||||
nonUniqueId: NonUniqueId
|
|
||||||
// The Id of the account
|
|
||||||
accountKey: string
|
|
||||||
// Unix time
|
|
||||||
date: number
|
|
||||||
// Amount in NOK
|
|
||||||
amount: number
|
|
||||||
cleanedDescription: string
|
|
||||||
remoteAccountName: string
|
|
||||||
bookingStatus: BookingStatus
|
|
||||||
|
|
||||||
[key: string]: string | number | boolean | unknown
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface TransactionResponse {
|
|
||||||
transactions: ReadonlyArray<SB1Transaction>
|
|
||||||
}
|
|
18
pnpm-lock.yaml
generated
18
pnpm-lock.yaml
generated
@ -26,6 +26,9 @@ importers:
|
|||||||
dotenv:
|
dotenv:
|
||||||
specifier: ^16.4.7
|
specifier: ^16.4.7
|
||||||
version: 16.4.7
|
version: 16.4.7
|
||||||
|
pino:
|
||||||
|
specifier: ^9.6.0
|
||||||
|
version: 9.6.0
|
||||||
prettier:
|
prettier:
|
||||||
specifier: ^3.4.2
|
specifier: ^3.4.2
|
||||||
version: 3.4.2
|
version: 3.4.2
|
||||||
@ -48,6 +51,9 @@ importers:
|
|||||||
jest:
|
jest:
|
||||||
specifier: ^29.7.0
|
specifier: ^29.7.0
|
||||||
version: 29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3))
|
version: 29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3))
|
||||||
|
pino-pretty:
|
||||||
|
specifier: ^13.0.0
|
||||||
|
version: 13.0.0
|
||||||
ts-jest:
|
ts-jest:
|
||||||
specifier: ^29.2.5
|
specifier: ^29.2.5
|
||||||
version: 29.2.5(@babel/core@7.26.0)(@jest/transform@29.7.0)(@jest/types@29.6.3)(babel-jest@29.7.0(@babel/core@7.26.0))(jest@29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3)))(typescript@5.7.3)
|
version: 29.2.5(@babel/core@7.26.0)(@jest/transform@29.7.0)(@jest/types@29.6.3)(babel-jest@29.7.0(@babel/core@7.26.0))(jest@29.7.0(@types/node@22.10.7)(ts-node@10.9.2(@types/node@22.10.7)(typescript@5.7.3)))(typescript@5.7.3)
|
||||||
@ -58,18 +64,6 @@ importers:
|
|||||||
specifier: ^5.7.3
|
specifier: ^5.7.3
|
||||||
version: 5.7.3
|
version: 5.7.3
|
||||||
|
|
||||||
packages/common:
|
|
||||||
dependencies:
|
|
||||||
pino:
|
|
||||||
specifier: ^9.6.0
|
|
||||||
version: 9.6.0
|
|
||||||
devDependencies:
|
|
||||||
pino-pretty:
|
|
||||||
specifier: ^13.0.0
|
|
||||||
version: 13.0.0
|
|
||||||
|
|
||||||
packages/sparebank1Api: {}
|
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
|
|
||||||
'@actual-app/api@25.1.0':
|
'@actual-app/api@25.1.0':
|
||||||
|
@ -1,2 +0,0 @@
|
|||||||
packages:
|
|
||||||
- "packages/*"
|
|
@ -4,10 +4,10 @@ import {
|
|||||||
ACTUAL_PASSWORD,
|
ACTUAL_PASSWORD,
|
||||||
ACTUAL_SERVER_URL,
|
ACTUAL_SERVER_URL,
|
||||||
ACTUAL_SYNC_ID,
|
ACTUAL_SYNC_ID,
|
||||||
} from "@/config.ts"
|
} from "../config.ts"
|
||||||
import type { TransactionEntity } from "@actual-app/api/@types/loot-core/types/models"
|
import type { TransactionEntity } from "@actual-app/api/@types/loot-core/types/models"
|
||||||
import { type UUID } from "node:crypto"
|
import { type UUID } from "node:crypto"
|
||||||
import logger from "@common/logger.ts"
|
import logger from "@/logger.ts"
|
||||||
|
|
||||||
export interface Actual {
|
export interface Actual {
|
||||||
importTransactions: (
|
importTransactions: (
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import Database from "better-sqlite3"
|
import Database from "better-sqlite3"
|
||||||
|
|
||||||
|
import { type OAuthTokenResponse } from "@/bank/sparebank1.ts"
|
||||||
import dayjs, { type Dayjs } from "dayjs"
|
import dayjs, { type Dayjs } from "dayjs"
|
||||||
import type { OAuthTokenResponse } from "@sb1/types.ts"
|
|
||||||
|
|
||||||
export type TokenResponse = {
|
export type TokenResponse = {
|
||||||
key: TokenKey
|
key: TokenKey
|
||||||
@ -9,8 +9,10 @@ export type TokenResponse = {
|
|||||||
expires_at: Dayjs
|
expires_at: Dayjs
|
||||||
}
|
}
|
||||||
|
|
||||||
type TokenResponseRaw = {
|
export type TokenResponseRaw = {
|
||||||
[K in keyof TokenResponse]: K extends "expires_at" ? string : TokenResponse[K]
|
key: TokenResponse["key"]
|
||||||
|
token: TokenResponse["token"]
|
||||||
|
expires_at: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TokenKey = "access-token" | "refresh-token"
|
export type TokenKey = "access-token" | "refresh-token"
|
||||||
|
@ -1,22 +1,47 @@
|
|||||||
import {
|
import { BANK_INITIAL_REFRESH_TOKEN } from "@/../config.ts"
|
||||||
BANK_INITIAL_REFRESH_TOKEN,
|
import logger from "@/logger.ts"
|
||||||
BANK_OAUTH_CLIENT_ID,
|
import dayjs, { Dayjs } from "dayjs"
|
||||||
BANK_OAUTH_CLIENT_SECRET,
|
import { Database } from "better-sqlite3"
|
||||||
} from "@/config.ts"
|
|
||||||
import logger from "@common/logger.ts"
|
|
||||||
import dayjs, { type Dayjs } from "dayjs"
|
|
||||||
import type { Database } from "better-sqlite3"
|
|
||||||
import {
|
import {
|
||||||
clearTokens,
|
clearTokens,
|
||||||
fetchToken,
|
fetchToken,
|
||||||
insertTokens,
|
insertTokens,
|
||||||
type TokenResponse,
|
type TokenResponse,
|
||||||
} from "@/bank/db/queries.ts"
|
} from "@/bank/db/queries.ts"
|
||||||
import * as Oauth from "@sb1/oauth.ts"
|
import * as Api from "./sparebank1Api.ts"
|
||||||
import * as Transactions from "@sb1/transactions.ts"
|
import { ActualTransaction } from "@/actual.ts"
|
||||||
import type { ActualTransaction } from "@/actual.ts"
|
|
||||||
import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
|
import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
|
||||||
import type { OAuthTokenResponse } from "@sb1/types.ts"
|
|
||||||
|
export interface OAuthTokenResponse {
|
||||||
|
access_token: string
|
||||||
|
expires_in: number
|
||||||
|
refresh_token_expires_in: number
|
||||||
|
refresh_token_absolute_expires_in: number
|
||||||
|
token_type: "Bearer"
|
||||||
|
refresh_token: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export type BookingStatus = "PENDING" | "BOOKED"
|
||||||
|
|
||||||
|
export interface Transaction {
|
||||||
|
id: string
|
||||||
|
nonUniqueId: string
|
||||||
|
// The Id of the account
|
||||||
|
accountKey: string
|
||||||
|
// Unix time
|
||||||
|
date: number
|
||||||
|
// Amount in NOK
|
||||||
|
amount: number
|
||||||
|
cleanedDescription: string
|
||||||
|
remoteAccountName: string
|
||||||
|
bookingStatus: BookingStatus
|
||||||
|
|
||||||
|
[key: string]: string | number | boolean | unknown
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TransactionResponse {
|
||||||
|
transactions: ReadonlyArray<Transaction>
|
||||||
|
}
|
||||||
|
|
||||||
export interface Bank {
|
export interface Bank {
|
||||||
fetchTransactions: (
|
fetchTransactions: (
|
||||||
@ -41,7 +66,7 @@ export class Sparebank1Impl implements Bank {
|
|||||||
interval: Interval,
|
interval: Interval,
|
||||||
...accountKeys: ReadonlyArray<string>
|
...accountKeys: ReadonlyArray<string>
|
||||||
): Promise<ReadonlyArray<ActualTransaction>> {
|
): Promise<ReadonlyArray<ActualTransaction>> {
|
||||||
const response = await Transactions.list(
|
const response = await Api.transactions(
|
||||||
await this.getAccessToken(),
|
await this.getAccessToken(),
|
||||||
accountKeys,
|
accountKeys,
|
||||||
interval,
|
interval,
|
||||||
@ -80,11 +105,7 @@ export class Sparebank1Impl implements Bank {
|
|||||||
|
|
||||||
private async fetchNewTokens(): Promise<OAuthTokenResponse> {
|
private async fetchNewTokens(): Promise<OAuthTokenResponse> {
|
||||||
const refreshToken = await this.getRefreshToken()
|
const refreshToken = await this.getRefreshToken()
|
||||||
const result = await Oauth.refreshToken(
|
const result = await Api.refreshToken(refreshToken)
|
||||||
BANK_OAUTH_CLIENT_ID,
|
|
||||||
BANK_OAUTH_CLIENT_SECRET,
|
|
||||||
refreshToken,
|
|
||||||
)
|
|
||||||
|
|
||||||
if (result.status === "failure") {
|
if (result.status === "failure") {
|
||||||
throw new Error(`Failed to fetch refresh token: '${result.data}'`)
|
throw new Error(`Failed to fetch refresh token: '${result.data}'`)
|
||||||
|
72
src/bank/sparebank1Api.ts
Normal file
72
src/bank/sparebank1Api.ts
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
import { BANK_OAUTH_CLIENT_ID, BANK_OAUTH_CLIENT_SECRET } from "@/../config.ts"
|
||||||
|
import type {
|
||||||
|
Interval,
|
||||||
|
OAuthTokenResponse,
|
||||||
|
TransactionResponse,
|
||||||
|
} from "@/bank/sparebank1.ts"
|
||||||
|
import logger from "@/logger.ts"
|
||||||
|
import { toISODateString } from "@/date.ts"
|
||||||
|
import * as querystring from "node:querystring"
|
||||||
|
|
||||||
|
const baseUrl = "https://api.sparebank1.no"
|
||||||
|
|
||||||
|
type Success<T> = { status: "success"; data: T }
|
||||||
|
type Failure<T> = { status: "failure"; data: T }
|
||||||
|
type Result<OK, Err> = Success<OK> | Failure<Err>
|
||||||
|
|
||||||
|
const success = <T>(data: T): Success<T> => ({ status: "success", data: data })
|
||||||
|
const failure = <T>(data: T): Failure<T> => ({ status: "failure", data: data })
|
||||||
|
|
||||||
|
export async function transactions(
|
||||||
|
accessToken: string,
|
||||||
|
accountKeys: string | ReadonlyArray<string>,
|
||||||
|
interval?: Interval,
|
||||||
|
): Promise<TransactionResponse> {
|
||||||
|
const queryString = querystring.stringify({
|
||||||
|
accountKey: accountKeys,
|
||||||
|
...(interval && {
|
||||||
|
fromDate: toISODateString(interval.fromDate),
|
||||||
|
toDate: toISODateString(interval.toDate),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
const url = `${baseUrl}/personal/banking/transactions?${queryString}`
|
||||||
|
logger.debug(`Sending GET request to '${url}'`)
|
||||||
|
const response = await fetch(url, {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${accessToken}`,
|
||||||
|
Accept: "application/vnd.sparebank1.v1+json;charset=utf-8",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
logger.debug(`Received response with status '${response.status}'`)
|
||||||
|
if (response.ok) {
|
||||||
|
return response.json()
|
||||||
|
} else {
|
||||||
|
logger.warn(await response.json())
|
||||||
|
return { transactions: [] }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function refreshToken(
|
||||||
|
refreshToken: string,
|
||||||
|
): Promise<Result<OAuthTokenResponse, string>> {
|
||||||
|
const queries = querystring.stringify({
|
||||||
|
client_id: BANK_OAUTH_CLIENT_ID,
|
||||||
|
client_secret: BANK_OAUTH_CLIENT_SECRET,
|
||||||
|
refresh_token: refreshToken,
|
||||||
|
grant_type: "refresh_token",
|
||||||
|
})
|
||||||
|
const url = `${baseUrl}/oauth/token?${queries}`
|
||||||
|
logger.debug(`Sending POST request to url: '${url}'`)
|
||||||
|
const response = await fetch(url, {
|
||||||
|
method: "post",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
logger.debug(`Received response with status '${response.status}'`)
|
||||||
|
if (!response.ok) {
|
||||||
|
return failure(await response.text())
|
||||||
|
}
|
||||||
|
return success(await response.json())
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
import { CronJob } from "cron"
|
import { CronJob } from "cron"
|
||||||
import logger from "@common/logger.ts"
|
import logger from "@/logger.ts"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run a function every day at 1 AM, Oslo time.
|
* Run a function every day at 1 AM, Oslo time.
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import * as fs from "node:fs"
|
import * as fs from "node:fs"
|
||||||
import logger from "@common/logger"
|
import logger from "./logger"
|
||||||
|
|
||||||
export function createDirsIfMissing(...directories: string[]): void {
|
export function createDirsIfMissing(...directories: string[]): void {
|
||||||
directories.forEach(createDirIfMissing)
|
directories.forEach(createDirIfMissing)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import pino from "pino"
|
import pino from "pino"
|
||||||
import { LOG_LEVEL } from "@/config.ts"
|
import { LOG_LEVEL } from "../config.ts"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* / Returns a logging instance with the default log-level "info"
|
* / Returns a logging instance with the default log-level "info"
|
||||||
@ -11,11 +11,7 @@ const logger = pino(
|
|||||||
)
|
)
|
||||||
|
|
||||||
console.log = function (...args): void {
|
console.log = function (...args): void {
|
||||||
if (args.length > 1) {
|
logger.info(args, args?.[0])
|
||||||
logger.info(args?.slice(1), args?.[0])
|
|
||||||
} else {
|
|
||||||
logger.info(args?.[0])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default logger
|
export default logger
|
@ -8,8 +8,8 @@ import {
|
|||||||
DB_FILENAME,
|
DB_FILENAME,
|
||||||
TRANSACTION_RELATIVE_FROM_DATE,
|
TRANSACTION_RELATIVE_FROM_DATE,
|
||||||
TRANSACTION_RELATIVE_TO_DATE,
|
TRANSACTION_RELATIVE_TO_DATE,
|
||||||
} from "@/config.ts"
|
} from "../config.ts"
|
||||||
import logger from "@common/logger.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 { createDb } from "@/bank/db/queries.ts"
|
||||||
import { CronJob } from "cron"
|
import { CronJob } from "cron"
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
|
import type { Transaction } from "@/bank/sparebank1.ts"
|
||||||
import type { UUID } from "node:crypto"
|
import type { UUID } from "node:crypto"
|
||||||
import dayjs from "dayjs"
|
import dayjs from "dayjs"
|
||||||
|
import { toISODateString } from "@/date.ts"
|
||||||
import { type ActualTransaction } from "@/actual.ts"
|
import { type ActualTransaction } from "@/actual.ts"
|
||||||
import { ACTUAL_ACCOUNT_IDS, BANK_ACCOUNT_IDS } from "@/config.ts"
|
import { ACTUAL_ACCOUNT_IDS, BANK_ACCOUNT_IDS } from "../config.ts"
|
||||||
import logger from "@common/logger.ts"
|
import logger from "@/logger.ts"
|
||||||
import { toISODateString } from "@common/date.ts"
|
|
||||||
import type { SB1Transaction } from "@sb1/types.ts"
|
|
||||||
|
|
||||||
export function bankTransactionIntoActualTransaction(
|
export function bankTransactionIntoActualTransaction(
|
||||||
transaction: SB1Transaction,
|
transaction: Transaction,
|
||||||
): ActualTransaction {
|
): ActualTransaction {
|
||||||
return {
|
return {
|
||||||
id: transaction.id,
|
id: transaction.id,
|
||||||
@ -23,12 +23,12 @@ export function bankTransactionIntoActualTransaction(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isCleared(transaction: SB1Transaction): boolean {
|
export function isCleared(transaction: Transaction): boolean {
|
||||||
const id = Number(transaction.nonUniqueId)
|
const id = Number(transaction.nonUniqueId)
|
||||||
return transaction.bookingStatus === "BOOKED" && !Number.isNaN(id) && id > 0
|
return transaction.bookingStatus === "BOOKED" && !Number.isNaN(id) && id > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
function getActualAccountId(transcation: SB1Transaction): UUID {
|
function getActualAccountId(transcation: Transaction): UUID {
|
||||||
for (
|
for (
|
||||||
let i = 0;
|
let i = 0;
|
||||||
i < Math.min(ACTUAL_ACCOUNT_IDS.length, BANK_ACCOUNT_IDS.length);
|
i < Math.min(ACTUAL_ACCOUNT_IDS.length, BANK_ACCOUNT_IDS.length);
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
import type { Bank, Interval } from "@/bank/sparebank1.ts"
|
import type {
|
||||||
|
Bank,
|
||||||
|
BookingStatus,
|
||||||
|
Interval,
|
||||||
|
Transaction,
|
||||||
|
} from "@/bank/sparebank1.ts"
|
||||||
import dayjs from "dayjs"
|
import dayjs from "dayjs"
|
||||||
import type { ActualTransaction } from "@/actual.ts"
|
import { ActualTransaction } from "@/actual.ts"
|
||||||
import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
|
import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
|
||||||
import type { BookingStatus, SB1Transaction } from "@sb1/types.ts"
|
|
||||||
|
|
||||||
export class BankStub implements Bank {
|
export class BankStub implements Bank {
|
||||||
async fetchTransactions(
|
async fetchTransactions(
|
||||||
@ -16,7 +20,7 @@ export class BankStub implements Bank {
|
|||||||
bookingStatus: "BOOKED" as BookingStatus,
|
bookingStatus: "BOOKED" as BookingStatus,
|
||||||
accountKey: "1",
|
accountKey: "1",
|
||||||
}
|
}
|
||||||
const bankTransactions: ReadonlyArray<SB1Transaction> = [
|
const bankTransactions: ReadonlyArray<Transaction> = [
|
||||||
{
|
{
|
||||||
id: "1",
|
id: "1",
|
||||||
nonUniqueId: "1",
|
nonUniqueId: "1",
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"include": ["./src/**/*.ts", "./packages/**/*.ts", "./tests/**/*.ts"],
|
"include": ["./src/**/*.ts", "./tests/**/*.ts"],
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "esnext",
|
"target": "esnext",
|
||||||
"module": "ESNext",
|
"module": "ESNext",
|
||||||
@ -9,11 +9,8 @@
|
|||||||
"strict": true,
|
"strict": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"allowImportingTsExtensions": true,
|
"allowImportingTsExtensions": true,
|
||||||
"noEmit": true,
|
|
||||||
"paths": {
|
"paths": {
|
||||||
"@/*": ["./src/*"],
|
"@/*": ["./src/*"]
|
||||||
"@common/*": ["./packages/common/*"],
|
|
||||||
"@sb1/*": ["./packages/sparebank1Api/*"]
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"exclude": ["node_modules", "./*.ts", "__test__"]
|
"exclude": ["node_modules", "./*.ts", "__test__"]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user