cce0de5704
* feat(mcp): already-explained voucher guard at stage and commit for match_batch_allocate The dashboard match-batch route refused BATCH_TX_POSSIBLE_DUPLICATE when posted, unlinked vouchers already summed to the bank row (PR #2300), but the MCP door (gnubok_match_batch_allocate staging + commitMatchBatchAllocate) called the RPC with no guard, so an agent could book a Bankgirot aggregate a second time. The detector existed once; the guard lived in one door. One shared decision helper, lib/invoices/already-explained-guard.ts, now sits on top of the existing detectors (no fork) and is called by the dashboard route, the MCP staging tools and the commit executors: - gnubok_match_batch_allocate refuses to stage, coded BATCH_TX_POSSIBLE_DUPLICATE, naming the vouchers, the reconcile_match / link_transaction_to_journal_entry call that resolves the row, and the exact force + expected_journal_entry_ids binding. - commitMatchBatchAllocate runs the same guard before the RPC and re-validates a staged force binding against the set detected at commit, so a stale approval cannot book a duplicate; 409 auto-rejects with the vouchers in result_data. - force + expected_journal_entry_ids on the tool mirror MatchBatchSchema; an honoured override stages with a compliance_warning and, after the booking succeeds, writes BankTransactionDuplicateDismissed to behandlingshistorik (dashboard route included; it only logged before). - gnubok_match_transaction_to_invoice and commitMatchTransactionInvoice get the dashboard's 1:1 soft-duplicate guard (MATCH_INVOICE_POSSIBLE_DUPLICATE / MATCH_INVOICE_FORCE_CANDIDATE_MISMATCH) with force + expected_journal_entry_id; at commit it runs before the storno. - Registry: both duplicate codes gain retryable: false and a remediation. Catalog payload held under the 60K ceiling by trimming the two tools' own descriptions (59 988 measured, ledger entry in payload-size.bench.test.ts). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019SaJfqNi4VmsG8FMKq99G6 * docs(decisions): record the 2026-09-06 ten-issue batch's first-principles choices Carries the DECISIONS.md lines for PRs #2337 #2339 #2340 #2341 #2342 #2343 #2344 #2345 #2346 #2347 in one place so the ten branches do not conflict on this file. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019SaJfqNi4VmsG8FMKq99G6 * fix(mcp): refuse an unverifiable forced override, surface a failed duplicate check, validate the binding (#2294 review) Review round on PR #2346 (CodeRabbit + compliance): - guardAlreadyExplained returned 'clear' when the detector threw even with force=true, so a forced 1:N override could book without re-validating expected_journal_entry_ids and left no behandlingshistorik record. It now returns a distinct 'unverifiable' outcome under force (mirrors guardDuplicatePaymentVoucher); the dashboard route, the MCP staging tool and the commit executor all refuse it with the new registry code BATCH_TX_EXPLAINED_CHECK_FAILED (409, retryable, remediation). Regression tests on every caller. - A detector failure without force still fails open at stage time, but no longer silently: the tools track onDetectError and stage a complianceNote, so preview_data.compliance_warning is set on both match_batch_allocate (GenericPreview renders it) and match_transaction_invoice (MatchTransactionInvoicePreview now renders data.compliance_warning through AttnLine). - expected_journal_entry_ids / expected_journal_entry_id are validated at the MCP boundary (array of 1 to 10 non-empty strings / non-empty string) and refused with VALIDATION_ERROR instead of being silently filtered. No schema description text added: catalog payload unchanged. - RoPA: .compliance/ropa.yaml gains bookkeeping.duplicate_dismissal_history for the BankTransactionDuplicateDismissed record (Art. 6(1)(c), BFNAR 2013:2 p. 9.16, retention per BFL 7 kap, stored in processing_history). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> --------- Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
631 lines
24 KiB
TypeScript
631 lines
24 KiB
TypeScript
/**
|
|
* Settlement-account resolution coverage for the agent/MCP match-transaction-
|
|
* to-invoice commit path (`commitMatchTransactionInvoice` in
|
|
* lib/pending-operations/commit.ts).
|
|
*
|
|
* This path books the customer-payment verifikat exactly like the dashboard's
|
|
* POST /api/transactions/[id]/match-invoice route, and previously shared the
|
|
* same gap: the bank leg was unconditionally hardcoded to 1930 instead of
|
|
* being resolved from the matched transaction's own cash_account_id. Mirrors
|
|
* the fix and the regression tests added to that route's test suite.
|
|
*/
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { eventBus } from '@/lib/events/bus'
|
|
import { createQueuedMockSupabase } from '@/tests/helpers'
|
|
import type { PendingOperation } from '@/types'
|
|
|
|
const mockCreatePaymentEntry = vi.fn()
|
|
const mockCreateCashEntry = vi.fn()
|
|
vi.mock('@/lib/bookkeeping/invoice-entries', async () => {
|
|
const actual = await vi.importActual<typeof import('@/lib/bookkeeping/invoice-entries')>(
|
|
'@/lib/bookkeeping/invoice-entries',
|
|
)
|
|
return {
|
|
...actual,
|
|
createInvoicePaymentJournalEntry: (...args: unknown[]) => mockCreatePaymentEntry(...args),
|
|
createInvoiceCashEntry: (...args: unknown[]) => mockCreateCashEntry(...args),
|
|
}
|
|
})
|
|
|
|
// The accrual branch books via findFiscalPeriod + createJournalEntry with
|
|
// lines from the shared buildInvoicePaymentClearingLines helper (parity with
|
|
// the dashboard/v1 routes); the settlement account shows up as the bank leg's
|
|
// account_number.
|
|
const mockFindFiscalPeriod = vi.fn()
|
|
const mockCreateJournalEntry = vi.fn()
|
|
vi.mock('@/lib/bookkeeping/engine', async () => {
|
|
const actual = await vi.importActual<typeof import('@/lib/bookkeeping/engine')>(
|
|
'@/lib/bookkeeping/engine',
|
|
)
|
|
return {
|
|
...actual,
|
|
findFiscalPeriod: (...args: unknown[]) => mockFindFiscalPeriod(...args),
|
|
createJournalEntry: (...args: unknown[]) => mockCreateJournalEntry(...args),
|
|
}
|
|
})
|
|
|
|
// Riksbanken lookup for cross-currency parity tests: mocked so no network or
|
|
// DB call happens; per-test mockResolvedValue supplies the rate.
|
|
const mockFetchExchangeRate = vi.fn()
|
|
vi.mock('@/lib/currency/riksbanken', async () => {
|
|
const actual = await vi.importActual<typeof import('@/lib/currency/riksbanken')>(
|
|
'@/lib/currency/riksbanken',
|
|
)
|
|
return {
|
|
...actual,
|
|
fetchExchangeRate: (...args: unknown[]) => mockFetchExchangeRate(...args),
|
|
}
|
|
})
|
|
|
|
// Issue #1259: settling the invoice retires the suggestion pointers at it.
|
|
// Mocked so it consumes no slot in the queued Supabase mock; the helper's own
|
|
// query shape is pinned by lib/invoices/__tests__/clear-settled-invoice-suggestions.test.ts.
|
|
const { mockClearSuggestions } = vi.hoisted(() => ({ mockClearSuggestions: vi.fn() }))
|
|
vi.mock('@/lib/invoices/clear-settled-invoice-suggestions', () => ({
|
|
clearSettledInvoiceSuggestions: mockClearSuggestions,
|
|
}))
|
|
|
|
// The soft-duplicate guard runs before the storno (issue #2294). Mocked clean
|
|
// so it consumes no slot in the queued Supabase mock; its behaviour is pinned
|
|
// by match-transaction-invoice-duplicate-guard.test.ts.
|
|
vi.mock('@/lib/invoices/duplicate-payment-detection', () => ({
|
|
detectDuplicatePaymentVoucher: vi.fn(async () => null),
|
|
detectExplainingVoucherSetForTransaction: vi.fn(async () => null),
|
|
}))
|
|
|
|
import { commitPendingOperation } from '../commit'
|
|
|
|
function makePendingOp(overrides: Partial<PendingOperation>): PendingOperation {
|
|
return {
|
|
id: 'op-1',
|
|
user_id: 'user-1',
|
|
company_id: 'company-1',
|
|
operation_type: 'match_transaction_invoice',
|
|
status: 'pending',
|
|
title: 'test',
|
|
params: {},
|
|
preview_data: {},
|
|
result_data: null,
|
|
actor_type: 'user',
|
|
actor_id: null,
|
|
actor_label: null,
|
|
risk_level: 'medium',
|
|
created_at: '2026-05-03T00:00:00Z',
|
|
resolved_at: null,
|
|
updated_at: '2026-05-03T00:00:00Z',
|
|
...overrides,
|
|
} as PendingOperation
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
eventBus.clear()
|
|
mockCreatePaymentEntry.mockResolvedValue({ id: 'je-1' })
|
|
mockCreateCashEntry.mockResolvedValue({ id: 'je-1' })
|
|
mockFindFiscalPeriod.mockResolvedValue('fp-1')
|
|
mockCreateJournalEntry.mockResolvedValue({ id: 'je-1' })
|
|
})
|
|
|
|
describe('commitPendingOperation: match_transaction_invoice settlement account resolution', () => {
|
|
it('rejects a credit note before creating a payment journal entry', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
enqueue({ data: { id: 'op-1' }, error: null }) // CAS claim
|
|
enqueue({
|
|
data: {
|
|
id: 'tx-1',
|
|
company_id: 'company-1',
|
|
amount: 12500,
|
|
currency: 'SEK',
|
|
date: '2026-05-12',
|
|
invoice_id: null,
|
|
journal_entry_id: null,
|
|
},
|
|
error: null,
|
|
}) // transaction fetch
|
|
enqueue({
|
|
data: {
|
|
id: 'credit-1',
|
|
invoice_number: 'KR-F-2026001',
|
|
status: 'sent',
|
|
total: -12500,
|
|
credited_invoice_id: 'inv-1',
|
|
},
|
|
error: null,
|
|
}) // invoice fetch
|
|
enqueue({ data: null, error: null }) // dispatcher pending_operations update
|
|
|
|
const op = makePendingOp({ params: { transaction_id: 'tx-1', invoice_id: 'credit-1' } })
|
|
const result = await commitPendingOperation(supabase as never, 'user-1', 'company-1', op)
|
|
|
|
expect(result.status).toBe('rejected')
|
|
expect(result.http_status).toBe(409)
|
|
expect(mockCreatePaymentEntry).not.toHaveBeenCalled()
|
|
expect(mockCreateCashEntry).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('credits the payment JE to the transaction\'s own linked cash account, not a hardcoded 1930', async () => {
|
|
const { supabase, enqueue, findCalls } = createQueuedMockSupabase()
|
|
const matchedHandler = vi.fn()
|
|
eventBus.on('invoice.match_confirmed', matchedHandler)
|
|
enqueue({ data: { id: 'op-1' }, error: null }) // CAS claim
|
|
enqueue({
|
|
data: {
|
|
id: 'tx-1',
|
|
company_id: 'company-1',
|
|
amount: 12500,
|
|
currency: 'SEK',
|
|
date: '2026-05-12',
|
|
invoice_id: null,
|
|
journal_entry_id: null,
|
|
cash_account_id: 'ca-1940',
|
|
},
|
|
error: null,
|
|
}) // transaction fetch
|
|
enqueue({
|
|
data: {
|
|
id: 'inv-1',
|
|
invoice_number: 'F-2026001',
|
|
status: 'sent',
|
|
total: 12500,
|
|
remaining_amount: 12500,
|
|
paid_amount: 0,
|
|
currency: 'SEK',
|
|
exchange_rate: null,
|
|
journal_entry_id: null,
|
|
customer: { name: 'Test AB' },
|
|
},
|
|
error: null,
|
|
}) // invoice fetch
|
|
enqueue({ data: { accounting_method: 'accrual', entity_type: 'aktiebolag' }, error: null }) // settings
|
|
enqueue({ data: { ledger_account: '1940' }, error: null }) // cash_accounts lookup
|
|
enqueue({ data: [{ id: 'inv-1' }], error: null }) // invoice CAS update
|
|
enqueue({ data: { id: 'ip-1' }, error: null }) // invoice_payments insert
|
|
enqueue({ data: null, error: null }) // transactions update (link)
|
|
enqueue({ data: null, error: null }) // dispatcher pending_operations update
|
|
|
|
const op = makePendingOp({ params: { transaction_id: 'tx-1', invoice_id: 'inv-1' } })
|
|
const result = await commitPendingOperation(supabase as never, 'user-1', 'company-1', op)
|
|
|
|
expect(result.status).toBe('committed')
|
|
expect(mockCreateJournalEntry).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
'company-1',
|
|
'user-1',
|
|
expect.objectContaining({
|
|
source_type: 'invoice_paid',
|
|
source_id: 'inv-1',
|
|
entry_date: '2026-05-12',
|
|
lines: expect.arrayContaining([
|
|
expect.objectContaining({ account_number: '1940', debit_amount: 12500 }),
|
|
expect.objectContaining({ account_number: '1510', credit_amount: 12500 }),
|
|
]),
|
|
}),
|
|
)
|
|
expect(mockCreateCashEntry).not.toHaveBeenCalled()
|
|
const invoiceUpdate = findCalls('invoices', 'update').at(-1)?.[0]
|
|
expect(invoiceUpdate).toMatchObject({ paid_at: '2026-05-12T12:00:00Z' })
|
|
// No residual: the applied amount IS the cash received (#2250).
|
|
expect(findCalls('invoice_payments', 'insert').at(-1)?.[0]).toMatchObject({
|
|
invoice_id: 'inv-1',
|
|
transaction_id: 'tx-1',
|
|
payment_date: '2026-05-12',
|
|
amount: 12500,
|
|
currency: 'SEK',
|
|
journal_entry_id: 'je-1',
|
|
})
|
|
expect(matchedHandler).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
invoice: expect.objectContaining({
|
|
status: 'paid',
|
|
paid_at: '2026-05-12T12:00:00Z',
|
|
paid_amount: 12500,
|
|
remaining_amount: 0,
|
|
}),
|
|
transaction: expect.objectContaining({
|
|
invoice_id: 'inv-1',
|
|
journal_entry_id: 'je-1',
|
|
}),
|
|
}),
|
|
)
|
|
// Issue #1259: the invoice is settled, so every OTHER transaction still
|
|
// carrying a suggestion pointer at it is retired; this op's own row is
|
|
// cleared by the link update.
|
|
expect(mockClearSuggestions).toHaveBeenCalledTimes(1)
|
|
expect(mockClearSuggestions).toHaveBeenCalledWith(supabase, 'company-1', 'invoice', 'inv-1', {
|
|
exceptTransactionId: 'tx-1',
|
|
})
|
|
})
|
|
|
|
it('leaves the suggestions alone on a partial payment: the invoice is still matchable', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
enqueue({ data: { id: 'op-1' }, error: null }) // CAS claim
|
|
enqueue({
|
|
data: {
|
|
id: 'tx-1',
|
|
company_id: 'company-1',
|
|
amount: 5000,
|
|
currency: 'SEK',
|
|
date: '2026-05-12',
|
|
invoice_id: null,
|
|
journal_entry_id: null,
|
|
cash_account_id: null,
|
|
},
|
|
error: null,
|
|
}) // transaction fetch
|
|
enqueue({
|
|
data: {
|
|
id: 'inv-1',
|
|
invoice_number: 'F-2026001',
|
|
status: 'sent',
|
|
total: 12500,
|
|
remaining_amount: 12500,
|
|
paid_amount: 0,
|
|
currency: 'SEK',
|
|
exchange_rate: null,
|
|
journal_entry_id: null,
|
|
customer: { name: 'Test AB' },
|
|
},
|
|
error: null,
|
|
}) // invoice fetch
|
|
enqueue({ data: { accounting_method: 'accrual', entity_type: 'aktiebolag' }, error: null }) // settings
|
|
enqueue({ data: [], error: null }) // resolveSettlementAccount: no enabled cash accounts -> 1930
|
|
enqueue({ data: [{ id: 'inv-1' }], error: null }) // invoice CAS update
|
|
enqueue({ data: { id: 'ip-1' }, error: null }) // invoice_payments insert
|
|
enqueue({ data: null, error: null }) // transactions update (link)
|
|
enqueue({ data: null, error: null }) // dispatcher pending_operations update
|
|
|
|
const op = makePendingOp({ params: { transaction_id: 'tx-1', invoice_id: 'inv-1' } })
|
|
const result = await commitPendingOperation(supabase as never, 'user-1', 'company-1', op)
|
|
|
|
expect(result.status).toBe('committed')
|
|
expect(result.data).toMatchObject({ invoice_status: 'partially_paid' })
|
|
expect(mockClearSuggestions).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('defaults to 1930 when the transaction has no linked cash account', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
enqueue({ data: { id: 'op-1' }, error: null }) // CAS claim
|
|
enqueue({
|
|
data: {
|
|
id: 'tx-1',
|
|
company_id: 'company-1',
|
|
amount: 12500,
|
|
currency: 'SEK',
|
|
date: '2026-05-12',
|
|
invoice_id: null,
|
|
journal_entry_id: null,
|
|
cash_account_id: null,
|
|
},
|
|
error: null,
|
|
}) // transaction fetch
|
|
enqueue({
|
|
data: {
|
|
id: 'inv-1',
|
|
invoice_number: 'F-2026001',
|
|
status: 'sent',
|
|
total: 12500,
|
|
remaining_amount: 12500,
|
|
paid_amount: 0,
|
|
currency: 'SEK',
|
|
exchange_rate: null,
|
|
journal_entry_id: null,
|
|
customer: { name: 'Test AB' },
|
|
},
|
|
error: null,
|
|
}) // invoice fetch
|
|
enqueue({ data: { accounting_method: 'accrual', entity_type: 'aktiebolag' }, error: null }) // settings
|
|
// With cash_account_id null, resolveSettlementAccount lists the company's
|
|
// enabled cash accounts for the currency (issue #1722); no rows here, so
|
|
// it keeps the 1930 fallback.
|
|
enqueue({ data: [], error: null }) // resolveSettlementAccount: no enabled cash accounts
|
|
enqueue({ data: [{ id: 'inv-1' }], error: null }) // invoice CAS update
|
|
enqueue({ data: { id: 'ip-1' }, error: null }) // invoice_payments insert
|
|
enqueue({ data: null, error: null }) // transactions update (link)
|
|
enqueue({ data: null, error: null }) // dispatcher pending_operations update
|
|
|
|
const op = makePendingOp({ params: { transaction_id: 'tx-1', invoice_id: 'inv-1' } })
|
|
const result = await commitPendingOperation(supabase as never, 'user-1', 'company-1', op)
|
|
|
|
expect(result.status).toBe('committed')
|
|
expect(mockCreateJournalEntry).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
'company-1',
|
|
'user-1',
|
|
expect.objectContaining({
|
|
lines: expect.arrayContaining([
|
|
expect.objectContaining({ account_number: '1930', debit_amount: 12500 }),
|
|
expect.objectContaining({ account_number: '1510', credit_amount: 12500 }),
|
|
]),
|
|
}),
|
|
)
|
|
})
|
|
|
|
it('rejects the operation (mutates nothing) when the cash_accounts lookup errors', async () => {
|
|
// Regression: an explicit cash_account_id almost certainly resolves to a
|
|
// non-1930 account, so a transient lookup failure must not silently
|
|
// degrade to 1930 -- the same misbooking risk this fix exists to close,
|
|
// just triggered by infra flakiness instead of a stale setting.
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
enqueue({ data: { id: 'op-1' }, error: null }) // CAS claim
|
|
enqueue({
|
|
data: {
|
|
id: 'tx-1',
|
|
company_id: 'company-1',
|
|
amount: 12500,
|
|
currency: 'SEK',
|
|
date: '2026-05-12',
|
|
invoice_id: null,
|
|
journal_entry_id: null,
|
|
cash_account_id: 'ca-broken',
|
|
},
|
|
error: null,
|
|
}) // transaction fetch
|
|
enqueue({
|
|
data: {
|
|
id: 'inv-1',
|
|
invoice_number: 'F-2026001',
|
|
status: 'sent',
|
|
total: 12500,
|
|
remaining_amount: 12500,
|
|
paid_amount: 0,
|
|
currency: 'SEK',
|
|
exchange_rate: null,
|
|
journal_entry_id: null,
|
|
customer: { name: 'Test AB' },
|
|
},
|
|
error: null,
|
|
}) // invoice fetch
|
|
enqueue({ data: { accounting_method: 'accrual', entity_type: 'aktiebolag' }, error: null }) // settings
|
|
enqueue({ data: null, error: { message: 'connection reset' } }) // cash_accounts lookup errors
|
|
enqueue({ data: null, error: null }) // dispatcher marks the op 'rejected'
|
|
|
|
const op = makePendingOp({ params: { transaction_id: 'tx-1', invoice_id: 'inv-1' } })
|
|
const result = await commitPendingOperation(supabase as never, 'user-1', 'company-1', op)
|
|
|
|
expect(result.status).toBe('failed')
|
|
expect(mockCreatePaymentEntry).not.toHaveBeenCalled()
|
|
expect(mockCreateCashEntry).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('settles a whole-krona payment of an öre-carrying invoice in full (3740 absorbs the residual)', async () => {
|
|
// gnubok_feedback 2026-07-24: a bank tx exactly matching the invoice's
|
|
// rounded "Att betala" was rejected as MATCH_AMOUNT_EXCEEDS_REMAINING
|
|
// because this path called planInvoicePayment without absorbOreRounding.
|
|
const { supabase, enqueue, findCalls } = createQueuedMockSupabase()
|
|
enqueue({ data: { id: 'op-1' }, error: null }) // CAS claim
|
|
enqueue({
|
|
data: {
|
|
id: 'tx-1',
|
|
company_id: 'company-1',
|
|
amount: 12500,
|
|
currency: 'SEK',
|
|
date: '2026-05-12',
|
|
invoice_id: null,
|
|
journal_entry_id: null,
|
|
cash_account_id: null,
|
|
},
|
|
error: null,
|
|
}) // transaction fetch
|
|
enqueue({
|
|
data: {
|
|
id: 'inv-1',
|
|
invoice_number: 'F-2026001',
|
|
status: 'sent',
|
|
total: 12499.63,
|
|
remaining_amount: 12499.63,
|
|
paid_amount: 0,
|
|
currency: 'SEK',
|
|
exchange_rate: null,
|
|
journal_entry_id: null,
|
|
customer: { name: 'Test AB' },
|
|
},
|
|
error: null,
|
|
}) // invoice fetch
|
|
enqueue({ data: { accounting_method: 'accrual', entity_type: 'aktiebolag' }, error: null }) // settings
|
|
enqueue({ data: [], error: null }) // resolveSettlementAccount: no enabled cash accounts -> 1930
|
|
enqueue({ data: [{ id: 'inv-1' }], error: null }) // invoice CAS update
|
|
enqueue({ data: { id: 'ip-1' }, error: null }) // invoice_payments insert
|
|
enqueue({ data: null, error: null }) // transactions update (link)
|
|
enqueue({ data: null, error: null }) // dispatcher pending_operations update
|
|
|
|
const op = makePendingOp({ params: { transaction_id: 'tx-1', invoice_id: 'inv-1' } })
|
|
const result = await commitPendingOperation(supabase as never, 'user-1', 'company-1', op)
|
|
|
|
expect(result.status).toBe('committed')
|
|
expect(mockCreateJournalEntry).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
'company-1',
|
|
'user-1',
|
|
expect.objectContaining({
|
|
lines: expect.arrayContaining([
|
|
expect.objectContaining({ account_number: '1930', debit_amount: 12500 }),
|
|
expect.objectContaining({ account_number: '1510', credit_amount: 12499.63 }),
|
|
expect.objectContaining({ account_number: '3740' }),
|
|
]),
|
|
}),
|
|
)
|
|
const invoiceUpdate = findCalls('invoices', 'update').at(-1)?.[0]
|
|
expect(invoiceUpdate).toMatchObject({
|
|
status: 'paid',
|
|
paid_amount: 12499.63,
|
|
remaining_amount: 0,
|
|
})
|
|
})
|
|
|
|
it('3740 residual: the invoice_payments row carries the applied amount, not the cash received (#2250)', async () => {
|
|
// Remaining 999.60 settled by a whole-krona 1 000.00 bank line: 3740
|
|
// absorbs the 0.40 and paid_amount advances by the remaining only, so the
|
|
// AR sub-ledger row must be 999.60 as well (parity with the dashboard and
|
|
// v1 routes; the #2236 definition every reader relies on).
|
|
const { supabase, enqueue, findCalls } = createQueuedMockSupabase()
|
|
enqueue({ data: { id: 'op-1' }, error: null }) // CAS claim
|
|
enqueue({
|
|
data: {
|
|
id: 'tx-1',
|
|
company_id: 'company-1',
|
|
amount: 1000,
|
|
currency: 'SEK',
|
|
date: '2026-05-12',
|
|
invoice_id: null,
|
|
journal_entry_id: null,
|
|
cash_account_id: null,
|
|
},
|
|
error: null,
|
|
}) // transaction fetch
|
|
enqueue({
|
|
data: {
|
|
id: 'inv-1',
|
|
invoice_number: 'F-2026002',
|
|
status: 'sent',
|
|
total: 999.6,
|
|
remaining_amount: 999.6,
|
|
paid_amount: 0,
|
|
currency: 'SEK',
|
|
exchange_rate: null,
|
|
journal_entry_id: null,
|
|
customer: { name: 'Test AB' },
|
|
},
|
|
error: null,
|
|
}) // invoice fetch
|
|
enqueue({ data: { accounting_method: 'accrual', entity_type: 'aktiebolag' }, error: null }) // settings
|
|
enqueue({ data: [], error: null }) // resolveSettlementAccount: no enabled cash accounts -> 1930
|
|
enqueue({ data: [{ id: 'inv-1' }], error: null }) // invoice CAS update
|
|
enqueue({ data: { id: 'ip-1' }, error: null }) // invoice_payments insert
|
|
enqueue({ data: null, error: null }) // transactions update (link)
|
|
enqueue({ data: null, error: null }) // dispatcher pending_operations update
|
|
|
|
const op = makePendingOp({ params: { transaction_id: 'tx-1', invoice_id: 'inv-1' } })
|
|
const result = await commitPendingOperation(supabase as never, 'user-1', 'company-1', op)
|
|
|
|
expect(result.status).toBe('committed')
|
|
expect(mockCreateJournalEntry).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
'company-1',
|
|
'user-1',
|
|
expect.objectContaining({
|
|
lines: expect.arrayContaining([
|
|
expect.objectContaining({ account_number: '1930', debit_amount: 1000 }),
|
|
expect.objectContaining({ account_number: '1510', credit_amount: 999.6 }),
|
|
expect.objectContaining({ account_number: '3740', credit_amount: 0.4 }),
|
|
]),
|
|
}),
|
|
)
|
|
expect(findCalls('invoices', 'update').at(-1)?.[0]).toMatchObject({
|
|
status: 'paid',
|
|
paid_amount: 999.6,
|
|
remaining_amount: 0,
|
|
})
|
|
expect(findCalls('invoice_payments', 'insert').at(-1)?.[0]).toMatchObject({
|
|
invoice_id: 'inv-1',
|
|
transaction_id: 'tx-1',
|
|
payment_date: '2026-05-12',
|
|
amount: 999.6,
|
|
currency: 'SEK',
|
|
journal_entry_id: 'je-1',
|
|
})
|
|
})
|
|
|
|
it('converts a cross-currency payment into the invoice currency before recording it', async () => {
|
|
// Parity with the dashboard/v1 routes: feeding the raw SEK amount into a
|
|
// USD invoice corrupts the units of paid_amount / remaining_amount and
|
|
// the invoice_payments row.
|
|
mockFetchExchangeRate.mockResolvedValue({ currency: 'USD', rate: 10, date: '2026-05-12' })
|
|
const { supabase, enqueue, findCalls } = createQueuedMockSupabase()
|
|
enqueue({ data: { id: 'op-1' }, error: null }) // CAS claim
|
|
enqueue({
|
|
data: {
|
|
id: 'tx-1',
|
|
company_id: 'company-1',
|
|
amount: 12500,
|
|
amount_sek: 12500,
|
|
currency: 'SEK',
|
|
date: '2026-05-12',
|
|
invoice_id: null,
|
|
journal_entry_id: null,
|
|
cash_account_id: null,
|
|
},
|
|
error: null,
|
|
}) // transaction fetch
|
|
enqueue({
|
|
data: {
|
|
id: 'inv-1',
|
|
invoice_number: 'F-2026001',
|
|
status: 'sent',
|
|
total: 1250,
|
|
remaining_amount: 1250,
|
|
paid_amount: 0,
|
|
currency: 'USD',
|
|
exchange_rate: 10,
|
|
journal_entry_id: null,
|
|
customer: { name: 'US Inc' },
|
|
},
|
|
error: null,
|
|
}) // invoice fetch
|
|
enqueue({ data: { accounting_method: 'accrual', entity_type: 'aktiebolag' }, error: null }) // settings
|
|
enqueue({ data: [], error: null }) // resolveSettlementAccount: no enabled cash accounts -> 1930
|
|
enqueue({ data: [{ id: 'inv-1' }], error: null }) // invoice CAS update
|
|
enqueue({ data: { id: 'ip-1' }, error: null }) // invoice_payments insert
|
|
enqueue({ data: null, error: null }) // transactions update (link)
|
|
enqueue({ data: null, error: null }) // dispatcher pending_operations update
|
|
|
|
const op = makePendingOp({ params: { transaction_id: 'tx-1', invoice_id: 'inv-1' } })
|
|
const result = await commitPendingOperation(supabase as never, 'user-1', 'company-1', op)
|
|
|
|
expect(result.status).toBe('committed')
|
|
// paid/remaining accumulate in INVOICE currency (1250 USD), not 12500 SEK.
|
|
const invoiceUpdate = findCalls('invoices', 'update').at(-1)?.[0]
|
|
expect(invoiceUpdate).toMatchObject({
|
|
status: 'paid',
|
|
paid_amount: 1250,
|
|
remaining_amount: 0,
|
|
})
|
|
const paymentInsert = findCalls('invoice_payments', 'insert').at(-1)?.[0]
|
|
expect(paymentInsert).toMatchObject({ amount: 1250, currency: 'USD', exchange_rate: 10 })
|
|
})
|
|
|
|
it('rejects a cross-currency match when no exchange rate is available', async () => {
|
|
mockFetchExchangeRate.mockResolvedValue(null)
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
enqueue({ data: { id: 'op-1' }, error: null }) // CAS claim
|
|
enqueue({
|
|
data: {
|
|
id: 'tx-1',
|
|
company_id: 'company-1',
|
|
amount: 12500,
|
|
amount_sek: 12500,
|
|
currency: 'SEK',
|
|
date: '2026-05-12',
|
|
invoice_id: null,
|
|
journal_entry_id: null,
|
|
cash_account_id: null,
|
|
},
|
|
error: null,
|
|
}) // transaction fetch
|
|
enqueue({
|
|
data: {
|
|
id: 'inv-1',
|
|
invoice_number: 'F-2026001',
|
|
status: 'sent',
|
|
total: 1250,
|
|
remaining_amount: 1250,
|
|
paid_amount: 0,
|
|
currency: 'USD',
|
|
exchange_rate: 10,
|
|
journal_entry_id: null,
|
|
customer: { name: 'US Inc' },
|
|
},
|
|
error: null,
|
|
}) // invoice fetch
|
|
enqueue({ data: null, error: null }) // dispatcher pending_operations update
|
|
|
|
const op = makePendingOp({ params: { transaction_id: 'tx-1', invoice_id: 'inv-1' } })
|
|
const result = await commitPendingOperation(supabase as never, 'user-1', 'company-1', op)
|
|
|
|
// 400 (unlike 404/409 auto-rejects) surfaces as a plain failure; the op
|
|
// row itself is released as 'rejected' with the error payload.
|
|
expect(result.status).toBe('failed')
|
|
expect(result.http_status).toBe(400)
|
|
expect(mockCreateJournalEntry).not.toHaveBeenCalled()
|
|
})
|
|
})
|