/** * Admin ops: config round-trip, group/product/store lifecycle, and two orders * placed concurrently. */ import { apiClient, captureError, uniqueSuffix } from '../helpers/api' import { asNumber, getConstants, setConst } from '../helpers/consts' import { createFutureSlot, createStoreWithSku, ensureUserAddress } from '../helpers/fixtures' import { staffSession, userSession } from '../helpers/sessions' describe('admin ops', () => { let admin: ReturnType let user: ReturnType let staffId = 0 let skuId = 0 beforeAll(async () => { const staff = await staffSession() staffId = staff.staffId admin = apiClient(staff.token) const session = await userSession() user = apiClient(session.token) // A suspended user is rejected by placeOrder — make sure the shared test user // is active regardless of what other suites left behind. await admin.admin.user.updateUserSuspension.mutate({ userId: session.userId, isSuspended: false, }) const store = await createStoreWithSku() skuId = store.skuId }) it('round-trips a constant through updateConstants', async () => { const before = await getConstants() const original = before.minOrderAmountForCoupon await setConst('minOrderAmountForCoupon', 123) expect(asNumber((await getConstants()).minOrderAmountForCoupon)).toBe(123) if (original !== undefined) await setConst('minOrderAmountForCoupon', original) }) it('updates and deletes a product group', async () => { const base = `API Group ${uniqueSuffix()}` const renamed = `${base} renamed` const created = (await admin.admin.product.createGroup.mutate({ group_name: base, product_ids: [skuId], })) as { group?: { id: number }; id?: number } const groupId = created.group?.id ?? created.id expect(groupId).toBeGreaterThan(0) await admin.admin.product.updateGroup.mutate({ id: groupId!, group_name: renamed }) const groups = await admin.admin.product.getGroups.query() const name = (g: { groupName?: string; group_name?: string }) => g.groupName ?? g.group_name expect(groups.groups.some((g) => name(g) === renamed)).toBe(true) await admin.admin.product.deleteGroup.mutate({ id: groupId! }) const after = await admin.admin.product.getGroups.query() expect(after.groups.some((g: { id: number }) => g.id === groupId)).toBe(false) }) it('deleteProduct rejects for a product that still has SKUs', async () => { // The delete is a hard `delete from product_info`, which fails on the FK from // the product's SKUs/market stats — so a product cannot be deleted this way. const target = await createStoreWithSku() const error = await captureError(() => admin.admin.product.deleteProduct.mutate({ id: target.productId }) ) expect(error).toBeInstanceOf(Error) }) it('deletes a store', async () => { const created = (await admin.admin.store.createStore.mutate({ name: `API Store to delete ${uniqueSuffix()}`, owner: staffId, })) as { store?: { id: number } } const storeId = created.store!.id // NOTE: the input field is `storeId`, not `id`. const res = await admin.admin.store.deleteStore.mutate({ storeId }) expect(res).toBeTruthy() const error = await captureError(() => admin.admin.store.getStoreById.query({ id: storeId }) ) expect(error).toBeInstanceOf(Error) }) it('places two concurrent orders for different slots', async () => { const slotA = await createFutureSlot([skuId], { deliveryInHours: 4, freezeInHours: 3 }) const slotB = await createFutureSlot([skuId], { deliveryInHours: 8, freezeInHours: 7 }) const address = await ensureUserAddress() const [a, b] = await Promise.all([ user.user.order.placeOrder.mutate({ selectedItems: [{ skuId, quantity: 1, slotId: slotA.slotId }], addressId: address.addressId, paymentMethod: 'cod', }), user.user.order.placeOrder.mutate({ selectedItems: [{ skuId, quantity: 1, slotId: slotB.slotId }], addressId: address.addressId, paymentMethod: 'cod', }), ]) expect(a.data[0].id).toBeGreaterThan(0) expect(b.data[0].id).toBeGreaterThan(0) expect(a.data[0].id).not.toBe(b.data[0].id) }) })