233 lines
6.2 KiB
TypeScript
233 lines
6.2 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { identityApp, type IdentityApp } from './helpers/identity-app';
|
|
import { secondActor } from './helpers/commerce';
|
|
import { seedPartnerQuote } from './helpers/b2b-partner-order';
|
|
|
|
describe('B2B partner purchase orders', () => {
|
|
let ctx: IdentityApp;
|
|
const auth = { type: 'bearer' as const };
|
|
const url = '/api/v1/b2b/partner-orders';
|
|
|
|
const quote = (accepted = true) => seedPartnerQuote(ctx, accepted);
|
|
const input = (quoteId: string) => ({
|
|
quoteId,
|
|
partnerReference: `PO-${randomUUID()}`,
|
|
note: 'Requested by purchasing team',
|
|
idempotencyKey: randomUUID(),
|
|
});
|
|
|
|
beforeAll(async () => {
|
|
ctx = await identityApp();
|
|
}, 60000);
|
|
afterAll(async () => {
|
|
await ctx?.close();
|
|
});
|
|
beforeEach(async () => {
|
|
await ctx.clearLimits();
|
|
});
|
|
|
|
it('requires accepted quote and freezes its account, quantities, and prices', async () => {
|
|
const pending = await quote(false);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send(input(pending.quoteId))
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('B2B_ORDER_QUOTE_NOT_ACCEPTED');
|
|
const source = await quote();
|
|
const request = input(source.quoteId);
|
|
const created = await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send(request)
|
|
.expect(201);
|
|
expect(created.body).toMatchObject({
|
|
status: 'RECEIVED',
|
|
accountId: source.accountId,
|
|
quoteId: source.quoteId,
|
|
merchandiseTotal: '3303.00',
|
|
partnerReference: request.partnerReference,
|
|
accountSnapshot: { legalName: 'Order Partner Limited' },
|
|
lines: [
|
|
{
|
|
variantId: source.variantId,
|
|
quantity: 12,
|
|
minQuantity: 12,
|
|
unitPrice: '275.25',
|
|
lineTotal: '3303.00',
|
|
},
|
|
],
|
|
});
|
|
expect(created.body.number).toMatch(/^BO-[A-F0-9]{20}$/);
|
|
const replay = await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send(request)
|
|
.expect(201);
|
|
expect(replay.body.id).toBe(created.body.id);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send({ ...request, partnerReference: 'CHANGED' })
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('B2B_ORDER_REPLAY_CONFLICT');
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send(input(source.quoteId))
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('B2B_ORDER_QUOTE_USED');
|
|
const listed = await ctx
|
|
.api()
|
|
.get(`${url}?accountId=${source.accountId}`)
|
|
.auth(ctx.token, auth)
|
|
.expect(200);
|
|
expect(listed.body.map((order: { id: string }) => order.id)).toContain(
|
|
created.body.id,
|
|
);
|
|
});
|
|
|
|
it('prevents duplicate partner references and records one immutable cancellation', async () => {
|
|
const source = await quote();
|
|
const request = input(source.quoteId);
|
|
const created = await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send(request)
|
|
.expect(201);
|
|
const secondQuote = await ctx
|
|
.api()
|
|
.post('/api/v1/b2b/quotes')
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
accountId: source.accountId,
|
|
priceListId: source.priceListId,
|
|
expiresAt: new Date(Date.now() + 7 * 86400000).toISOString(),
|
|
customerReference: 'Second inquiry',
|
|
lines: [{ variantId: source.variantId, quantity: 12 }],
|
|
idempotencyKey: randomUUID(),
|
|
})
|
|
.expect(201);
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/b2b/quotes/${secondQuote.body.id}/response`)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
kind: 'ACCEPTED',
|
|
note: 'Second quote accepted',
|
|
idempotencyKey: randomUUID(),
|
|
})
|
|
.expect(201);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
...input(secondQuote.body.id),
|
|
partnerReference: request.partnerReference,
|
|
})
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('B2B_ORDER_REFERENCE_USED');
|
|
const cancel = {
|
|
reason: 'Partner withdrew purchase order',
|
|
idempotencyKey: randomUUID(),
|
|
};
|
|
const path = `${url}/${created.body.id}/cancel`;
|
|
const cancelled = await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send(cancel)
|
|
.expect(201);
|
|
expect(cancelled.body).toMatchObject({
|
|
status: 'CANCELLED',
|
|
cancellation: {
|
|
reason: cancel.reason,
|
|
actorId: ctx.owner.userId,
|
|
},
|
|
});
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send(cancel)
|
|
.expect(201)
|
|
).body.status,
|
|
).toBe('CANCELLED');
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send({ ...cancel, reason: 'Different reason' })
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('B2B_ORDER_CANCEL_REPLAY_CONFLICT');
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(path)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
reason: 'Another cancellation',
|
|
idempotencyKey: randomUUID(),
|
|
})
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('B2B_ORDER_ALREADY_CANCELLED');
|
|
});
|
|
|
|
it('scopes reads and commands to organization and permissions', async () => {
|
|
const source = await quote();
|
|
const created = await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send(input(source.quoteId))
|
|
.expect(201);
|
|
const outsider = await secondActor(ctx, false, [
|
|
'b2b.order.read',
|
|
'b2b.order.manage',
|
|
]);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.get(`${url}/${created.body.id}`)
|
|
.auth(outsider.token, auth)
|
|
.expect(404)
|
|
).body.code,
|
|
).toBe('B2B_ORDER_NOT_FOUND');
|
|
const noPermission = await secondActor(ctx);
|
|
await ctx.api().get(url).auth(noPermission.token, auth).expect(403);
|
|
await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(noPermission.token, auth)
|
|
.send(input(source.quoteId))
|
|
.expect(403);
|
|
});
|
|
});
|