9ed0b9515a
* feat(invoices): add Plusgiro input to bank details settings Plusgiro was already persisted, validated by the API schema, rendered on the invoice PDF and toggleable via "Visa plusgiro" — but the settings UI had no field to enter the number, so plusgiro-only users could not fill it in. Add the input next to Bankgiro with Luhn validation and hyphen formatting, include it in the save payload (normalised on save so raw digits still match the dashed schema format), and add sv/en strings. Adds validatePlusgiroNumber/formatPlusgiroNumber helpers + tests. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(invoices): respect non-VAT-registered seller in PDF preview + portal tooltips Two user-reported bugs: - PDF preview (/api/invoices/preview-pdf) ignored company.vat_registered and fell back to the customer-driven 25% rate, so a non-momsregistrerad seller saw VAT in the review step even though the created invoice books none. Mirror the server-side write gate (build-invoice-write.ts): force 0% when vat_registered is false (delivery notes excepted). - InfoTooltip rendered TooltipContent without a Portal, so tooltips were clipped by the scrollable DialogContent (overflow-y-auto) in the send-invoice journal-entry review. Wrap in TooltipPrimitive.Portal. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(transactions): book library mall from its literal lines, not a lossy fallback Booking a bank transaction with a user-created booking-template (mall) via the convertible "QuickReview" fast path reduced the template to a single category + one account_override, silently discarding the chosen debit/credit. A kundinbetalning mall (D 1930 / K 1510) booked as a generic cost (D 6991 / K 1930), or with a VAT line as D 1930 / K 1930 / K 2611 — and the result flipped with the direction inferred from the business/settlement line tags, so visually-identical templates produced different verifikationer. Route every library template through the journal-entry editor (applyTemplate -> /book), which posts the literal lines, regardless of convertibility. Add regression tests locking the contract. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(bookkeeping): make the booking-time duplicate guard bypassable TRANSACTION_BOOK_POSSIBLE_DUPLICATE told users they could "book anyway" but the UI dead-ended on a toast with no way to do so. Add a shared DuplicateBookingDialog that surfaces the already-booked sibling and lets the user review it or book anyway (force bound to the reviewed candidate, which the server re-detects so a stale id cannot wave the guard away). - Wire the dialog into the /transactions categorize flow and the manual booking dialog (JournalEntryForm -> /api/transactions/[id]/book) - Bind the override to expected_duplicate_transaction_id OR expected_duplicate_journal_entry_id so ledger-only vouchers (paid invoice, salary run) can be confirmed too - Extend the guard to the pending-operations commit path and the MCP server - Tests for book/categorize routes, detection, and the commit guard Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(bookkeeping): log duplicate-guard bypass to behandlingshistorik in the agent commit path The web /book and /categorize routes append a durable BankTransactionDuplicateDismissed event when a user books over a detected possible double-booking. The agent commit path (commitCategorizeTransaction, commitMarkInvoicePaid) skipped the guard silently on allow_duplicate=true, leaving no behandlingshistorik — an auditor could not reconstruct why the duplicate was allowed (BFNAR 2013:2 kap 8). When allow_duplicate=true, re-detect the candidate and append the dismissal event (BankTransactionDuplicateDismissed for the bank-line path, InvoiceDuplicatePaymentDismissed for mark-paid). Best-effort — a logging failure never blocks a legitimate booking. Payloads stay PII-safe (ids, amounts, dates only — no customer or merchant name). Also fix the misleading DuplicateBookingDialog JSDoc: the retry binds expected_duplicate_journal_entry_id, not candidate.transaction_id, so the systemdokumentation matches the actual control (BFL 7 kap). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * test(mcp-server): stub booking-duplicate guard in receipt-matcher categorize tests The gnubok_categorize_transaction tool runs the booking-time duplicate guard before staging; its detection queries consumed the queued supabase mock results, so the staging assertions saw a thrown duplicate error instead of a staged op. Mock detectBookingDuplicate to "no duplicate" since these tests don't exercise that path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * refactor(transactions): use roundOre for duplicate-guard öre rounding Replace naive Math.round(x*100)/100 with roundOre() from @/lib/money in the booking-time duplicate guard (detection lib, commit executor, MCP categorize tool), satisfying the no-new-antipatterns ratchet guard. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
238 lines
9.3 KiB
TypeScript
238 lines
9.3 KiB
TypeScript
/**
|
|
* The agent/MCP commit path (lib/pending-operations/commit.ts) must run the same
|
|
* duplicate guards as the web routes — it previously bypassed them entirely,
|
|
* which let an approved staged op double-book an affärshändelse already in the
|
|
* ledger (the production case: a bank line booked on top of an invoice
|
|
* "markera som betald" voucher or a salary payout).
|
|
*
|
|
* These tests drive the public `commitPendingOperation` dispatcher (the executor
|
|
* functions are private) and assert the op is auto-rejected (409) when a
|
|
* duplicate is detected. The detection functions themselves are unit-tested in
|
|
* lib/transactions/__tests__/booking-duplicate-detection.test.ts and
|
|
* lib/invoices/__tests__/duplicate-payment-detection.test.ts.
|
|
*/
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { eventBus } from '@/lib/events/bus'
|
|
import type { PendingOperation } from '@/types'
|
|
|
|
const mockDetectBookingDuplicate = vi.fn()
|
|
vi.mock('@/lib/transactions/booking-duplicate-detection', () => ({
|
|
detectBookingDuplicate: (...args: unknown[]) => mockDetectBookingDuplicate(...args),
|
|
}))
|
|
|
|
const mockFindDupPayments = vi.fn()
|
|
vi.mock('@/lib/invoices/duplicate-payment-candidates', () => ({
|
|
findDuplicatePaymentCandidatesForInvoice: (...args: unknown[]) => mockFindDupPayments(...args),
|
|
}))
|
|
|
|
const mockAppendProcessingHistory = vi.fn()
|
|
vi.mock('@/lib/processing-history/append', () => ({
|
|
appendProcessingHistory: (...args: unknown[]) => mockAppendProcessingHistory(...args),
|
|
}))
|
|
|
|
import { commitPendingOperation } from '../commit'
|
|
|
|
/** Queue-based supabase mock: each `from()` resolves to 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
|
|
}
|
|
|
|
function makePendingOp(overrides: Partial<PendingOperation>): PendingOperation {
|
|
return {
|
|
id: 'op-1',
|
|
user_id: 'user-1',
|
|
company_id: 'company-1',
|
|
operation_type: 'categorize_transaction',
|
|
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
|
|
}
|
|
|
|
const voucherCandidate = {
|
|
transaction_id: null,
|
|
journal_entry_id: 'je-existing',
|
|
voucher_label: 'A2',
|
|
entry_date: '2026-03-30',
|
|
description: 'Inbetalning kundfaktura 2026001',
|
|
amount: 98565,
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
eventBus.clear()
|
|
})
|
|
|
|
describe('commit duplicate guard: categorize_transaction (reverse / book the bank line)', () => {
|
|
it('auto-rejects (409) when a ledger voucher already books this movement', async () => {
|
|
mockDetectBookingDuplicate.mockResolvedValue(voucherCandidate)
|
|
// claim → transaction fetch → reject update
|
|
const supabase = queuedSupabase([
|
|
{ data: { id: 'op-1' } },
|
|
{ data: { id: 'tx-1', date: '2026-03-26', amount: 98565, cash_account_id: null, journal_entry_id: null } },
|
|
{ data: null },
|
|
])
|
|
|
|
const op = makePendingOp({
|
|
operation_type: 'categorize_transaction',
|
|
params: { transaction_id: 'tx-1', category: 'income' },
|
|
})
|
|
|
|
const result = await commitPendingOperation(supabase, 'user-1', 'company-1', op)
|
|
|
|
expect(mockDetectBookingDuplicate).toHaveBeenCalledTimes(1)
|
|
expect(result.status).toBe('rejected')
|
|
expect(result.http_status).toBe(409)
|
|
})
|
|
|
|
it('does not enforce the guard when allow_duplicate=true, but records the dismissal to behandlingshistorik', async () => {
|
|
mockDetectBookingDuplicate.mockResolvedValue(voucherCandidate)
|
|
// The booking proceeds past the guard (not auto-rejected); the downstream
|
|
// booking is allowed to fail against the bare mock. Before that, the bypass
|
|
// must leave a durable BankTransactionDuplicateDismissed record so an
|
|
// auditor can reconstruct why the duplicate was allowed (BFNAR 2013:2 kap 8).
|
|
const supabase = queuedSupabase([
|
|
{ data: { id: 'op-1' } },
|
|
{ data: { id: 'tx-1', date: '2026-03-26', amount: 98565, cash_account_id: null, journal_entry_id: null } },
|
|
{ data: { entity_type: 'aktiebolag', fiscal_year_start_month: 1 } },
|
|
{ data: [] },
|
|
])
|
|
|
|
const op = makePendingOp({
|
|
operation_type: 'categorize_transaction',
|
|
params: { transaction_id: 'tx-1', category: 'income', allow_duplicate: true },
|
|
})
|
|
|
|
const result = await commitPendingOperation(supabase, 'user-1', 'company-1', op)
|
|
|
|
// Guard not enforced: the op is not auto-rejected at the duplicate guard.
|
|
expect(result.status).not.toBe('rejected')
|
|
// Detection still runs once — to capture the dismissed candidate for audit.
|
|
expect(mockDetectBookingDuplicate).toHaveBeenCalledTimes(1)
|
|
expect(mockAppendProcessingHistory).toHaveBeenCalledTimes(1)
|
|
const event = mockAppendProcessingHistory.mock.calls[0][0]
|
|
expect(event).toMatchObject({
|
|
companyId: 'company-1',
|
|
aggregateType: 'BankTransaction',
|
|
aggregateId: 'tx-1',
|
|
eventType: 'BankTransactionDuplicateDismissed',
|
|
actor: { type: 'user', id: 'user-1' },
|
|
})
|
|
expect(event.payload).toMatchObject({
|
|
transaction_id: 'tx-1',
|
|
dismissed_journal_entry_id: 'je-existing',
|
|
via: 'allow_duplicate',
|
|
})
|
|
})
|
|
|
|
it('records no dismissal when allow_duplicate=true but no duplicate is actually present', async () => {
|
|
mockDetectBookingDuplicate.mockResolvedValue(null)
|
|
const supabase = queuedSupabase([
|
|
{ data: { id: 'op-1' } },
|
|
{ data: { id: 'tx-1', date: '2026-03-26', amount: 98565, cash_account_id: null, journal_entry_id: null } },
|
|
{ data: { entity_type: 'aktiebolag', fiscal_year_start_month: 1 } },
|
|
{ data: [] },
|
|
])
|
|
|
|
const op = makePendingOp({
|
|
operation_type: 'categorize_transaction',
|
|
params: { transaction_id: 'tx-1', category: 'income', allow_duplicate: true },
|
|
})
|
|
|
|
await commitPendingOperation(supabase, 'user-1', 'company-1', op)
|
|
|
|
expect(mockDetectBookingDuplicate).toHaveBeenCalledTimes(1)
|
|
expect(mockAppendProcessingHistory).not.toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('commit duplicate guard: mark_invoice_paid (forward / book the payment)', () => {
|
|
it('auto-rejects (409) when an unlinked bank transaction already looks like the payment', async () => {
|
|
mockFindDupPayments.mockResolvedValue([
|
|
{ id: 'tx-9', date: '2026-03-26', amount: 98565, description: '2026001', merchant_name: null, reference: null, match_reason: 'ocr_exact', match_confidence: 0.99 },
|
|
])
|
|
// claim → invoice fetch → reject update
|
|
const supabase = queuedSupabase([
|
|
{ data: { id: 'op-1' } },
|
|
{ data: { id: 'inv-1', invoice_number: '2026001', status: 'sent', total: 98565, remaining_amount: 98565, customer: { name: 'Arcim Technology AB' } } },
|
|
{ data: null },
|
|
])
|
|
|
|
const op = makePendingOp({
|
|
operation_type: 'mark_invoice_paid',
|
|
params: { invoice_id: 'inv-1', payment_date: '2026-03-30' },
|
|
})
|
|
|
|
const result = await commitPendingOperation(supabase, 'user-1', 'company-1', op)
|
|
|
|
expect(mockFindDupPayments).toHaveBeenCalledTimes(1)
|
|
expect(result.status).toBe('rejected')
|
|
expect(result.http_status).toBe(409)
|
|
})
|
|
|
|
it('does not enforce the guard when allow_duplicate=true, but records the dismissal to behandlingshistorik', async () => {
|
|
mockFindDupPayments.mockResolvedValue([
|
|
{ id: 'tx-9', date: '2026-03-26', amount: 98565, description: '2026001', merchant_name: null, reference: null, match_reason: 'ocr_exact', match_confidence: 0.99 },
|
|
])
|
|
// claim → invoice fetch → company_settings → bare downstream (allowed to fail)
|
|
const supabase = queuedSupabase([
|
|
{ data: { id: 'op-1' } },
|
|
{ data: { id: 'inv-1', invoice_number: '2026001', status: 'sent', total: 98565, remaining_amount: 98565, customer: { name: 'Arcim Technology AB' } } },
|
|
{ data: { accounting_method: 'accrual', entity_type: 'aktiebolag' } },
|
|
])
|
|
|
|
const op = makePendingOp({
|
|
operation_type: 'mark_invoice_paid',
|
|
params: { invoice_id: 'inv-1', payment_date: '2026-03-30', allow_duplicate: true },
|
|
})
|
|
|
|
const result = await commitPendingOperation(supabase, 'user-1', 'company-1', op)
|
|
|
|
// Guard not enforced: not auto-rejected at the duplicate-payment guard.
|
|
expect(result.status).not.toBe('rejected')
|
|
expect(mockFindDupPayments).toHaveBeenCalledTimes(1)
|
|
expect(mockAppendProcessingHistory).toHaveBeenCalledTimes(1)
|
|
const event = mockAppendProcessingHistory.mock.calls[0][0]
|
|
expect(event).toMatchObject({
|
|
companyId: 'company-1',
|
|
aggregateType: 'System',
|
|
aggregateId: 'inv-1',
|
|
eventType: 'InvoiceDuplicatePaymentDismissed',
|
|
actor: { type: 'user', id: 'user-1' },
|
|
})
|
|
expect(event.payload).toMatchObject({
|
|
invoice_id: 'inv-1',
|
|
dismissed_transaction_ids: ['tx-9'],
|
|
candidate_count: 1,
|
|
via: 'allow_duplicate',
|
|
})
|
|
// PII-safe: no customer or merchant name in the payload.
|
|
expect(JSON.stringify(event.payload)).not.toContain('Arcim')
|
|
})
|
|
})
|