manicanldes-backend/test/b2b-quotations.spec.ts

288 lines
7.9 KiB
TypeScript

import { randomUUID } from 'node:crypto';
import { identityApp, type IdentityApp } from './helpers/identity-app';
import { secondActor, seedProduct } from './helpers/commerce';
describe('B2B quotations', () => {
let ctx: IdentityApp;
const auth = { type: 'bearer' as const };
const quotes = '/api/v1/b2b/quotes';
async function account() {
const created = await ctx
.api()
.post('/api/v1/b2b/accounts')
.auth(ctx.token, auth)
.send({
code: `Q-${randomUUID().slice(0, 8)}`,
legalName: 'Quotation Partner Limited',
tradingName: null,
registrationNumber: randomUUID(),
taxIdentifier: null,
countryCode: 'IN',
contactEmail: 'quotes@example.com',
contactPhone: null,
billingLine1: '10 Market Lane',
billingCity: 'Pune',
billingRegion: 'Maharashtra',
billingPostalCode: '411001',
})
.expect(201);
return created.body.id as string;
}
async function verifiedAccount() {
const id = await account();
await ctx
.api()
.post(`/api/v1/b2b/accounts/${id}/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/${id}/decision`)
.auth(reviewer.token, auth)
.send({
decision: 'APPROVED',
note: 'Registration checked by staff',
idempotencyKey: randomUUID(),
})
.expect(201);
return id;
}
async function activeList(accountId: string) {
const { variant } = await seedProduct(ctx, true);
const created = await ctx
.api()
.post(`/api/v1/b2b/accounts/${accountId}/price-lists`)
.auth(ctx.token, auth)
.send({ name: 'Partner wholesale', currency: 'INR' })
.expect(201);
const listId = created.body.id as string;
await ctx
.api()
.put(`/api/v1/b2b/price-lists/${listId}/items/${variant.id}`)
.auth(ctx.token, auth)
.send({ unitPrice: '275.25', minQuantity: 12 })
.expect(200);
await ctx
.api()
.post(`/api/v1/b2b/price-lists/${listId}/activate`)
.auth(ctx.token, auth)
.expect(201);
return {
listId,
variantId: variant.id as string,
sku: variant.sku as string,
};
}
const issueInput = (
accountId: string,
priceListId: string,
variantId: string,
) => ({
accountId,
priceListId,
expiresAt: new Date(Date.now() + 7 * 86400000).toISOString(),
customerReference: 'Inquiry 123',
lines: [{ variantId, quantity: 12 }],
idempotencyKey: randomUUID(),
});
beforeAll(async () => {
ctx = await identityApp();
}, 60000);
afterAll(async () => {
await ctx?.close();
});
beforeEach(async () => {
await ctx.clearLimits();
});
it('issues an immutable merchandise snapshot and replays after list archival', async () => {
const accountId = await verifiedAccount();
const { listId, variantId, sku } = await activeList(accountId);
const input = issueInput(accountId, listId, variantId);
const issued = await ctx
.api()
.post(quotes)
.auth(ctx.token, auth)
.send(input)
.expect(201);
expect(issued.body).toMatchObject({
status: 'ISSUED',
currency: 'INR',
merchandiseTotal: '3303.00',
accountSnapshot: { legalName: 'Quotation Partner Limited' },
lines: [
{
variantId,
sku,
quantity: 12,
minQuantity: 12,
unitPrice: '275.25',
lineTotal: '3303.00',
},
],
});
expect(issued.body.number).toMatch(/^BQ-[A-F0-9]{20}$/);
await ctx
.api()
.post(`/api/v1/b2b/price-lists/${listId}/archive`)
.auth(ctx.token, auth)
.expect(201);
expect(
(
await ctx
.api()
.post(quotes)
.auth(ctx.token, auth)
.send(input)
.expect(201)
).body.id,
).toBe(issued.body.id);
expect(
(
await ctx
.api()
.get(`${quotes}/${issued.body.id}`)
.auth(ctx.token, auth)
.expect(200)
).body.lines[0].unitPrice,
).toBe('275.25');
expect(
(
await ctx
.api()
.post(quotes)
.auth(ctx.token, auth)
.send({ ...input, customerReference: 'Changed' })
.expect(409)
).body.code,
).toBe('B2B_QUOTE_REPLAY_CONFLICT');
});
it('enforces verified active terms, expiry, MOQ, tenant scope, and permissions', async () => {
const unverified = await account();
const accountId = await verifiedAccount();
const { listId, variantId } = await activeList(accountId);
const input = issueInput(accountId, listId, variantId);
expect(
(
await ctx
.api()
.post(quotes)
.auth(ctx.token, auth)
.send({ ...input, accountId: unverified })
.expect(409)
).body.code,
).toBe('B2B_QUOTE_ACCOUNT_UNVERIFIED');
expect(
(
await ctx
.api()
.post(quotes)
.auth(ctx.token, auth)
.send({ ...input, lines: [{ variantId, quantity: 11 }] })
.expect(409)
).body.code,
).toBe('B2B_PRICE_MINIMUM_QUANTITY');
expect(
(
await ctx
.api()
.post(quotes)
.auth(ctx.token, auth)
.send({
...input,
expiresAt: new Date(Date.now() - 1000).toISOString(),
})
.expect(400)
).body.code,
).toBe('B2B_QUOTE_EXPIRY_INVALID');
const issued = await ctx
.api()
.post(quotes)
.auth(ctx.token, auth)
.send(input)
.expect(201);
const other = await secondActor(ctx, false, ['b2b.quote.read']);
await ctx
.api()
.get(`${quotes}/${issued.body.id}`)
.auth(other.token, auth)
.expect(404);
const reader = await secondActor(ctx, true, ['b2b.quote.read']);
await ctx
.api()
.post(quotes)
.auth(reader.token, auth)
.send(issueInput(accountId, listId, variantId))
.expect(403);
});
it('records one staff-attested response and rejects changed or later responses', async () => {
const accountId = await verifiedAccount();
const { listId, variantId } = await activeList(accountId);
const issued = await ctx
.api()
.post(quotes)
.auth(ctx.token, auth)
.send(issueInput(accountId, listId, variantId))
.expect(201);
const route = `${quotes}/${issued.body.id}/response`;
const body = {
kind: 'ACCEPTED',
note: 'Customer accepted by email',
idempotencyKey: randomUUID(),
};
const accepted = await ctx
.api()
.post(route)
.auth(ctx.token, auth)
.send(body)
.expect(201);
expect(accepted.body).toMatchObject({
status: 'ACCEPTED',
response: { kind: 'ACCEPTED', note: body.note },
});
expect(
(await ctx.api().post(route).auth(ctx.token, auth).send(body).expect(201))
.body.response.id,
).toBe(accepted.body.response.id);
expect(
(
await ctx
.api()
.post(route)
.auth(ctx.token, auth)
.send({ ...body, note: 'Changed note' })
.expect(409)
).body.code,
).toBe('B2B_QUOTE_RESPONSE_REPLAY_CONFLICT');
expect(
(
await ctx
.api()
.post(route)
.auth(ctx.token, auth)
.send({
kind: 'WITHDRAWN',
note: 'Staff withdrawal',
idempotencyKey: randomUUID(),
})
.expect(409)
).body.code,
).toBe('B2B_QUOTE_ALREADY_RESOLVED');
const list = await ctx
.api()
.get(`${quotes}?accountId=${accountId}`)
.auth(ctx.token, auth)
.expect(200);
expect(list.body).toEqual(
expect.arrayContaining([
expect.objectContaining({ id: issued.body.id, status: 'ACCEPTED' }),
]),
);
});
});