291 lines
11 KiB
TypeScript
291 lines
11 KiB
TypeScript
/**
|
|
* SGCOUP-01..17 — `superadmin.globalCoupons` (catalogue §8.7).
|
|
*
|
|
* Live HTTP/tRPC suite. One `it()` per catalogue row.
|
|
*
|
|
* Validation failures here are `ApiError`s (BAD_REQUEST), not raw 500s — the
|
|
* global-coupons router validates before touching the DB, unlike the admin
|
|
* coupon router.
|
|
*/
|
|
|
|
import { apiClient, expectTrpcError, uniqueSuffix } from '../helpers/api'
|
|
import { anonClient, staffSession, superadminClient, userClient, userSession } from '../helpers/sessions'
|
|
|
|
jest.setTimeout(180_000)
|
|
|
|
/** A unique, uppercase global coupon code. */
|
|
function code(prefix: string): string {
|
|
return `${prefix}${uniqueSuffix().replace(/[^a-z0-9]/gi, '').toUpperCase()}`
|
|
}
|
|
|
|
/** Create global coupons and return the created rows. */
|
|
async function create(payload: Record<string, unknown>): Promise<any[]> {
|
|
const superadmin = await superadminClient()
|
|
const res: any = await superadmin.superadmin.globalCoupons.create.mutate(payload as any)
|
|
return res.coupons
|
|
}
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* reads */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('superadmin.globalCoupons reads (SGCOUP-01..03)', () => {
|
|
it('SGCOUP-01: list returns the global coupons', async () => {
|
|
const superadmin = await superadminClient()
|
|
// The cleaned local DB has no global coupons, so create one first.
|
|
const created = await create({ couponCode: code('SGCOUP01'), flatDiscount: 30, isApplyForAll: true })
|
|
|
|
const res: any = await superadmin.superadmin.globalCoupons.list.query()
|
|
expect(Array.isArray(res.coupons)).toBe(true)
|
|
expect(res.coupons.length).toBeGreaterThan(0)
|
|
expect(res.coupons.map((c: any) => Number(c.id))).toEqual(expect.arrayContaining(created.map((c: any) => Number(c.id))))
|
|
for (const coupon of res.coupons) {
|
|
expect(coupon).toEqual(
|
|
expect.objectContaining({
|
|
id: expect.any(Number),
|
|
couponCode: expect.any(String),
|
|
regionId: null,
|
|
})
|
|
)
|
|
}
|
|
})
|
|
|
|
it('SGCOUP-02: get returns a global coupon', async () => {
|
|
const superadmin = await superadminClient()
|
|
const created = await create({ couponCode: code('SGCOUP02'), flatDiscount: 25, isApplyForAll: true })
|
|
const target = created[0]
|
|
|
|
const res: any = await superadmin.superadmin.globalCoupons.get.query({ id: Number(target.id) })
|
|
expect(Number(res.coupon.id)).toBe(Number(target.id))
|
|
expect(res.coupon.couponCode).toBe(target.couponCode)
|
|
expect(res.coupon.regionId).toBeNull()
|
|
})
|
|
|
|
it('SGCOUP-03: getting an unknown coupon is 404', async () => {
|
|
const superadmin = await superadminClient()
|
|
await expectTrpcError(
|
|
() => superadmin.superadmin.globalCoupons.get.query({ id: 987_654_321 }),
|
|
'NOT_FOUND',
|
|
'not found'
|
|
)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* create */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('superadmin.globalCoupons.create (SGCOUP-04..10)', () => {
|
|
it('SGCOUP-04: create makes flat and percent coupons in a coupons array', async () => {
|
|
const flatCode = code('GFLAT')
|
|
const flat = await create({ couponCode: flatCode, flatDiscount: 40, isApplyForAll: true, skuIds: [] })
|
|
expect(flat).toHaveLength(1)
|
|
expect(flat[0].couponCode).toBe(flatCode)
|
|
expect(Number(flat[0].flatDiscount)).toBe(40)
|
|
expect(flat[0].discountPercent).toBeNull()
|
|
expect(flat[0].regionId).toBeNull()
|
|
|
|
const pctCode = code('GPCT')
|
|
const pct = await create({ couponCode: pctCode, discountPercent: 25, isApplyForAll: true, skuIds: [] })
|
|
expect(pct).toHaveLength(1)
|
|
expect(Number(pct[0].discountPercent)).toBe(25)
|
|
expect(pct[0].flatDiscount).toBeNull()
|
|
})
|
|
|
|
it('SGCOUP-05: both discount types, or neither, is BAD_REQUEST', async () => {
|
|
await expectTrpcError(
|
|
() => create({ couponCode: code('GBOTH'), flatDiscount: 10, discountPercent: 10, isApplyForAll: true }),
|
|
'BAD_REQUEST',
|
|
'not both'
|
|
)
|
|
await expectTrpcError(
|
|
() => create({ couponCode: code('GNONE'), isApplyForAll: true }),
|
|
'BAD_REQUEST',
|
|
'discountPercent or flatDiscount'
|
|
)
|
|
})
|
|
|
|
it('SGCOUP-06: user-based with no applicable users is BAD_REQUEST', async () => {
|
|
await expectTrpcError(
|
|
() => create({ couponCode: code('GUB'), flatDiscount: 10, isUserBased: true }),
|
|
'BAD_REQUEST',
|
|
'applicableUsers is required'
|
|
)
|
|
})
|
|
|
|
it('SGCOUP-07: user-based combined with apply-for-all is BAD_REQUEST', async () => {
|
|
await expectTrpcError(
|
|
() =>
|
|
create({
|
|
couponCode: code('GUBALL'),
|
|
flatDiscount: 10,
|
|
isUserBased: true,
|
|
isApplyForAll: true,
|
|
applicableUsers: [1],
|
|
}),
|
|
'BAD_REQUEST',
|
|
'Cannot be both user-based'
|
|
)
|
|
})
|
|
|
|
it('SGCOUP-08: an unknown applicable user is BAD_REQUEST', async () => {
|
|
await expectTrpcError(
|
|
() =>
|
|
create({
|
|
couponCode: code('GINVU'),
|
|
flatDiscount: 10,
|
|
isUserBased: true,
|
|
applicableUsers: [987_654_321],
|
|
}),
|
|
'BAD_REQUEST',
|
|
'not found'
|
|
)
|
|
})
|
|
|
|
it('SGCOUP-09: a duplicate code is BAD_REQUEST', async () => {
|
|
const duplicate = code('GDUP')
|
|
await create({ couponCode: duplicate, flatDiscount: 10, isApplyForAll: true, skuIds: [] })
|
|
await expectTrpcError(
|
|
() => create({ couponCode: duplicate, flatDiscount: 20, isApplyForAll: true, skuIds: [] }),
|
|
'BAD_REQUEST',
|
|
'already exists'
|
|
)
|
|
})
|
|
|
|
it('SGCOUP-10: with no code a generated MF code is used', async () => {
|
|
const coupons = await create({ flatDiscount: 10, isApplyForAll: true, skuIds: [] })
|
|
expect(coupons).toHaveLength(1)
|
|
expect(coupons[0].couponCode).toMatch(/^MF/)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* update */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('superadmin.globalCoupons.update (SGCOUP-11..13)', () => {
|
|
it('SGCOUP-11: a partial update preserves untouched fields', async () => {
|
|
const superadmin = await superadminClient()
|
|
const createdCode = code('GUPD')
|
|
const [coupon] = await create({
|
|
couponCode: createdCode,
|
|
flatDiscount: 50,
|
|
isApplyForAll: true,
|
|
skuIds: [],
|
|
})
|
|
|
|
const res: any = await superadmin.superadmin.globalCoupons.update.mutate({
|
|
id: coupon.id,
|
|
flatDiscount: 75,
|
|
minOrder: 100,
|
|
})
|
|
expect(res.message).toBe('Global coupon updated successfully')
|
|
expect(res.coupon.couponCode).toBe(createdCode)
|
|
expect(Number(res.coupon.flatDiscount)).toBe(75)
|
|
expect(Number(res.coupon.minOrder)).toBe(100)
|
|
expect(res.coupon.isApplyForAll).toBe(true)
|
|
|
|
const fetched: any = await superadmin.superadmin.globalCoupons.get.query({ id: coupon.id })
|
|
expect(fetched.coupon.couponCode).toBe(createdCode)
|
|
expect(Number(fetched.coupon.flatDiscount)).toBe(75)
|
|
})
|
|
|
|
it('SGCOUP-12: updating to both discount types is BAD_REQUEST', async () => {
|
|
const superadmin = await superadminClient()
|
|
const [coupon] = await create({
|
|
couponCode: code('GUPDBOTH'),
|
|
flatDiscount: 50,
|
|
isApplyForAll: true,
|
|
skuIds: [],
|
|
})
|
|
await expectTrpcError(
|
|
() =>
|
|
superadmin.superadmin.globalCoupons.update.mutate({
|
|
id: coupon.id,
|
|
flatDiscount: 10,
|
|
discountPercent: 10,
|
|
}),
|
|
'BAD_REQUEST',
|
|
'Cannot have both'
|
|
)
|
|
})
|
|
|
|
it('SGCOUP-13: updating an unknown coupon is 404', async () => {
|
|
const superadmin = await superadminClient()
|
|
await expectTrpcError(
|
|
() => superadmin.superadmin.globalCoupons.update.mutate({ id: 987_654_321, flatDiscount: 10 }),
|
|
'NOT_FOUND'
|
|
)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* delete */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('superadmin.globalCoupons.delete (SGCOUP-14..15)', () => {
|
|
it('SGCOUP-14: delete invalidates the coupon', async () => {
|
|
const superadmin = await superadminClient()
|
|
const [coupon] = await create({
|
|
couponCode: code('GDEL'),
|
|
flatDiscount: 50,
|
|
isApplyForAll: true,
|
|
skuIds: [],
|
|
})
|
|
|
|
const res: any = await superadmin.superadmin.globalCoupons.delete.mutate({ id: coupon.id })
|
|
expect(res.message).toBe('Global coupon invalidated successfully')
|
|
|
|
const fetched: any = await superadmin.superadmin.globalCoupons.get.query({ id: coupon.id })
|
|
expect(fetched.coupon.isInvalidated).toBe(true)
|
|
})
|
|
|
|
it('SGCOUP-15: deleting an unknown coupon is 404', async () => {
|
|
const superadmin = await superadminClient()
|
|
await expectTrpcError(
|
|
() => superadmin.superadmin.globalCoupons.delete.mutate({ id: 987_654_321 }),
|
|
'NOT_FOUND'
|
|
)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* user visibility + auth */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('superadmin.globalCoupons user visibility + auth (SGCOUP-16..17)', () => {
|
|
it('SGCOUP-16: a user sees a global coupon in getEligible', async () => {
|
|
const createdCode = code('GELIG')
|
|
await create({ couponCode: createdCode, flatDiscount: 30, isApplyForAll: true, skuIds: [] })
|
|
|
|
const user = await userClient()
|
|
const res: any = await user.user.coupon.getEligible.query({})
|
|
expect(res.success).toBe(true)
|
|
expect(res.data.some((coupon: any) => coupon.couponCode === createdCode)).toBe(true)
|
|
})
|
|
|
|
it('SGCOUP-17: anonymous, staff and user tokens are rejected', async () => {
|
|
const staff = await staffSession()
|
|
const user = await userSession()
|
|
|
|
for (const client of [anonClient(), apiClient(staff.token), apiClient(user.token)]) {
|
|
await expectTrpcError(() => client.superadmin.globalCoupons.list.query(), 'UNAUTHORIZED')
|
|
await expectTrpcError(
|
|
() => client.superadmin.globalCoupons.get.query({ id: 1 }),
|
|
'UNAUTHORIZED'
|
|
)
|
|
await expectTrpcError(
|
|
() => client.superadmin.globalCoupons.create.mutate({ flatDiscount: 10 } as any),
|
|
'UNAUTHORIZED'
|
|
)
|
|
await expectTrpcError(
|
|
() => client.superadmin.globalCoupons.update.mutate({ id: 1, flatDiscount: 10 }),
|
|
'UNAUTHORIZED'
|
|
)
|
|
await expectTrpcError(
|
|
() => client.superadmin.globalCoupons.delete.mutate({ id: 1 }),
|
|
'UNAUTHORIZED'
|
|
)
|
|
}
|
|
})
|
|
})
|