Compare commits
3 Commits
41cb92cce5
...
75ad4946d2
Author | SHA1 | Date | |
---|---|---|---|
75ad4946d2 | |||
14257fa058 | |||
9b0391be7c |
27
flake.lock
generated
Normal file
27
flake.lock
generated
Normal file
@ -0,0 +1,27 @@
|
||||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1738277201,
|
||||
"narHash": "sha256-6L+WXKCw5mqnUIExvqkD99pJQ41xgyCk6z/H9snClwk=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "666e1b3f09c267afd66addebe80fb05a5ef2b554",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixos-24.11",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
28
flake.nix
Normal file
28
flake.nix
Normal file
@ -0,0 +1,28 @@
|
||||
{
|
||||
inputs = {
|
||||
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
|
||||
};
|
||||
|
||||
outputs = inputs@{ nixpkgs, ... }:
|
||||
let
|
||||
system = "x86_64-linux";
|
||||
in
|
||||
{
|
||||
devShells.${system}.default =
|
||||
let
|
||||
pkgs = import nixpkgs {
|
||||
inherit system;
|
||||
};
|
||||
in
|
||||
pkgs.mkShell {
|
||||
packages = with pkgs; [
|
||||
git
|
||||
nodejs_22
|
||||
pnpm
|
||||
nodePackages.prettier
|
||||
];
|
||||
|
||||
shellHook = "fish";
|
||||
};
|
||||
};
|
||||
}
|
@ -51,6 +51,8 @@ GET {{bankingBaseUrl}}/accounts
|
||||
Authorization: Bearer {{ACCESS_TOKEN}}
|
||||
|
||||
### 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}}
|
||||
Accept: application/vnd.sparebank1.v1+json; charset=utf-8
|
||||
|
@ -12,13 +12,14 @@ import logger from "@/logger.ts"
|
||||
export interface Actual {
|
||||
importTransactions: (
|
||||
accountId: UUID,
|
||||
transactions: ReadonlyArray<ActualTransaction>,
|
||||
transactions: Iterable<ActualTransaction>,
|
||||
) => Promise<ImportTransactionsResponse>
|
||||
|
||||
shutdown: () => Promise<void>
|
||||
}
|
||||
|
||||
export interface ActualTransaction extends TransactionEntity {
|
||||
account: UUID
|
||||
payee_name?: string
|
||||
}
|
||||
|
||||
@ -51,7 +52,7 @@ export class ActualImpl implements Actual {
|
||||
|
||||
async importTransactions(
|
||||
accountId: UUID,
|
||||
transactions: ReadonlyArray<ActualTransaction>,
|
||||
transactions: Iterable<ActualTransaction>,
|
||||
): Promise<ImportTransactionsResponse> {
|
||||
return actual.importTransactions(accountId, transactions)
|
||||
}
|
||||
|
@ -27,8 +27,12 @@ export type BookingStatus = "PENDING" | "BOOKED"
|
||||
export interface Transaction {
|
||||
id: string
|
||||
nonUniqueId: string
|
||||
date: number // Unix time
|
||||
amount: number // Amount in NOK
|
||||
// The Id of the account
|
||||
accountKey: string
|
||||
// Unix time
|
||||
date: number
|
||||
// Amount in NOK
|
||||
amount: number
|
||||
cleanedDescription: string
|
||||
remoteAccountName: string
|
||||
bookingStatus: BookingStatus
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { BANK_OAUTH_CLIENT_ID, BANK_OAUTH_CLIENT_SECRET } from "../../config.ts"
|
||||
import { BANK_OAUTH_CLIENT_ID, BANK_OAUTH_CLIENT_SECRET } from "@/../config.ts"
|
||||
import type {
|
||||
OAuthTokenResponse,
|
||||
TransactionResponse,
|
||||
@ -6,6 +6,7 @@ import type {
|
||||
import logger from "@/logger.ts"
|
||||
import { type Dayjs } from "dayjs"
|
||||
import { toISODateString } from "@/date.ts"
|
||||
import * as querystring from "node:querystring"
|
||||
|
||||
const baseUrl = "https://api.sparebank1.no"
|
||||
|
||||
@ -13,13 +14,8 @@ 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 }
|
||||
}
|
||||
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,
|
||||
@ -29,16 +25,15 @@ export async function transactions(
|
||||
toDate: Dayjs
|
||||
},
|
||||
): Promise<TransactionResponse> {
|
||||
const queries = new URLSearchParams({
|
||||
// TODO allow multiple accountKeys
|
||||
accountKey: typeof accountKeys === "string" ? accountKeys : accountKeys[0],
|
||||
const queryString = querystring.stringify({
|
||||
accountKey: accountKeys,
|
||||
...(timePeriod && {
|
||||
fromDate: toISODateString(timePeriod.fromDate),
|
||||
toDate: toISODateString(timePeriod.toDate),
|
||||
}),
|
||||
})
|
||||
|
||||
const url = `${baseUrl}/personal/banking/transactions?${queries}`
|
||||
const url = `${baseUrl}/personal/banking/transactions?${queryString}`
|
||||
logger.debug(`Sending GET request to '${url}'`)
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
@ -58,7 +53,7 @@ export async function transactions(
|
||||
export async function refreshToken(
|
||||
refreshToken: string,
|
||||
): Promise<Result<OAuthTokenResponse, string>> {
|
||||
const queries = new URLSearchParams({
|
||||
const queries = querystring.stringify({
|
||||
client_id: BANK_OAUTH_CLIENT_ID,
|
||||
client_secret: BANK_OAUTH_CLIENT_SECRET,
|
||||
refresh_token: refreshToken,
|
||||
|
38
src/main.ts
38
src/main.ts
@ -7,7 +7,6 @@ import {
|
||||
} from "@/bank/sparebank1.ts"
|
||||
import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
|
||||
import {
|
||||
ACTUAL_ACCOUNT_IDS,
|
||||
ACTUAL_DATA_DIR,
|
||||
BANK_ACCOUNT_IDS,
|
||||
DB_DIRECTORY,
|
||||
@ -21,7 +20,6 @@ import { CronJob } from "cron"
|
||||
|
||||
// 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 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
|
||||
@ -31,22 +29,23 @@ export async function daily(actual: Actual, bank: Bank): Promise<void> {
|
||||
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),
|
||||
bankTransactionIntoActualTransaction(transaction),
|
||||
)
|
||||
|
||||
// TODO Import transactions into Actual
|
||||
// If multiple accounts, loop over them
|
||||
// Get account ID from mapper
|
||||
|
||||
const response = await actual.importTransactions(
|
||||
accountId,
|
||||
const transactionsGroup = Object.groupBy(
|
||||
actualTransactions,
|
||||
(transaction) => transaction.account,
|
||||
)
|
||||
logger.info(`ImportTransactionsResponse=${JSON.stringify(response)}`)
|
||||
|
||||
const response = await Promise.all(
|
||||
Object.entries(transactionsGroup).map(([accountId, transactions]) =>
|
||||
actual.importTransactions(accountId as UUID, transactions || []),
|
||||
),
|
||||
)
|
||||
|
||||
logger.debug(response, "Finished importing transactions")
|
||||
}
|
||||
|
||||
async function fetchTransactionsFromPastDay(
|
||||
@ -82,14 +81,21 @@ async function main(): Promise<void> {
|
||||
|
||||
let cronJob: CronJob | undefined
|
||||
if (process.env.ONCE) {
|
||||
await daily(actual, bank)
|
||||
await shutdown()
|
||||
return
|
||||
try {
|
||||
return await daily(actual, bank)
|
||||
} finally {
|
||||
await shutdown()
|
||||
}
|
||||
}
|
||||
|
||||
logger.info("Waiting for CRON job to start")
|
||||
// TODO init and shutdown resources when job runs?
|
||||
cronJob = cronJobDaily(async () => await daily(actual, bank))
|
||||
try {
|
||||
cronJob = cronJobDaily(async () => await daily(actual, bank))
|
||||
} catch (exception) {
|
||||
logger.error(exception, "Caught exception at daily job, shutting down!")
|
||||
await shutdown()
|
||||
}
|
||||
|
||||
async function shutdown(): Promise<void> {
|
||||
logger.info("Shutting down, Bye!")
|
||||
|
@ -3,27 +3,44 @@ import type { UUID } from "node:crypto"
|
||||
import dayjs from "dayjs"
|
||||
import { toISODateString } from "@/date.ts"
|
||||
import { type ActualTransaction } from "@/actual.ts"
|
||||
import { ACTUAL_ACCOUNT_IDS, BANK_ACCOUNT_IDS } from "../config.ts"
|
||||
import logger from "@/logger.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,
|
||||
account: getActualAccountId(transaction),
|
||||
// The value without decimals
|
||||
amount: transaction.amount * 100,
|
||||
amount: Math.floor(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",
|
||||
cleared: isCleared(transaction),
|
||||
}
|
||||
}
|
||||
|
||||
// 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")
|
||||
export function isCleared(transaction: Transaction): boolean {
|
||||
const id = Number(transaction.nonUniqueId)
|
||||
return transaction.bookingStatus === "BOOKED" && !Number.isNaN(id) && id > 0
|
||||
}
|
||||
|
||||
function getActualAccountId(transcation: Transaction): UUID {
|
||||
for (
|
||||
let i = 0;
|
||||
i < Math.min(ACTUAL_ACCOUNT_IDS.length, BANK_ACCOUNT_IDS.length);
|
||||
i++
|
||||
) {
|
||||
if (BANK_ACCOUNT_IDS[i] === transcation.accountKey) {
|
||||
return ACTUAL_ACCOUNT_IDS[i] as UUID
|
||||
}
|
||||
}
|
||||
const error = new Error(
|
||||
"Failed to find ActualAccountId, length of BANK_ACCOUNT_IDS and ACTUAL_ACCOUNT_IDS must match",
|
||||
)
|
||||
logger.error(error)
|
||||
throw error
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user