Files
accounted/lib/invoices/__tests__/invoice-number-nullable.pg.test.ts
T
Mattsson fd1db89603 Fix/invoice numbers (#365)
* feat: make invoice_number nullable and assign on send

- Updated the invoices table to allow invoice_number to be nullable.
- Modified the logic to assign invoice numbers only when the invoice status transitions to 'sent'.
- Refactored related code to handle nullable invoice numbers, including UI components and API routes.
- Added tests to ensure correct behavior when handling invoices with null invoice numbers.
- Introduced a utility function to display invoice numbers, defaulting to '(Utkast)' for drafts.

* fix: update fiscal period handling to return names of open periods in error messages

* fix: enhance period creation logic to account for company-wide bookkeeping lock-through

* fix: remove unnecessary customer_type field from customer insertion query

* fix: scope invoice number count query to specific companies to avoid test interference

* feat: Implement atomic invoice number generation and ensure compliance with invoice numbering rules

- Introduced `ensureInvoiceNumber` function to assign invoice numbers atomically, handling concurrency and ensuring compliance with document types.
- Updated invoice-related components to utilize the new `invoiceNumberDisplay` utility for consistent invoice number formatting.
- Added checks to ensure that invoices in non-draft statuses have valid invoice numbers, preventing violations of legal requirements.
- Created tests for the new invoice number generation logic, ensuring correct behavior under various scenarios, including concurrent requests.
- Added a draft banner to PDF templates for invoices without assigned numbers, clarifying their status to users.
- Updated database migrations to support the new atomic invoice number generation logic and enforce constraints on invoice statuses.
2026-04-27 16:29:58 +02:00

85 lines
3.2 KiB
TypeScript

import { randomUUID } from 'node:crypto'
import { describe, expect, it } from 'vitest'
import { getPool } from '@/tests/pg/setup'
import { seedCompany } from '@/tests/pg/fixtures'
describe('invoices.invoice_number nullable + partial unique index', () => {
async function insertInvoice(params: {
userId: string
companyId: string
invoiceNumber: string | null
}): Promise<string> {
const id = randomUUID()
const customerId = randomUUID()
await getPool().query(
`INSERT INTO public.customers (id, user_id, company_id, name)
VALUES ($1, $2, $3, 'Test Customer')`,
[customerId, params.userId, params.companyId],
)
await getPool().query(
`INSERT INTO public.invoices
(id, user_id, company_id, customer_id, invoice_number,
invoice_date, due_date, currency, subtotal, vat_amount, total,
vat_treatment, vat_rate, moms_ruta, status)
VALUES ($1, $2, $3, $4, $5,
'2026-04-27', '2026-05-27', 'SEK', 1000, 250, 1250,
'standard_25', 25, '10', 'draft')`,
[id, params.userId, params.companyId, customerId, params.invoiceNumber],
)
return id
}
it('accepts NULL invoice_number for drafts (constraint dropped)', async () => {
const { userId, companyId } = await seedCompany()
const id = await insertInvoice({ userId, companyId, invoiceNumber: null })
const { rows } = await getPool().query<{ invoice_number: string | null }>(
'SELECT invoice_number FROM public.invoices WHERE id = $1',
[id],
)
expect(rows[0]!.invoice_number).toBeNull()
})
it('allows multiple drafts with NULL invoice_number in the same company', async () => {
const { userId, companyId } = await seedCompany()
const a = await insertInvoice({ userId, companyId, invoiceNumber: null })
const b = await insertInvoice({ userId, companyId, invoiceNumber: null })
expect(a).not.toBe(b)
const { rows } = await getPool().query(
'SELECT count(*)::int FROM public.invoices WHERE company_id = $1 AND invoice_number IS NULL',
[companyId],
)
expect(rows[0]!.count).toBe(2)
})
it('still rejects duplicate non-NULL numbers within a company', async () => {
const { userId, companyId } = await seedCompany()
await insertInvoice({ userId, companyId, invoiceNumber: 'F-2026001' })
await expect(
insertInvoice({ userId, companyId, invoiceNumber: 'F-2026001' }),
).rejects.toThrow(/idx_invoices_company_invoice_number|duplicate key/i)
})
it('lets two different companies use the same invoice number', async () => {
const a = await seedCompany()
const b = await seedCompany()
await insertInvoice({ userId: a.userId, companyId: a.companyId, invoiceNumber: 'F-2026001' })
await insertInvoice({ userId: b.userId, companyId: b.companyId, invoiceNumber: 'F-2026001' })
// Scope the count to these two companies — earlier tests in the suite leave
// 'F-2026001' rows behind in their own companies, and pg-real has no
// per-test cleanup.
const { rows } = await getPool().query(
'SELECT count(*)::int FROM public.invoices WHERE invoice_number = $1 AND company_id = ANY($2::uuid[])',
['F-2026001', [a.companyId, b.companyId]],
)
expect(rows[0]!.count).toBe(2)
})
})