/** * Order business rules: delivery charge vs the minimum order value, per-slot * grouping, slot capacity and the flash-delivery gate. * * Uses a tiny-priced SKU (5) so a single unit sits below `minRegularOrderValue`. */ 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 minOrder = 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) minOrder = asNumber(consts.minRegularOrderValue) }) it('adds the configured delivery charge below the minimum order value', async () => { // Guard: this case is only meaningful while a single unit is below the minimum. expect(minOrder).toBeGreaterThan(UNIT_PRICE) const res = await apiClient(token).user.order.placeOrder.mutate({ selectedItems: [{ skuId, quantity: 1, slotId }], addressId, paymentMethod: 'cod', }) expect(res.data[0].deliveryCharge).toBe(String(charge)) }) it('waives the delivery charge at or above the minimum order value', async () => { const quantity = Math.ceil(minOrder / UNIT_PRICE) + 1 const res = await apiClient(token).user.order.placeOrder.mutate({ selectedItems: [{ skuId, quantity, slotId }], addressId, 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, 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, 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, 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, paymentMethod: 'cod', }) ) expect(error).toBeInstanceOf(Error) }) })