- Moved mappings into Sb1 impl - Moved Actual types to @common - Moved createDirIfMissing to respective functions - Refactored main into multiple functions - Moved create db into Sb1impl and close - Log requests on info
47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import type { UUID } from "node:crypto"
|
|
import dayjs from "dayjs"
|
|
import { ACTUAL_ACCOUNT_IDS, BANK_ACCOUNT_IDS } from "@/config.ts"
|
|
import logger from "@common/logger.ts"
|
|
import { toISODateString } from "@common/date.ts"
|
|
import type { SB1Transaction } from "@sb1/types.ts"
|
|
import type { ActualTransaction } from "@common/types.ts"
|
|
|
|
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,
|
|
// TODO if not cleared or nonUniqueId is 0, rerun later
|
|
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
|
|
}
|
|
}
|
|
const error = new Error(
|
|
"Failed to find ActualAccountId, length of BANK_ACCOUNT_IDS and ACTUAL_ACCOUNT_IDS must match",
|
|
)
|
|
logger.error(error)
|
|
throw error
|
|
}
|