import { randomUUID } from 'node:crypto'; import { identityApp, type IdentityApp } from './helpers/identity-app'; import { secondActor } from './helpers/commerce'; import { receiveAccepted, seedPurchaseOrder } from './helpers/procurement'; describe('warehouse material stock posting', () => { let ctx: IdentityApp; const auth = { type: 'bearer' as const }; const warehouse = async () => ( await ctx .api() .post('/api/v1/inventory/warehouses') .auth(ctx.token, auth) .send({ name: `Materials ${randomUUID().slice(0, 8)}` }) .expect(201) ).body.id as string; const post = (receiptId: string, body: object) => ctx .api() .post(`/api/v1/goods-receipts/${receiptId}/stock-posting`) .auth(ctx.token, auth) .send(body); const listing = (warehouseId: string) => ctx .api() .get(`/api/v1/inventory/material-stock-items?warehouseId=${warehouseId}`) .auth(ctx.token, auth); beforeAll(async () => { ctx = await identityApp(); }, 60000); afterAll(async () => { await ctx?.close(); }); beforeEach(async () => { await ctx.clearLimits(); }); it('posts accepted QC into the chosen warehouse and keeps a scoped ledger', async () => { const order = await seedPurchaseOrder(ctx); const firstReceipt = await receiveAccepted(ctx, order, '1.000'); const secondReceipt = await receiveAccepted(ctx, order, '1.000'); const firstWarehouse = await warehouse(); const secondWarehouse = await warehouse(); const body = { warehouseId: firstWarehouse, idempotencyKey: randomUUID() }; const first = await post(firstReceipt.id, body).expect(201); expect(first.body.entries).toHaveLength(1); expect(first.body.entries[0].delta).toBe('1.000'); const retry = await post(firstReceipt.id, body).expect(201); expect(retry.body.id).toBe(first.body.id); await post(secondReceipt.id, { warehouseId: secondWarehouse, idempotencyKey: randomUUID(), }).expect(201); const stock1 = await listing(firstWarehouse).expect(200); const stock2 = await listing(secondWarehouse).expect(200); expect(stock1.body).toHaveLength(1); expect(stock2.body).toHaveLength(1); expect(stock1.body[0].onHand).toBe('1.000'); expect(stock2.body[0].onHand).toBe('1.000'); const ledger = await ctx .api() .get(`/api/v1/inventory/material-stock-items/${stock1.body[0].id}/ledger`) .auth(ctx.token, auth) .expect(200); expect(ledger.body[0]).toMatchObject({ kind: 'RECEIPT_ACCEPTED', delta: '1.000', }); }); it('requires completed QC and posts only accepted material', async () => { const order = await seedPurchaseOrder(ctx); const receipt = await ctx .api() .post(`/api/v1/purchase-orders/${order.id}/receipts`) .auth(ctx.token, auth) .send({ lines: [{ purchaseOrderLineId: order.lineId, quantity: '1.000' }], }) .expect(201); const warehouseId = await warehouse(); const body = { warehouseId, idempotencyKey: randomUUID() }; const pending = await post(receipt.body.id, body).expect(409); expect(pending.body.code).toBe('MATERIAL_RECEIPT_QC_INCOMPLETE'); await ctx .api() .post(`/api/v1/goods-receipts/${receipt.body.id}/inspect`) .auth(ctx.token, auth) .send({ lines: [ { receiptLineId: receipt.body.lines[0].id, acceptedQuantity: '0.750', rejectedQuantity: '0.250', rejectionReason: 'Damaged', }, ], }) .expect(201); await post(receipt.body.id, body).expect(201); const stock = await listing(warehouseId).expect(200); expect(stock.body[0].onHand).toBe('0.750'); }); it('rejects changed retries, duplicate receipt posting and capacity overflow', async () => { const order = await seedPurchaseOrder(ctx); const receipt = await receiveAccepted(ctx, order, '1.000'); const warehouseId = await warehouse(); const body = { warehouseId, idempotencyKey: randomUUID() }; await post(receipt.id, body).expect(201); const changed = await post(receipt.id, { ...body, warehouseId: await warehouse(), }).expect(409); expect(changed.body.code).toBe('MATERIAL_RECEIPT_REPLAY_CONFLICT'); const duplicate = await post(receipt.id, { warehouseId, idempotencyKey: randomUUID(), }).expect(409); expect(duplicate.body.code).toBe('MATERIAL_RECEIPT_ALREADY_POSTED'); const next = await receiveAccepted(ctx, order, '1.000'); const stock = (await listing(warehouseId).expect(200)).body[0]; await ctx.db.materialStockItem.update({ where: { id: stock.id }, data: { onHand: '999999999.500' }, }); const capacity = await post(next.id, { warehouseId, idempotencyKey: randomUUID(), }).expect(409); expect(capacity.body.code).toBe('MATERIAL_STOCK_CAPACITY'); expect( await ctx.db.materialReceiptPosting.count({ where: { receiptId: next.id }, }), ).toBe(0); }); it('enforces posting permission and hides another organization', async () => { const order = await seedPurchaseOrder(ctx); const receipt = await receiveAccepted(ctx, order, '1.000'); const warehouseId = await warehouse(); const reader = await secondActor(ctx, true, ['inventory.read']); await ctx .api() .post(`/api/v1/goods-receipts/${receipt.id}/stock-posting`) .auth(reader.token, auth) .send({ warehouseId, idempotencyKey: randomUUID() }) .expect(403); await post(receipt.id, { warehouseId, idempotencyKey: randomUUID(), }).expect(201); const outsider = await secondActor(ctx, false, [ 'inventory.read', 'inventory.manage', ]); await ctx .api() .get(`/api/v1/goods-receipts/${receipt.id}/stock-posting`) .auth(outsider.token, auth) .expect(404); await ctx .api() .get(`/api/v1/inventory/material-stock-items?warehouseId=${warehouseId}`) .auth(outsider.token, auth) .expect(200, []); }); it('keeps stock ledger entries immutable in PostgreSQL', async () => { const order = await seedPurchaseOrder(ctx); const receipt = await receiveAccepted(ctx, order, '1.000'); const posting = await post(receipt.id, { warehouseId: await warehouse(), idempotencyKey: randomUUID(), }).expect(201); await expect( ctx.db.materialStockLedger.update({ where: { id: posting.body.entries[0].id }, data: { delta: '2.000' }, }), ).rejects.toThrow(); }); });