manicanldes-backend/test/b2b-price-lists.spec.ts

238 lines
7.2 KiB
TypeScript

import { randomUUID } from 'node:crypto';
import { identityApp, type IdentityApp } from './helpers/identity-app';
import { secondActor, seedProduct } from './helpers/commerce';
describe('B2B partner price lists and minimum quantities', () => {
let ctx: IdentityApp;
const auth = { type: 'bearer' as const };
const listRoute = (id: string) => `/api/v1/b2b/price-lists/${id}`;
const profile = () => ({
code: `PL-${randomUUID().slice(0, 8)}`,
legalName: 'Wholesale Candles Private Limited',
tradingName: null,
registrationNumber: randomUUID(),
taxIdentifier: null,
countryCode: 'IN',
contactEmail: 'wholesale@example.com',
contactPhone: null,
billingLine1: '12 Trade Road',
billingCity: 'Pune',
billingRegion: 'Maharashtra',
billingPostalCode: '411001',
});
async function account() {
const created = await ctx
.api()
.post('/api/v1/b2b/accounts')
.auth(ctx.token, auth)
.send(profile())
.expect(201);
return created.body.id as string;
}
async function verify(accountId: string) {
await ctx
.api()
.post(`/api/v1/b2b/accounts/${accountId}/submit`)
.auth(ctx.token, auth)
.send({ idempotencyKey: randomUUID() })
.expect(201);
const reviewer = await secondActor(ctx, true, ['b2b.verify']);
await ctx
.api()
.post(`/api/v1/b2b/accounts/${accountId}/decision`)
.auth(reviewer.token, auth)
.send({
decision: 'APPROVED',
note: 'Registry details reviewed',
idempotencyKey: randomUUID(),
})
.expect(201);
}
async function createList(accountId: string, currency = 'INR') {
const created = await ctx
.api()
.post(`/api/v1/b2b/accounts/${accountId}/price-lists`)
.auth(ctx.token, auth)
.send({ name: 'Wholesale standard', currency })
.expect(201);
return created.body.id as string;
}
const putItem = (
listId: string,
variantId: string,
unitPrice = '350.00',
minQuantity = 10,
) =>
ctx
.api()
.put(`${listRoute(listId)}/items/${variantId}`)
.auth(ctx.token, auth)
.send({ unitPrice, minQuantity });
beforeAll(async () => {
ctx = await identityApp();
}, 60000);
afterAll(async () => {
await ctx?.close();
});
beforeEach(async () => {
await ctx.clearLimits();
});
it('gates activation on verification and items, then enforces MOQ and exact prices', async () => {
const accountId = await account();
const listId = await createList(accountId);
expect(
(
await ctx
.api()
.post(`${listRoute(listId)}/activate`)
.auth(ctx.token, auth)
.expect(409)
).body.code,
).toBe('B2B_PRICE_ACCOUNT_UNVERIFIED');
await verify(accountId);
expect(
(
await ctx
.api()
.post(`${listRoute(listId)}/activate`)
.auth(ctx.token, auth)
.expect(409)
).body.code,
).toBe('B2B_PRICE_LIST_EMPTY');
const { variant } = await seedProduct(ctx, true);
await putItem(listId, variant.id).expect(200);
const active = await ctx
.api()
.post(`${listRoute(listId)}/activate`)
.auth(ctx.token, auth)
.expect(201);
expect(active.body.status).toBe('ACTIVE');
expect((await putItem(listId, variant.id).expect(409)).body.code).toBe(
'B2B_PRICE_LIST_DRAFT_REQUIRED',
);
expect(
(
await ctx
.api()
.post(`${listRoute(listId)}/price-check`)
.auth(ctx.token, auth)
.send({ lines: [{ variantId: variant.id, quantity: 9 }] })
.expect(409)
).body.code,
).toBe('B2B_PRICE_MINIMUM_QUANTITY');
const check = await ctx
.api()
.post(`${listRoute(listId)}/price-check`)
.auth(ctx.token, auth)
.send({ lines: [{ variantId: variant.id, quantity: 12 }] })
.expect(201);
expect(check.body).toMatchObject({
currency: 'INR',
merchandiseTotal: '4200.00',
lines: [{ unitPrice: '350.00', lineTotal: '4200.00', minQuantity: 10 }],
});
await ctx.db.productVariant.update({
where: { id: variant.id },
data: { active: false },
});
expect(
(
await ctx
.api()
.post(`${listRoute(listId)}/price-check`)
.auth(ctx.token, auth)
.send({ lines: [{ variantId: variant.id, quantity: 12 }] })
.expect(409)
).body.code,
).toBe('B2B_PRICE_VARIANT_INELIGIBLE');
});
it('keeps one active list per account/currency and permits a replacement after archival', async () => {
const accountId = await account();
await verify(accountId);
const { variant } = await seedProduct(ctx, true);
const first = await createList(accountId);
const replacement = await createList(accountId);
await putItem(first, variant.id).expect(200);
await putItem(replacement, variant.id, '330.00', 20).expect(200);
await ctx
.api()
.post(`${listRoute(first)}/activate`)
.auth(ctx.token, auth)
.expect(201);
expect(
(
await ctx
.api()
.post(`${listRoute(replacement)}/activate`)
.auth(ctx.token, auth)
.expect(409)
).body.code,
).toBe('B2B_PRICE_LIST_ALREADY_ACTIVE');
await ctx
.api()
.post(`${listRoute(first)}/archive`)
.auth(ctx.token, auth)
.expect(201);
expect(
(
await ctx
.api()
.delete(`${listRoute(first)}/items/${variant.id}`)
.auth(ctx.token, auth)
.expect(409)
).body.code,
).toBe('B2B_PRICE_LIST_DRAFT_REQUIRED');
expect(
(
await ctx
.api()
.post(`${listRoute(first)}/price-check`)
.auth(ctx.token, auth)
.send({ lines: [{ variantId: variant.id, quantity: 10 }] })
.expect(409)
).body.code,
).toBe('B2B_PRICE_LIST_ACTIVE_REQUIRED');
await ctx
.api()
.post(`${listRoute(replacement)}/activate`)
.auth(ctx.token, auth)
.expect(201);
expect(
(await ctx.api().get(listRoute(first)).auth(ctx.token, auth).expect(200))
.body.items,
).toEqual([
expect.objectContaining({ variantId: variant.id, minQuantity: 10 }),
]);
});
it('rejects ineligible variants, cross-tenant access and missing permissions', async () => {
const accountId = await account();
const listId = await createList(accountId, 'USD');
const { variant } = await seedProduct(ctx, true);
expect((await putItem(listId, variant.id).expect(409)).body.code).toBe(
'B2B_PRICE_CURRENCY_MISMATCH',
);
await putItem(listId, variant.id, '0.00').expect(400);
const draft = await seedProduct(ctx);
expect(
(await putItem(listId, draft.variant.id).expect(409)).body.code,
).toBe('B2B_PRICE_VARIANT_INELIGIBLE');
const other = await secondActor(ctx, false, ['b2b.price.read']);
await ctx.api().get(listRoute(listId)).auth(other.token, auth).expect(404);
const reader = await secondActor(ctx, true, ['b2b.price.read']);
await ctx
.api()
.post(`${listRoute(listId)}/activate`)
.auth(reader.token, auth)
.expect(403);
await ctx
.api()
.get(`/api/v1/b2b/accounts/${accountId}/price-lists`)
.auth(reader.token, auth)
.expect(200);
});
});