🎉 Allow syncing multiple accounts at once
All checks were successful
Deploy application / deploy (push) Successful in 3s

- By defining multiple account keys in the .env, will fetch from all and upload to correct account
- If transaction id is 0, will not be marked as cleared
- Added accountKey to Transaction interface
This commit is contained in:
Martin Berg Alstad 2025-02-02 12:37:43 +01:00
parent 14257fa058
commit 75ad4946d2
Signed by: martials
GPG Key ID: A3824877B269F2E2
6 changed files with 56 additions and 37 deletions

View File

@ -51,6 +51,8 @@ GET {{bankingBaseUrl}}/accounts
Authorization: Bearer {{ACCESS_TOKEN}} Authorization: Bearer {{ACCESS_TOKEN}}
### Fetch all transactions of specific days (inclusive) ### Fetch all transactions of specific days (inclusive)
GET {{bankingBaseUrl}}/transactions?accountKey={{brukskontoAccountKey}}&fromDate=2025-01-20&toDate=2025-01-22 GET {{bankingBaseUrl}}/transactions?accountKey={{accountKey1}}&accountKey={{accountKey2}}&
fromDate=2025-01-20&
toDate=2025-01-24
Authorization: Bearer {{ACCESS_TOKEN}} Authorization: Bearer {{ACCESS_TOKEN}}
Accept: application/vnd.sparebank1.v1+json; charset=utf-8 Accept: application/vnd.sparebank1.v1+json; charset=utf-8

View File

