209 lines
5.8 KiB
TypeScript
209 lines
5.8 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { identityApp, type IdentityApp } from './helpers/identity-app';
|
|
import { seedPurchaseOrder, receiveAccepted } from './helpers/procurement';
|
|
import { secondActor } from './helpers/commerce';
|
|
|
|
describe('supplier invoice API', () => {
|
|
let ctx: IdentityApp;
|
|
const auth = { type: 'bearer' as const };
|
|
beforeAll(async () => {
|
|
ctx = await identityApp();
|
|
}, 60000);
|
|
afterAll(async () => {
|
|
await ctx?.close();
|
|
});
|
|
beforeEach(async () => {
|
|
await ctx.clearLimits();
|
|
});
|
|
const payload = (
|
|
lineId: string,
|
|
invoiceNumber: string,
|
|
quantity = '1.000',
|
|
) => ({
|
|
invoiceNumber,
|
|
invoiceDate: '2026-09-28',
|
|
taxAmount: '18.00',
|
|
shippingAmount: '5.00',
|
|
lines: [{ purchaseOrderLineId: lineId, quantity }],
|
|
});
|
|
|
|
it('matches accepted goods, computes exact totals, and releases quantity on void', async () => {
|
|
const order = await seedPurchaseOrder(ctx);
|
|
await receiveAccepted(ctx, order, '2.000');
|
|
const path = `/api/v1/purchase-orders/${order.id}/invoices`;
|
|
const first = await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send(payload(order.lineId, 'inv-001'))
|
|
.expect(201);
|
|
expect(first.body).toMatchObject({
|
|
invoiceNumber: 'INV-001',
|
|
currency: 'INR',
|
|
merchandiseTotal: '120',
|
|
taxAmount: '18',
|
|
shippingAmount: '5',
|
|
payableTotal: '143',
|
|
});
|
|
const duplicate = await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send(payload(order.lineId, 'INV-001'))
|
|
.expect(409);
|
|
expect(duplicate.body.code).toBe('INVOICE_NUMBER_CONFLICT');
|
|
const over = await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send(payload(order.lineId, 'INV-002', '2.000'))
|
|
.expect(409);
|
|
expect(over.body.code).toBe('INVOICE_QUANTITY_LIMIT');
|
|
await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send(payload(order.lineId, 'INV-002'))
|
|
.expect(201);
|
|
const voided = await ctx
|
|
.api()
|
|
.post(`/api/v1/supplier-invoices/${first.body.id}/void`)
|
|
.auth(ctx.token, auth)
|
|
.send({ reason: 'Supplier cancelled the document' })
|
|
.expect(201);
|
|
expect(voided.body.void.reason).toBe('Supplier cancelled the document');
|
|
await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send(payload(order.lineId, 'INV-003'))
|
|
.expect(201);
|
|
expect(
|
|
(await ctx.api().get(path).auth(ctx.token, auth).expect(200)).body,
|
|
).toHaveLength(3);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.get(`${path}?limit=1&offset=1`)
|
|
.auth(ctx.token, auth)
|
|
.expect(200)
|
|
).body,
|
|
).toHaveLength(1);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send(payload(order.lineId, 'INV-001'))
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('INVOICE_NUMBER_CONFLICT');
|
|
});
|
|
|
|
it('rejects draft orders, uninspected goods, and unrelated order lines', async () => {
|
|
const order = await seedPurchaseOrder(ctx, false);
|
|
const path = `/api/v1/purchase-orders/${order.id}/invoices`;
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send(payload(order.lineId, 'INV-DRAFT'))
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('INVOICE_ORDER_STATE');
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/purchase-orders/${order.id}/issue`)
|
|
.auth(ctx.token, auth)
|
|
.expect(201);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send(payload(order.lineId, 'INV-NO-QC'))
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('INVOICE_QC_REQUIRED');
|
|
await receiveAccepted(ctx, order, '1.000');
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send(payload(randomUUID(), 'INV-WRONG'))
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('INVOICE_LINE_INVALID');
|
|
});
|
|
|
|
it('enforces read and manage permissions, organization scope, and one-time void', async () => {
|
|
const order = await seedPurchaseOrder(ctx);
|
|
await receiveAccepted(ctx, order, '1.000');
|
|
const path = `/api/v1/purchase-orders/${order.id}/invoices`;
|
|
const invoice = await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send(payload(order.lineId, 'INV-SCOPE'))
|
|
.expect(201);
|
|
const reader = await secondActor(ctx, true, ['procurement.read']);
|
|
await ctx
|
|
.api()
|
|
.get(`/api/v1/supplier-invoices/${invoice.body.id}`)
|
|
.auth(reader.token, auth)
|
|
.expect(200);
|
|
await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(reader.token, auth)
|
|
.send(payload(order.lineId, 'INV-DENIED'))
|
|
.expect(403);
|
|
const foreign = await secondActor(ctx, false, [
|
|
'procurement.read',
|
|
'procurement.manage',
|
|
]);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.get(`/api/v1/supplier-invoices/${invoice.body.id}`)
|
|
.auth(foreign.token, auth)
|
|
.expect(404)
|
|
).body.code,
|
|
).toBe('INVOICE_NOT_FOUND');
|
|
const voidPath = `/api/v1/supplier-invoices/${invoice.body.id}/void`;
|
|
await ctx
|
|
.api()
|
|
.post(voidPath)
|
|
.auth(ctx.token, auth)
|
|
.send({ reason: 'Wrong invoice' })
|
|
.expect(201);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(voidPath)
|
|
.auth(ctx.token, auth)
|
|
.send({ reason: 'Again' })
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('INVOICE_ALREADY_VOID');
|
|
expect(
|
|
await ctx.db.auditEvent.count({
|
|
where: {
|
|
organizationId: ctx.owner.organizationId,
|
|
action: 'supplier_invoice.voided',
|
|
targetId: invoice.body.id,
|
|
},
|
|
}),
|
|
).toBe(1);
|
|
});
|
|
});
|