Bug/open banking flow (#854)
* fix(enable-banking): pin Mobile BankID (decoupled) auth_method so Handelsbanken corporate connects We never sent auth_method to Enable Banking, so it fell back to the ASPSP's visible default — REDIRECT for Handelsbanken. For Handelsbanken *corporate* PSUs the redirect flow does not support Mobile BankID, so authorization failed right after the user approved in the BankID app. Mobile BankID at Handelsbanken is a DECOUPLED method flagged hidden_method=true, which Enable Banking only uses when requested explicitly. Resolve the bank's preferred auth method before /auth: query the ASPSP's auth_methods and pick the DECOUPLED (Mobile BankID) method when present, otherwise leave auth_method unset so banks that already work are untouched. The method name is read dynamically per psu_type, so it is robust across sandbox/production naming. - api-client: add approach/hidden_method to AuthMethod, fix ASPSP.auth_methods field name (was available_auth_methods, never populated), add getPreferredAuthMethod(), thread optional authMethod through startAuthorization - index: resolve authMethod in /connect and pass it on both fresh + reconnect - tests: cover method selection and request-body shaping Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * refactor(invoice-inbox): clean up bulk-selection toolbar UI Redesign the selection toolbar shown when inbox items are checked: one solid primary "Bokför valda" button with outlined secondary actions ("Fråga assistenten", "Ta bort") and a plain selection count. Removes the redundant "Avmarkera" button (users uncheck the still-visible box), fixes label clipping, and gives the toolbar more breathing room. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * chore(entitlements): bypass paywall in local development Add isPaywallBypassed() so all gated capabilities are testable locally without a subscription. Fires only on NODE_ENV=development (npm run dev) or an explicit DISABLE_PAYWALL=true escape hatch — production builds run under NODE_ENV=production and the entitlement suite runs under 'test', so both keep exercising the real gate. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(tic): resolve enskild firma bolagsuppgifter via 12-digit personnummer TIC's Lens search is fuzzy and only resolves an enskild firma from the 12-digit (century-prefixed) personnummer; a 10-digit form fuzzy-matched an unrelated entity. Expand personnummer to 12 digits before querying and reject hits whose registration number is unrelated to the request. Add a "Hämta" action to the settings Bolagsuppgifter panel to (re)fetch on demand. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(transactions): implement categorize core for bank transaction categorization - Added `categorize-core.ts` to handle categorization of bank transactions, supporting single and bulk operations. - Introduced `categorizeMatchedTransaction` and `bulkBookMatchedInboxItems` functions for transaction processing. - Implemented fiscal period validation and duplicate booking detection. - Enhanced logging and error handling for transaction categorization. feat(scripts): add diagnostic script for Handelsbanken ASPSP metadata - Created `check-handelsbanken-aspsp.mjs` to fetch and display available authentication methods for Handelsbanken. - Outputs metadata for business and personal PSU types, including default authentication methods. fix(migrations): increase statement timeout for SIE bulk delete operations - Updated `20260629160000_sie_bulk_delete_statement_timeout.sql` to set a longer statement timeout for bulk delete RPCs to prevent cancellations during large imports. feat(migrations): add bulk book inbox items to pending operations - Expanded `pending_operations` table to include `bulk_book_inbox_items` operation type in `20260630120000_pending_operations_add_bulk_book_inbox_items.sql`. - Supports bulk booking of matched inbox items against bank transactions. test(pg): add tests for replace_period_opening_balance_link RPC - Implemented tests in `replace-period-opening-balance-link.pg.test.ts` to validate the functionality of the opening-balance correction flow. - Ensured immutability of opening balance links and proper handling of posted vs. non-posted entries. * fix(sie-export): update journal entries and lines handling in SIE export tests * fix(migrations): resolve version collision on 20260629160000 The SIE bulk-delete statement_timeout migration shared version 20260629160000 with journal_entries_list_series_filter (merged from main via #798/#823), causing a schema_migrations_pkey duplicate key error on apply. Rename the branch's migration to 20260629160100. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(compliance): resolve compliance-swarm + review findings - opening-balance/correct: compensating rollback for the non-atomic storno+rebook so a mid-sequence failure never leaves two posted OB entries (ASVS V2.3); durable audit event on every failure path (V16); reference the original verifikationsnummer in the corrected entry per BFL 5 kap 5§; document that requireWrite already enforces write-role + membership (V8.2.1 was a false positive) - reports sources routes: validate the cursor date component as ISO (/^\d{4}-\d{2}-\d{2}$/) before use, 400 on malformed (ASVS V1.2), applied to both the VAT-declaration and trial-balance routes - AgentSessionList: await the rename PATCH, revert the optimistic title and toast on failure (ASVS V4.5) - bank booking: exclude same-batch siblings from the booking-time duplicate guard so bulk-booking distinct same-(date,amount) transactions no longer false-positives; pre-existing duplicate detection is preserved - BulkBookInboxDialog: drop the unsafe currency-based reverse_charge default, add an omvänd skattskyldighet advisory, and type VAT options to the backend VatTreatment union - OpeningBalanceRowEditor: hold onChange in a ref (synced in effect, not during render) so an unstable callback can't cause a render loop Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
2da9c71eb3
commit
f63d3e3100
@@ -129,6 +129,30 @@ describe('detectBookedDuplicateTransaction', () => {
|
||||
})
|
||||
expect(result?.transaction_id).toBe('sib-2')
|
||||
})
|
||||
|
||||
// ── Intra-batch exclusion (bulk-book false-positive fix) ────────────────
|
||||
it('excludes a same-batch sibling whose id is in excludeTransactionIds', async () => {
|
||||
const supabase = makeSupabase([sibling({ id: 'sib-batch' })])
|
||||
const result = await detectBookedDuplicateTransaction(
|
||||
supabase,
|
||||
COMPANY,
|
||||
{ id: 'self', date: '2025-12-19', amount: -1616, cash_account_id: null },
|
||||
{ excludeTransactionIds: ['sib-batch'] },
|
||||
)
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it('STILL flags a pre-existing sibling not in excludeTransactionIds (invariant preserved)', async () => {
|
||||
// 'sib-old' existed before the batch; only 'sib-batch' was booked this run.
|
||||
const supabase = makeSupabase([sibling({ id: 'sib-old' })])
|
||||
const result = await detectBookedDuplicateTransaction(
|
||||
supabase,
|
||||
COMPANY,
|
||||
{ id: 'self', date: '2025-12-19', amount: -1616, cash_account_id: null },
|
||||
{ excludeTransactionIds: ['sib-batch'] },
|
||||
)
|
||||
expect(result?.transaction_id).toBe('sib-old')
|
||||
})
|
||||
})
|
||||
|
||||
// ── Ledger-only voucher guard (the orphan with no sibling transaction) ───────
|
||||
@@ -305,6 +329,29 @@ describe('detectLedgerDuplicateVoucher', () => {
|
||||
})
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
// ── Intra-batch exclusion (bulk-book false-positive fix) ────────────────
|
||||
it('excludes a same-batch voucher whose journal_entry.id is in excludeJournalEntryIds', async () => {
|
||||
const supabase = makeLedgerSupabase({ lines: [jel()] }) // jel() → journal_entry.id 'je-2'
|
||||
const result = await detectLedgerDuplicateVoucher(
|
||||
supabase,
|
||||
COMPANY,
|
||||
{ id: 'self', date: '2026-03-26', amount: 98565, cash_account_id: null },
|
||||
{ excludeJournalEntryIds: ['je-2'] },
|
||||
)
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it('STILL flags a pre-existing voucher not in excludeJournalEntryIds (invariant preserved)', async () => {
|
||||
const supabase = makeLedgerSupabase({ lines: [jel()] })
|
||||
const result = await detectLedgerDuplicateVoucher(
|
||||
supabase,
|
||||
COMPANY,
|
||||
{ id: 'self', date: '2026-03-26', amount: 98565, cash_account_id: null },
|
||||
{ excludeJournalEntryIds: ['je-booked-this-batch'] },
|
||||
)
|
||||
expect(result?.journal_entry_id).toBe('je-2')
|
||||
})
|
||||
})
|
||||
|
||||
describe('detectBookingDuplicate (orchestrator)', () => {
|
||||
@@ -332,4 +379,20 @@ describe('detectBookingDuplicate (orchestrator)', () => {
|
||||
})
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it('propagates exclusions to BOTH the sibling scan and the ledger scan', async () => {
|
||||
// A matching sibling AND a matching ledger voucher exist, but both belong to
|
||||
// this same batch (excluded) → the orchestrator must report no duplicate.
|
||||
const supabase = makeLedgerSupabase({
|
||||
transactionRows: [sibling({ id: 'sib-batch', amount: 98565, journal_entry_id: 'je-sib' })],
|
||||
lines: [jel()], // journal_entry.id 'je-2'
|
||||
})
|
||||
const result = await detectBookingDuplicate(
|
||||
supabase,
|
||||
COMPANY,
|
||||
{ id: 'self', date: '2026-03-26', amount: 98565, cash_account_id: null },
|
||||
{ excludeTransactionIds: ['sib-batch'], excludeJournalEntryIds: ['je-2'] },
|
||||
)
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,332 @@
|
||||
/**
|
||||
* Bulk-book Underlag (Modell B) core logic.
|
||||
*
|
||||
* `bulkBookMatchedInboxItems` is shared by the direct UI route
|
||||
* (POST /items/bulk-book) and the `bulk_book_inbox_items` pending-operation
|
||||
* executor. These tests pin the "Bokför valda hoppar över" contract — items
|
||||
* that aren't matched / already booked / linked to a leverantörsfaktura are
|
||||
* SKIPPED, never errored — and the happy path where a matched item is booked
|
||||
* against its transaction via the shared categorize core.
|
||||
*
|
||||
* The single-item categorize core itself (createJE, duplicate guard, VAT
|
||||
* mapping, underlag propagation) is covered by
|
||||
* lib/pending-operations/__tests__/commit-duplicate-guard.test.ts and the
|
||||
* inbox-link pg tests; here we mock its downstream modules and assert the
|
||||
* loop's classification + collection.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
const mockCreateJE = vi.fn()
|
||||
const mockDetectDup = vi.fn()
|
||||
const mockMapping = vi.fn()
|
||||
const mockUpsertTemplate = vi.fn()
|
||||
const mockLinkToJE = vi.fn()
|
||||
|
||||
vi.mock('@/lib/bookkeeping/transaction-entries', () => ({
|
||||
createTransactionJournalEntry: (...args: unknown[]) => mockCreateJE(...args),
|
||||
}))
|
||||
vi.mock('@/lib/transactions/booking-duplicate-detection', () => ({
|
||||
detectBookingDuplicate: (...args: unknown[]) => mockDetectDup(...args),
|
||||
}))
|
||||
vi.mock('@/lib/bookkeeping/category-mapping', () => ({
|
||||
buildMappingResultFromCategory: (...args: unknown[]) => mockMapping(...args),
|
||||
}))
|
||||
vi.mock('@/lib/bookkeeping/counterparty-templates', () => ({
|
||||
upsertCounterpartyTemplate: (...args: unknown[]) => mockUpsertTemplate(...args),
|
||||
}))
|
||||
vi.mock('@/lib/core/documents/document-service', () => ({
|
||||
linkToJournalEntry: (...args: unknown[]) => mockLinkToJE(...args),
|
||||
}))
|
||||
|
||||
import { bulkBookMatchedInboxItems } from '../categorize-core'
|
||||
import { BulkBookInboxSchema } from '@/lib/api/schemas'
|
||||
import { eventBus } from '@/lib/events/bus'
|
||||
|
||||
/** Queue-based supabase mock: each `from()` consumes the next queued result. */
|
||||
function queuedSupabase(results: Array<{ data?: unknown; error?: unknown }>) {
|
||||
const queue = [...results]
|
||||
const from = vi.fn(() => {
|
||||
const raw = queue.shift() ?? { data: null, error: null }
|
||||
const result = { data: raw.data ?? null, error: raw.error ?? null }
|
||||
const chain: object = new Proxy(
|
||||
{},
|
||||
{
|
||||
get(_t, prop) {
|
||||
if (prop === 'then') return (resolve: (v: unknown) => void) => resolve(result)
|
||||
return () => chain
|
||||
},
|
||||
},
|
||||
)
|
||||
return chain
|
||||
})
|
||||
return { from } as never
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
eventBus.clear()
|
||||
mockDetectDup.mockResolvedValue(null)
|
||||
mockMapping.mockReturnValue({
|
||||
rule: null,
|
||||
debit_account: '5420',
|
||||
credit_account: '1930',
|
||||
risk_level: 'LOW',
|
||||
confidence: 1,
|
||||
requires_review: false,
|
||||
default_private: false,
|
||||
vat_lines: [],
|
||||
description: 'Programvara',
|
||||
})
|
||||
mockCreateJE.mockResolvedValue({ id: 'je-1' })
|
||||
})
|
||||
|
||||
describe('BulkBookInboxSchema', () => {
|
||||
it('accepts a valid payload', () => {
|
||||
const r = BulkBookInboxSchema.safeParse({
|
||||
item_ids: ['11111111-1111-4111-8111-111111111111'],
|
||||
category: 'expense_software',
|
||||
vat_treatment: 'reverse_charge',
|
||||
})
|
||||
expect(r.success).toBe(true)
|
||||
})
|
||||
|
||||
// Regression: the bulk_book_inbox_items pending operation persists absent
|
||||
// optionals as explicit JSON null (stagePendingOperation in server.ts). A bare
|
||||
// `.optional()` rejected those on approval ("expected number, received null").
|
||||
it('accepts persisted params with explicit nulls and normalizes them to undefined', () => {
|
||||
const r = BulkBookInboxSchema.safeParse({
|
||||
item_ids: ['11111111-1111-4111-8111-111111111111'],
|
||||
category: 'expense_software',
|
||||
vat_treatment: null,
|
||||
vat_amount: null,
|
||||
notes: null,
|
||||
allow_duplicate: false,
|
||||
})
|
||||
expect(r.success).toBe(true)
|
||||
if (r.success) {
|
||||
// null must not leak downstream to categorizeMatchedTransaction.
|
||||
expect(r.data.vat_treatment).toBeUndefined()
|
||||
expect(r.data.vat_amount).toBeUndefined()
|
||||
expect(r.data.notes).toBeUndefined()
|
||||
}
|
||||
})
|
||||
|
||||
it('rejects an empty item_ids array', () => {
|
||||
const r = BulkBookInboxSchema.safeParse({ item_ids: [], category: 'expense_software' })
|
||||
expect(r.success).toBe(false)
|
||||
})
|
||||
|
||||
it('rejects a missing category', () => {
|
||||
const r = BulkBookInboxSchema.safeParse({ item_ids: ['11111111-1111-1111-1111-111111111111'] })
|
||||
expect(r.success).toBe(false)
|
||||
})
|
||||
|
||||
it('rejects an invalid category', () => {
|
||||
const r = BulkBookInboxSchema.safeParse({
|
||||
item_ids: ['11111111-1111-1111-1111-111111111111'],
|
||||
category: 'expense_unicorns',
|
||||
})
|
||||
expect(r.success).toBe(false)
|
||||
})
|
||||
|
||||
it('rejects an invalid vat_treatment', () => {
|
||||
const r = BulkBookInboxSchema.safeParse({
|
||||
item_ids: ['11111111-1111-1111-1111-111111111111'],
|
||||
category: 'expense_software',
|
||||
vat_treatment: 'omvänd',
|
||||
})
|
||||
expect(r.success).toBe(false)
|
||||
})
|
||||
|
||||
it('rejects more than 200 items', () => {
|
||||
const ids = Array.from({ length: 201 }, (_, i) => `id-${i}`)
|
||||
const r = BulkBookInboxSchema.safeParse({ item_ids: ids, category: 'expense_software' })
|
||||
expect(r.success).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('bulkBookMatchedInboxItems — skip classification (never errors)', () => {
|
||||
const base = { category: 'expense_software' as const }
|
||||
|
||||
it('skips an item that is not found', async () => {
|
||||
const supabase = queuedSupabase([{ data: null }])
|
||||
const { booked, skipped } = await bulkBookMatchedInboxItems(supabase, 'u1', 'c1', {
|
||||
...base,
|
||||
item_ids: ['missing'],
|
||||
})
|
||||
expect(booked).toEqual([])
|
||||
expect(skipped).toEqual([{ item_id: 'missing', reason: 'not_found' }])
|
||||
expect(mockCreateJE).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('skips an item already booked (created_journal_entry_id)', async () => {
|
||||
const supabase = queuedSupabase([
|
||||
{ data: { id: 'i1', matched_transaction_id: 'tx-1', created_journal_entry_id: 'je-x', created_supplier_invoice_id: null } },
|
||||
])
|
||||
const { booked, skipped } = await bulkBookMatchedInboxItems(supabase, 'u1', 'c1', { ...base, item_ids: ['i1'] })
|
||||
expect(booked).toEqual([])
|
||||
expect(skipped).toEqual([{ item_id: 'i1', reason: 'already_booked' }])
|
||||
expect(mockCreateJE).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('skips an item linked to a supplier invoice', async () => {
|
||||
const supabase = queuedSupabase([
|
||||
{ data: { id: 'i1', matched_transaction_id: 'tx-1', created_journal_entry_id: null, created_supplier_invoice_id: 'si-x' } },
|
||||
])
|
||||
const { booked, skipped } = await bulkBookMatchedInboxItems(supabase, 'u1', 'c1', { ...base, item_ids: ['i1'] })
|
||||
expect(booked).toEqual([])
|
||||
expect(skipped).toEqual([{ item_id: 'i1', reason: 'is_supplier_invoice' }])
|
||||
expect(mockCreateJE).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('skips an item without a matched transaction', async () => {
|
||||
const supabase = queuedSupabase([
|
||||
{ data: { id: 'i1', matched_transaction_id: null, created_journal_entry_id: null, created_supplier_invoice_id: null } },
|
||||
])
|
||||
const { booked, skipped } = await bulkBookMatchedInboxItems(supabase, 'u1', 'c1', { ...base, item_ids: ['i1'] })
|
||||
expect(booked).toEqual([])
|
||||
expect(skipped).toEqual([{ item_id: 'i1', reason: 'not_matched' }])
|
||||
expect(mockCreateJE).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('bulkBookMatchedInboxItems — booking', () => {
|
||||
it('books a matched, unbooked item against its transaction', async () => {
|
||||
const supabase = queuedSupabase([
|
||||
// 1. inbox item fetch → bookable
|
||||
{ data: { id: 'i1', matched_transaction_id: 'tx-1', created_journal_entry_id: null, created_supplier_invoice_id: null } },
|
||||
// 2. transactions fetch (categorize core)
|
||||
{ data: { id: 'tx-1', date: '2026-06-01', amount: -700.28, currency: 'SEK', cash_account_id: null, journal_entry_id: null } },
|
||||
// 3. company_settings
|
||||
{ data: { entity_type: 'aktiebolag', fiscal_year_start_month: 1 } },
|
||||
// 4. ensureFiscalPeriod → existing period
|
||||
{ data: [{ id: 'fp-1' }] },
|
||||
// 5. transactions update (mark booked)
|
||||
{ error: null },
|
||||
// 6. propagation select (no matched inbox rows to stamp in this mock)
|
||||
{ data: [] },
|
||||
])
|
||||
|
||||
const { booked, skipped } = await bulkBookMatchedInboxItems(supabase, 'u1', 'c1', {
|
||||
item_ids: ['i1'],
|
||||
category: 'expense_software',
|
||||
vat_treatment: 'reverse_charge',
|
||||
})
|
||||
|
||||
expect(skipped).toEqual([])
|
||||
expect(booked).toEqual([{ item_id: 'i1', transaction_id: 'tx-1', journal_entry_id: 'je-1' }])
|
||||
expect(mockCreateJE).toHaveBeenCalledTimes(1)
|
||||
// The shared core received the chosen category + reverse-charge treatment.
|
||||
expect(mockMapping).toHaveBeenCalledWith(
|
||||
'expense_software',
|
||||
expect.objectContaining({ id: 'tx-1' }),
|
||||
true,
|
||||
'aktiebolag',
|
||||
'reverse_charge',
|
||||
undefined,
|
||||
)
|
||||
})
|
||||
|
||||
it('books the matched item and skips the unmatched one in a mixed batch', async () => {
|
||||
const supabase = queuedSupabase([
|
||||
// item i1 → not matched (1 from())
|
||||
{ data: { id: 'i1', matched_transaction_id: null, created_journal_entry_id: null, created_supplier_invoice_id: null } },
|
||||
// item i2 → bookable, then its categorize chain
|
||||
{ data: { id: 'i2', matched_transaction_id: 'tx-2', created_journal_entry_id: null, created_supplier_invoice_id: null } },
|
||||
{ data: { id: 'tx-2', date: '2026-06-02', amount: -25, currency: 'SEK', cash_account_id: null, journal_entry_id: null } },
|
||||
{ data: { entity_type: 'aktiebolag', fiscal_year_start_month: 1 } },
|
||||
{ data: [{ id: 'fp-1' }] },
|
||||
{ error: null },
|
||||
{ data: [] },
|
||||
])
|
||||
|
||||
const { booked, skipped } = await bulkBookMatchedInboxItems(supabase, 'u1', 'c1', {
|
||||
item_ids: ['i1', 'i2'],
|
||||
category: 'expense_software',
|
||||
})
|
||||
|
||||
expect(skipped).toEqual([{ item_id: 'i1', reason: 'not_matched' }])
|
||||
expect(booked).toEqual([{ item_id: 'i2', transaction_id: 'tx-2', journal_entry_id: 'je-1' }])
|
||||
expect(mockCreateJE).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('bulkBookMatchedInboxItems — intra-batch duplicate handling', () => {
|
||||
/** Six queued from() results for one successfully-booked item. */
|
||||
const bookableItem = (itemId: string, txId: string, amount: number) => [
|
||||
{ data: { id: itemId, matched_transaction_id: txId, created_journal_entry_id: null, created_supplier_invoice_id: null } },
|
||||
{ data: { id: txId, date: '2026-06-01', amount, currency: 'SEK', cash_account_id: null, journal_entry_id: null } },
|
||||
{ data: { entity_type: 'aktiebolag', fiscal_year_start_month: 1 } },
|
||||
{ data: [{ id: 'fp-1' }] },
|
||||
{ error: null },
|
||||
{ data: [] },
|
||||
]
|
||||
|
||||
it('books BOTH distinct transactions that share (date, amount) in one bulk run', async () => {
|
||||
// Model the reviewer-reported bug: the guard WOULD flag the second tx as a
|
||||
// duplicate of the first tx's freshly-created verifikat — but only when the
|
||||
// first tx is NOT excluded as a same-batch sibling. The fix must pass tx-1
|
||||
// in as an exclusion so tx-2 books instead of being skipped 409.
|
||||
mockDetectDup.mockImplementation(
|
||||
(_sb: unknown, _co: unknown, target: { id: string }, exclude?: { excludeTransactionIds?: string[] }) => {
|
||||
if (target.id === 'tx-2' && !(exclude?.excludeTransactionIds ?? []).includes('tx-1')) {
|
||||
return Promise.resolve({
|
||||
transaction_id: 'tx-1', journal_entry_id: 'je-1', voucher_label: 'A1',
|
||||
entry_date: '2026-06-01', description: null, amount: 700.28,
|
||||
})
|
||||
}
|
||||
return Promise.resolve(null)
|
||||
},
|
||||
)
|
||||
|
||||
const supabase = queuedSupabase([
|
||||
...bookableItem('i1', 'tx-1', -700.28),
|
||||
...bookableItem('i2', 'tx-2', -700.28),
|
||||
])
|
||||
|
||||
const { booked, skipped } = await bulkBookMatchedInboxItems(supabase, 'u1', 'c1', {
|
||||
item_ids: ['i1', 'i2'],
|
||||
category: 'expense_software',
|
||||
})
|
||||
|
||||
expect(skipped).toEqual([])
|
||||
expect(booked).toEqual([
|
||||
{ item_id: 'i1', transaction_id: 'tx-1', journal_entry_id: 'je-1' },
|
||||
{ item_id: 'i2', transaction_id: 'tx-2', journal_entry_id: 'je-1' },
|
||||
])
|
||||
expect(mockCreateJE).toHaveBeenCalledTimes(2)
|
||||
|
||||
// The SECOND booking was handed tx-1 (and its verifikat) as an intra-batch
|
||||
// exclusion; the first was handed an empty set.
|
||||
const firstCall = mockDetectDup.mock.calls.find((c) => (c[2] as { id: string }).id === 'tx-1')
|
||||
const secondCall = mockDetectDup.mock.calls.find((c) => (c[2] as { id: string }).id === 'tx-2')
|
||||
expect(firstCall?.[3]).toEqual({ excludeTransactionIds: [], excludeJournalEntryIds: [] })
|
||||
expect(secondCall?.[3]).toEqual({ excludeTransactionIds: ['tx-1'], excludeJournalEntryIds: ['je-1'] })
|
||||
})
|
||||
|
||||
it('STILL skips a pre-existing already-booked duplicate (cross-batch detection preserved)', async () => {
|
||||
// The guard fires on a duplicate that existed BEFORE this batch: its ids are
|
||||
// absent from the (empty) exclusion set, so the booking is refused (409) and
|
||||
// the item is skipped as a possible duplicate rather than double-booked.
|
||||
mockDetectDup.mockResolvedValue({
|
||||
transaction_id: 'tx-preexisting', journal_entry_id: 'je-old', voucher_label: 'A9',
|
||||
entry_date: '2026-06-01', description: null, amount: 700.28,
|
||||
})
|
||||
|
||||
const supabase = queuedSupabase([
|
||||
{ data: { id: 'i1', matched_transaction_id: 'tx-1', created_journal_entry_id: null, created_supplier_invoice_id: null } },
|
||||
{ data: { id: 'tx-1', date: '2026-06-01', amount: -700.28, currency: 'SEK', cash_account_id: null, journal_entry_id: null } },
|
||||
])
|
||||
|
||||
const { booked, skipped } = await bulkBookMatchedInboxItems(supabase, 'u1', 'c1', {
|
||||
item_ids: ['i1'],
|
||||
category: 'expense_software',
|
||||
})
|
||||
|
||||
expect(booked).toEqual([])
|
||||
expect(skipped).toHaveLength(1)
|
||||
expect(skipped[0].item_id).toBe('i1')
|
||||
expect(skipped[0].reason).toBe('already_booked_or_duplicate')
|
||||
expect(mockCreateJE).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user