217 lines
6.5 KiB
TypeScript
217 lines
6.5 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { identityApp, type IdentityApp } from './helpers/identity-app';
|
|
import {
|
|
seedPartnerQuote,
|
|
seedPartnerOrder,
|
|
} from './helpers/b2b-partner-order';
|
|
import { secondActor } from './helpers/commerce';
|
|
|
|
describe('B2B partner reporting', () => {
|
|
let ctx: IdentityApp;
|
|
let source: Awaited<ReturnType<typeof seedPartnerQuote>>;
|
|
const auth = { type: 'bearer' as const };
|
|
const reportUrl = () => `/api/v1/b2b/accounts/${source.accountId}/report`;
|
|
const report = () =>
|
|
ctx.api().get(reportUrl()).auth(ctx.token, auth).expect(200);
|
|
beforeAll(async () => {
|
|
ctx = await identityApp(true);
|
|
source = await seedPartnerQuote(ctx);
|
|
}, 60000);
|
|
afterAll(async () => {
|
|
await ctx?.close();
|
|
});
|
|
beforeEach(async () => {
|
|
await ctx.clearLimits();
|
|
});
|
|
|
|
async function order(
|
|
priceListId = source.priceListId,
|
|
variantId = source.variantId,
|
|
) {
|
|
const quote = await ctx
|
|
.api()
|
|
.post('/api/v1/b2b/quotes')
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
accountId: source.accountId,
|
|
priceListId,
|
|
expiresAt: new Date(Date.now() + 86400000).toISOString(),
|
|
customerReference: 'Report fixture',
|
|
lines: [{ variantId, quantity: 12 }],
|
|
idempotencyKey: randomUUID(),
|
|
})
|
|
.expect(201);
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/b2b/quotes/${quote.body.id}/response`)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
kind: 'ACCEPTED',
|
|
note: 'Partner accepted',
|
|
idempotencyKey: randomUUID(),
|
|
})
|
|
.expect(201);
|
|
return (
|
|
await ctx
|
|
.api()
|
|
.post('/api/v1/b2b/partner-orders')
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
quoteId: quote.body.id,
|
|
partnerReference: randomUUID(),
|
|
idempotencyKey: randomUUID(),
|
|
})
|
|
.expect(201)
|
|
).body.id as string;
|
|
}
|
|
async function pay(orderId: string, amount: string) {
|
|
return (
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/b2b/partner-orders/${orderId}/payments`)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
amount,
|
|
reference: randomUUID(),
|
|
method: 'BANK_TRANSFER',
|
|
receivedOn: '2026-10-04',
|
|
idempotencyKey: randomUUID(),
|
|
})
|
|
.expect(201)
|
|
).body.id as string;
|
|
}
|
|
|
|
it('reports zero orders as an empty currency list and supports Swagger', async () => {
|
|
expect((await report()).body).toEqual({
|
|
accountId: source.accountId,
|
|
currencies: [],
|
|
});
|
|
const docs = await ctx.api().get('/api/docs-json').expect(200);
|
|
expect(docs.body.paths['/api/v1/b2b/accounts/{id}/report']).toBeDefined();
|
|
});
|
|
|
|
it('aggregates each order once, ignores reversed receipts and excludes cancelled merchandise', async () => {
|
|
const partial = await order();
|
|
await pay(partial, '100.25');
|
|
await pay(partial, '200.25');
|
|
const paid = await order();
|
|
await pay(paid, '3303.00');
|
|
const cancelled = await order();
|
|
const paymentId = await pay(cancelled, '500.00');
|
|
await ctx
|
|
.api()
|
|
.post(
|
|
`/api/v1/b2b/partner-orders/${cancelled}/payments/${paymentId}/reverse`,
|
|
)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
reason: 'Incorrect receipt',
|
|
idempotencyKey: randomUUID(),
|
|
})
|
|
.expect(201);
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/b2b/partner-orders/${cancelled}/cancel`)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
reason: 'Partner withdrew',
|
|
idempotencyKey: randomUUID(),
|
|
})
|
|
.expect(201);
|
|
await order();
|
|
expect((await report()).body.currencies).toEqual([
|
|
{
|
|
currency: 'INR',
|
|
orderCount: 4,
|
|
activeOrderCount: 3,
|
|
cancelledOrderCount: 1,
|
|
unpaidOrderCount: 1,
|
|
partiallyPaidOrderCount: 1,
|
|
paidOrderCount: 1,
|
|
merchandiseTotal: '9909.00',
|
|
paidTotal: '3603.50',
|
|
outstandingTotal: '6305.50',
|
|
},
|
|
]);
|
|
const before = await ctx.db.auditEvent.count();
|
|
await report();
|
|
expect(await ctx.db.auditEvent.count()).toBe(before);
|
|
});
|
|
|
|
it('keeps currencies separate and excludes other business accounts', async () => {
|
|
const original = await ctx.db.productVariant.findUniqueOrThrow({
|
|
where: { id: source.variantId },
|
|
});
|
|
const variant = await ctx
|
|
.api()
|
|
.post(`/api/v1/products/${original.productId}/variants`)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
sku: randomUUID(),
|
|
name: 'USD candle',
|
|
price: '400.00',
|
|
currency: 'USD',
|
|
active: true,
|
|
attributes: {},
|
|
})
|
|
.expect(201);
|
|
const list = await ctx
|
|
.api()
|
|
.post(`/api/v1/b2b/accounts/${source.accountId}/price-lists`)
|
|
.auth(ctx.token, auth)
|
|
.send({ name: 'USD wholesale', currency: 'USD' })
|
|
.expect(201);
|
|
await ctx
|
|
.api()
|
|
.put(`/api/v1/b2b/price-lists/${list.body.id}/items/${variant.body.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 usdOrder = await order(list.body.id, variant.body.id);
|
|
await pay(usdOrder, '0.01');
|
|
const other = await seedPartnerOrder(ctx);
|
|
await pay(other.order.id, '100.00');
|
|
const result = (await report()).body.currencies;
|
|
expect(result.map((row: { currency: string }) => row.currency)).toEqual([
|
|
'INR',
|
|
'USD',
|
|
]);
|
|
expect(result[0]).toMatchObject({ orderCount: 4, paidTotal: '3603.50' });
|
|
expect(result[1]).toMatchObject({
|
|
currency: 'USD',
|
|
orderCount: 1,
|
|
merchandiseTotal: '3303.00',
|
|
paidTotal: '0.01',
|
|
outstandingTotal: '3302.99',
|
|
});
|
|
});
|
|
|
|
it('requires dedicated report permission and scopes account lookup', async () => {
|
|
await ctx.api().get(reportUrl()).expect(401);
|
|
const denied = await secondActor(ctx, true, [
|
|
'b2b.order.read',
|
|
'b2b.payment.read',
|
|
]);
|
|
await ctx.api().get(reportUrl()).auth(denied.token, auth).expect(403);
|
|
const reader = await secondActor(ctx, true, ['b2b.report.read']);
|
|
await ctx.api().get(reportUrl()).auth(reader.token, auth).expect(200);
|
|
const outsider = await secondActor(ctx, false, ['b2b.report.read']);
|
|
await ctx.api().get(reportUrl()).auth(outsider.token, auth).expect(404);
|
|
await ctx
|
|
.api()
|
|
.get(`/api/v1/b2b/accounts/${randomUUID()}/report`)
|
|
.auth(ctx.token, auth)
|
|
.expect(404);
|
|
await ctx
|
|
.api()
|
|
.get('/api/v1/b2b/accounts/invalid/report')
|
|
.auth(ctx.token, auth)
|
|
.expect(400);
|
|
});
|
|
});
|