🎉 Allow syncing multiple accounts at once
All checks were successful
Deploy application / deploy (push) Successful in 3s
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:
parent
14257fa058
commit
75ad4946d2
@ -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,
|
||||
|
22
src/main.ts
22
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,
|
||||
@ -30,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(
|
||||
|
@ -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