|
| 1 | +import { |
| 2 | + printIban, |
| 3 | + amountToInteger, |
| 4 | + sortByBookingDateOrValueDate, |
| 5 | +} from '../utils.js'; |
| 6 | + |
| 7 | +/** @type {import('./bank.interface.js').IBank} */ |
| 8 | +export default { |
| 9 | + institutionIds: ['SPK_KARLSRUHE_KARSDE66XXX'], |
| 10 | + |
| 11 | + accessValidForDays: 90, |
| 12 | + |
| 13 | + normalizeAccount(account) { |
| 14 | + return { |
| 15 | + account_id: account.id, |
| 16 | + institution: account.institution, |
| 17 | + mask: account.iban.slice(-4), |
| 18 | + iban: account.iban, |
| 19 | + name: [account.name, printIban(account)].join(' '), |
| 20 | + official_name: account.product, |
| 21 | + type: 'checking', |
| 22 | + }; |
| 23 | + }, |
| 24 | + |
| 25 | + /** |
| 26 | + * Following the GoCardless documentation[0] we should prefer `bookingDate` |
| 27 | + * here, though some of their bank integrations uses the date field |
| 28 | + * differently from what's describen in their documentation and so it's |
| 29 | + * sometimes necessary to use `valueDate` instead. |
| 30 | + * |
| 31 | + * [0]: https://nordigen.zendesk.com/hc/en-gb/articles/7899367372829-valueDate-and-bookingDate-for-transactions |
| 32 | + */ |
| 33 | + normalizeTransaction(transaction, _booked) { |
| 34 | + const date = |
| 35 | + transaction.bookingDate || |
| 36 | + transaction.bookingDateTime || |
| 37 | + transaction.valueDate || |
| 38 | + transaction.valueDateTime; |
| 39 | + |
| 40 | + // If we couldn't find a valid date field we filter out this transaction |
| 41 | + // and hope that we will import it again once the bank has processed the |
| 42 | + // transaction further. |
| 43 | + if (!date) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + let remittanceInformationUnstructured; |
| 48 | + |
| 49 | + if (transaction.remittanceInformationUnstructured) { |
| 50 | + remittanceInformationUnstructured = |
| 51 | + transaction.remittanceInformationUnstructured; |
| 52 | + } else if (transaction.remittanceInformationStructured) { |
| 53 | + remittanceInformationUnstructured = |
| 54 | + transaction.remittanceInformationStructured; |
| 55 | + } else if (transaction.remittanceInformationStructuredArray?.length > 0) { |
| 56 | + remittanceInformationUnstructured = |
| 57 | + transaction.remittanceInformationStructuredArray?.join(' '); |
| 58 | + } |
| 59 | + |
| 60 | + if (transaction.additionalInformation) |
| 61 | + remittanceInformationUnstructured += |
| 62 | + ' ' + transaction.additionalInformation; |
| 63 | + |
| 64 | + const usefulCreditorName = |
| 65 | + transaction.ultimateCreditor || |
| 66 | + transaction.creditorName || |
| 67 | + transaction.debtorName; |
| 68 | + |
| 69 | + return { |
| 70 | + ...transaction, |
| 71 | + creditorName: usefulCreditorName, |
| 72 | + remittanceInformationUnstructured: remittanceInformationUnstructured, |
| 73 | + date: transaction.bookingDate || transaction.valueDate, |
| 74 | + }; |
| 75 | + }, |
| 76 | + |
| 77 | + sortTransactions(transactions = []) { |
| 78 | + return sortByBookingDateOrValueDate(transactions); |
| 79 | + }, |
| 80 | + |
| 81 | + /** |
| 82 | + * For SANDBOXFINANCE_SFIN0000 we don't know what balance was |
| 83 | + * after each transaction so we have to calculate it by getting |
| 84 | + * current balance from the account and subtract all the transactions |
| 85 | + * |
| 86 | + * As a current balance we use `interimBooked` balance type because |
| 87 | + * it includes transaction placed during current day |
| 88 | + */ |
| 89 | + calculateStartingBalance(sortedTransactions = [], balances = []) { |
| 90 | + const currentBalance = balances.find( |
| 91 | + (balance) => 'interimAvailable' === balance.balanceType, |
| 92 | + ); |
| 93 | + |
| 94 | + return sortedTransactions.reduce((total, trans) => { |
| 95 | + return total - amountToInteger(trans.transactionAmount.amount); |
| 96 | + }, amountToInteger(currentBalance.balanceAmount.amount)); |
| 97 | + }, |
| 98 | +}; |
0 commit comments