139 lines
4.7 KiB
TypeScript
139 lines
4.7 KiB
TypeScript
/**
|
|
* Admin order operations and their user-visible effect.
|
|
*
|
|
* `admin.order.*` uses a mix of numeric and string ids:
|
|
* getOrderDetails / updateNotes / removeDeliveryCharge / cancelOrder -> orderId: number
|
|
* updatePackaged / updateDelivered / getSlotOrders -> orderId/slotId: string
|
|
*/
|
|
|
|
import { apiClient } from '../helpers/api'
|
|
import { createFutureSlot, createStoreWithSku, ensureUserAddress } from '../helpers/fixtures'
|
|
import { staffSession, userSession } from '../helpers/sessions'
|
|
|
|
describe('admin order operations', () => {
|
|
let admin: ReturnType<typeof apiClient>
|
|
let user: ReturnType<typeof apiClient>
|
|
let orderId = 0
|
|
let skuId = 0
|
|
let slotId = 0
|
|
let addressId = 0
|
|
|
|
beforeAll(async () => {
|
|
const staff = await staffSession()
|
|
admin = apiClient(staff.token)
|
|
|
|
const store = await createStoreWithSku()
|
|
const slot = await createFutureSlot([store.skuId])
|
|
const address = await ensureUserAddress()
|
|
const session = await userSession()
|
|
user = apiClient(session.token)
|
|
|
|
skuId = store.skuId
|
|
slotId = slot.slotId
|
|
addressId = address.addressId
|
|
|
|
const placed = await user.user.order.placeOrder.mutate({
|
|
selectedItems: [{ skuId, quantity: 1, slotId }],
|
|
addressId,
|
|
paymentMethod: 'cod',
|
|
})
|
|
orderId = placed.data[0].id
|
|
})
|
|
|
|
it('getAll returns recent orders', async () => {
|
|
const res = await admin.admin.order.getAll.query({ limit: 5 })
|
|
expect(Array.isArray(res?.orders)).toBe(true)
|
|
})
|
|
|
|
it('getOrderDetails returns the order', async () => {
|
|
const details = (await admin.admin.order.getOrderDetails.query({ orderId })) as {
|
|
id?: number
|
|
}
|
|
|
|
expect(details).toBeTruthy()
|
|
if (details.id != null) expect(details.id).toBe(orderId)
|
|
})
|
|
|
|
it('getSlotOrders lists the orders for a slot (string slotId)', async () => {
|
|
const res = await admin.admin.order.getSlotOrders.query({ slotId: String(slotId) })
|
|
expect(res).toBeTruthy()
|
|
})
|
|
|
|
it('updateNotes stores admin notes on the order', async () => {
|
|
const notes = `Admin note ${Date.now()}`
|
|
|
|
await admin.admin.order.updateNotes.mutate({ orderId, adminNotes: notes })
|
|
|
|
const details = (await admin.admin.order.getOrderDetails.query({ orderId })) as {
|
|
adminNotes?: string
|
|
}
|
|
if (details.adminNotes != null) expect(details.adminNotes).toBe(notes)
|
|
})
|
|
|
|
it('marks the order packaged', async () => {
|
|
const res = await admin.admin.order.updatePackaged.mutate({
|
|
orderId: String(orderId),
|
|
isPackaged: true,
|
|
})
|
|
expect(res.success).toBe(true)
|
|
})
|
|
|
|
it('marks the order delivered and the user sees deliveryStatus "success"', async () => {
|
|
const res = await admin.admin.order.updateDelivered.mutate({
|
|
orderId: String(orderId),
|
|
isDelivered: true,
|
|
})
|
|
expect(res.success).toBe(true)
|
|
|
|
const detail = await user.user.order.getOrderById.query({ orderId: String(orderId) })
|
|
expect(String(detail.deliveryStatus).toLowerCase()).toBe('success')
|
|
})
|
|
|
|
it('removes the delivery charge', async () => {
|
|
await admin.admin.order.removeDeliveryCharge.mutate({ orderId })
|
|
|
|
const detail = await user.user.order.getOrderById.query({ orderId: String(orderId) })
|
|
expect(Number(detail.deliveryCharge)).toBe(0)
|
|
})
|
|
|
|
it('updates the address coordinates', async () => {
|
|
const res = await admin.admin.order.updateAddressCoords.mutate({
|
|
addressId,
|
|
latitude: 16.7488,
|
|
longitude: 77.9852,
|
|
})
|
|
expect(res).toBeTruthy()
|
|
|
|
// NOTE: the coordinates set here are not exposed by the user-facing
|
|
// `address.getUserAddresses` read (latitude comes back null/0), so the
|
|
// contract under test is the mutation succeeding + the address surviving.
|
|
const addresses = await user.user.address.getUserAddresses.query()
|
|
expect(addresses.data.some((a) => a.id === addressId)).toBe(true)
|
|
})
|
|
|
|
it('cancels a fresh order as staff and the user sees it cancelled', async () => {
|
|
// The main order above is already delivered, which admin cancel refuses —
|
|
// so place a new one to cancel.
|
|
const placed = await user.user.order.placeOrder.mutate({
|
|
selectedItems: [{ skuId, quantity: 1, slotId }],
|
|
addressId,
|
|
paymentMethod: 'cod',
|
|
})
|
|
const freshOrderId = placed.data[0].id
|
|
|
|
await admin.admin.order.cancelOrder.mutate({
|
|
orderId: freshOrderId,
|
|
reason: `Cancelled by API test ${Date.now()}`,
|
|
})
|
|
|
|
const detail = await user.user.order.getOrderById.query({
|
|
orderId: String(freshOrderId),
|
|
})
|
|
expect(String(detail.orderStatus).toLowerCase()).toMatch(/cancel/)
|
|
})
|
|
|
|
it('rebalances the orders for a slot', async () => {
|
|
const res = await admin.admin.order.rebalanceSlots.mutate({ slotIds: [slotId] })
|
|
expect(res).toBeTruthy()
|
|
})
|
|
})
|