91 lines
2.3 KiB
TypeScript
91 lines
2.3 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import type { IdentityApp } from './identity-app';
|
|
|
|
const auth = { type: 'bearer' as const };
|
|
export async function seedPurchaseOrder(ctx: IdentityApp, 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,
|
|
supplierId: supplier.body.id as string,
|
|
};
|
|
}
|
|
|
|
export async function receiveAccepted(
|
|
ctx: IdentityApp,
|
|
order: { id: string; lineId: string },
|
|
quantity: string,
|
|
) {
|
|
const receipt = await ctx
|
|
.api()
|
|
.post(`/api/v1/purchase-orders/${order.id}/receipts`)
|
|
.auth(ctx.token, auth)
|
|
.send({ lines: [{ purchaseOrderLineId: order.lineId, quantity }] })
|
|
.expect(201);
|
|
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: quantity,
|
|
rejectedQuantity: '0.000',
|
|
},
|
|
],
|
|
})
|
|
.expect(201);
|
|
return receipt.body;
|
|
}
|