165 lines
5.5 KiB
TypeScript
165 lines
5.5 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { identityApp, type IdentityApp } from './helpers/identity-app';
|
|
import { secondActor } from './helpers/commerce';
|
|
import {
|
|
prepareCostBatch,
|
|
startCostBatch,
|
|
recordCostUsage,
|
|
completeCostBatch,
|
|
packageCostBatch,
|
|
setCostRate,
|
|
} from './helpers/batch-cost';
|
|
|
|
describe('indicative batch material contribution', () => {
|
|
let ctx: IdentityApp;
|
|
beforeAll(async () => {
|
|
ctx = await identityApp();
|
|
});
|
|
afterAll(async () => {
|
|
await ctx.close();
|
|
});
|
|
beforeEach(async () => {
|
|
await ctx.clearLimits();
|
|
});
|
|
const report = (
|
|
id: string,
|
|
unitNetPrice = '5.00',
|
|
currency = 'INR',
|
|
token = ctx.token,
|
|
) =>
|
|
ctx
|
|
.api()
|
|
.get('/api/v1/profitability/batches/' + id)
|
|
.auth(token, { type: 'bearer' })
|
|
.query({ unitNetPrice, currency });
|
|
async function ready(good = 20) {
|
|
const batch = await prepareCostBatch(ctx);
|
|
await startCostBatch(ctx, batch.batchId);
|
|
await recordCostUsage(
|
|
ctx,
|
|
batch.batchId,
|
|
batch.productionLineId,
|
|
'CONSUMED',
|
|
'200.000',
|
|
);
|
|
await recordCostUsage(
|
|
ctx,
|
|
batch.batchId,
|
|
batch.productionLineId,
|
|
'WASTED',
|
|
'10.000',
|
|
);
|
|
await completeCostBatch(ctx, batch.batchId, good);
|
|
if (good)
|
|
await packageCostBatch(
|
|
ctx,
|
|
batch.batchId,
|
|
batch.packagingBomId,
|
|
batch.packagingLineId,
|
|
);
|
|
await setCostRate(ctx, batch.productionMaterialId, '0.1000');
|
|
await setCostRate(ctx, batch.packagingMaterialId, '2.0000');
|
|
const snapshot = await ctx
|
|
.api()
|
|
.post('/api/v1/production-batches/' + batch.batchId + '/cost-snapshot')
|
|
.auth(ctx.token, { type: 'bearer' })
|
|
.send({ currency: 'INR', idempotencyKey: randomUUID() })
|
|
.expect(201);
|
|
return { ...batch, snapshot: snapshot.body };
|
|
}
|
|
it('compares an explicit price with sealed costs and preserves frozen rates', async () => {
|
|
const f = await ready();
|
|
const result = await report(f.batchId).expect(200);
|
|
expect(result.body).toMatchObject({
|
|
batchId: f.batchId,
|
|
variantId: f.variantId,
|
|
costSnapshotId: f.snapshot.id,
|
|
basis: 'SEALED_BATCH_STANDARD_MATERIAL_COST',
|
|
currency: 'INR',
|
|
goodQuantity: 20,
|
|
unitNetPrice: '5.00',
|
|
productionCost: '21.00',
|
|
packagingCost: '40.00',
|
|
standardMaterialCost: '61.00',
|
|
comparisonNetValue: '100.00',
|
|
materialContribution: '39.00',
|
|
contributionRateBps: '3900',
|
|
breakEvenUnitNetPrice: '3.05',
|
|
comparisonState: 'ABOVE_MATERIAL_BREAK_EVEN',
|
|
});
|
|
expect(JSON.stringify(result.body)).not.toMatch(
|
|
/createdById|idempotencyKey|requestHash|revenue|netProfit/,
|
|
);
|
|
await setCostRate(ctx, f.productionMaterialId, '9.0000');
|
|
await ctx.db.productVariant.update({
|
|
where: { id: f.variantId },
|
|
data: { price: '999.00' },
|
|
});
|
|
const after = await report(f.batchId).expect(200);
|
|
expect(after.body.standardMaterialCost).toBe('61.00');
|
|
expect(after.body.materialContribution).toBe('39.00');
|
|
expect((await report(f.batchId, '1.00').expect(200)).body).toMatchObject({
|
|
comparisonNetValue: '20.00',
|
|
materialContribution: '-41.00',
|
|
contributionRateBps: '-20500',
|
|
comparisonState: 'BELOW_MATERIAL_BREAK_EVEN',
|
|
});
|
|
expect((await report(f.batchId, '3.05').expect(200)).body).toMatchObject({
|
|
materialContribution: '0.00',
|
|
contributionRateBps: '0',
|
|
comparisonState: 'AT_MATERIAL_BREAK_EVEN',
|
|
});
|
|
expect((await report(f.batchId, '0.00').expect(200)).body).toMatchObject({
|
|
materialContribution: '-61.00',
|
|
contributionRateBps: null,
|
|
});
|
|
});
|
|
it('reports zero good output without dividing by zero', async () => {
|
|
const f = await ready(0);
|
|
const result = await report(f.batchId).expect(200);
|
|
expect(result.body).toMatchObject({
|
|
goodQuantity: 0,
|
|
comparisonNetValue: '0.00',
|
|
standardMaterialCost: '21.00',
|
|
materialContribution: '-21.00',
|
|
breakEvenUnitNetPrice: null,
|
|
contributionRateBps: null,
|
|
comparisonState: 'NO_GOOD_OUTPUT',
|
|
});
|
|
});
|
|
it('requires sealed evidence, matching currency and scoped profitability permission', async () => {
|
|
const f = await prepareCostBatch(ctx);
|
|
await report(f.batchId).expect(404);
|
|
await report(randomUUID()).expect(404);
|
|
const readyBatch = await ready();
|
|
expect(
|
|
(await report(readyBatch.batchId, '5.00', 'USD').expect(409)).body.code,
|
|
).toBe('PROFITABILITY_CURRENCY');
|
|
const reader = await secondActor(ctx, true, ['profitability.read']);
|
|
await report(readyBatch.batchId, '5.00', 'INR', reader.token).expect(200);
|
|
const costReader = await secondActor(ctx, true, ['cost.read']);
|
|
await report(readyBatch.batchId, '5.00', 'INR', costReader.token).expect(
|
|
403,
|
|
);
|
|
const foreign = await secondActor(ctx, false, ['profitability.read']);
|
|
await report(readyBatch.batchId, '5.00', 'INR', foreign.token).expect(404);
|
|
await ctx
|
|
.api()
|
|
.get('/api/v1/profitability/batches/' + readyBatch.batchId)
|
|
.query({ currency: 'INR', unitNetPrice: '5.00' })
|
|
.expect(401);
|
|
for (const price of ['-1.00', '1.001', '1', '10000000000.00', 'NaN'])
|
|
await report(readyBatch.batchId, price).expect(400);
|
|
await ctx
|
|
.api()
|
|
.get('/api/v1/profitability/batches/' + readyBatch.batchId)
|
|
.auth(ctx.token, { type: 'bearer' })
|
|
.query({
|
|
currency: 'INR',
|
|
unitNetPrice: '5.00',
|
|
organizationId: foreign.organizationId,
|
|
})
|
|
.expect(400);
|
|
});
|
|
});
|