@ -12,13 +12,14 @@ import logger from "@/logger.ts"
export interface Actual { export interface Actual {
importTransactions: ( importTransactions: (
accountId: UUID, accountId: UUID,
transactions: ReadonlyArray<ActualTransaction>, transactions: Iterable<ActualTransaction>,
) => Promise<ImportTransactionsResponse> ) => Promise<ImportTransactionsResponse>
shutdown: () => Promise<void> shutdown: () => Promise<void>
} }
export interface ActualTransaction extends TransactionEntity { export interface ActualTransaction extends TransactionEntity {
account: UUID
payee_name?: string payee_name?: string
} }
@ -51,7 +52,7 @@ export class ActualImpl implements Actual {
async importTransactions( async importTransactions(
accountId: UUID, accountId: UUID,
transactions: ReadonlyArray<ActualTransaction>, transactions: Iterable<ActualTransaction>,
): Promise<ImportTransactionsResponse> { ): Promise<ImportTransactionsResponse> {
return actual.importTransactions(accountId, transactions) return actual.importTransactions(accountId, transactions)
} }

View File

@ -27,8 +27,12 @@ export type BookingStatus = "PENDING" | "BOOKED"
export interface Transaction { export interface Transaction {
id: string id: string
nonUniqueId: string nonUniqueId: string
date: number // Unix time // The Id of the account
amount: number // Amount in NOK accountKey: string
// Unix time
date: number
// Amount in NOK
amount: number
cleanedDescription: string cleanedDescription: string
remoteAccountName: string remoteAccountName: string
bookingStatus: BookingStatus bookingStatus: BookingStatus

View File

@ -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 { import type {
OAuthTokenResponse, OAuthTokenResponse,
TransactionResponse, TransactionResponse,
@ -6,6 +6,7 @@ import type {
import logger from "@/logger.ts" import logger from "@/logger.ts"
import { type Dayjs } from "dayjs" import { type Dayjs } from "dayjs"
import { toISODateString } from "@/date.ts" import { toISODateString } from "@/date.ts"
import * as querystring from "node:querystring"
const baseUrl = "https://api.sparebank1.no" 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 Failure<T> = { status: "failure"; data: T }
type Result<OK, Err> = Success<OK> | Failure<Err> type Result<OK, Err> = Success<OK> | Failure<Err>
function success<T>(data: T): Success<T> { const success = <T>(data: T): Success<T> => ({ status: "success", data: data })
return { status: "success", data: data } const failure = <T>(data: T): Failure<T> => ({ status: "failure", data: data })
}
function failure<T>(data: T): Failure<T> {
return { status: "failure", data: data }
}
export async function transactions( export async function transactions(
accessToken: string, accessToken: string,
@ -29,16 +25,15 @@ export async function transactions(
toDate: Dayjs toDate: Dayjs
}, },
): Promise<TransactionResponse> { ): Promise<TransactionResponse> {
const queries = new URLSearchParams({ const queryString = querystring.stringify({
// TODO allow multiple accountKeys accountKey: accountKeys,
accountKey: typeof accountKeys === "string" ? accountKeys : accountKeys[0],
...(timePeriod && { ...(timePeriod && {
fromDate: toISODateString(timePeriod.fromDate), fromDate: toISODateString(timePeriod.fromDate),
toDate: toISODateString(timePeriod.toDate), 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}'`) logger.debug(`Sending GET request to '${url}'`)
const response = await fetch(url, { const response = await fetch(url, {
headers: { headers: {
@ -58,7 +53,7 @@ export async function transactions(
export async function refreshToken( export async function refreshToken(
refreshToken: string, refreshToken: string,
): Promise<Result<OAuthTokenResponse, string>> { ): Promise<Result<OAuthTokenResponse, string>> {
const queries = new URLSearchParams({ const queries = querystring.stringify({
client_id: BANK_OAUTH_CLIENT_ID, client_id: BANK_OAUTH_CLIENT_ID,
client_secret: BANK_OAUTH_CLIENT_SECRET, client_secret: BANK_OAUTH_CLIENT_SECRET,
refresh_token: refreshToken, refresh_token: refreshToken,

View File

@ -7,7 +7,6 @@ import {
} from "@/bank/sparebank1.ts" } from "@/bank/sparebank1.ts"
import { bankTransactionIntoActualTransaction } from "@/mappings.ts" import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
import { import {
ACTUAL_ACCOUNT_IDS,
ACTUAL_DATA_DIR, ACTUAL_DATA_DIR,
BANK_ACCOUNT_IDS, BANK_ACCOUNT_IDS,
DB_DIRECTORY, DB_DIRECTORY,
@ -30,22 +29,23 @@ export async function daily(actual: Actual, bank: Bank): Promise<void> {
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
const accountId = ACTUAL_ACCOUNT_IDS[0] as UUID
const actualTransactions = transactions.map((transaction) => const actualTransactions = transactions.map((transaction) =>
// TODO move to Bank interface? // TODO move to Bank interface?
bankTransactionIntoActualTransaction(transaction, accountId), bankTransactionIntoActualTransaction(transaction),
) )
// TODO Import transactions into Actual const transactionsGroup = Object.groupBy(
// If multiple accounts, loop over them
// Get account ID from mapper
const response = await actual.importTransactions(
accountId,
actualTransactions, 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( async function fetchTransactionsFromPastDay(

View File

@ -3,27 +3,44 @@ import type { UUID } from "node:crypto"
import dayjs from "dayjs" import dayjs from "dayjs"
import { toISODateString } from "@/date.ts" import { toISODateString } from "@/date.ts"
import { type ActualTransaction } from "@/actual.ts" import { type ActualTransaction } from "@/actual.ts"
import { ACTUAL_ACCOUNT_IDS, BANK_ACCOUNT_IDS } from "../config.ts"
import logger from "@/logger.ts"
export function bankTransactionIntoActualTransaction( export function bankTransactionIntoActualTransaction(
transaction: Transaction, transaction: Transaction,
accountId: UUID,
): ActualTransaction { ): ActualTransaction {
return { return {
id: transaction.id, id: transaction.id,
// Transactions with the same id will be ignored // Transactions with the same id will be ignored
imported_id: transaction.nonUniqueId, imported_id: transaction.nonUniqueId,
account: accountId, account: getActualAccountId(transaction),
// The value without decimals // The value without decimals
amount: transaction.amount * 100, amount: Math.floor(transaction.amount * 100),
date: toISODateString(dayjs(transaction.date)), date: toISODateString(dayjs(transaction.date)),
payee_name: transaction.cleanedDescription, payee_name: transaction.cleanedDescription,
// TODO if not cleared or nonUniqueId is 0, rerun later // TODO if not cleared or nonUniqueId is 0, rerun later
cleared: transaction.bookingStatus === "BOOKED", cleared: isCleared(transaction),
} }
} }
// TODO take the account from the bank and match it to the actual account export function isCleared(transaction: Transaction): boolean {
// Use ENV const id = Number(transaction.nonUniqueId)
export function bankAccountIntoActualAccount(account: string): string { return transaction.bookingStatus === "BOOKED" && !Number.isNaN(id) && id > 0
throw new Error("Not implemented") }
function getActualAccountId(transcation: 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
} }