manicanldes-backend/test/helpers/b2b-partner-order.ts

113 lines
3.2 KiB
TypeScript

import { randomUUID } from 'node:crypto';
import { secondActor, seedProduct } from './commerce';
import type { IdentityApp } from './identity-app';
const auth = { type: 'bearer' as const };
export async function seedPartnerQuote(ctx: IdentityApp, accepted = true) {
const created = await ctx
.api()
.post('/api/v1/b2b/accounts')
.auth(ctx.token, auth)
.send({
code: `PO-${randomUUID().slice(0, 8)}`,
legalName: 'Order Partner Limited',
tradingName: null,
registrationNumber: randomUUID(),
taxIdentifier: null,
countryCode: 'IN',
contactEmail: 'partner-orders@example.com',
contactPhone: null,
billingLine1: '10 Market Lane',
billingCity: 'Pune',
billingRegion: 'Maharashtra',
billingPostalCode: '411001',
})
.expect(201);
const accountId = created.body.id as 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: 'Registration checked',
idempotencyKey: randomUUID(),
})
.expect(201);
const { variant } = await seedProduct(ctx, true);
const list = await ctx
.api()
.post(`/api/v1/b2b/accounts/${accountId}/price-lists`)
.auth(ctx.token, auth)
.send({ name: 'Partner wholesale', currency: 'INR' })
.expect(201);
await ctx
.api()
.put(`/api/v1/b2b/price-lists/${list.body.id}/items/${variant.id}`)
.auth(ctx.token, auth)
.send({ unitPrice: '275.25', minQuantity: 12 })
.expect(200);
await ctx
.api()
.post(`/api/v1/b2b/price-lists/${list.body.id}/activate`)
.auth(ctx.token, auth)
.expect(201);
const issued = await ctx
.api()
.post('/api/v1/b2b/quotes')
.auth(ctx.token, auth)
.send({
accountId,
priceListId: list.body.id,
expiresAt: new Date(Date.now() + 7 * 86400000).toISOString(),
customerReference: 'Customer inquiry',
lines: [{ variantId: variant.id, quantity: 12 }],
idempotencyKey: randomUUID(),
})
.expect(201);
if (accepted)
await ctx
.api()
.post(`/api/v1/b2b/quotes/${issued.body.id}/response`)
.auth(ctx.token, auth)
.send({
kind: 'ACCEPTED',
note: 'Partner approved price',
idempotencyKey: randomUUID(),
})
.expect(201);
return {
accountId,
quoteId: issued.body.id as string,
variantId: variant.id as string,
priceListId: list.body.id as string,
};
}
export async function seedPartnerOrder(ctx: IdentityApp) {
const source = await seedPartnerQuote(ctx);
const created = await ctx
.api()
.post('/api/v1/b2b/partner-orders')
.auth(ctx.token, auth)
.send({
quoteId: source.quoteId,
partnerReference: `PO-${randomUUID()}`,
note: 'Requested by purchasing team',
idempotencyKey: randomUUID(),
})
.expect(201);
return {
...source,
order: created.body as { id: string; merchandiseTotal: string },
};
}