import { randomUUID } from 'node:crypto'; import { identityApp, type IdentityApp } from './helpers/identity-app'; import { couponInput } from './helpers/checkout'; import { secondActor } from './helpers/commerce'; describe('draft campaign coupon binding', () => { 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(); }); async function draft() { const group = await ctx .api() .post('/api/v1/catalog-groups') .auth(ctx.token, auth) .send({ name: 'Campaign collection', slug: 'group-' + randomUUID(), kind: 'COLLECTION', }) .expect(201); return ( await ctx .api() .post('/api/v1/campaigns') .auth(ctx.token, auth) .send({ slug: 'discount-' + randomUUID(), title: 'Collection offer', collectionId: group.body.id, startsAt: new Date(Date.now() - 120000).toISOString(), endsAt: new Date(Date.now() + 7200000).toISOString(), idempotencyKey: randomUUID(), }) .expect(201) ).body.id as string; } const bind = (campaignId: string, campaignVersion = 0) => ({ ...couponInput(), campaignId, campaignVersion, }); it('links one coupon atomically and increments the draft publication version', async () => { const campaignId = await draft(); const body = bind(campaignId); const created = await ctx .api() .post('/api/v1/coupons') .auth(ctx.token, auth) .send(body) .expect(201); expect(created.body.campaignId).toBe(campaignId); expect( ( await ctx .api() .get(`/api/v1/campaigns/${campaignId}`) .auth(ctx.token, auth) .expect(200) ).body.version, ).toBe(1); await ctx .api() .post('/api/v1/coupons') .auth(ctx.token, auth) .send(bind(campaignId, 1)) .expect(409); expect(await ctx.db.coupon.count({ where: { campaignId } })).toBe(1); await expect( ctx.executeSql( `UPDATE coupons SET percent_bps = 5000 WHERE id = '${created.body.id}'`, ), ).rejects.toThrow('Create a new coupon'); await ctx .api() .patch(`/api/v1/campaigns/${campaignId}`) .auth(ctx.token, auth) .send({ version: 1, endsAt: new Date(Date.now() + 1000).toISOString() }) .expect(400); await expect( ctx.executeSql( `UPDATE seasonal_campaigns SET ends_at = starts_at + INTERVAL '1 second', version = version + 1 WHERE id = '${campaignId}'`, ), ).rejects.toThrow(); }); it('requires both permissions, a current version and an organization-scoped draft', async () => { const campaignId = await draft(); const couponsOnly = await secondActor(ctx, true, ['coupons.manage']); await ctx .api() .post('/api/v1/coupons') .auth(couponsOnly.token, auth) .send(bind(campaignId)) .expect(403); const outsider = await secondActor(ctx, false, [ 'coupons.manage', 'campaigns.manage', ]); await ctx .api() .post('/api/v1/coupons') .auth(outsider.token, auth) .send(bind(campaignId)) .expect(404); await ctx .api() .post('/api/v1/coupons') .auth(ctx.token, auth) .send(bind(campaignId, 1)) .expect(409); await ctx .api() .post('/api/v1/coupons') .auth(ctx.token, auth) .send({ ...bind(campaignId), startsAt: new Date(Date.now() - 3600000).toISOString(), }) .expect(400); const { campaignVersion: _version, ...missingVersion } = bind(campaignId); await ctx .api() .post('/api/v1/coupons') .auth(ctx.token, auth) .send(missingVersion) .expect(400); const empty = await draft(); await ctx .api() .post(`/api/v1/campaigns/${empty}/cancellation`) .auth(ctx.token, auth) .send({ reason: 'Withdraw draft' }) .expect(201); await ctx .api() .post('/api/v1/coupons') .auth(ctx.token, auth) .send(bind(empty)) .expect(409); }); });