Files
accounted/extensions/general/enable-banking/lib/transaction-label.ts
T
MattssonandClaude Opus 4.8 c6c86cded4 Mcp/template data feedback (#617)
* fix(booking-templates): scope template list to the active company

GET /api/settings/booking-templates relied solely on the btl_select RLS
policy, which is membership-wide (user_company_ids) and returns templates
from every company the user belongs to. A user who owns multiple companies
saw all their templates merged regardless of which company was active.

Narrow the list in the API layer (mirroring counterparty-templates) to
system + the active company + the active company's team. RLS stays the
security backstop; this fixes the cross-company merge within a single
user's own view (it was never a cross-tenant data leak).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(import): show proper message for duplicate bank file upload

The bank file import page mis-parsed the structured error envelope
({ error: { code, message, details } }), so a BANK_FILE_DUPLICATE
(409) fell through to the generic "Kunde inte läsa filen" fallback.
The upload step also hardcoded that same string as the error heading,
so duplicates were doubly misreported as parse failures.

- Parse the structured envelope by error.code; surface error.message
  for all codes instead of rendering the error object.
- Add a dedicated BANK_FILE_DUPLICATE message using the importedAt /
  importedCount details the route already returns.
- Add an optional errorTitle prop to BankFileUploadStep (defaults to
  the previous text) and pass "Filen är redan importerad" for dupes.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(tests): add comprehensive tests for recordateEntry, inbox-linking, and external-id handling

- Implemented unit tests for recordateEntry in the bookkeeping module to validate various scenarios including date changes, non-posted entries, and fiscal period restrictions.
- Created tests for inbox-linking status in pending operations to ensure correct handling of invoice inbox items and supplier invoices, addressing historical bugs related to status updates.
- Added tests for external-id utilities to ensure consistent handling of monetary amounts and deduplication keys across different transaction sources.
- Introduced new functions in external-id.ts for stable external ID generation and normalization of imported descriptions, enhancing transaction deduplication reliability.

feat(migrations): add new database migrations for transaction handling

- Created migration to exclude storno and correction vouchers from unmatched GL lines, ensuring accurate reconciliation.
- Added a migration to preserve original bank transaction descriptions in a new immutable column, allowing for user edits while maintaining audit trails and deduplication integrity.

* feat(migrations): add function to exclude storno/correction vouchers from unmatched GL lines

* feat(transactions): enhance transaction handling with improved description normalization and preloaded original entries

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-01 14:45:49 +02:00

101 lines
4.2 KiB
TypeScript

/**
* Derive a Swedish, human-readable working label for a bank transaction from
* the structured codes an ASPSP DOES send when free-text remittance and a
* counterparty name are both absent — the classic card-purchase / ATM / fee /
* interest case that otherwise falls through to a generic placeholder.
*
* Pure and side-effect free, so it is trivially unit-testable and safe to call
* inside the transaction conversion fallback chain.
*
* Precedence (most specific first):
* 1. MCC (merchant_category_code) — identifies the merchant kind for a card
* purchase. Already trusted for auto-categorization
* (lib/bookkeeping/mapping-engine.ts).
* 2. ISO 20022 bank_transaction_code Domain/Family (e.g. "PMNT/CCRD").
* 3. Keyword scan over the (often proprietary, non-normalized) code strings.
* 4. Bare "PMNT" domain with no recognized family → direction-based generic.
*
* Returns null when nothing is recognized — the caller then falls through to
* its own final fallback (the ingest boundary normalizes any leftover empty /
* 'Unknown' value to 'Okänd transaktion').
*
* The mapping tables are intentionally small starters. ASPSP coverage of these
* codes varies and proprietary formats differ per bank — extend the tables
* against real archived `psd2-response_*.json` samples as they surface.
*/
export interface TransactionLabelInput {
/** ISO 20022 bank transaction code, e.g. "PMNT-CCRD-POSD" or "PMNT/RCDT". */
bankTransactionCode?: string | null
/** ASPSP-proprietary code (free-form, varies per bank). */
proprietaryBankTransactionCode?: string | null
/** Merchant category code (card transactions). */
mcc?: string | number | null
/** CRDT (money in) vs DBIT (money out) — used only for the bare-domain case. */
isCredit?: boolean
}
// ISO 20022 External Bank Transaction Codes, keyed by `DOMAIN/FAMILY`.
const ISO20022_LABELS: Record<string, string> = {
'PMNT/RCDT': 'Inbetalning', // ReceivedCreditTransfers
'PMNT/ICDT': 'Betalning', // IssuedCreditTransfers
'PMNT/CCRD': 'Kortköp', // CustomerCardTransactions
'PMNT/MCRD': 'Kortköp', // MerchantCardTransactions
'PMNT/RDDT': 'Autogiro', // ReceivedDirectDebits
'PMNT/IDDT': 'Autogiro', // IssuedDirectDebits
'PMNT/CWDL': 'Uttag', // CashWithdrawal
'PMNT/CAJT': 'Justering', // CashAdjustments
}
// MCC → coarse Swedish label. Tiny starter set.
const MCC_LABELS: Record<string, string> = {
'6011': 'Uttag', // ATM / automated cash disbursements
'5411': 'Inköp dagligvaror', // Grocery stores, supermarkets
}
// Keyword → label, scanned over the raw (incl. proprietary) code strings as a
// last resort before null. Covers banks that send free-form codes, not ISO.
const KEYWORD_LABELS: Array<[RegExp, string]> = [
[/INTRST|INTEREST|RÄNTA|RANTA/i, 'Ränta'],
[/\bFEE\b|CHRG|CHARGE|AVGIFT/i, 'Avgift'],
[/ATM|CASH.?WDL|WITHDRAW|UTTAG/i, 'Uttag'],
[/\bCARD\b|KORT|\bPOS\b/i, 'Kortköp'],
[/SALA|SALARY|\bLÖN\b|\bLON\b/i, 'Lön'],
]
export function deriveTransactionLabel(input: TransactionLabelInput): string | null {
// 1. MCC — most specific signal for card purchases.
const mcc = input.mcc != null ? String(input.mcc).trim() : ''
if (mcc && MCC_LABELS[mcc]) return MCC_LABELS[mcc]
const codes = [input.bankTransactionCode, input.proprietaryBankTransactionCode].filter(
(c): c is string => typeof c === 'string' && c.trim().length > 0,
)
// 2. ISO 20022 Domain/Family from the structured code.
for (const raw of codes) {
const parts = raw.toUpperCase().split(/[/\-_.\s]+/).filter(Boolean)
if (parts.length >= 2) {
const key = `${parts[0]}/${parts[1]}`
if (ISO20022_LABELS[key]) return ISO20022_LABELS[key]
}
}
// 3. Keyword scan over the raw code strings (covers proprietary formats).
for (const raw of codes) {
for (const [re, label] of KEYWORD_LABELS) {
if (re.test(raw)) return label
}
}
// 4. Bare "PMNT" domain with no recognized family → direction-based generic.
if (input.isCredit != null) {
for (const raw of codes) {
const domain = raw.toUpperCase().split(/[/\-_.\s]+/)[0]
if (domain === 'PMNT') return input.isCredit ? 'Inbetalning' : 'Betalning'
}
}
return null
}