180 lines
5.7 KiB
TypeScript
180 lines
5.7 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { identityApp, type IdentityApp } from './helpers/identity-app';
|
|
import { secondActor, seedProduct } from './helpers/commerce';
|
|
|
|
describe('product BOM API', () => {
|
|
let ctx: IdentityApp;
|
|
const auth = { type: 'bearer' as const };
|
|
const materialInput = (code: string, active = true) => ({
|
|
name: 'Soy Wax',
|
|
code,
|
|
kind: 'WAX',
|
|
unit: 'GRAM',
|
|
active,
|
|
});
|
|
const createMaterial = async (active = true) => {
|
|
const code = `W-${randomUUID().slice(0, 8)}`;
|
|
return (
|
|
await ctx
|
|
.api()
|
|
.post('/api/v1/materials')
|
|
.auth(ctx.token, auth)
|
|
.send(materialInput(code, active))
|
|
.expect(201)
|
|
).body as { id: string; code: string };
|
|
};
|
|
const createBom = (variantId: string, materialId: string) =>
|
|
ctx
|
|
.api()
|
|
.post(`/api/v1/variants/${variantId}/boms`)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
outputQuantity: 10,
|
|
lines: [{ materialId, quantity: '125.000' }],
|
|
});
|
|
|
|
beforeAll(async () => {
|
|
ctx = await identityApp();
|
|
}, 60000);
|
|
afterAll(async () => {
|
|
await ctx?.close();
|
|
});
|
|
beforeEach(async () => {
|
|
await ctx.clearLimits();
|
|
});
|
|
|
|
it('creates immutable snapshots and activates a new revision', async () => {
|
|
const { variant } = await seedProduct(ctx);
|
|
const material = await createMaterial();
|
|
const first = await createBom(variant.id, material.id).expect(201);
|
|
expect(first.body).toMatchObject({
|
|
revision: 1,
|
|
status: 'DRAFT',
|
|
outputQuantity: 10,
|
|
});
|
|
expect(first.body.lines[0]).toMatchObject({
|
|
materialCode: material.code,
|
|
unit: 'GRAM',
|
|
quantity: '125',
|
|
});
|
|
await ctx
|
|
.api()
|
|
.put(`/api/v1/materials/${material.id}`)
|
|
.auth(ctx.token, auth)
|
|
.send({ ...materialInput(material.code), name: 'Updated Wax' })
|
|
.expect(200);
|
|
const active = await ctx
|
|
.api()
|
|
.post(`/api/v1/boms/${first.body.id}/activate`)
|
|
.auth(ctx.token, auth)
|
|
.expect(201);
|
|
expect(active.body.status).toBe('ACTIVE');
|
|
const again = await ctx
|
|
.api()
|
|
.post(`/api/v1/boms/${first.body.id}/activate`)
|
|
.auth(ctx.token, auth)
|
|
.expect(409);
|
|
expect(again.body.code).toBe('BOM_NOT_DRAFT');
|
|
const second = await createBom(variant.id, material.id).expect(201);
|
|
expect(second.body.revision).toBe(2);
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/boms/${second.body.id}/activate`)
|
|
.auth(ctx.token, auth)
|
|
.expect(201);
|
|
const former = await ctx
|
|
.api()
|
|
.get(`/api/v1/boms/${first.body.id}`)
|
|
.auth(ctx.token, auth)
|
|
.expect(200);
|
|
expect(former.body.status).toBe('SUPERSEDED');
|
|
expect(former.body.activatedAt).toBe(active.body.activatedAt);
|
|
expect(former.body.lines[0].materialName).toBe('Soy Wax');
|
|
const current = await ctx
|
|
.api()
|
|
.get(`/api/v1/variants/${variant.id}/boms/active`)
|
|
.auth(ctx.token, auth)
|
|
.expect(200);
|
|
expect(current.body.id).toBe(second.body.id);
|
|
const list = await ctx
|
|
.api()
|
|
.get(`/api/v1/variants/${variant.id}/boms?status=SUPERSEDED`)
|
|
.auth(ctx.token, auth)
|
|
.expect(200);
|
|
expect(list.body.map((row: { id: string }) => row.id)).toEqual([
|
|
first.body.id,
|
|
]);
|
|
expect(
|
|
await ctx.db.auditEvent.count({
|
|
where: {
|
|
organizationId: ctx.owner.organizationId,
|
|
action: 'bom.activated',
|
|
},
|
|
}),
|
|
).toBe(2);
|
|
});
|
|
|
|
it('rejects missing, cross-organization and inactive materials', async () => {
|
|
const { variant } = await seedProduct(ctx);
|
|
const missing = await createBom(variant.id, randomUUID()).expect(404);
|
|
expect(missing.body.code).toBe('BOM_MATERIAL_MISSING');
|
|
const material = await createMaterial(false);
|
|
const inactive = await createBom(variant.id, material.id).expect(409);
|
|
expect(inactive.body.code).toBe('BOM_MATERIAL_INACTIVE');
|
|
const outsider = await secondActor(ctx, false, ['bom.read', 'bom.manage']);
|
|
const denied = await ctx
|
|
.api()
|
|
.get(`/api/v1/variants/${variant.id}/boms`)
|
|
.auth(outsider.token, auth)
|
|
.expect(404);
|
|
expect(denied.body.code).toBe('VARIANT_NOT_FOUND');
|
|
});
|
|
|
|
it('enforces BOM permissions and rechecks material state at activation', async () => {
|
|
const { variant } = await seedProduct(ctx);
|
|
const material = await createMaterial();
|
|
const bom = await createBom(variant.id, material.id).expect(201);
|
|
const reader = await secondActor(ctx, true, ['bom.read']);
|
|
await ctx
|
|
.api()
|
|
.get(`/api/v1/boms/${bom.body.id}`)
|
|
.auth(reader.token, auth)
|
|
.expect(200);
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/boms/${bom.body.id}/activate`)
|
|
.auth(reader.token, auth)
|
|
.expect(403);
|
|
await ctx
|
|
.api()
|
|
.put(`/api/v1/materials/${material.id}`)
|
|
.auth(ctx.token, auth)
|
|
.send(materialInput(material.code, false))
|
|
.expect(200);
|
|
const inactive = await ctx
|
|
.api()
|
|
.post(`/api/v1/boms/${bom.body.id}/activate`)
|
|
.auth(ctx.token, auth)
|
|
.expect(409);
|
|
expect(inactive.body.code).toBe('BOM_MATERIAL_INACTIVE');
|
|
});
|
|
|
|
it('does not activate a stale draft after a newer revision', async () => {
|
|
const { variant } = await seedProduct(ctx);
|
|
const material = await createMaterial();
|
|
const oldDraft = await createBom(variant.id, material.id).expect(201);
|
|
const newDraft = await createBom(variant.id, material.id).expect(201);
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/boms/${newDraft.body.id}/activate`)
|
|
.auth(ctx.token, auth)
|
|
.expect(201);
|
|
const stale = await ctx
|
|
.api()
|
|
.post(`/api/v1/boms/${oldDraft.body.id}/activate`)
|
|
.auth(ctx.token, auth)
|
|
.expect(409);
|
|
expect(stale.body.code).toBe('BOM_REVISION_STALE');
|
|
});
|
|
});
|