149 lines
4.5 KiB
TypeScript
149 lines
4.5 KiB
TypeScript
/**
|
|
* Order business rules: delivery charge vs the free-delivery threshold, per-slot
|
|
* grouping, slot capacity and the flash-delivery gate.
|
|
*
|
|
* Uses a tiny-priced SKU (5) so a single unit sits below `freeDeliveryThreshold`.
|
|
*/
|
|
|
|
import { apiClient, captureError } from '../helpers/api'
|
|
import { asNumber, getConstants, setConst } from '../helpers/consts'
|
|
import { createFutureSlot, createStoreWithSku, ensureUserAddress } from '../helpers/fixtures'
|
|
import { staffSession, userSession } from '../helpers/sessions'
|
|
|
|
const UNIT_PRICE = 5
|
|
|
|
describe('order rules', () => {
|
|
let token = ''
|
|
let skuId = 0
|
|
let addressId = 0
|
|
let slotId = 0
|
|
let secondSlotId = 0
|
|
let charge = 0
|
|
let freeDeliveryThreshold = 0
|
|
|
|
beforeAll(async () => {
|
|
const store = await createStoreWithSku({ price: UNIT_PRICE })
|
|
const slot = await createFutureSlot([store.skuId], { deliveryInHours: 3, freezeInHours: 2 })
|
|
const secondSlot = await createFutureSlot([store.skuId], {
|
|
deliveryInHours: 6,
|
|
freezeInHours: 5,
|
|
})
|
|
const address = await ensureUserAddress()
|
|
const user = await userSession()
|
|
const consts = await getConstants()
|
|
|
|
token = user.token
|
|
skuId = store.skuId
|
|
addressId = address.addressId
|
|
slotId = slot.slotId
|
|
secondSlotId = secondSlot.slotId
|
|
charge = asNumber(consts.deliveryCharge)
|
|
freeDeliveryThreshold = asNumber(consts.freeDeliveryThreshold)
|
|
})
|
|
|
|
it('adds the configured delivery charge below the free-delivery threshold', async () => {
|
|
// Guard: this case is only meaningful while a single unit is below the threshold.
|
|
expect(freeDeliveryThreshold).toBeGreaterThan(UNIT_PRICE)
|
|
|
|
const res = await apiClient(token).user.order.placeOrder.mutate({
|
|
selectedItems: [{ skuId, quantity: 1, slotId }],
|
|
addressId,
|
|
regionId: 1,
|
|
paymentMethod: 'cod',
|
|
})
|
|
|
|
expect(res.data[0].deliveryCharge).toBe(String(charge))
|
|
})
|
|
|
|
it('waives the delivery charge at or above the free-delivery threshold', async () => {
|
|
const quantity = Math.ceil(freeDeliveryThreshold / UNIT_PRICE) + 1
|
|
|
|
const res = await apiClient(token).user.order.placeOrder.mutate({
|
|
selectedItems: [{ skuId, quantity, slotId }],
|
|
addressId,
|
|
regionId: 1,
|
|
paymentMethod: 'cod',
|
|
})
|
|
|
|
expect(res.data[0].deliveryCharge).toBe('0')
|
|
})
|
|
|
|
it('groups items from different slots into one order per slot, charging once', async () => {
|
|
const res = await apiClient(token).user.order.placeOrder.mutate({
|
|
selectedItems: [
|
|
{ skuId, quantity: 1, slotId },
|
|
{ skuId, quantity: 1, slotId: secondSlotId },
|
|
],
|
|
addressId,
|
|
regionId: 1,
|
|
paymentMethod: 'cod',
|
|
})
|
|
|
|
expect(res.data).toHaveLength(2)
|
|
expect(new Set(res.data.map((o) => o.slotId))).toEqual(new Set([slotId, secondSlotId]))
|
|
|
|
const charged = res.data.filter((o) => o.deliveryCharge !== '0')
|
|
expect(charged).toHaveLength(1)
|
|
})
|
|
|
|
it('rejects an order for a full slot', async () => {
|
|
const staff = await staffSession()
|
|
const admin = apiClient(staff.token)
|
|
|
|
await admin.admin.slots.updateSlotCapacity.mutate({
|
|
slotId,
|
|
isCapacityFull: true,
|
|
})
|
|
|
|
try {
|
|
const error = await captureError(() =>
|
|
apiClient(token).user.order.placeOrder.mutate({
|
|
selectedItems: [{ skuId, quantity: 1, slotId }],
|
|
addressId,
|
|
regionId: 1,
|
|
paymentMethod: 'cod',
|
|
})
|
|
)
|
|
expect(error.message).toMatch(/capacity/i)
|
|
} finally {
|
|
await admin.admin.slots.updateSlotCapacity.mutate({
|
|
slotId,
|
|
isCapacityFull: false,
|
|
})
|
|
}
|
|
})
|
|
|
|
it('rejects flash delivery while it is disabled', async () => {
|
|
const consts = await getConstants()
|
|
const wasEnabled = consts.isFlashDeliveryEnabled === true
|
|
if (wasEnabled) await setConst('isFlashDeliveryEnabled', false)
|
|
|
|
try {
|
|
const error = await captureError(() =>
|
|
apiClient(token).user.order.placeOrder.mutate({
|
|
selectedItems: [{ skuId, quantity: 1, slotId: null }],
|
|
addressId,
|
|
regionId: 1,
|
|
paymentMethod: 'cod',
|
|
isFlashDelivery: true,
|
|
})
|
|
)
|
|
expect(error.message).toMatch(/flash/i)
|
|
} finally {
|
|
if (wasEnabled) await setConst('isFlashDeliveryEnabled', true)
|
|
}
|
|
})
|
|
|
|
it('rejects an invalid quantity', async () => {
|
|
const error = await captureError(() =>
|
|
apiClient(token).user.order.placeOrder.mutate({
|
|
selectedItems: [{ skuId, quantity: 0, slotId }],
|
|
addressId,
|
|
regionId: 1,
|
|
paymentMethod: 'cod',
|
|
})
|
|
)
|
|
|
|
expect(error).toBeInstanceOf(Error)
|
|
})
|
|
})
|