manicanldes-backend/test/packaging-boms.spec.ts

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('packaging BOM API', () => {
let ctx: IdentityApp;
const auth = { type: 'bearer' as const };
const material = async (
kind: 'PACKAGING' | 'LABEL' | 'WAX',
unit = 'PIECE',
) => {
const code = `${kind[0]}-${randomUUID().slice(0, 8)}`;
const row = await ctx
.api()
.post('/api/v1/materials')
.auth(ctx.token, auth)
.send({ name: `${kind} item`, code, kind, unit })
.expect(201);
return { id: row.body.id as string, code };
};
const create = (variantId: string, materialId: string) =>
ctx
.api()
.post(`/api/v1/variants/${variantId}/packaging-boms`)
.auth(ctx.token, auth)
.send({
outputQuantity: 10,
lines: [{ materialId, quantity: '10.000' }],
});
beforeAll(async () => {
ctx = await identityApp();
}, 60000);
afterAll(async () => {
await ctx?.close();
});
beforeEach(async () => {
await ctx.clearLimits();
});
it('keeps packaging and production revisions and activations independent', async () => {
const { variant } = await seedProduct(ctx);
const box = await material('PACKAGING');
const wax = await material('WAX', 'GRAM');
const production = await ctx
.api()
.post(`/api/v1/variants/${variant.id}/boms`)
.auth(ctx.token, auth)
.send({
outputQuantity: 10,
lines: [{ materialId: wax.id, quantity: '125.000' }],
})
.expect(201);
const packaging = await create(variant.id, box.id).expect(201);
expect(production.body).toMatchObject({ kind: 'PRODUCTION', revision: 1 });
expect(packaging.body).toMatchObject({ kind: 'PACKAGING', revision: 1 });
await ctx
.api()
.post(`/api/v1/boms/${production.body.id}/activate`)
.auth(ctx.token, auth)
.expect(201);
await ctx
.api()
.post(`/api/v1/packaging-boms/${packaging.body.id}/activate`)
.auth(ctx.token, auth)
.expect(201);
const next = await create(variant.id, box.id).expect(201);
expect(next.body.revision).toBe(2);
await ctx
.api()
.post(`/api/v1/packaging-boms/${next.body.id}/activate`)
.auth(ctx.token, auth)
.expect(201);
const activePackaging = await ctx
.api()
.get(`/api/v1/variants/${variant.id}/packaging-boms/active`)
.auth(ctx.token, auth)
.expect(200);
const activeProduction = await ctx
.api()
.get(`/api/v1/variants/${variant.id}/boms/active`)
.auth(ctx.token, auth)
.expect(200);
expect(activePackaging.body.id).toBe(next.body.id);
expect(activeProduction.body.id).toBe(production.body.id);
await ctx
.api()
.get(`/api/v1/boms/${packaging.body.id}`)
.auth(ctx.token, auth)
.expect(404);
const former = await ctx
.api()
.get(`/api/v1/packaging-boms/${packaging.body.id}`)
.auth(ctx.token, auth)
.expect(200);
expect(former.body.status).toBe('SUPERSEDED');
const batch = await ctx
.api()
.post('/api/v1/production-batches')
.auth(ctx.token, auth)
.send({
bomId: next.body.id,
plannedQuantity: 10,
reference: `B-${randomUUID().slice(0, 8)}`,
})
.expect(409);
expect(batch.body.code).toBe('BATCH_BOM_KIND');
});
it('requires packaging or label materials and rechecks at activation', async () => {
const { variant } = await seedProduct(ctx);
const wax = await material('WAX', 'GRAM');
const wrong = await create(variant.id, wax.id).expect(409);
expect(wrong.body.code).toBe('PACKAGING_MATERIAL_KIND');
const label = await material('LABEL');
const draft = await create(variant.id, label.id).expect(201);
await ctx
.api()
.put(`/api/v1/materials/${label.id}`)
.auth(ctx.token, auth)
.send({
name: 'Changed label',
code: label.code,
kind: 'WAX',
unit: 'PIECE',
})
.expect(200);
const changed = await ctx
.api()
.post(`/api/v1/packaging-boms/${draft.body.id}/activate`)
.auth(ctx.token, auth)
.expect(409);
expect(changed.body.code).toBe('PACKAGING_MATERIAL_KIND');
});
it('uses separate permissions and hides other organizations', async () => {
const { variant } = await seedProduct(ctx);
const box = await material('PACKAGING');
const packaging = await create(variant.id, box.id).expect(201);
const reader = await secondActor(ctx, true, ['packaging.read']);
await ctx
.api()
.get(`/api/v1/packaging-boms/${packaging.body.id}`)
.auth(reader.token, auth)
.expect(200);
await ctx
.api()
.get(`/api/v1/boms/${packaging.body.id}`)
.auth(reader.token, auth)
.expect(403);
await ctx
.api()
.post(`/api/v1/packaging-boms/${packaging.body.id}/activate`)
.auth(reader.token, auth)
.expect(403);
const outsider = await secondActor(ctx, false, [
'packaging.read',
'packaging.manage',
]);
const hidden = await ctx
.api()
.get(`/api/v1/packaging-boms/${packaging.body.id}`)
.auth(outsider.token, auth)
.expect(404);
expect(hidden.body.code).toBe('BOM_NOT_FOUND');
});
it('keeps BOM kind immutable in PostgreSQL', async () => {
const { variant } = await seedProduct(ctx);
const box = await material('PACKAGING');
const packaging = await create(variant.id, box.id).expect(201);
await expect(
ctx.db.productBom.update({
where: { id: packaging.body.id },
data: { kind: 'PRODUCTION', status: 'ACTIVE', activatedAt: new Date() },
}),
).rejects.toThrow();
});
});