707 lines
25 KiB
TypeScript
707 lines
25 KiB
TypeScript
/**
|
|
* ACOUP-01..36 — `admin.coupon` (catalogue §7.6).
|
|
*
|
|
* Live HTTP/tRPC suite against the running backend. Every `it()` maps to one
|
|
* catalogue row; rows that pin current (not intended) behaviour are prefixed
|
|
* `(current)` with a comment recording the observed contract.
|
|
*
|
|
* Notable observed contracts (do not "fix" here):
|
|
* - Validation/business errors are thrown as raw `Error`s, so every one of them
|
|
* surfaces as INTERNAL_SERVER_ERROR — including a duplicate code (not 409).
|
|
* - `createCoupon` (by mobile) creates a user on the fly for an unknown mobile:
|
|
* there is no "unknown mobile" rejection.
|
|
* - The router uses `protectedProcedure`, so a user token passes the guard and
|
|
* then fails on the missing `ctx.staffUser`.
|
|
* - `getById` maps the join rows to plain user objects (`applicableUsers[].id`).
|
|
*/
|
|
|
|
import { apiClient, expectTrpcError, uniqueSuffix } from '../helpers/api'
|
|
import { anonClient, staffClient, superadminClient, userClient } from '../helpers/sessions'
|
|
import {
|
|
createCatalogOrderFixture,
|
|
placeOrder,
|
|
registerThrowawayUser,
|
|
} from '../helpers/fixtures'
|
|
import { getEssentialConsts, versionOf } from '../helpers/cache'
|
|
import { TEST_REGION_ID } from '../helpers/env'
|
|
|
|
jest.setTimeout(180_000)
|
|
|
|
const REGION_1 = TEST_REGION_ID
|
|
|
|
/** A unique, uppercase coupon code carrying the given prefix. */
|
|
function code(prefix: string): string {
|
|
return `${prefix}${uniqueSuffix().replace(/[^a-z0-9]/gi, '').toUpperCase()}`
|
|
}
|
|
|
|
/** Create one coupon, returning the raw (array) response. */
|
|
async function create(payload: Record<string, unknown>): Promise<any[]> {
|
|
const admin = await staffClient()
|
|
return (await admin.admin.coupon.create.mutate(payload as any)) as any[]
|
|
}
|
|
|
|
/** Create one reserved (secret) coupon, returning the raw response. */
|
|
async function createReserved(payload: Record<string, unknown>): Promise<any[]> {
|
|
const admin = await staffClient()
|
|
return (await admin.admin.coupon.createReservedCoupon.mutate(payload as any)) as any[]
|
|
}
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* 1. create (ACOUP-01..13) */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('admin.coupon.create (ACOUP-01..13)', () => {
|
|
it('ACOUP-01: a flat coupon is created as an array and the code is trimmed/uppercased', async () => {
|
|
const admin = await staffClient()
|
|
const expected = code('FLAT')
|
|
const res: any = await admin.admin.coupon.create.mutate({
|
|
couponCode: ` ${expected.toLowerCase()} `,
|
|
skuIds: [],
|
|
isApplyForAll: true,
|
|
flatDiscount: 40,
|
|
})
|
|
|
|
expect(Array.isArray(res)).toBe(true)
|
|
expect(res).toHaveLength(1)
|
|
expect(res[0].couponCode).toBe(expected)
|
|
expect(Number(res[0].flatDiscount)).toBe(40)
|
|
expect(res[0].discountPercent).toBeNull()
|
|
expect(res[0].regionId).toBe(REGION_1)
|
|
expect(res[0].isInvalidated).toBe(false)
|
|
})
|
|
|
|
it('ACOUP-02: a percent coupon stores discountPercent', async () => {
|
|
const admin = await staffClient()
|
|
const res: any = await create({
|
|
couponCode: code('PCT'),
|
|
skuIds: [],
|
|
isApplyForAll: true,
|
|
discountPercent: 25,
|
|
})
|
|
|
|
expect(Number(res[0].discountPercent)).toBe(25)
|
|
expect(res[0].flatDiscount).toBeNull()
|
|
|
|
const fetched: any = await admin.admin.coupon.getById.query({ id: res[0].id })
|
|
expect(Number(fetched.discountPercent)).toBe(25)
|
|
expect(fetched.flatDiscount).toBeNull()
|
|
})
|
|
|
|
it('ACOUP-03: multiple codes produce one coupon each', async () => {
|
|
const admin = await staffClient()
|
|
const codes = [code('M'), code('M')]
|
|
const res: any = await admin.admin.coupon.create.mutate({
|
|
couponCodes: codes,
|
|
skuIds: [],
|
|
isApplyForAll: true,
|
|
flatDiscount: 10,
|
|
})
|
|
|
|
expect(res).toHaveLength(2)
|
|
expect(res.map((c: any) => c.couponCode).sort()).toEqual([...codes].sort())
|
|
for (const c of res) {
|
|
expect(Number(c.flatDiscount)).toBe(10)
|
|
}
|
|
})
|
|
|
|
it('ACOUP-04: with no code a generated MF code is used', async () => {
|
|
const res: any = await create({ skuIds: [], isApplyForAll: true, flatDiscount: 10 })
|
|
expect(res).toHaveLength(1)
|
|
expect(res[0].couponCode).toMatch(/^MF/)
|
|
})
|
|
|
|
it('ACOUP-05 (current): both discount types is an INTERNAL_SERVER_ERROR', async () => {
|
|
await expectTrpcError(
|
|
() =>
|
|
create({
|
|
couponCode: code('BOTH'),
|
|
skuIds: [],
|
|
isApplyForAll: true,
|
|
flatDiscount: 10,
|
|
discountPercent: 10,
|
|
}),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'not both'
|
|
)
|
|
})
|
|
|
|
it('ACOUP-06 (current): neither discount type is an INTERNAL_SERVER_ERROR', async () => {
|
|
await expectTrpcError(
|
|
() => create({ couponCode: code('NONE'), skuIds: [], isApplyForAll: true }),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'discountPercent or flatDiscount'
|
|
)
|
|
})
|
|
|
|
it('ACOUP-07 (current): user-based without users is an INTERNAL_SERVER_ERROR', async () => {
|
|
await expectTrpcError(
|
|
() => create({ couponCode: code('UB'), flatDiscount: 10, isUserBased: true }),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'applicableUsers is required'
|
|
)
|
|
})
|
|
|
|
it('ACOUP-08 (current): user-based + apply-for-all is an INTERNAL_SERVER_ERROR', async () => {
|
|
await expectTrpcError(
|
|
() =>
|
|
create({
|
|
couponCode: code('UBALL'),
|
|
flatDiscount: 10,
|
|
isUserBased: true,
|
|
isApplyForAll: true,
|
|
applicableUsers: [1],
|
|
}),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'Cannot be both user-based'
|
|
)
|
|
})
|
|
|
|
it('ACOUP-09 (current): an unknown applicable user is an INTERNAL_SERVER_ERROR', async () => {
|
|
await expectTrpcError(
|
|
() =>
|
|
create({
|
|
couponCode: code('BADUSER'),
|
|
flatDiscount: 10,
|
|
applicableUsers: [999_999_999],
|
|
}),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'applicable users not found'
|
|
)
|
|
})
|
|
|
|
it('ACOUP-10 (current): a duplicate code is an INTERNAL_SERVER_ERROR, not 409', async () => {
|
|
const dup = code('DUP')
|
|
await create({ couponCode: dup, skuIds: [], isApplyForAll: true, flatDiscount: 10 })
|
|
await expectTrpcError(
|
|
() => create({ couponCode: dup, skuIds: [], isApplyForAll: true, flatDiscount: 10 }),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'already exists'
|
|
)
|
|
})
|
|
|
|
it('ACOUP-11: null/empty skuIds target the whole cart', async () => {
|
|
const admin = await staffClient()
|
|
const nullRes: any = await create({
|
|
couponCode: code('NSKU'),
|
|
skuIds: null,
|
|
isApplyForAll: true,
|
|
flatDiscount: 10,
|
|
})
|
|
const emptyRes: any = await create({
|
|
couponCode: code('ESKU'),
|
|
skuIds: [],
|
|
isApplyForAll: true,
|
|
flatDiscount: 10,
|
|
})
|
|
|
|
const nullFetched: any = await admin.admin.coupon.getById.query({ id: nullRes[0].id })
|
|
const emptyFetched: any = await admin.admin.coupon.getById.query({ id: emptyRes[0].id })
|
|
expect(nullFetched.skuIds).toBeUndefined()
|
|
expect(emptyFetched.skuIds).toBeUndefined()
|
|
})
|
|
|
|
it('ACOUP-12: max/min/maxLimit/exclusive/first-order are stored', async () => {
|
|
const admin = await staffClient()
|
|
const res: any = await create({
|
|
couponCode: code('ALL'),
|
|
skuIds: [],
|
|
isApplyForAll: true,
|
|
flatDiscount: 10,
|
|
minOrder: 500,
|
|
maxValue: 100,
|
|
maxLimitForUser: 3,
|
|
exclusiveApply: true,
|
|
isFirstOrderOnly: true,
|
|
})
|
|
|
|
const fetched: any = await admin.admin.coupon.getById.query({ id: res[0].id })
|
|
expect(Number(fetched.minOrder)).toBe(500)
|
|
expect(Number(fetched.maxValue)).toBe(100)
|
|
expect(fetched.maxLimitForUser).toBe(3)
|
|
expect(fetched.exclusiveApply).toBe(true)
|
|
expect(fetched.isFirstOrderOnly).toBe(true)
|
|
})
|
|
|
|
it('ACOUP-13: validTill is parsed and stored', async () => {
|
|
const admin = await staffClient()
|
|
const till = new Date(Date.now() + 86_400_000).toISOString()
|
|
const res: any = await create({
|
|
couponCode: code('TILL'),
|
|
skuIds: [],
|
|
isApplyForAll: true,
|
|
flatDiscount: 10,
|
|
validTill: till,
|
|
})
|
|
|
|
const fetched: any = await admin.admin.coupon.getById.query({ id: res[0].id })
|
|
expect(new Date(fetched.validTill).toISOString()).toBe(till)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* 2. getAll / getById (ACOUP-14..16) */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('admin.coupon.getAll / getById (ACOUP-14..16)', () => {
|
|
it('ACOUP-14 (current): getAll supports search and id-cursor pagination', async () => {
|
|
const admin = await staffClient()
|
|
const target = code('SEARCH')
|
|
await create({ couponCode: target, skuIds: [], isApplyForAll: true, flatDiscount: 10 })
|
|
|
|
const searched: any = await admin.admin.coupon.getAll.query({ search: target, limit: 50 })
|
|
expect(searched.coupons.map((c: any) => c.couponCode)).toContain(target)
|
|
|
|
const page1: any = await admin.admin.coupon.getAll.query({ limit: 2 })
|
|
expect(page1.coupons).toHaveLength(2)
|
|
expect(typeof page1.nextCursor).toBe('number')
|
|
|
|
const page2: any = await admin.admin.coupon.getAll.query({ limit: 2, cursor: page1.nextCursor })
|
|
expect(page2.coupons).toHaveLength(2)
|
|
|
|
// Rows are ordered by `createdAt desc` but the cursor is an id bound, so a
|
|
// same-second page boundary can repeat a row. Pin the keyset guarantee that
|
|
// does hold: every row on page 2 has an id below the cursor.
|
|
const ids2 = page2.coupons.map((c: any) => c.id)
|
|
expect(ids2.every((id: number) => id < page1.nextCursor)).toBe(true)
|
|
})
|
|
|
|
it('ACOUP-15: getById returns the coupon plus applicable user ids', async () => {
|
|
const admin = await staffClient()
|
|
const user = await registerThrowawayUser()
|
|
const res: any = await create({
|
|
couponCode: code('BYID'),
|
|
skuIds: [],
|
|
flatDiscount: 10,
|
|
isUserBased: true,
|
|
applicableUsers: [user.userId],
|
|
})
|
|
|
|
const fetched: any = await admin.admin.coupon.getById.query({ id: res[0].id })
|
|
expect(fetched.id).toBe(res[0].id)
|
|
expect(fetched.couponCode).toBe(res[0].couponCode)
|
|
expect(fetched.applicableUsers.map((u: any) => u.id)).toEqual([user.userId])
|
|
expect(fetched.creator).toBeTruthy()
|
|
})
|
|
|
|
it('ACOUP-15B: a region admin can read a global coupon by id', async () => {
|
|
const superadmin = await superadminClient()
|
|
const created: any = await superadmin.superadmin.globalCoupons.create.mutate({
|
|
couponCode: code('GBYID'),
|
|
flatDiscount: 15,
|
|
isApplyForAll: true,
|
|
})
|
|
const global = created.coupons[0]
|
|
|
|
const admin = await staffClient()
|
|
const fetched: any = await admin.admin.coupon.getById.query({ id: Number(global.id) })
|
|
|
|
expect(fetched.id).toBe(Number(global.id))
|
|
expect(fetched.regionId).toBeNull()
|
|
expect(fetched.couponCode).toBe(global.couponCode)
|
|
expect(Number(fetched.flatDiscount)).toBe(15)
|
|
})
|
|
|
|
it('ACOUP-16 (current): an unknown id is an INTERNAL_SERVER_ERROR; a non-numeric id is BAD_REQUEST', async () => {
|
|
const admin = await staffClient()
|
|
await expectTrpcError(
|
|
() => admin.admin.coupon.getById.query({ id: 999_999_999 }),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'Coupon not found'
|
|
)
|
|
await expectTrpcError(
|
|
() => admin.admin.coupon.getById.query({ id: 'x' as any }),
|
|
'BAD_REQUEST'
|
|
)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* 3. update (ACOUP-17..21) */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('admin.coupon.update (ACOUP-17..21)', () => {
|
|
it('ACOUP-17: a partial update changes only the supplied fields', async () => {
|
|
const admin = await staffClient()
|
|
const res: any = await create({
|
|
couponCode: code('UPD'),
|
|
skuIds: [],
|
|
isApplyForAll: true,
|
|
flatDiscount: 10,
|
|
minOrder: 200,
|
|
})
|
|
const originalCode = res[0].couponCode
|
|
|
|
await admin.admin.coupon.update.mutate({ id: res[0].id, updates: { flatDiscount: 99 } })
|
|
|
|
const fetched: any = await admin.admin.coupon.getById.query({ id: res[0].id })
|
|
expect(Number(fetched.flatDiscount)).toBe(99)
|
|
expect(fetched.couponCode).toBe(originalCode)
|
|
expect(Number(fetched.minOrder)).toBe(200)
|
|
})
|
|
|
|
it('ACOUP-18: an updated code is trimmed/uppercased (top-level and array forms)', async () => {
|
|
const admin = await staffClient()
|
|
const res: any = await create({
|
|
couponCode: code('UPDC'),
|
|
skuIds: [],
|
|
isApplyForAll: true,
|
|
flatDiscount: 10,
|
|
})
|
|
|
|
const viaTopLevel = code('TOPLVL')
|
|
await admin.admin.coupon.update.mutate({
|
|
id: res[0].id,
|
|
updates: { couponCode: ` ${viaTopLevel.toLowerCase()} ` },
|
|
})
|
|
let fetched: any = await admin.admin.coupon.getById.query({ id: res[0].id })
|
|
expect(fetched.couponCode).toBe(viaTopLevel)
|
|
|
|
const viaArray = code('ARRAY')
|
|
await admin.admin.coupon.update.mutate({
|
|
id: res[0].id,
|
|
updates: { couponCodes: [viaArray.toLowerCase()] },
|
|
})
|
|
fetched = await admin.admin.coupon.getById.query({ id: res[0].id })
|
|
expect(fetched.couponCode).toBe(viaArray)
|
|
})
|
|
|
|
it('ACOUP-19: updating applicableUsers replaces the relationships', async () => {
|
|
const admin = await staffClient()
|
|
const first = await registerThrowawayUser()
|
|
const second = await registerThrowawayUser()
|
|
const res: any = await create({
|
|
couponCode: code('REL'),
|
|
skuIds: [],
|
|
flatDiscount: 10,
|
|
isUserBased: true,
|
|
applicableUsers: [first.userId],
|
|
})
|
|
|
|
await admin.admin.coupon.update.mutate({
|
|
id: res[0].id,
|
|
updates: { applicableUsers: [second.userId] },
|
|
})
|
|
|
|
const fetched: any = await admin.admin.coupon.getById.query({ id: res[0].id })
|
|
expect(fetched.applicableUsers.map((u: any) => u.id)).toEqual([second.userId])
|
|
})
|
|
|
|
it('ACOUP-20 (current): updating to both discount types is an INTERNAL_SERVER_ERROR', async () => {
|
|
const res: any = await create({
|
|
couponCode: code('UPBOTH'),
|
|
skuIds: [],
|
|
isApplyForAll: true,
|
|
flatDiscount: 10,
|
|
})
|
|
await expectTrpcError(
|
|
() =>
|
|
staffClient().then((admin) =>
|
|
admin.admin.coupon.update.mutate({
|
|
id: res[0].id,
|
|
updates: { flatDiscount: 10, discountPercent: 10 },
|
|
})
|
|
),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'Cannot have both'
|
|
)
|
|
})
|
|
|
|
it('ACOUP-21 (current): updating an unknown coupon is an INTERNAL_SERVER_ERROR', async () => {
|
|
await expectTrpcError(
|
|
() =>
|
|
staffClient().then((admin) =>
|
|
admin.admin.coupon.update.mutate({ id: 999_999_999, updates: { flatDiscount: 10 } })
|
|
),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'Coupon not found'
|
|
)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* 4. delete (ACOUP-22..24) */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('admin.coupon.delete (ACOUP-22..24)', () => {
|
|
it('ACOUP-22: delete soft-invalidates and returns a message', async () => {
|
|
const admin = await staffClient()
|
|
const res: any = await create({
|
|
couponCode: code('DEL'),
|
|
skuIds: [],
|
|
isApplyForAll: true,
|
|
flatDiscount: 10,
|
|
})
|
|
|
|
const deleted: any = await admin.admin.coupon.delete.mutate({ id: res[0].id })
|
|
expect(deleted).toEqual({ message: 'Coupon invalidated successfully' })
|
|
|
|
await expectTrpcError(
|
|
() => admin.admin.coupon.delete.mutate({ id: 999_999_999 }),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'Coupon not found'
|
|
)
|
|
})
|
|
|
|
it('ACOUP-23: getById after delete still returns the invalidated row', async () => {
|
|
const admin = await staffClient()
|
|
const res: any = await create({
|
|
couponCode: code('DEL2'),
|
|
skuIds: [],
|
|
isApplyForAll: true,
|
|
flatDiscount: 10,
|
|
})
|
|
await admin.admin.coupon.delete.mutate({ id: res[0].id })
|
|
|
|
const fetched: any = await admin.admin.coupon.getById.query({ id: res[0].id })
|
|
expect(fetched.id).toBe(res[0].id)
|
|
expect(fetched.isInvalidated).toBe(true)
|
|
})
|
|
|
|
it('ACOUP-24: an invalidated coupon is excluded from the user getEligible list', async () => {
|
|
const admin = await staffClient()
|
|
const fx = await createCatalogOrderFixture({ price: 100 })
|
|
const res: any = await create({
|
|
couponCode: code('ELIG'),
|
|
skuIds: [],
|
|
isApplyForAll: true,
|
|
flatDiscount: 15,
|
|
})
|
|
await admin.admin.coupon.delete.mutate({ id: res[0].id })
|
|
|
|
const eligible: any = await apiClient(fx.user.token).user.coupon.getEligible.query({
|
|
regionId: REGION_1,
|
|
})
|
|
expect(eligible.data.some((c: any) => c.id === res[0].id)).toBe(false)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* 5. reserved coupons (ACOUP-25..28) */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('admin.coupon reserved (ACOUP-25..28)', () => {
|
|
it('ACOUP-25: getReservedCoupons supports search and cursor pagination', async () => {
|
|
const admin = await staffClient()
|
|
const secrets = [code('SEC'), code('SEC')]
|
|
await createReserved({ couponCodes: secrets, skuIds: [], flatDiscount: 10 })
|
|
|
|
const page1: any = await admin.admin.coupon.getReservedCoupons.query({ limit: 1 })
|
|
expect(page1.coupons).toHaveLength(1)
|
|
expect(typeof page1.nextCursor).toBe('number')
|
|
|
|
const page2: any = await admin.admin.coupon.getReservedCoupons.query({
|
|
limit: 1,
|
|
cursor: page1.nextCursor,
|
|
})
|
|
expect(page2.coupons).toHaveLength(1)
|
|
expect(page2.coupons[0].id).not.toBe(page1.coupons[0].id)
|
|
|
|
const searched: any = await admin.admin.coupon.getReservedCoupons.query({ search: secrets[0] })
|
|
expect(searched.coupons.map((c: any) => c.secretCode)).toContain(secrets[0])
|
|
})
|
|
|
|
it('ACOUP-26: createReservedCoupon accepts one or multiple secret codes', async () => {
|
|
const single = code('SINGLE')
|
|
const one: any = await createReserved({
|
|
couponCode: single,
|
|
skuIds: [],
|
|
flatDiscount: 10,
|
|
})
|
|
expect(Array.isArray(one)).toBe(true)
|
|
expect(one).toHaveLength(1)
|
|
expect(one[0].secretCode).toBe(single)
|
|
|
|
const pair = [code('PAIR'), code('PAIR')]
|
|
const many: any = await createReserved({ couponCodes: pair, skuIds: [], flatDiscount: 10 })
|
|
expect(many).toHaveLength(2)
|
|
expect(many.map((c: any) => c.secretCode).sort()).toEqual([...pair].sort())
|
|
})
|
|
|
|
it('ACOUP-27 (current): a duplicate secret code is an INTERNAL_SERVER_ERROR', async () => {
|
|
const secret = code('DUPSEC')
|
|
await createReserved({ couponCode: secret, skuIds: [], flatDiscount: 10 })
|
|
await expectTrpcError(
|
|
() => createReserved({ couponCode: secret, skuIds: [], flatDiscount: 10 }),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'already exists'
|
|
)
|
|
})
|
|
|
|
it('ACOUP-28 (current): both/neither discount type is an INTERNAL_SERVER_ERROR', async () => {
|
|
await expectTrpcError(
|
|
() =>
|
|
createReserved({
|
|
couponCode: code('RBOTH'),
|
|
skuIds: [],
|
|
flatDiscount: 10,
|
|
discountPercent: 10,
|
|
}),
|
|
'INTERNAL_SERVER_ERROR'
|
|
)
|
|
await expectTrpcError(
|
|
() => createReserved({ couponCode: code('RNONE'), skuIds: [] }),
|
|
'INTERNAL_SERVER_ERROR'
|
|
)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* 6. getUsersMiniInfo (ACOUP-29..30) */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('admin.coupon.getUsersMiniInfo (ACOUP-29..30)', () => {
|
|
it('ACOUP-29: getUsersMiniInfo filters by mobile with limit/offset', async () => {
|
|
const admin = await staffClient()
|
|
const fx = await createCatalogOrderFixture({ price: 100 })
|
|
await placeOrder(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
})
|
|
|
|
const res: any = await admin.admin.coupon.getUsersMiniInfo.query({
|
|
search: fx.user.mobile,
|
|
limit: 5,
|
|
offset: 0,
|
|
})
|
|
expect(res.users.map((u: any) => u.id)).toContain(fx.user.userId)
|
|
expect(res.users[0]).toEqual(
|
|
expect.objectContaining({ id: expect.any(Number), mobile: expect.any(String) })
|
|
)
|
|
expect(res.users.length).toBeLessThanOrEqual(5)
|
|
|
|
const limited: any = await admin.admin.coupon.getUsersMiniInfo.query({
|
|
search: fx.user.mobile,
|
|
limit: 1,
|
|
})
|
|
expect(limited.users).toHaveLength(1)
|
|
|
|
await expectTrpcError(
|
|
() => admin.admin.coupon.getUsersMiniInfo.query({ limit: 100 }),
|
|
'BAD_REQUEST'
|
|
)
|
|
})
|
|
|
|
it('ACOUP-30: getUsersMiniInfo search matches by name', async () => {
|
|
const admin = await staffClient()
|
|
const fx = await createCatalogOrderFixture({ price: 100 })
|
|
await placeOrder(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
})
|
|
|
|
const res: any = await admin.admin.coupon.getUsersMiniInfo.query({
|
|
search: fx.user.name,
|
|
limit: 50,
|
|
})
|
|
expect(res.users.map((u: any) => u.id)).toContain(fx.user.userId)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* 7. createCoupon by mobile (ACOUP-31..35) */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('admin.coupon.createCoupon by mobile (ACOUP-31..35)', () => {
|
|
it('ACOUP-31: createCoupon issues a 20% / min 1000 / max 500 / limit 1 coupon', async () => {
|
|
const admin = await staffClient()
|
|
const fx = await createCatalogOrderFixture({ price: 100 })
|
|
await placeOrder(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
})
|
|
|
|
const res: any = await admin.admin.coupon.createCoupon.mutate({ mobile: fx.user.mobile })
|
|
expect(res.success).toBe(true)
|
|
expect(res.coupon.userId).toBe(fx.user.userId)
|
|
expect(res.coupon.discountPercent).toBe(20)
|
|
expect(res.coupon.minOrder).toBe(1000)
|
|
expect(res.coupon.maxValue).toBe(500)
|
|
expect(res.coupon.maxLimitForUser).toBe(1)
|
|
|
|
const fetched: any = await admin.admin.coupon.getById.query({ id: res.coupon.id })
|
|
expect(Number(fetched.discountPercent)).toBe(20)
|
|
expect(Number(fetched.minOrder)).toBe(1000)
|
|
expect(Number(fetched.maxValue)).toBe(500)
|
|
expect(fetched.maxLimitForUser).toBe(1)
|
|
expect(fetched.isUserBased).toBe(true)
|
|
expect(fetched.applicableUsers.map((u: any) => u.id)).toEqual([fx.user.userId])
|
|
})
|
|
|
|
it('ACOUP-32 (current): a mobile that is not 10 digits is an INTERNAL_SERVER_ERROR', async () => {
|
|
await expectTrpcError(
|
|
() => staffClient().then((admin) => admin.admin.coupon.createCoupon.mutate({ mobile: '123' })),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'10 digits'
|
|
)
|
|
await expectTrpcError(
|
|
() =>
|
|
staffClient().then((admin) =>
|
|
admin.admin.coupon.createCoupon.mutate({ mobile: '+91 98765-43210' })
|
|
),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'10 digits'
|
|
)
|
|
})
|
|
|
|
it('ACOUP-33 (current): an unknown mobile is created on the fly, not rejected', async () => {
|
|
// The handler inserts a bare user row when the mobile is unknown, so there
|
|
// is no "unknown mobile" error path.
|
|
const mobile = `9${String(Math.floor(Math.random() * 1e9)).padStart(9, '0')}`
|
|
const res: any = await staffClient().then((admin) =>
|
|
admin.admin.coupon.createCoupon.mutate({ mobile })
|
|
)
|
|
expect(res.success).toBe(true)
|
|
expect(res.coupon.userMobile).toBe(mobile)
|
|
expect(res.coupon.userId).toBeGreaterThan(0)
|
|
})
|
|
|
|
it('ACOUP-34: generated codes are unique', async () => {
|
|
const admin = await staffClient()
|
|
const mobile = `9${String(Math.floor(Math.random() * 1e9)).padStart(9, '0')}`
|
|
const first: any = await admin.admin.coupon.createCoupon.mutate({ mobile })
|
|
const second: any = await admin.admin.coupon.createCoupon.mutate({ mobile })
|
|
|
|
expect(first.coupon.couponCode).not.toBe(second.coupon.couponCode)
|
|
expect(first.coupon.couponCode).toMatch(/^MF/)
|
|
const fetched: any = await admin.admin.coupon.getById.query({ id: second.coupon.id })
|
|
expect(fetched.couponCode).toBe(second.coupon.couponCode)
|
|
})
|
|
|
|
it('ACOUP-35 (current): a user token passes protectedProcedure then 500s on staffUser', async () => {
|
|
const user = await userClient()
|
|
await expectTrpcError(
|
|
() =>
|
|
user.admin.coupon.create.mutate({
|
|
couponCode: code('UTOK'),
|
|
skuIds: [],
|
|
isApplyForAll: true,
|
|
flatDiscount: 10,
|
|
}),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'Unauthorized'
|
|
)
|
|
await expectTrpcError(
|
|
() => anonClient().admin.coupon.getById.query({ id: 1 }),
|
|
'UNAUTHORIZED'
|
|
)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* 8. cache (ACOUP-36) */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('admin.coupon cache (ACOUP-36)', () => {
|
|
it('ACOUP-36: creating a coupon does not bump any cache version', async () => {
|
|
const before = await getEssentialConsts()
|
|
await create({ couponCode: code('CACHE'), skuIds: [], isApplyForAll: true, flatDiscount: 5 })
|
|
const after = await getEssentialConsts()
|
|
|
|
expect(versionOf(after, 'products')).toBe(versionOf(before, 'products'))
|
|
expect(versionOf(after, 'slots')).toBe(versionOf(before, 'slots'))
|
|
expect(versionOf(after, 'availability')).toBe(versionOf(before, 'availability'))
|
|
})
|
|
})
|