194 lines
5.6 KiB
TypeScript
194 lines
5.6 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { identityApp, type IdentityApp } from './helpers/identity-app';
|
|
import { seedStock } from './helpers/commerce';
|
|
import { campaignDiscountFixture } from './helpers/campaign-discounts';
|
|
|
|
describe('campaign collection discounts at checkout', () => {
|
|
let ctx: IdentityApp;
|
|
const auth = { type: 'bearer' as const };
|
|
beforeAll(async () => {
|
|
ctx = await identityApp(true);
|
|
}, 60000);
|
|
afterAll(async () => {
|
|
await ctx?.close();
|
|
});
|
|
beforeEach(async () => {
|
|
await ctx.clearLimits();
|
|
});
|
|
|
|
it('discounts only eligible lines in a mixed cart and freezes campaign evidence', async () => {
|
|
const f = await campaignDiscountFixture(ctx);
|
|
const outside = await seedStock(ctx);
|
|
await ctx.db.productVariant.update({
|
|
where: { id: outside.variant.id },
|
|
data: { price: '100.00' },
|
|
});
|
|
await ctx
|
|
.api()
|
|
.put(`/api/v1/cart/lines/${outside.variant.id}`)
|
|
.auth(f.actor.token, auth)
|
|
.send({ version: 1, quantity: 1 })
|
|
.expect(200);
|
|
const input = { ...f.input, cartVersion: 2 };
|
|
const created = await ctx
|
|
.api()
|
|
.post('/api/v1/checkout')
|
|
.auth(f.actor.token, auth)
|
|
.send(input)
|
|
.expect(201);
|
|
expect(created.body).toMatchObject({
|
|
subtotal: '1098',
|
|
discount: '99.8',
|
|
merchandiseTotal: '998.2',
|
|
paymentAvailable: false,
|
|
coupon: {
|
|
campaign: {
|
|
id: f.campaignId,
|
|
collectionId: f.groupId,
|
|
eligibleSubtotal: '998.00',
|
|
eligibleVariantIds: [f.variant.id],
|
|
},
|
|
},
|
|
});
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/campaigns/${f.campaignId}/cancellation`)
|
|
.auth(ctx.token, auth)
|
|
.send({ reason: 'End campaign' })
|
|
.expect(201);
|
|
const replay = await ctx
|
|
.api()
|
|
.post('/api/v1/checkout')
|
|
.auth(f.actor.token, auth)
|
|
.send(input)
|
|
.expect(201);
|
|
expect(replay.body).toEqual(created.body);
|
|
expect(
|
|
await ctx.db.order.count({ where: { userId: f.actor.userId } }),
|
|
).toBe(1);
|
|
});
|
|
|
|
it('caps fixed discounts at collection value rather than discounting outside items', async () => {
|
|
const f = await campaignDiscountFixture(ctx, { fixed: '2000.00' });
|
|
const outside = await seedStock(ctx);
|
|
await ctx
|
|
.api()
|
|
.put(`/api/v1/cart/lines/${outside.variant.id}`)
|
|
.auth(f.actor.token, auth)
|
|
.send({ version: 1, quantity: 1 })
|
|
.expect(200);
|
|
const order = await ctx
|
|
.api()
|
|
.post('/api/v1/checkout')
|
|
.auth(f.actor.token, auth)
|
|
.send({ ...f.input, cartVersion: 2 })
|
|
.expect(201);
|
|
expect(order.body).toMatchObject({
|
|
subtotal: '1497',
|
|
discount: '998',
|
|
merchandiseTotal: '499',
|
|
});
|
|
});
|
|
|
|
it('uses collection subtotal for minimum spend and preserves cart/stock on failure', async () => {
|
|
const f = await campaignDiscountFixture(ctx, {
|
|
minimumSubtotal: '1000.00',
|
|
});
|
|
const outside = await seedStock(ctx);
|
|
await ctx
|
|
.api()
|
|
.put(`/api/v1/cart/lines/${outside.variant.id}`)
|
|
.auth(f.actor.token, auth)
|
|
.send({ version: 1, quantity: 1 })
|
|
.expect(200);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post('/api/v1/checkout')
|
|
.auth(f.actor.token, auth)
|
|
.send({ ...f.input, cartVersion: 2 })
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('COUPON_INELIGIBLE');
|
|
expect(
|
|
await ctx.db.order.count({ where: { userId: f.actor.userId } }),
|
|
).toBe(0);
|
|
expect(
|
|
await ctx.db.stockReservation.count({
|
|
where: { userId: f.actor.userId },
|
|
}),
|
|
).toBe(0);
|
|
expect(
|
|
await ctx.db.cartLine.count({
|
|
where: { cart: { userId: f.actor.userId } },
|
|
}),
|
|
).toBe(2);
|
|
});
|
|
|
|
it('blocks drafts, cancelled campaigns, and carts no longer in the collection', async () => {
|
|
for (const mode of ['draft', 'future', 'cancelled', 'removed'] as const) {
|
|
const f = await campaignDiscountFixture(ctx, {
|
|
publish: mode !== 'draft',
|
|
future: mode === 'future',
|
|
});
|
|
if (mode === 'cancelled')
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/campaigns/${f.campaignId}/cancellation`)
|
|
.auth(ctx.token, auth)
|
|
.send({ reason: 'Campaign withdrawn' })
|
|
.expect(201);
|
|
if (mode === 'removed')
|
|
await ctx.db.productGroup.deleteMany({ where: { groupId: f.groupId } });
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post('/api/v1/checkout')
|
|
.auth(f.actor.token, auth)
|
|
.send(f.input)
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('COUPON_CAMPAIGN_INELIGIBLE');
|
|
expect(
|
|
await ctx.db.order.count({ where: { userId: f.actor.userId } }),
|
|
).toBe(0);
|
|
}
|
|
});
|
|
|
|
it('preserves coupon limits and releases capacity on order cancellation', async () => {
|
|
const f = await campaignDiscountFixture(ctx, { maxUses: 1 });
|
|
const first = await ctx
|
|
.api()
|
|
.post('/api/v1/checkout')
|
|
.auth(f.actor.token, auth)
|
|
.send(f.input)
|
|
.expect(201);
|
|
await ctx
|
|
.api()
|
|
.put(`/api/v1/cart/lines/${f.variant.id}`)
|
|
.auth(f.actor.token, auth)
|
|
.send({ version: 2, quantity: 1 })
|
|
.expect(200);
|
|
const next = { ...f.input, cartVersion: 3, idempotencyKey: randomUUID() };
|
|
await ctx
|
|
.api()
|
|
.post('/api/v1/checkout')
|
|
.auth(f.actor.token, auth)
|
|
.send(next)
|
|
.expect(409);
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/orders/${first.body.id}/cancel`)
|
|
.auth(f.actor.token, auth)
|
|
.expect(201);
|
|
await ctx
|
|
.api()
|
|
.post('/api/v1/checkout')
|
|
.auth(f.actor.token, auth)
|
|
.send(next)
|
|
.expect(201);
|
|
});
|
|
});
|