2025-02-09 13:01:34 +01:00
|
|
|
import { ACTUAL_ACCOUNT_IDS, BANK_ACCOUNT_IDS } from "@/config.ts"
|
2025-02-09 12:35:08 +01:00
|
|
|
import { toISODateString } from "@common/date.ts"
|
2025-02-16 18:22:10 +01:00
|
|
|
import dayjs from "dayjs"
|
|
|
|
|
2025-02-13 21:07:30 +01:00
|
|
|
import type { ActualTransaction } from "@common/types.ts"
|
2025-02-16 18:22:10 +01:00
|
|
|
import type { SB1Transaction } from "@sb1/types.ts"
|
|
|
|
import type { UUID } from "node:crypto"
|
2024-11-17 22:27:29 +01:00
|
|
|
|
2024-12-01 20:48:42 +01:00
|
|
|
export function bankTransactionIntoActualTransaction(
|
2025-02-09 12:35:08 +01:00
|
|
|
transaction: SB1Transaction,
|
2025-01-25 18:01:47 +01:00
|
|
|
): ActualTransaction {
|
2024-11-17 22:27:29 +01:00
|
|
|
return {
|
|
|
|
id: transaction.id,
|
2024-12-01 20:48:42 +01:00
|
|
|
// Transactions with the same id will be ignored
|
2025-01-25 18:01:47 +01:00
|
|
|
imported_id: transaction.nonUniqueId,
|
2025-02-02 12:37:43 +01:00
|
|
|
account: getActualAccountId(transaction),
|
2024-12-01 20:48:42 +01:00
|
|
|
// The value without decimals
|
2025-02-02 12:37:43 +01:00
|
|
|
amount: Math.floor(transaction.amount * 100),
|
2025-01-25 18:01:47 +01:00
|
|
|
date: toISODateString(dayjs(transaction.date)),
|
|
|
|
payee_name: transaction.cleanedDescription,
|
2025-02-02 12:37:43 +01:00
|
|
|
cleared: isCleared(transaction),
|
2024-11-17 22:27:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-09 12:35:08 +01:00
|
|
|
export function isCleared(transaction: SB1Transaction): boolean {
|
2025-02-02 12:37:43 +01:00
|
|
|
const id = Number(transaction.nonUniqueId)
|
|
|
|
return transaction.bookingStatus === "BOOKED" && !Number.isNaN(id) && id > 0
|
|
|
|
}
|
|
|
|
|
2025-02-09 12:35:08 +01:00
|
|
|
function getActualAccountId(transcation: SB1Transaction): UUID {
|
2025-02-02 12:37:43 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2025-02-16 18:22:10 +01:00
|
|
|
throw new Error(
|
2025-02-02 12:37:43 +01:00
|
|
|
"Failed to find ActualAccountId, length of BANK_ACCOUNT_IDS and ACTUAL_ACCOUNT_IDS must match",
|
|
|
|
)
|
2024-11-17 22:27:29 +01:00
|
|
|
}
|