176 lines
5.5 KiB
TypeScript
176 lines
5.5 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { identityApp, type IdentityApp } from './helpers/identity-app';
|
|
import { secondActor } from './helpers/commerce';
|
|
import { createBatch, seedActiveBom } from './helpers/production';
|
|
|
|
describe('production material usage API', () => {
|
|
let ctx: IdentityApp;
|
|
const auth = { type: 'bearer' as const };
|
|
const seedStartedBatch = async () => {
|
|
const bom = await seedActiveBom(ctx);
|
|
const batch = await createBatch(ctx, bom.bomId).expect(201);
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/production-batches/${batch.body.id}/start`)
|
|
.auth(ctx.token, auth)
|
|
.expect(201);
|
|
return { ...bom, batchId: batch.body.id as string };
|
|
};
|
|
const postUsage = (batchId: string, input: Record<string, unknown>) =>
|
|
ctx
|
|
.api()
|
|
.post(`/api/v1/production-batches/${batchId}/material-usage`)
|
|
.auth(ctx.token, auth)
|
|
.send(input);
|
|
|
|
beforeAll(async () => {
|
|
ctx = await identityApp();
|
|
}, 60000);
|
|
afterAll(async () => {
|
|
await ctx?.close();
|
|
});
|
|
beforeEach(async () => {
|
|
await ctx.clearLimits();
|
|
});
|
|
|
|
it('records consumed and wasted material and summarizes plan versus actual', async () => {
|
|
const { batchId, bomLineId } = await seedStartedBatch();
|
|
const consumed = await postUsage(batchId, {
|
|
bomLineId,
|
|
kind: 'CONSUMED',
|
|
quantity: '200.125',
|
|
idempotencyKey: randomUUID(),
|
|
}).expect(201);
|
|
expect(consumed.body).toMatchObject({
|
|
kind: 'CONSUMED',
|
|
bomLineId,
|
|
quantity: '200.125',
|
|
});
|
|
const wasted = await postUsage(batchId, {
|
|
bomLineId,
|
|
kind: 'WASTED',
|
|
quantity: '10.000',
|
|
reason: 'Spill',
|
|
idempotencyKey: randomUUID(),
|
|
}).expect(201);
|
|
expect(wasted.body.reason).toBe('Spill');
|
|
const report = await ctx
|
|
.api()
|
|
.get(
|
|
`/api/v1/production-batches/${batchId}/material-usage?limit=1&offset=1`,
|
|
)
|
|
.auth(ctx.token, auth)
|
|
.expect(200);
|
|
expect(report.body.total).toBe(2);
|
|
expect(report.body.events).toHaveLength(1);
|
|
expect(report.body.materials[0]).toMatchObject({
|
|
plannedQuantity: '250.000',
|
|
consumedQuantity: '200.125',
|
|
wastedQuantity: '10.000',
|
|
varianceQuantity: '39.875',
|
|
});
|
|
});
|
|
|
|
it('returns an idempotent retry but rejects a changed payload', async () => {
|
|
const { batchId, bomLineId } = await seedStartedBatch();
|
|
const input = {
|
|
bomLineId,
|
|
kind: 'CONSUMED',
|
|
quantity: '125.000',
|
|
idempotencyKey: randomUUID(),
|
|
};
|
|
const first = await postUsage(batchId, input).expect(201);
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/production-batches/${batchId}/complete`)
|
|
.auth(ctx.token, auth)
|
|
.send({ goodQuantity: 20, rejectedQuantity: 0 })
|
|
.expect(201);
|
|
const retry = await postUsage(batchId, input).expect(201);
|
|
expect(retry.body.id).toBe(first.body.id);
|
|
const conflict = await postUsage(batchId, {
|
|
...input,
|
|
quantity: '126.000',
|
|
}).expect(409);
|
|
expect(conflict.body.code).toBe('USAGE_REPLAY_CONFLICT');
|
|
expect(
|
|
await ctx.db.productionMaterialUsage.count({ where: { batchId } }),
|
|
).toBe(1);
|
|
});
|
|
|
|
it('rejects wrong state, foreign BOM lines and missing waste reasons', async () => {
|
|
const bom = await seedActiveBom(ctx);
|
|
const batch = await createBatch(ctx, bom.bomId).expect(201);
|
|
const input = {
|
|
bomLineId: bom.bomLineId,
|
|
kind: 'CONSUMED',
|
|
quantity: '1.000',
|
|
idempotencyKey: randomUUID(),
|
|
};
|
|
const draft = await postUsage(batch.body.id, input).expect(409);
|
|
expect(draft.body.code).toBe('USAGE_BATCH_STATE');
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/production-batches/${batch.body.id}/start`)
|
|
.auth(ctx.token, auth)
|
|
.expect(201);
|
|
const wrongLine = await postUsage(batch.body.id, {
|
|
...input,
|
|
bomLineId: randomUUID(),
|
|
}).expect(404);
|
|
expect(wrongLine.body.code).toBe('USAGE_LINE_NOT_FOUND');
|
|
const noReason = await postUsage(batch.body.id, {
|
|
...input,
|
|
kind: 'WASTED',
|
|
}).expect(400);
|
|
expect(noReason.body.code).toBe('USAGE_WASTE_REASON_REQUIRED');
|
|
});
|
|
|
|
it('separates recording permission and organization scope', async () => {
|
|
const { batchId, bomLineId } = await seedStartedBatch();
|
|
const reader = await secondActor(ctx, true, ['production.read']);
|
|
await ctx
|
|
.api()
|
|
.get(`/api/v1/production-batches/${batchId}/material-usage`)
|
|
.auth(reader.token, auth)
|
|
.expect(200);
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/production-batches/${batchId}/material-usage`)
|
|
.auth(reader.token, auth)
|
|
.send({
|
|
bomLineId,
|
|
kind: 'CONSUMED',
|
|
quantity: '1.000',
|
|
idempotencyKey: randomUUID(),
|
|
})
|
|
.expect(403);
|
|
const outsider = await secondActor(ctx, false, [
|
|
'production.read',
|
|
'production.material.record',
|
|
]);
|
|
const hidden = await ctx
|
|
.api()
|
|
.get(`/api/v1/production-batches/${batchId}/material-usage`)
|
|
.auth(outsider.token, auth)
|
|
.expect(404);
|
|
expect(hidden.body.code).toBe('BATCH_NOT_FOUND');
|
|
});
|
|
|
|
it('keeps recorded usage immutable in PostgreSQL', async () => {
|
|
const { batchId, bomLineId } = await seedStartedBatch();
|
|
const event = await postUsage(batchId, {
|
|
bomLineId,
|
|
kind: 'CONSUMED',
|
|
quantity: '1.000',
|
|
idempotencyKey: randomUUID(),
|
|
}).expect(201);
|
|
await expect(
|
|
ctx.db.productionMaterialUsage.update({
|
|
where: { id: event.body.id },
|
|
data: { quantity: '2.000' },
|
|
}),
|
|
).rejects.toThrow();
|
|
});
|
|
});
|