44 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

import { ACTUAL_ACCOUNT_IDS, BANK_ACCOUNT_IDS } from "@/config.ts"
import { toISODateString } from "@common/date.ts"
import dayjs from "dayjs"
import type { ActualTransaction } from "@common/types.ts"
import type { SB1Transaction } from "@sb1/types.ts"
import type { UUID } from "node:crypto"
export function bankTransactionIntoActualTransaction(
transaction: SB1Transaction,
): ActualTransaction {
return {
id: transaction.id,
// Transactions with the same id will be ignored
imported_id: transaction.nonUniqueId,
account: getActualAccountId(transaction),
// The value without decimals
amount: Math.floor(transaction.amount * 100),
date: toISODateString(dayjs(transaction.date)),
payee_name: transaction.cleanedDescription,
cleared: isCleared(transaction),
}
}
export function isCleared(transaction: SB1Transaction): boolean {
const id = Number(transaction.nonUniqueId)
return transaction.bookingStatus === "BOOKED" && !Number.isNaN(id) && id > 0
}
function getActualAccountId(transcation: SB1Transaction): 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
}
}
throw new Error(
"Failed to find ActualAccountId, length of BANK_ACCOUNT_IDS and ACTUAL_ACCOUNT_IDS must match",
)
}