247 lines
6.6 KiB
TypeScript
247 lines
6.6 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { identityApp, type IdentityApp } from './helpers/identity-app';
|
|
|
|
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();
|
|
});
|
|
|
|
async function order(issue = true) {
|
|
const code = randomUUID().slice(0, 8);
|
|
const supplier = await ctx
|
|
.api()
|
|
.post('/api/v1/suppliers')
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
name: `Supplier ${code}`,
|
|
code: `S-${code}`,
|
|
contactName: 'Asha Shah',
|
|
email: `${code}@example.com`,
|
|
phone: '+919876543210',
|
|
})
|
|
.expect(201);
|
|
const material = await ctx
|
|
.api()
|
|
.post('/api/v1/materials')
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
name: `Wax ${code}`,
|
|
code: `M-${code}`,
|
|
kind: 'WAX',
|
|
unit: 'KILOGRAM',
|
|
})
|
|
.expect(201);
|
|
await ctx
|
|
.api()
|
|
.put(
|
|
`/api/v1/suppliers/${supplier.body.id}/materials/${material.body.id}`,
|
|
)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
supplierSku: `SKU-${code}`,
|
|
leadTimeDays: 7,
|
|
minOrderQuantity: '1.000',
|
|
unitPrice: '120.00',
|
|
currency: 'INR',
|
|
})
|
|
.expect(200);
|
|
const purchase = await ctx
|
|
.api()
|
|
.post('/api/v1/purchase-orders')
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
supplierId: supplier.body.id,
|
|
lines: [{ materialId: material.body.id, quantity: '3.000' }],
|
|
})
|
|
.expect(201);
|
|
if (issue)
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/purchase-orders/${purchase.body.id}/issue`)
|
|
.auth(ctx.token, auth)
|
|
.expect(201);
|
|
return {
|
|
id: purchase.body.id as string,
|
|
lineId: purchase.body.lines[0].id as string,
|
|
};
|
|
}
|
|
|
|
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);
|
|
});
|
|
});
|