* fix(invoices): apply configured voucher series to payments + preview next voucher The booking engine resolves the series from default_voucher_series_per_source_type, but the global "Standardserie" dropdown wrote a separate field the engine ignored, and cash-method invoice payments (invoice_cash_payment) weren't exposed in settings — so configured series were silently dropped to "A". - Expose cash/private payment source types in the per-source-type form - Write the global default through to the map on save, keeping overrides - Resolve voucher-sequences/next by source_type (+date) to match the engine - Show the upcoming voucher (V2) in the payment dialog title - Share resolveInvoicePaymentSourceType so preview and booking can't drift Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(salary): keep AGI panel in sync with Skatteverket signing state The AGI panel mixed run-scoped generation state (agi_generated_at, agi_declarations) with period-scoped submission state (extension_data agi_submission_{period}), so the two could drift and present contradictory UI. Reconcile them: - Auto-detect a Mina Sidor BankID signature: while awaiting_signing, poll /agi/kvittenser on mount and on tab refocus so the panel flips to "signed" (hiding the signing actions) without a manual "Hamta kvittens" click. - Warn instead of offering to sign when the locked granskningsunderlag predates the run's latest AGI generation (draftIsStale) — avoids filing superseded figures. - Self-heal a stale "AGI-XML saknas" error once the run's AGI is (re)generated out-of-band (MCP/API/other tab). - Refetch the salary run on tab focus so agi_generated_at reflects out-of-band generation without a hard reload. - /agi/lasUpp now clears the cached agi_submission_{period} record, so unlocking drops the panel back to the pre-submission state instead of stranding it on a released "redo att signeras" draft. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat: Implement VAT registration handling and invoice item line types - Added VAT registration check in commitCreateInvoice to set VAT rate to 0% for non-VAT registered companies. - Updated invoice creation logic to reflect 'exempt' VAT treatment and adjusted related fields accordingly. - Introduced support for free-text and blank spacer rows in invoice items by adding a new line_type field. - Enhanced invoice and credit note handling to accommodate new line types. - Added new localized messages for text rows in English and Swedish. - Created tests for salary run approval logic, ensuring bank details are validated correctly. - Implemented effective net payout calculation for salary runs, considering tax overrides. - Added SQL migrations to support new invoice item line types and accounting method awareness for linking invoices to vouchers. * feat(articles): artikelregister with revenue account + VAT rate per article Article register (non-inventory) with per-article VAT rate and optional BAS class-3 revenue-account override. Includes API routes, UI pages, MCP tools, pending-operation staging, and the activate-or-create account flow (ACCOUNTS_NOT_IN_CHART -> ActivateAccountsDialog, unknown numbers -> AddAccountDialog) reusing the journal entry UX. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(bookkeeping): no-doc-required batch + bulk-missing endpoints Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(payments): supplier payment lines + cash-method invoice matching Shared payment-line proposal for supplier invoices, improved match-invoice/match-supplier-invoice flows (kontantmetoden-aware), and voucher-link support without requiring a 151x clearing entry. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(bookkeeping): new journal entry dialog, SIE import tweaks, misc New journal entry dialog component, journal list/page updates, invoice editor updates, SIE import adjustments, transaction ingest and api-key tweaks, pr-agent workflow update. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(invoices): implement tax reduction features and localization updates * feat(tests): add VAT registration gate to pending operations commit tests --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
528 lines
18 KiB
TypeScript
528 lines
18 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import {
|
|
createMockRequest,
|
|
parseJsonResponse,
|
|
createQueuedMockSupabase,
|
|
makeInvoice,
|
|
makeCustomer,
|
|
} from '@/tests/helpers'
|
|
import { eventBus } from '@/lib/events'
|
|
|
|
const { supabase: mockSupabase, enqueue, reset } = createQueuedMockSupabase()
|
|
vi.mock('@/lib/supabase/server', () => ({
|
|
createClient: () => Promise.resolve(mockSupabase),
|
|
}))
|
|
|
|
vi.mock('@/lib/init', () => ({
|
|
ensureInitialized: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/company/context', () => ({
|
|
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
|
|
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
|
|
}))
|
|
|
|
vi.mock('@/lib/auth/require-write', () => ({
|
|
requireWritePermission: vi.fn().mockResolvedValue({ ok: true }),
|
|
}))
|
|
|
|
const mockGetVatRules = vi.fn()
|
|
const mockCalculateVat = vi.fn()
|
|
const mockGetAvailableVatRates = vi.fn()
|
|
vi.mock('@/lib/invoices/vat-rules', () => ({
|
|
getVatRules: (...args: unknown[]) => mockGetVatRules(...args),
|
|
calculateVat: (...args: unknown[]) => mockCalculateVat(...args),
|
|
getAvailableVatRates: (...args: unknown[]) => mockGetAvailableVatRates(...args),
|
|
calculateTotal: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/currency/riksbanken', () => ({
|
|
fetchExchangeRate: vi.fn().mockResolvedValue(null),
|
|
convertToSEK: vi.fn(),
|
|
}))
|
|
|
|
const mockCreateCreditNoteJournalEntry = vi.fn()
|
|
vi.mock('@/lib/bookkeeping/invoice-entries', () => ({
|
|
createCreditNoteJournalEntry: (...args: unknown[]) =>
|
|
mockCreateCreditNoteJournalEntry(...args),
|
|
}))
|
|
|
|
import { GET, POST } from '../route'
|
|
|
|
describe('GET /api/invoices', () => {
|
|
const mockUser = { id: 'user-1', email: 'test@test.se' }
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
reset()
|
|
eventBus.clear()
|
|
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: mockUser } })
|
|
})
|
|
|
|
it('returns 401 when not authenticated', async () => {
|
|
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: null } })
|
|
|
|
const request = createMockRequest('/api/invoices')
|
|
const response = await GET(request)
|
|
const { status, body } = await parseJsonResponse(response)
|
|
|
|
expect(status).toBe(401)
|
|
expect(body).toEqual({ error: 'Unauthorized' })
|
|
})
|
|
|
|
it('returns invoices list', async () => {
|
|
const invoices = [makeInvoice(), makeInvoice()]
|
|
enqueue({ data: invoices, error: null, count: 2 })
|
|
|
|
const request = createMockRequest('/api/invoices')
|
|
const response = await GET(request)
|
|
const { status, body } = await parseJsonResponse<{ data: unknown[]; count: number }>(response)
|
|
|
|
expect(status).toBe(200)
|
|
expect(body.data).toEqual(invoices)
|
|
expect(body.count).toBe(2)
|
|
})
|
|
|
|
it('applies status filter', async () => {
|
|
enqueue({ data: [], error: null, count: 0 })
|
|
|
|
const request = createMockRequest('/api/invoices', {
|
|
searchParams: { status: 'sent' },
|
|
})
|
|
const response = await GET(request)
|
|
const { status } = await parseJsonResponse(response)
|
|
|
|
expect(status).toBe(200)
|
|
expect(mockSupabase.from).toHaveBeenCalledWith('invoices')
|
|
})
|
|
|
|
it('applies pagination', async () => {
|
|
enqueue({ data: [], error: null, count: 0 })
|
|
|
|
const request = createMockRequest('/api/invoices', {
|
|
searchParams: { limit: '10', offset: '20' },
|
|
})
|
|
const response = await GET(request)
|
|
const { status } = await parseJsonResponse(response)
|
|
|
|
expect(status).toBe(200)
|
|
})
|
|
|
|
it('returns 500 on database error', async () => {
|
|
enqueue({ data: null, error: { message: 'DB error' } })
|
|
|
|
const request = createMockRequest('/api/invoices')
|
|
const response = await GET(request)
|
|
const { status, body } = await parseJsonResponse<{ error: string }>(response)
|
|
|
|
expect(status).toBe(500)
|
|
// GET passes through errorResponse which maps unknown DB errors to INTERNAL_ERROR
|
|
expect((body.error as unknown as { code: string }).code).toBe('INTERNAL_ERROR')
|
|
})
|
|
})
|
|
|
|
const VALID_UUID = '550e8400-e29b-41d4-a716-446655440000'
|
|
const VALID_UUID_2 = '550e8400-e29b-41d4-a716-446655440001'
|
|
|
|
describe('POST /api/invoices (create invoice)', () => {
|
|
const mockUser = { id: 'user-1', email: 'test@test.se' }
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
reset()
|
|
eventBus.clear()
|
|
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: mockUser } })
|
|
})
|
|
|
|
it('returns 401 when not authenticated', async () => {
|
|
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: null } })
|
|
|
|
const request = createMockRequest('/api/invoices', {
|
|
method: 'POST',
|
|
body: { customer_id: VALID_UUID, items: [] },
|
|
})
|
|
const response = await POST(request)
|
|
const { status, body } = await parseJsonResponse(response)
|
|
|
|
expect(status).toBe(401)
|
|
expect(body).toEqual({ error: 'Unauthorized' })
|
|
})
|
|
|
|
it('returns 404 when customer not found', async () => {
|
|
enqueue({ data: null, error: { message: 'Not found' } })
|
|
|
|
const request = createMockRequest('/api/invoices', {
|
|
method: 'POST',
|
|
body: {
|
|
customer_id: VALID_UUID_2,
|
|
invoice_date: '2024-06-15',
|
|
due_date: '2024-07-15',
|
|
currency: 'SEK',
|
|
items: [{ description: 'Test', quantity: 1, unit: 'st', unit_price: 1000 }],
|
|
},
|
|
})
|
|
const response = await POST(request)
|
|
const { status, body } = await parseJsonResponse<{ error: string }>(response)
|
|
|
|
expect(status).toBe(404)
|
|
expect((body.error as unknown as { code: string }).code).toBe('INVOICE_CUSTOMER_NOT_FOUND')
|
|
})
|
|
|
|
it('creates invoice with items and emits event', async () => {
|
|
const customer = makeCustomer({ id: VALID_UUID })
|
|
const createdInvoice = makeInvoice({ id: 'inv-1', invoice_number: null })
|
|
|
|
mockGetVatRules.mockReturnValue({
|
|
treatment: 'standard_25',
|
|
rate: 25,
|
|
momsRuta: '10',
|
|
reverseChargeText: null,
|
|
})
|
|
mockCalculateVat.mockReturnValue(2500)
|
|
mockGetAvailableVatRates.mockReturnValue([
|
|
{ rate: 25, label: '25%', treatment: 'standard_25' },
|
|
{ rate: 12, label: '12%', treatment: 'reduced_12' },
|
|
{ rate: 6, label: '6%', treatment: 'reduced_6' },
|
|
{ rate: 0, label: '0% (momsfri)', treatment: 'exempt' },
|
|
])
|
|
|
|
// Fetch customer
|
|
enqueue({ data: customer, error: null })
|
|
// company_settings.vat_registered gate (registered → VAT flows as before)
|
|
enqueue({ data: { vat_registered: true }, error: null })
|
|
// Insert invoice (number is null on insert; allocated immediately after items)
|
|
enqueue({ data: createdInvoice, error: null })
|
|
// Insert items
|
|
enqueue({ data: null, error: null })
|
|
// ensureInvoiceNumber → generate_invoice_number RPC
|
|
enqueue({ data: '2026001', error: null })
|
|
// Fetch complete invoice
|
|
enqueue({ data: { ...createdInvoice, invoice_number: '2026001', customer, items: [] }, error: null })
|
|
|
|
const emitSpy = vi.spyOn(eventBus, 'emit')
|
|
|
|
const request = createMockRequest('/api/invoices', {
|
|
method: 'POST',
|
|
body: {
|
|
customer_id: VALID_UUID,
|
|
invoice_date: '2024-06-15',
|
|
due_date: '2024-07-15',
|
|
currency: 'SEK',
|
|
items: [{ description: 'Consulting', quantity: 10, unit: 'tim', unit_price: 1000 }],
|
|
},
|
|
})
|
|
const response = await POST(request)
|
|
const { status, body } = await parseJsonResponse<{ data: unknown }>(response)
|
|
|
|
expect(status).toBe(200)
|
|
expect(body.data).toBeTruthy()
|
|
expect(emitSpy).toHaveBeenCalledWith(
|
|
expect.objectContaining({ type: 'invoice.created' })
|
|
)
|
|
})
|
|
|
|
it('saves an unnumbered draft without a number or event when save_as_draft is true', async () => {
|
|
const customer = makeCustomer({ id: VALID_UUID })
|
|
const createdInvoice = makeInvoice({ id: 'inv-1', invoice_number: null })
|
|
|
|
mockGetVatRules.mockReturnValue({
|
|
treatment: 'standard_25',
|
|
rate: 25,
|
|
momsRuta: '10',
|
|
reverseChargeText: null,
|
|
})
|
|
mockCalculateVat.mockReturnValue(2500)
|
|
mockGetAvailableVatRates.mockReturnValue([
|
|
{ rate: 25, label: '25%', treatment: 'standard_25' },
|
|
{ rate: 12, label: '12%', treatment: 'reduced_12' },
|
|
{ rate: 6, label: '6%', treatment: 'reduced_6' },
|
|
{ rate: 0, label: '0% (momsfri)', treatment: 'exempt' },
|
|
])
|
|
|
|
// Fetch customer
|
|
enqueue({ data: customer, error: null })
|
|
// company_settings.vat_registered gate (registered → VAT flows as before)
|
|
enqueue({ data: { vat_registered: true }, error: null })
|
|
// Insert invoice (stays unnumbered — the allocation step is skipped)
|
|
enqueue({ data: createdInvoice, error: null })
|
|
// Insert items
|
|
enqueue({ data: null, error: null })
|
|
// Fetch complete invoice (still unnumbered; no generate_invoice_number RPC)
|
|
enqueue({ data: { ...createdInvoice, invoice_number: null, customer, items: [] }, error: null })
|
|
|
|
const emitSpy = vi.spyOn(eventBus, 'emit')
|
|
|
|
const request = createMockRequest('/api/invoices', {
|
|
method: 'POST',
|
|
body: {
|
|
customer_id: VALID_UUID,
|
|
invoice_date: '2024-06-15',
|
|
due_date: '2024-07-15',
|
|
currency: 'SEK',
|
|
save_as_draft: true,
|
|
items: [{ description: 'Consulting', quantity: 10, unit: 'tim', unit_price: 1000 }],
|
|
},
|
|
})
|
|
const response = await POST(request)
|
|
const { status, body } = await parseJsonResponse<{ data: { invoice_number: string | null } }>(response)
|
|
|
|
expect(status).toBe(200)
|
|
expect(body.data.invoice_number).toBeNull()
|
|
expect(emitSpy).not.toHaveBeenCalledWith(
|
|
expect.objectContaining({ type: 'invoice.created' })
|
|
)
|
|
})
|
|
|
|
it('rolls back invoice when items insertion fails', async () => {
|
|
const customer = makeCustomer({ id: VALID_UUID })
|
|
const createdInvoice = makeInvoice({ id: 'inv-1' })
|
|
|
|
mockGetVatRules.mockReturnValue({
|
|
treatment: 'standard_25',
|
|
rate: 25,
|
|
momsRuta: '10',
|
|
reverseChargeText: null,
|
|
})
|
|
mockCalculateVat.mockReturnValue(2500)
|
|
mockGetAvailableVatRates.mockReturnValue([
|
|
{ rate: 25, label: '25%', treatment: 'standard_25' },
|
|
{ rate: 12, label: '12%', treatment: 'reduced_12' },
|
|
{ rate: 6, label: '6%', treatment: 'reduced_6' },
|
|
{ rate: 0, label: '0% (momsfri)', treatment: 'exempt' },
|
|
])
|
|
|
|
enqueue({ data: customer, error: null })
|
|
// company_settings.vat_registered gate (registered → VAT flows as before)
|
|
enqueue({ data: { vat_registered: true }, error: null })
|
|
enqueue({ data: createdInvoice, error: null })
|
|
// Items insertion fails
|
|
enqueue({ data: null, error: { message: 'Items insert failed' } })
|
|
// Rollback delete
|
|
enqueue({ data: null, error: null })
|
|
|
|
const request = createMockRequest('/api/invoices', {
|
|
method: 'POST',
|
|
body: {
|
|
customer_id: VALID_UUID,
|
|
invoice_date: '2024-06-15',
|
|
due_date: '2024-07-15',
|
|
currency: 'SEK',
|
|
items: [{ description: 'Test', quantity: 1, unit: 'st', unit_price: 1000 }],
|
|
},
|
|
})
|
|
const response = await POST(request)
|
|
const { status, body } = await parseJsonResponse<{ error: string }>(response)
|
|
|
|
expect(status).toBe(500)
|
|
expect((body.error as unknown as { code: string }).code).toBe('INVOICE_CREATE_ITEMS_FAILED')
|
|
})
|
|
|
|
it('soft-cancels the invoice when invoice-number allocation fails', async () => {
|
|
const customer = makeCustomer({ id: VALID_UUID })
|
|
const createdInvoice = makeInvoice({ id: 'inv-1', invoice_number: null })
|
|
|
|
mockGetVatRules.mockReturnValue({
|
|
treatment: 'standard_25',
|
|
rate: 25,
|
|
momsRuta: '10',
|
|
reverseChargeText: null,
|
|
})
|
|
mockCalculateVat.mockReturnValue(2500)
|
|
mockGetAvailableVatRates.mockReturnValue([
|
|
{ rate: 25, label: '25%', treatment: 'standard_25' },
|
|
{ rate: 12, label: '12%', treatment: 'reduced_12' },
|
|
{ rate: 6, label: '6%', treatment: 'reduced_6' },
|
|
{ rate: 0, label: '0% (momsfri)', treatment: 'exempt' },
|
|
])
|
|
|
|
enqueue({ data: customer, error: null })
|
|
// company_settings.vat_registered gate (registered → VAT flows as before)
|
|
enqueue({ data: { vat_registered: true }, error: null })
|
|
enqueue({ data: createdInvoice, error: null })
|
|
// Items insertion succeeds
|
|
enqueue({ data: null, error: null })
|
|
// generate_invoice_number RPC fails
|
|
enqueue({ data: null, error: { message: 'sequence locked' } })
|
|
// Rollback path: re-fetch invoice_number, then soft-cancel.
|
|
enqueue({ data: { invoice_number: null }, error: null })
|
|
enqueue({ data: null, error: null })
|
|
|
|
const request = createMockRequest('/api/invoices', {
|
|
method: 'POST',
|
|
body: {
|
|
customer_id: VALID_UUID,
|
|
invoice_date: '2024-06-15',
|
|
due_date: '2024-07-15',
|
|
currency: 'SEK',
|
|
items: [{ description: 'Test', quantity: 1, unit: 'st', unit_price: 1000 }],
|
|
},
|
|
})
|
|
const response = await POST(request)
|
|
const { status, body } = await parseJsonResponse<{ error: string }>(response)
|
|
|
|
expect(status).toBe(500)
|
|
expect((body.error as unknown as { code: string }).code).toBe('INVOICE_CREATE_NUMBER_ASSIGN_FAILED')
|
|
})
|
|
})
|
|
|
|
describe('POST /api/invoices (create credit note)', () => {
|
|
const mockUser = { id: 'user-1', email: 'test@test.se' }
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
reset()
|
|
eventBus.clear()
|
|
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: mockUser } })
|
|
})
|
|
|
|
it('returns 404 when original invoice not found', async () => {
|
|
enqueue({ data: null, error: { message: 'Not found' } })
|
|
|
|
const request = createMockRequest('/api/invoices', {
|
|
method: 'POST',
|
|
body: { credited_invoice_id: VALID_UUID_2 },
|
|
})
|
|
const response = await POST(request)
|
|
const { status, body } = await parseJsonResponse<{ error: string }>(response)
|
|
|
|
expect(status).toBe(404)
|
|
expect((body.error as unknown as { code: string }).code).toBe('INVOICE_CREDIT_ORIGINAL_NOT_FOUND')
|
|
})
|
|
|
|
it('returns 400 when invoice is already credited', async () => {
|
|
const original = makeInvoice({ id: VALID_UUID, status: 'credited' })
|
|
enqueue({ data: original, error: null })
|
|
|
|
const request = createMockRequest('/api/invoices', {
|
|
method: 'POST',
|
|
body: { credited_invoice_id: VALID_UUID },
|
|
})
|
|
const response = await POST(request)
|
|
const { status, body } = await parseJsonResponse<{ error: string }>(response)
|
|
|
|
expect(status).toBe(400)
|
|
expect((body.error as unknown as { code: string }).code).toBe('INVOICE_CREDIT_ALREADY_CREDITED')
|
|
})
|
|
|
|
it('returns 400 when invoice is in draft status', async () => {
|
|
const original = makeInvoice({ id: VALID_UUID, status: 'draft' })
|
|
enqueue({ data: original, error: null })
|
|
|
|
const request = createMockRequest('/api/invoices', {
|
|
method: 'POST',
|
|
body: { credited_invoice_id: VALID_UUID },
|
|
})
|
|
const response = await POST(request)
|
|
const { status, body } = await parseJsonResponse<{ error: string }>(response)
|
|
|
|
expect(status).toBe(400)
|
|
expect((body.error as unknown as { code: string }).code).toBe('INVOICE_CREDIT_NOT_SENT')
|
|
})
|
|
|
|
it('creates credit note with negated amounts and emits event', async () => {
|
|
const items = [
|
|
{
|
|
id: 'item-1',
|
|
invoice_id: 'inv-1',
|
|
sort_order: 0,
|
|
description: 'Consulting',
|
|
quantity: 10,
|
|
unit: 'tim',
|
|
unit_price: 1000,
|
|
line_total: 10000,
|
|
vat_rate: 25,
|
|
vat_amount: 2500,
|
|
created_at: '2024-06-15T14:30:00Z',
|
|
},
|
|
]
|
|
const original = makeInvoice({
|
|
id: VALID_UUID,
|
|
status: 'sent',
|
|
subtotal: 10000,
|
|
vat_amount: 2500,
|
|
total: 12500,
|
|
items,
|
|
})
|
|
const creditNote = makeInvoice({
|
|
id: 'cn-1',
|
|
credited_invoice_id: VALID_UUID,
|
|
subtotal: -10000,
|
|
vat_amount: -2500,
|
|
total: -12500,
|
|
status: 'sent',
|
|
})
|
|
|
|
// Fetch original invoice
|
|
enqueue({ data: original, error: null })
|
|
// Insert credit note
|
|
enqueue({ data: creditNote, error: null })
|
|
// Insert credit note items
|
|
enqueue({ data: null, error: null })
|
|
// Update original status to 'credited'
|
|
enqueue({ data: null, error: null })
|
|
// Fetch complete credit note
|
|
enqueue({ data: { ...creditNote, items: [] }, error: null })
|
|
// Fetch company settings for entity type
|
|
enqueue({ data: { entity_type: 'enskild_firma' }, error: null })
|
|
|
|
mockCreateCreditNoteJournalEntry.mockResolvedValue({ id: 'je-1' })
|
|
// Update credit note with journal_entry_id
|
|
enqueue({ data: null, error: null })
|
|
|
|
const emitSpy = vi.spyOn(eventBus, 'emit')
|
|
|
|
const request = createMockRequest('/api/invoices', {
|
|
method: 'POST',
|
|
body: { credited_invoice_id: VALID_UUID },
|
|
})
|
|
const response = await POST(request)
|
|
const { status, body } = await parseJsonResponse<{ data: unknown }>(response)
|
|
|
|
expect(status).toBe(200)
|
|
expect(body.data).toBeTruthy()
|
|
expect(emitSpy).toHaveBeenCalledWith(
|
|
expect.objectContaining({ type: 'credit_note.created' })
|
|
)
|
|
})
|
|
|
|
it('rolls back credit note when items insertion fails', async () => {
|
|
const original = makeInvoice({
|
|
id: VALID_UUID,
|
|
status: 'sent',
|
|
items: [
|
|
{
|
|
id: 'item-1',
|
|
invoice_id: 'inv-1',
|
|
sort_order: 0,
|
|
description: 'Test',
|
|
quantity: 1,
|
|
unit: 'st',
|
|
unit_price: 1000,
|
|
line_total: 1000,
|
|
vat_rate: 25,
|
|
vat_amount: 250,
|
|
created_at: '2024-06-15T14:30:00Z',
|
|
},
|
|
],
|
|
})
|
|
const creditNote = makeInvoice({ id: 'cn-1' })
|
|
|
|
enqueue({ data: original, error: null })
|
|
enqueue({ data: creditNote, error: null })
|
|
// Items fail
|
|
enqueue({ data: null, error: { message: 'Items insert failed' } })
|
|
// Rollback delete
|
|
enqueue({ data: null, error: null })
|
|
|
|
const request = createMockRequest('/api/invoices', {
|
|
method: 'POST',
|
|
body: { credited_invoice_id: VALID_UUID },
|
|
})
|
|
const response = await POST(request)
|
|
const { status, body } = await parseJsonResponse<{ error: string }>(response)
|
|
|
|
expect(status).toBe(500)
|
|
expect((body.error as unknown as { code: string }).code).toBe('INVOICE_CREATE_ITEMS_FAILED')
|
|
})
|
|
})
|