* 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>
161 lines
4.8 KiB
TypeScript
161 lines
4.8 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { createQueuedMockSupabase } from '@/tests/helpers'
|
|
import { detectOwnAccountTransfer } from '../own-account-detector'
|
|
import type { Transaction } from '@/types'
|
|
|
|
function makeTx(overrides: Partial<Transaction> = {}): Transaction {
|
|
return {
|
|
id: 'tx-1',
|
|
user_id: 'user-1',
|
|
company_id: 'company-1',
|
|
bank_connection_id: 'conn-sek',
|
|
external_id: 'eb_sek_1',
|
|
date: '2026-06-12',
|
|
description: 'Överföring till EUR-konto',
|
|
original_description: 'Överföring till EUR-konto',
|
|
title_edited_at: null,
|
|
amount: -1000,
|
|
currency: 'SEK',
|
|
amount_sek: -1000,
|
|
exchange_rate: null,
|
|
exchange_rate_date: null,
|
|
category: 'uncategorized',
|
|
is_business: null,
|
|
invoice_id: null,
|
|
supplier_invoice_id: null,
|
|
potential_invoice_id: null,
|
|
potential_supplier_invoice_id: null,
|
|
journal_entry_id: null,
|
|
mcc_code: null,
|
|
merchant_name: null,
|
|
receipt_id: null,
|
|
document_id: null,
|
|
reconciliation_method: null,
|
|
is_ignored: false,
|
|
import_source: 'enable_banking',
|
|
reference: null,
|
|
counterparty_iban: 'SE9550000000054910000003',
|
|
counterparty_account: null,
|
|
notes: null,
|
|
created_at: '2026-06-12T00:00:00Z',
|
|
updated_at: '2026-06-12T00:00:00Z',
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
describe('detectOwnAccountTransfer', () => {
|
|
it('returns null when transaction has no counterparty_iban', async () => {
|
|
const { supabase } = createQueuedMockSupabase()
|
|
const result = await detectOwnAccountTransfer(
|
|
supabase as never,
|
|
'company-1',
|
|
makeTx({ counterparty_iban: null }),
|
|
)
|
|
expect(result).toBeNull()
|
|
})
|
|
|
|
it('returns null when IBAN does not match any cash account for the company', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
enqueue({ data: null }) // findByIban miss
|
|
const result = await detectOwnAccountTransfer(
|
|
supabase as never,
|
|
'company-1',
|
|
makeTx({ counterparty_iban: 'NORANDOMVALUE' }),
|
|
)
|
|
expect(result).toBeNull()
|
|
})
|
|
|
|
it('matches IBAN and returns counter ledger account when present', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
// findByIban hit
|
|
enqueue({
|
|
data: {
|
|
id: 'ca-eur',
|
|
company_id: 'company-1',
|
|
bank_connection_id: 'conn-eur',
|
|
currency: 'EUR',
|
|
ledger_account: '1932',
|
|
iban: 'SE9550000000054910000003',
|
|
is_primary: false,
|
|
enabled: true,
|
|
source: 'enable_banking',
|
|
},
|
|
})
|
|
// pair candidate lookup — find the matching EUR-side leg
|
|
enqueue({
|
|
data: [{ id: 'tx-eur-leg', amount: 90.50, date: '2026-06-12' }],
|
|
})
|
|
|
|
const result = await detectOwnAccountTransfer(
|
|
supabase as never,
|
|
'company-1',
|
|
makeTx({ amount: -1000, counterparty_iban: 'SE9550000000054910000003' }),
|
|
)
|
|
|
|
expect(result).not.toBeNull()
|
|
expect(result!.counterLedgerAccount).toBe('1932')
|
|
expect(result!.counterCurrency).toBe('EUR')
|
|
expect(result!.pairTransactionId).toBe('tx-eur-leg')
|
|
})
|
|
|
|
it('returns pairTransactionId: null when the other leg has not been ingested yet', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
enqueue({
|
|
data: {
|
|
id: 'ca-eur',
|
|
company_id: 'company-1',
|
|
bank_connection_id: 'conn-eur',
|
|
currency: 'EUR',
|
|
ledger_account: '1932',
|
|
iban: 'SE9550000000054910000003',
|
|
is_primary: false,
|
|
enabled: true,
|
|
source: 'enable_banking',
|
|
},
|
|
})
|
|
enqueue({ data: [] }) // pair not present yet
|
|
|
|
const result = await detectOwnAccountTransfer(
|
|
supabase as never,
|
|
'company-1',
|
|
makeTx({ counterparty_iban: 'SE9550000000054910000003' }),
|
|
)
|
|
expect(result).not.toBeNull()
|
|
expect(result!.pairTransactionId).toBeNull()
|
|
expect(result!.counterLedgerAccount).toBe('1932')
|
|
})
|
|
|
|
it('refuses to pair when the counter ledger code is outside the cash class (19xx)', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
enqueue({
|
|
data: {
|
|
id: 'ca-bad',
|
|
company_id: 'company-1',
|
|
bank_connection_id: null,
|
|
currency: 'SEK',
|
|
ledger_account: '6991', // not a cash account
|
|
iban: 'SE9550000000054910000003',
|
|
is_primary: false,
|
|
enabled: true,
|
|
source: 'manual',
|
|
},
|
|
})
|
|
const result = await detectOwnAccountTransfer(
|
|
supabase as never,
|
|
'company-1',
|
|
makeTx({ counterparty_iban: 'SE9550000000054910000003' }),
|
|
)
|
|
expect(result).toBeNull()
|
|
})
|
|
|
|
it('does not fall back to amount-only heuristics when IBAN missing — null instead', async () => {
|
|
const { supabase } = createQueuedMockSupabase()
|
|
const result = await detectOwnAccountTransfer(
|
|
supabase as never,
|
|
'company-1',
|
|
makeTx({ counterparty_iban: '' }),
|
|
)
|
|
expect(result).toBeNull()
|
|
})
|
|
})
|