190 lines
5.2 KiB
TypeScript
190 lines
5.2 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { identityApp, type IdentityApp } from './helpers/identity-app';
|
|
import { seedPurchaseOrder } from './helpers/procurement';
|
|
|
|
describe('goods receipt and QC 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 order = (issue = true) => seedPurchaseOrder(ctx, issue);
|
|
|
|
it('allows partial receipts, reconciles QC, and permits replacement of rejected goods', async () => {
|
|
const purchase = await order();
|
|
const path = `/api/v1/purchase-orders/${purchase.id}/receipts`;
|
|
const first = await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
deliveryNote: 'DN-001',
|
|
lines: [{ purchaseOrderLineId: purchase.lineId, quantity: '2.000' }],
|
|
})
|
|
.expect(201);
|
|
const over = await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
lines: [{ purchaseOrderLineId: purchase.lineId, quantity: '2.000' }],
|
|
})
|
|
.expect(409);
|
|
expect(over.body.code).toBe('RECEIPT_QUANTITY_LIMIT');
|
|
const badQc = await ctx
|
|
.api()
|
|
.post(`/api/v1/goods-receipts/${first.body.id}/inspect`)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
lines: [
|
|
{
|
|
receiptLineId: first.body.lines[0].id,
|
|
acceptedQuantity: '1.000',
|
|
rejectedQuantity: '0.000',
|
|
},
|
|
],
|
|
})
|
|
.expect(409);
|
|
expect(badQc.body.code).toBe('INSPECTION_QUANTITY_INVALID');
|
|
const checked = await ctx
|
|
.api()
|
|
.post(`/api/v1/goods-receipts/${first.body.id}/inspect`)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
lines: [
|
|
{
|
|
receiptLineId: first.body.lines[0].id,
|
|
acceptedQuantity: '1.000',
|
|
rejectedQuantity: '1.000',
|
|
rejectionReason: 'Cracked container',
|
|
},
|
|
],
|
|
})
|
|
.expect(201);
|
|
expect(checked.body.lines[0].inspection.acceptedQuantity).toBe('1');
|
|
const duplicate = await ctx
|
|
.api()
|
|
.post(`/api/v1/goods-receipts/${first.body.id}/inspect`)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
lines: [
|
|
{
|
|
receiptLineId: first.body.lines[0].id,
|
|
acceptedQuantity: '2.000',
|
|
rejectedQuantity: '0.000',
|
|
},
|
|
],
|
|
})
|
|
.expect(409);
|
|
expect(duplicate.body.code).toBe('RECEIPT_ALREADY_INSPECTED');
|
|
await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
lines: [{ purchaseOrderLineId: purchase.lineId, quantity: '2.000' }],
|
|
})
|
|
.expect(201);
|
|
const list = await ctx.api().get(path).auth(ctx.token, auth).expect(200);
|
|
expect(list.body).toHaveLength(2);
|
|
});
|
|
|
|
it('enforces scoped receipt lookups', async () => {
|
|
const purchase = await order();
|
|
const receipt = await ctx
|
|
.api()
|
|
.post(`/api/v1/purchase-orders/${purchase.id}/receipts`)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
lines: [{ purchaseOrderLineId: purchase.lineId, quantity: '1.000' }],
|
|
})
|
|
.expect(201);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.get(`/api/v1/goods-receipts/${receipt.body.id}`)
|
|
.auth(ctx.token, auth)
|
|
.expect(200)
|
|
).body.id,
|
|
).toBe(receipt.body.id);
|
|
await ctx
|
|
.api()
|
|
.get(`/api/v1/goods-receipts/${randomUUID()}`)
|
|
.auth(ctx.token, auth)
|
|
.expect(404);
|
|
});
|
|
|
|
it('rejects draft orders, unrelated lines, and rejected goods without a reason', async () => {
|
|
const draft = await order(false);
|
|
const path = `/api/v1/purchase-orders/${draft.id}/receipts`;
|
|
const payload = {
|
|
lines: [{ purchaseOrderLineId: draft.lineId, quantity: '1.000' }],
|
|
};
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send(payload)
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('RECEIPT_ORDER_STATE');
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/purchase-orders/${draft.id}/issue`)
|
|
.auth(ctx.token, auth)
|
|
.expect(201);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
lines: [{ purchaseOrderLineId: randomUUID(), quantity: '1.000' }],
|
|
})
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('RECEIPT_LINE_INVALID');
|
|
const receipt = await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send(payload)
|
|
.expect(201);
|
|
const inspect = `/api/v1/goods-receipts/${receipt.body.id}/inspect`;
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(inspect)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
lines: [
|
|
{
|
|
receiptLineId: receipt.body.lines[0].id,
|
|
acceptedQuantity: '0.000',
|
|
rejectedQuantity: '1.000',
|
|
},
|
|
],
|
|
})
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('INSPECTION_REASON_REQUIRED');
|
|
expect(
|
|
await ctx.db.receiptInspection.count({
|
|
where: { receiptLineId: receipt.body.lines[0].id },
|
|
}),
|
|
).toBe(0);
|
|
});
|
|
});
|