Files
accounted/tests/pg/sales-orders.pg.test.ts
T
Mattsson c0818bb2d2 feat(sales-orders): kundorder with partial delivery and partial invoicing (#2166)
* feat(sales-orders): kundorder with partial delivery and partial invoicing

Adds sales orders (kundorder) as their own non-ledger document between
agreement and invoice, for companies that deliver or invoice in parts.

Schema (20260902130000): sales_orders + sales_order_items with RLS via
user_company_ids(), OR-<n> numbering RPC (membership-gated, no anon
execute), company_settings.sales_orders_enabled UI gate, and back-links
invoices.sales_order_id / invoice_items.sales_order_item_id. The invoiced
quantity per order line is DERIVED from the linked invoice lines on
non-cancelled, non-credited invoices and enforced by a BEFORE trigger, so
no counter can drift and a credited invoice frees its quantity. Header
status is draft / confirmed / completed / cancelled; completion is kept
by DB triggers from the same derived quantity. Delivery and invoicing
progress are derived per line, never stored as status.

Service + API: lib/sales-orders (create/update with id-preserving line
replace, transitions with compare-and-set, cumulative delivery
registration, invoice-from-order through buildInvoiceWriteData so
booking stays in the engine, proforma -> order conversion), routes under
/api/sales-orders and /api/invoices/[id]/convert-to-order, structured
SALES_ORDER_* error codes, archive classification of the new tables.
The invoice editor round-trips sales_order_item_id so a draft edit
cannot drop the link; GET /api/invoices gains ?sales_order_id=.

UI: /sales-orders list, create/edit form reusing the invoice line
conventions, detail with deliver and create-invoice dialogs and linked
invoices; nav row behind the settings toggle; the webshop row is
relabelled webshop_orders; "Skapa order" on proformas.

MCP (20260902141000/141001): list/get reads plus four staged writes
(create, transition, register delivery, create invoice from order) whose
executors call the lib services; op types added to the pending
operations CHECK.

Tests: route tests for every route (401/400/404/happy), service unit
tests, executor and tool tests, and tests/pg/sales-orders.pg.test.ts
(16 cases, green on staging) covering RLS, numbering guards, the
over-invoice trigger incl. release on cancel/credit and cross-company
refusal, the quantity floor, and completion maintenance.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RQW7mXvbAPgjUHq7dSEamr

* fix(sales-orders): harden kundorder after skeptic and security review

Resolves every finding from the PR #2166 review pass in one batch.

Order link integrity: replaceInvoiceItems now refuses a line set that
drops an existing sales_order_item_id (INVOICE_UPDATE_DROPS_ORDER_LINK),
closing the MCP update_invoice header-only edit and the v1 PATCH path
that severed the link and freed the quantity for double invoicing. The
update_invoice re-fetch, gnubok_get_invoice and the v1 item projection
now carry sales_order_item_id so well-behaved clients round-trip it.

Quantity math: derived remaining/invoiced quantities are rounded to six
decimals and compared with an epsilon (roundQty, qtyGreater) so a float
remainder such as 0.5999999999999996 can neither refuse the final partial
invoice nor land as an invoice quantity; duplicate explicit picks are
summed before validation.

Leveransdatum: per-line last_delivery_date (migration 20260902160000);
an invoice takes the latest date over the lines it covers and only when
the covered quantity was delivered, never the header date and never for
an advance invoice (ML 17 kap 24 p.7, FX anchor per ML 8 kap 21-23).

VAT drift: the order stores the customer type and VAT-validation flag its
lines were priced under; invoicing refuses with
SALES_ORDER_CUSTOMER_VAT_CHANGED when they differ, and re-saving the
order re-validates the lines. Customer and currency are frozen once
invoices exist.

Tenant and role gates: composite FK (sales_order_id, company_id) ties a
line to its parent's company (Superagent P2); aa_enforce_company_writer_role
on both tables so a viewer cannot write through the browser client.

Proforma -> order refuses proformas with ROT/RUT, periodisering or
negative-quantity lines instead of dropping those fields. RESTRICT FK
errors on delete map to SALES_ORDER_LINE_LOCKED / SALES_ORDER_HAS_INVOICES.

Also: schema-guard literal payloads in lib/sales-orders (ceiling +2 with
reason), regenerated skills/accounted-api (sales_order_item_id on invoice
items), pg tests for the composite FK, the viewer gate and the new
columns, unit tests for every changed path.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XzFmmH82hCJNmZbPqycDiW

* fix(sales-orders): resolve CodeRabbit round on PR #2166

Quick wins from the review, all in one pass:

- replaceInvoiceItems fails closed when the invoice_items snapshot cannot
  be read (it is both the restore source and the input to the kundorder
  link guard); the guard branch is explicit in both PATCH routes.
- Cumulative delivery registration carries an optimistic predicate on the
  quantity it read, so two concurrent registrations cannot regress each
  other; DELETE of an order keeps its allowed status in the predicate and
  answers a conflict when zero rows match.
- Business dates (order date, delivery date, invoice date) default to the
  Europe/Stockholm calendar day (todayIsoStockholm), never UTC: the
  delivery date is also the Riksbanken rate anchor.
- The invoice-from-order executor treats an event emit failure as
  non-blocking: the draft already exists.
- sales_order_items are archived through their parent with the order
  currency denormalised, like invoice_items.
- Proforma "Skapa order" tolerates a 2xx without a parsable body; the
  settings toggle refreshes the server-rendered nav.
- List route doc states that q matches the order number (customer names
  are matched client-side).

Declined (out of scope for this PR): moving header + line writes and the
delivery loop into transactional RPCs (same PostgREST pattern as the
invoice PATCH path, tracked as a follow-up), the MCP approval handler's
error message shape (pre-existing code outside this change), and the
docstring-coverage warning (no repo convention).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XzFmmH82hCJNmZbPqycDiW

* fix(sales-orders): move hardening migration off a colliding version; archive contract; ceiling

- 20260902160000_sales_orders_hardening.sql collided with main's
  20260902160000_parties_substrate.sql after the third sync; renamed to
  20260902180000 and made idempotent (DROP ... IF EXISTS before each
  ADD CONSTRAINT) so a preview branch that applied it under the old
  version replays it cleanly. Staging's schema_migrations row renamed.
- sales_order_items goes back to a direct archive dump: the coverage
  contract (tests/pg/full-archive-coverage.pg.test.ts) requires it for a
  table with its own company_id; the currency lives on the parent order
  one file over, joined by sales_order_id.
- Scanner ceiling re-baselined after merging main (parties phase 1): 397.
- v1 PATCH test queues a real empty invoice_items snapshot now that
  replaceInvoiceItems fails closed on an unreadable one.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XzFmmH82hCJNmZbPqycDiW

* fix(sales-orders): drop the composite FK before its unique index on replay

The idempotent guard in 20260902180000_sales_orders_hardening.sql dropped
the unique (id, company_id) before the FK that depends on its index, so
the preview branch replay (which had applied the file under its former
version) failed with SQLSTATE 2BP01. Order swapped; replay verified on
staging.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XzFmmH82hCJNmZbPqycDiW

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-02 18:14:49 +02:00

467 lines
21 KiB
TypeScript

import { randomUUID } from 'node:crypto'
import { describe, it, expect } from 'vitest'
import { getClient, getPool, withUserContext } from './setup'
import { seedCompany, insertAuthUser, insertCompanyMember } from './fixtures'
// pg-real coverage for the kundorder migration (20260902130000): the
// generate_sales_order_number RPC (atomic, idempotent, membership-gated),
// RLS isolation on both tables, the derived invoiced-quantity RPC, the
// over-invoice BEFORE trigger on invoice_items (incl. release on
// cancel/credit and the cross-company refusal), the quantity floor on
// order lines, the delivered-within-ordered CHECK, and the completion
// triggers that flip confirmed <-> completed from the derived quantity.
async function seedSettings(companyId: string, userId: string): Promise<void> {
await getPool().query(
`INSERT INTO public.company_settings (user_id, company_id) VALUES ($1, $2)
ON CONFLICT (company_id) DO NOTHING`,
[userId, companyId],
)
}
async function insertCustomer(companyId: string, userId: string): Promise<string> {
const id = randomUUID()
await getPool().query(
`INSERT INTO public.customers (id, user_id, company_id, name, customer_type)
VALUES ($1, $2, $3, 'Testbrand AB', 'swedish_business')`,
[id, userId, companyId],
)
return id
}
async function insertOrder(
companyId: string,
userId: string,
customerId: string,
status: 'draft' | 'confirmed' | 'completed' | 'cancelled' = 'confirmed',
): Promise<string> {
const id = randomUUID()
await getPool().query(
`INSERT INTO public.sales_orders (id, company_id, user_id, customer_id, status, order_date)
VALUES ($1, $2, $3, $4, $5, '2026-09-01')`,
[id, companyId, userId, customerId, status],
)
return id
}
async function insertOrderItem(
companyId: string,
orderId: string,
quantity: number,
overrides: { lineType?: 'product' | 'text'; sortOrder?: number } = {},
): Promise<string> {
const id = randomUUID()
await getPool().query(
`INSERT INTO public.sales_order_items
(id, company_id, sales_order_id, sort_order, line_type, description, quantity, unit, unit_price, vat_rate, line_total)
VALUES ($1, $2, $3, $4, $5, 'Konsulttimmar', $6, 'tim', 1000, 25, $7)`,
[id, companyId, orderId, overrides.sortOrder ?? 0, overrides.lineType ?? 'product', quantity, quantity * 1000],
)
return id
}
async function insertInvoice(
companyId: string,
userId: string,
customerId: string,
overrides: { status?: string; salesOrderId?: string | null; creditedInvoiceId?: string | null } = {},
): Promise<string> {
const id = randomUUID()
await getPool().query(
`INSERT INTO public.invoices
(id, user_id, company_id, customer_id, invoice_number, document_type,
invoice_date, due_date, currency, subtotal, vat_amount, total,
vat_treatment, vat_rate, moms_ruta, status, sales_order_id, credited_invoice_id)
VALUES ($1, $2, $3, $4, NULL, 'invoice',
'2026-09-01', '2026-10-01', 'SEK', 1000, 250, 1250,
'standard_25', 25, '10', $5, $6, $7)`,
[id, userId, companyId, customerId, overrides.status ?? 'draft', overrides.salesOrderId ?? null, overrides.creditedInvoiceId ?? null],
)
return id
}
async function insertInvoiceItem(
invoiceId: string,
quantity: number,
salesOrderItemId: string | null,
): Promise<string> {
const id = randomUUID()
await getPool().query(
`INSERT INTO public.invoice_items
(id, invoice_id, sort_order, description, quantity, unit, unit_price, line_total, vat_rate, vat_amount, sales_order_item_id)
VALUES ($1, $2, 0, 'Konsulttimmar', $3, 'tim', 1000, $4, 25, $5, $6)`,
[id, invoiceId, quantity, quantity * 1000, quantity * 250, salesOrderItemId],
)
return id
}
async function orderStatus(orderId: string): Promise<{ status: string; completed_at: string | null }> {
const { rows } = await getPool().query<{ status: string; completed_at: string | null }>(
`SELECT status, completed_at FROM public.sales_orders WHERE id = $1`,
[orderId],
)
return rows[0]!
}
async function invoicedQty(orderId: string, itemId: string): Promise<number> {
const { rows } = await getPool().query<{ invoiced_qty: string }>(
`SELECT invoiced_qty FROM public.sales_order_invoiced_quantities(ARRAY[$1::uuid])
WHERE sales_order_item_id = $2`,
[orderId, itemId],
)
return Number(rows[0]?.invoiced_qty ?? 0)
}
async function seedOrderWithLine(quantity = 10) {
const { userId, companyId } = await seedCompany()
await seedSettings(companyId, userId)
const customerId = await insertCustomer(companyId, userId)
const orderId = await insertOrder(companyId, userId, customerId, 'confirmed')
const itemId = await insertOrderItem(companyId, orderId, quantity)
return { userId, companyId, customerId, orderId, itemId }
}
describe('generate_sales_order_number RPC', () => {
it('assigns OR-<n> sequentially, is idempotent, and advances the counter once per order', async () => {
const { userId, companyId } = await seedCompany()
await seedSettings(companyId, userId)
const customerId = await insertCustomer(companyId, userId)
const o1 = await insertOrder(companyId, userId, customerId, 'draft')
const o2 = await insertOrder(companyId, userId, customerId, 'draft')
const first = await getPool().query<{ n: string }>(
`SELECT public.generate_sales_order_number($1, $2) AS n`,
[companyId, o1],
)
expect(first.rows[0]!.n).toBe('OR-1')
const again = await getPool().query<{ n: string }>(
`SELECT public.generate_sales_order_number($1, $2) AS n`,
[companyId, o1],
)
expect(again.rows[0]!.n).toBe('OR-1')
const second = await getPool().query<{ n: string }>(
`SELECT public.generate_sales_order_number($1, $2) AS n`,
[companyId, o2],
)
expect(second.rows[0]!.n).toBe('OR-2')
const settings = await getPool().query<{ next_sales_order_number: number }>(
`SELECT next_sales_order_number FROM public.company_settings WHERE company_id = $1`,
[companyId],
)
expect(settings.rows[0]!.next_sales_order_number).toBe(3)
})
it('refuses a signed-in caller who is not a member of the company', async () => {
const { userId, companyId } = await seedCompany()
await seedSettings(companyId, userId)
const customerId = await insertCustomer(companyId, userId)
const orderId = await insertOrder(companyId, userId, customerId, 'draft')
const outsider = await seedCompany()
await expect(
withUserContext(outsider.userId, (client) =>
client.query('SELECT public.generate_sales_order_number($1, $2)', [companyId, orderId]),
),
).rejects.toThrow(/unauthorized/i)
const { rows } = await getPool().query<{ order_number: string | null }>(
`SELECT order_number FROM public.sales_orders WHERE id = $1`,
[orderId],
)
expect(rows[0]!.order_number).toBeNull()
})
it('refuses an anon-shaped caller (role claim, no sub)', async () => {
const { userId, companyId } = await seedCompany()
await seedSettings(companyId, userId)
const customerId = await insertCustomer(companyId, userId)
const orderId = await insertOrder(companyId, userId, customerId, 'draft')
const client = await getClient()
try {
await client.query('BEGIN')
await client.query(`SELECT set_config('request.jwt.claims', '{"role":"anon"}', true)`)
await client.query(`SELECT set_config('request.jwt.claim.role', 'anon', true)`)
await expect(
client.query('SELECT public.generate_sales_order_number($1, $2)', [companyId, orderId]),
).rejects.toThrow(/unauthorized/i)
} finally {
await client.query('ROLLBACK').catch(() => {})
client.release()
}
})
})
describe('sales_orders RLS', () => {
it('isolates orders and lines by company', async () => {
const { userId, companyId, orderId, itemId } = await seedOrderWithLine()
const stranger = await insertAuthUser()
const own = await withUserContext(userId, async (client) => {
const orders = await client.query(`SELECT id FROM public.sales_orders WHERE id = $1`, [orderId])
const items = await client.query(`SELECT id FROM public.sales_order_items WHERE id = $1`, [itemId])
return { orders: orders.rows.length, items: items.rows.length }
})
expect(own).toEqual({ orders: 1, items: 1 })
const other = await withUserContext(stranger, async (client) => {
const orders = await client.query(`SELECT id FROM public.sales_orders WHERE id = $1`, [orderId])
const items = await client.query(`SELECT id FROM public.sales_order_items WHERE id = $1`, [itemId])
return { orders: orders.rows.length, items: items.rows.length }
})
expect(other).toEqual({ orders: 0, items: 0 })
// A member can read the RPC; a stranger sees nothing through it.
const strangerRpc = await withUserContext(stranger, (client) =>
client.query(`SELECT * FROM public.sales_order_invoiced_quantities(ARRAY[$1::uuid])`, [orderId]),
)
expect(strangerRpc.rows).toHaveLength(0)
expect(companyId).toBeTruthy()
})
})
describe('over-invoicing guard on invoice_items', () => {
it('sums linked lines and refuses a line that would exceed the ordered quantity', async () => {
const { userId, companyId, customerId, orderId, itemId } = await seedOrderWithLine(10)
const invA = await insertInvoice(companyId, userId, customerId, { salesOrderId: orderId })
await insertInvoiceItem(invA, 6, itemId)
expect(await invoicedQty(orderId, itemId)).toBe(6)
const invB = await insertInvoice(companyId, userId, customerId, { salesOrderId: orderId })
await expect(insertInvoiceItem(invB, 5, itemId)).rejects.toThrow(/SALES_ORDER_OVER_INVOICED/)
await insertInvoiceItem(invB, 4, itemId)
expect(await invoicedQty(orderId, itemId)).toBe(10)
})
it('releases the quantity when the invoice is cancelled or credited', async () => {
const { userId, companyId, customerId, orderId, itemId } = await seedOrderWithLine(10)
const invA = await insertInvoice(companyId, userId, customerId, { salesOrderId: orderId })
await insertInvoiceItem(invA, 10, itemId)
const invB = await insertInvoice(companyId, userId, customerId, { salesOrderId: orderId })
await expect(insertInvoiceItem(invB, 1, itemId)).rejects.toThrow(/SALES_ORDER_OVER_INVOICED/)
await getPool().query(`UPDATE public.invoices SET status = 'cancelled' WHERE id = $1`, [invA])
expect(await invoicedQty(orderId, itemId)).toBe(0)
await insertInvoiceItem(invB, 10, itemId)
expect(await invoicedQty(orderId, itemId)).toBe(10)
// A credited invoice is by definition numbered (invoices_sent_requires_number).
await getPool().query(
`UPDATE public.invoices SET invoice_number = $2, status = 'credited' WHERE id = $1`,
[invB, `F-${invB.slice(0, 8)}`],
)
expect(await invoicedQty(orderId, itemId)).toBe(0)
})
it('refuses linking an invoice line to another company\'s order line', async () => {
const victim = await seedOrderWithLine(10)
const attacker = await seedCompany()
await seedSettings(attacker.companyId, attacker.userId)
const attackerCustomer = await insertCustomer(attacker.companyId, attacker.userId)
const inv = await insertInvoice(attacker.companyId, attacker.userId, attackerCustomer)
await expect(insertInvoiceItem(inv, 1, victim.itemId)).rejects.toThrow(/SALES_ORDER_ITEM_COMPANY_MISMATCH/)
})
it('refuses a dangling order line reference', async () => {
const { userId, companyId, customerId } = await seedOrderWithLine(1)
const inv = await insertInvoice(companyId, userId, customerId)
await expect(insertInvoiceItem(inv, 1, randomUUID())).rejects.toThrow()
})
})
describe('order line guards', () => {
it('refuses lowering quantity below the invoiced quantity', async () => {
const { userId, companyId, customerId, orderId, itemId } = await seedOrderWithLine(10)
const inv = await insertInvoice(companyId, userId, customerId, { salesOrderId: orderId })
await insertInvoiceItem(inv, 6, itemId)
await expect(
getPool().query(`UPDATE public.sales_order_items SET quantity = 5 WHERE id = $1`, [itemId]),
).rejects.toThrow(/SALES_ORDER_QUANTITY_BELOW_INVOICED/)
await getPool().query(`UPDATE public.sales_order_items SET quantity = 6 WHERE id = $1`, [itemId])
await getPool().query(`UPDATE public.sales_order_items SET quantity = 12 WHERE id = $1`, [itemId])
})
it('refuses delivered_qty above quantity', async () => {
const { itemId } = await seedOrderWithLine(3)
await expect(
getPool().query(`UPDATE public.sales_order_items SET delivered_qty = 4 WHERE id = $1`, [itemId]),
).rejects.toThrow(/sales_order_items_delivered_within_ordered/)
await getPool().query(`UPDATE public.sales_order_items SET delivered_qty = 3 WHERE id = $1`, [itemId])
})
it('refuses deleting an order line with linked invoice lines', async () => {
const { userId, companyId, customerId, orderId, itemId } = await seedOrderWithLine(2)
const inv = await insertInvoice(companyId, userId, customerId, { salesOrderId: orderId })
await insertInvoiceItem(inv, 1, itemId)
await expect(
getPool().query(`DELETE FROM public.sales_order_items WHERE id = $1`, [itemId]),
).rejects.toThrow(/foreign key|violates/i)
await expect(
getPool().query(`DELETE FROM public.sales_orders WHERE id = $1`, [orderId]),
).rejects.toThrow(/foreign key|violates/i)
})
})
describe('completion maintenance', () => {
it('flips confirmed -> completed when every product line is fully invoiced, and back on cancel', async () => {
const { userId, companyId, customerId, orderId, itemId } = await seedOrderWithLine(10)
await insertOrderItem(companyId, orderId, 0, { lineType: 'text', sortOrder: 1 })
const second = await insertOrderItem(companyId, orderId, 2, { sortOrder: 2 })
const inv = await insertInvoice(companyId, userId, customerId, { salesOrderId: orderId })
await insertInvoiceItem(inv, 10, itemId)
expect((await orderStatus(orderId)).status).toBe('confirmed')
await insertInvoiceItem(inv, 2, second)
const done = await orderStatus(orderId)
expect(done.status).toBe('completed')
expect(done.completed_at).not.toBeNull()
await getPool().query(`UPDATE public.invoices SET status = 'cancelled' WHERE id = $1`, [inv])
const reopened = await orderStatus(orderId)
expect(reopened.status).toBe('confirmed')
expect(reopened.completed_at).toBeNull()
})
it('reopens when a draft invoice created from the order is deleted', async () => {
const { userId, companyId, customerId, orderId, itemId } = await seedOrderWithLine(4)
const inv = await insertInvoice(companyId, userId, customerId, { salesOrderId: orderId })
await insertInvoiceItem(inv, 4, itemId)
expect((await orderStatus(orderId)).status).toBe('completed')
await getPool().query(`DELETE FROM public.invoice_items WHERE invoice_id = $1`, [inv])
await getPool().query(`DELETE FROM public.invoices WHERE id = $1`, [inv])
expect((await orderStatus(orderId)).status).toBe('confirmed')
})
it('never touches draft or cancelled orders', async () => {
const { userId, companyId } = await seedCompany()
await seedSettings(companyId, userId)
const customerId = await insertCustomer(companyId, userId)
const draft = await insertOrder(companyId, userId, customerId, 'draft')
const draftItem = await insertOrderItem(companyId, draft, 1)
const inv = await insertInvoice(companyId, userId, customerId, { salesOrderId: draft })
await insertInvoiceItem(inv, 1, draftItem)
expect((await orderStatus(draft)).status).toBe('draft')
})
it('completes on confirm-time refresh when lines are already covered, and gates the RPC on membership', async () => {
const { userId, companyId } = await seedCompany()
await seedSettings(companyId, userId)
const customerId = await insertCustomer(companyId, userId)
const draft = await insertOrder(companyId, userId, customerId, 'draft')
const item = await insertOrderItem(companyId, draft, 1)
const inv = await insertInvoice(companyId, userId, customerId, { salesOrderId: draft })
await insertInvoiceItem(inv, 1, item)
await getPool().query(`UPDATE public.sales_orders SET status = 'confirmed' WHERE id = $1`, [draft])
expect((await orderStatus(draft)).status).toBe('confirmed')
const outsider = await seedCompany()
await expect(
withUserContext(outsider.userId, (client) =>
client.query('SELECT public.refresh_sales_order_completion($1)', [draft]),
),
).rejects.toThrow(/unauthorized/i)
// withUserContext rolls back at the end, so read the effect inside it.
const memberView = await withUserContext(userId, async (client) => {
await client.query('SELECT public.refresh_sales_order_completion($1)', [draft])
const res = await client.query<{ status: string }>(
`SELECT status FROM public.sales_orders WHERE id = $1`,
[draft],
)
return res.rows[0]!.status
})
expect(memberView).toBe('completed')
})
})
describe('sales_order_invoiced_quantities RPC', () => {
it('returns one row per line with cancelled and credit-note lines excluded', async () => {
const { userId, companyId, customerId, orderId, itemId } = await seedOrderWithLine(10)
const second = await insertOrderItem(companyId, orderId, 5, { sortOrder: 1 })
const invA = await insertInvoice(companyId, userId, customerId, { salesOrderId: orderId })
await insertInvoiceItem(invA, 3, itemId)
await insertInvoiceItem(invA, 5, second)
const invCancelled = await insertInvoice(companyId, userId, customerId, { salesOrderId: orderId, status: 'cancelled' })
await insertInvoiceItem(invCancelled, 7, itemId)
const creditNote = await insertInvoice(companyId, userId, customerId, { creditedInvoiceId: invA })
await insertInvoiceItem(creditNote, -3, itemId)
const { rows } = await getPool().query<{ sales_order_item_id: string; invoiced_qty: string }>(
`SELECT sales_order_item_id, invoiced_qty FROM public.sales_order_invoiced_quantities(ARRAY[$1::uuid]) ORDER BY invoiced_qty`,
[orderId],
)
const byItem = new Map(rows.map((r) => [r.sales_order_item_id, Number(r.invoiced_qty)]))
expect(byItem.get(itemId)).toBe(3)
expect(byItem.get(second)).toBe(5)
})
})
describe('hardening (20260902180000)', () => {
it('refuses an order line whose company differs from its parent order (composite FK)', async () => {
const victim = await seedOrderWithLine(1)
const attacker = await seedCompany()
await expect(
getPool().query(
`INSERT INTO public.sales_order_items (company_id, sales_order_id, description, quantity)
VALUES ($1, $2, 'Smuggled line', 1)`,
[attacker.companyId, victim.orderId],
),
).rejects.toThrow(/sales_order_items_order_company_fkey|foreign key/i)
})
it('blocks a viewer from writing orders and lines through the session client, and lets a member through', async () => {
const { userId, companyId, customerId } = await seedOrderWithLine(1)
const viewer = await insertAuthUser()
await insertCompanyMember({ companyId, userId: viewer, role: 'viewer' })
const member = await insertAuthUser()
await insertCompanyMember({ companyId, userId: member, role: 'member' })
await expect(
withUserContext(viewer, (client) =>
client.query(
`INSERT INTO public.sales_orders (company_id, user_id, customer_id) VALUES ($1, $2, $3)`,
[companyId, viewer, customerId],
),
),
).rejects.toThrow(/no write access|row-level security/i)
const inserted = await withUserContext(member, async (client) => {
const res = await client.query<{ id: string }>(
`INSERT INTO public.sales_orders (company_id, user_id, customer_id) VALUES ($1, $2, $3) RETURNING id`,
[companyId, member, customerId],
)
return res.rows[0]!.id
})
expect(inserted).toBeTruthy()
expect(userId).toBeTruthy()
})
it('carries the per-line delivery date and the customer VAT snapshot columns', async () => {
const { orderId, itemId } = await seedOrderWithLine(2)
await getPool().query(
`UPDATE public.sales_order_items SET delivered_qty = 1, last_delivery_date = '2026-09-01' WHERE id = $1`,
[itemId],
)
await getPool().query(
`UPDATE public.sales_orders SET customer_type_snapshot = 'eu_business', customer_vat_validated_snapshot = true WHERE id = $1`,
[orderId],
)
const { rows } = await getPool().query<{ last_delivery_date: string; customer_type_snapshot: string }>(
`SELECT soi.last_delivery_date::text, so.customer_type_snapshot
FROM public.sales_order_items soi JOIN public.sales_orders so ON so.id = soi.sales_order_id
WHERE soi.id = $1`,
[itemId],
)
expect(rows[0]!.last_delivery_date).toBe('2026-09-01')
expect(rows[0]!.customer_type_snapshot).toBe('eu_business')
await expect(
getPool().query(`UPDATE public.sales_orders SET customer_type_snapshot = 'company' WHERE id = $1`, [orderId]),
).rejects.toThrow(/check/i)
})
})