755 lines
29 KiB
TypeScript
755 lines
29 KiB
TypeScript
/**
|
||
* UORD-71..108 — `user.order` reads and lifecycle (catalogue §6.11).
|
||
*
|
||
* Live HTTP/tRPC suite. Each `it()` maps to one catalogue row. Rows that pin
|
||
* current (not intended) behaviour are prefixed `(current)` with a comment.
|
||
*
|
||
* Observed contracts pinned here:
|
||
* - `getOrderById` only understands numeric ids: `ORD123` -> parseInt -> NaN ->
|
||
* INTERNAL_SERVER_ERROR "Order not found".
|
||
* - `cancelOrder` wraps every failure path in one generic
|
||
* INTERNAL_SERVER_ERROR "failed to cancel order", including already-cancelled,
|
||
* delivered and other-user orders.
|
||
* - `updateUserNotes` surfaces its own ApiError codes (BAD_REQUEST / NOT_FOUND).
|
||
*/
|
||
|
||
import { apiClient, expectTrpcError, sleep, uniqueSuffix } from '../helpers/api'
|
||
import { staffClient } from '../helpers/sessions'
|
||
import {
|
||
createAddress,
|
||
createCatalogOrderFixture,
|
||
createFutureSlot,
|
||
createProduct,
|
||
createStore,
|
||
placeOrder,
|
||
registerThrowawayUser,
|
||
} from '../helpers/fixtures'
|
||
|
||
jest.setTimeout(180_000)
|
||
|
||
const REGION_1 = 1
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* Local helpers */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
function placeCall(token: string, o: Record<string, any>): Promise<any> {
|
||
const items = o.items ?? [{ skuId: o.skuId, quantity: o.quantity ?? 1, slotId: o.slotId ?? null }]
|
||
return apiClient(token).user.order.placeOrder.mutate({
|
||
selectedItems: items,
|
||
addressId: o.addressId,
|
||
regionId: o.regionId ?? REGION_1,
|
||
...(o.couponIds ? { couponIds: o.couponIds } : {}),
|
||
...(o.userNotes !== undefined ? { userNotes: o.userNotes } : {}),
|
||
...(o.isFlashDelivery !== undefined ? { isFlashDelivery: o.isFlashDelivery } : {}),
|
||
} as any) as Promise<any>
|
||
}
|
||
|
||
async function detail(token: string, orderId: number): Promise<any> {
|
||
return apiClient(token).user.order.getOrderById.query({ orderId: String(orderId) })
|
||
}
|
||
|
||
async function list(token: string, page = 1, pageSize = 10): Promise<any> {
|
||
return apiClient(token).user.order.getOrders.query({ page, pageSize })
|
||
}
|
||
|
||
async function makeCoupon(options: Record<string, any> = {}): Promise<any> {
|
||
const admin = await staffClient()
|
||
const code = `ULC${uniqueSuffix().replace(/[^a-z0-9]/gi, '').toUpperCase()}`
|
||
const payload: any = {
|
||
couponCode: options.couponCode ?? code,
|
||
skuIds: options.skuIds ?? [],
|
||
isApplyForAll: options.isApplyForAll ?? true,
|
||
...options,
|
||
}
|
||
if (payload.discountPercent == null && payload.flatDiscount == null) payload.flatDiscount = 50
|
||
const res: any = await admin.admin.coupon.create.mutate(payload)
|
||
return Array.isArray(res) ? res[0] : res?.coupons?.[0]
|
||
}
|
||
|
||
/** A fresh user with N orders, spaced >1s so createdAt (second precision) differs. */
|
||
async function userWithOrders(n: number, price = 199): Promise<{ token: string; userId: number; ids: number[] }> {
|
||
const store = await createStore()
|
||
const product = await createProduct({ storeId: store.storeId, price })
|
||
const slot = await createFutureSlot([product.skuId])
|
||
const user = await registerThrowawayUser()
|
||
const address = await createAddress(user.token)
|
||
const ids: number[] = []
|
||
for (let i = 0; i < n; i++) {
|
||
const res = await placeCall(user.token, { addressId: address.id, skuId: product.skuId, slotId: slot.slotId })
|
||
ids.push(Number(res.data[0].id))
|
||
if (i < n - 1) await sleep(1100)
|
||
}
|
||
return { token: user.token, userId: user.userId, ids }
|
||
}
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* 1. Reads + pagination (UORD-71..75) */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('user.order reads (UORD-71..75)', () => {
|
||
it('UORD-71: getOrders defaults to page 1 / pageSize 10', async () => {
|
||
const { token } = await userWithOrders(1)
|
||
const res = await list(token)
|
||
expect(res.pagination.page).toBe(1)
|
||
expect(res.pagination.pageSize).toBe(10)
|
||
})
|
||
|
||
it('UORD-72: page 2 returns different orders with no overlap', async () => {
|
||
const { token, ids } = await userWithOrders(3)
|
||
const page1 = await list(token, 1, 2)
|
||
const page2 = await list(token, 2, 2)
|
||
|
||
expect(page1.pagination.totalCount).toBe(3)
|
||
expect(page1.pagination.totalPages).toBe(2)
|
||
expect(page1.data).toHaveLength(2)
|
||
expect(page2.data).toHaveLength(1)
|
||
|
||
const idsPage1 = page1.data.map((o: any) => Number(o.id))
|
||
const idsPage2 = page2.data.map((o: any) => Number(o.id))
|
||
expect(idsPage1.filter((id: number) => idsPage2.includes(id))).toHaveLength(0)
|
||
expect([...idsPage1, ...idsPage2].sort()).toEqual([...ids].sort())
|
||
})
|
||
|
||
it('UORD-73: pageSize 51 is BAD_REQUEST', async () => {
|
||
const { token } = await userWithOrders(1)
|
||
await expectTrpcError(() => list(token, 1, 51), 'BAD_REQUEST')
|
||
})
|
||
|
||
it('UORD-74: page 0 is BAD_REQUEST', async () => {
|
||
const { token } = await userWithOrders(1)
|
||
await expectTrpcError(() => list(token, 0, 10), 'BAD_REQUEST')
|
||
})
|
||
|
||
it('UORD-75: a user with no orders gets empty data and a zero count', async () => {
|
||
const user = await registerThrowawayUser()
|
||
const res = await list(user.token)
|
||
expect(res.success).toBe(true)
|
||
expect(res.data).toEqual([])
|
||
expect(res.pagination.totalCount).toBe(0)
|
||
expect(res.pagination.totalPages).toBe(0)
|
||
})
|
||
})
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* 2. Status derivation (UORD-76..80) */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('user.order status derivation (UORD-76..80)', () => {
|
||
it('UORD-76: a fresh order is deliveryStatus pending / orderStatus success', async () => {
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const d = await detail(fx.user.token, Number(placed[0].id))
|
||
expect(d.deliveryStatus).toBe('pending')
|
||
expect(d.orderStatus).toBe('success')
|
||
})
|
||
|
||
it('UORD-77: packaging all items makes the order packaged', async () => {
|
||
const admin = await staffClient()
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const id = Number(placed[0].id)
|
||
|
||
await admin.admin.order.updatePackaged.mutate({ orderId: String(id), isPackaged: true })
|
||
const d = await detail(fx.user.token, id)
|
||
expect(d.deliveryStatus).toBe('packaged')
|
||
expect(d.orderStatus).toBe('success')
|
||
})
|
||
|
||
it('UORD-78: delivering an order makes it success with no cancel reason', async () => {
|
||
const admin = await staffClient()
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const id = Number(placed[0].id)
|
||
|
||
await admin.admin.order.updateDelivered.mutate({ orderId: String(id), isDelivered: true })
|
||
const d = await detail(fx.user.token, id)
|
||
expect(d.deliveryStatus).toBe('success')
|
||
expect(d.orderStatus).toBe('success')
|
||
})
|
||
|
||
it('UORD-79: an admin cancel surfaces cancelled + the reason to the user', async () => {
|
||
const admin = await staffClient()
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const id = Number(placed[0].id)
|
||
|
||
await admin.admin.order.cancelOrder.mutate({ orderId: id, reason: 'out of stock' })
|
||
const d = await detail(fx.user.token, id)
|
||
expect(d.deliveryStatus).toBe('cancelled')
|
||
expect(d.orderStatus).toBe('cancelled')
|
||
expect(d.cancelReason).toBe('out of stock')
|
||
})
|
||
|
||
it('UORD-80: with mixed item packaging the order stays pending until all items are packaged', async () => {
|
||
const admin = await staffClient()
|
||
const store = await createStore()
|
||
const a = await createProduct({ storeId: store.storeId, price: 150 })
|
||
const b = await createProduct({ storeId: store.storeId, price: 250 })
|
||
const slot = await createFutureSlot([a.skuId, b.skuId])
|
||
const user = await registerThrowawayUser()
|
||
const address = await createAddress(user.token)
|
||
|
||
const res = await placeCall(user.token, {
|
||
addressId: address.id,
|
||
items: [
|
||
{ skuId: a.skuId, quantity: 1, slotId: slot.slotId },
|
||
{ skuId: b.skuId, quantity: 1, slotId: slot.slotId },
|
||
],
|
||
})
|
||
const id = Number(res.data[0].id)
|
||
const adminDetails: any = await admin.admin.order.getOrderDetails.query({ orderId: id })
|
||
const itemIds = adminDetails.items.map((i: any) => i.id)
|
||
expect(itemIds).toHaveLength(2)
|
||
|
||
await admin.admin.order.updateOrderItemPackaging.mutate({ orderItemId: itemIds[0], isPackaged: true })
|
||
const afterOne = await detail(user.token, id)
|
||
expect(afterOne.deliveryStatus).toBe('pending')
|
||
|
||
await admin.admin.order.updateOrderItemPackaging.mutate({ orderItemId: itemIds[1], isPackaged: true })
|
||
const afterBoth = await detail(user.token, id)
|
||
expect(afterBoth.deliveryStatus).toBe('packaged')
|
||
})
|
||
})
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* 3. Detail shape (UORD-81..92) */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('user.order detail shape (UORD-81..92)', () => {
|
||
it('UORD-81: order items expose name/skuName/quantity/unit/price/discountedPrice/amount/image', async () => {
|
||
const fx = await createCatalogOrderFixture({ price: 199 })
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
quantity: 3,
|
||
})
|
||
const d = await detail(fx.user.token, Number(placed[0].id))
|
||
expect(d.items[0]).toEqual(
|
||
expect.objectContaining({
|
||
productName: expect.any(String),
|
||
skuName: expect.anything(),
|
||
quantity: 3,
|
||
unitNotation: expect.anything(),
|
||
price: 199,
|
||
discountedPrice: 199,
|
||
amount: 199 * 3,
|
||
image: null,
|
||
})
|
||
)
|
||
// The returned amount is the line total, not the unit price.
|
||
expect(d.items[0].amount).toBe(d.items[0].price * d.items[0].quantity)
|
||
})
|
||
|
||
it('UORD-82: the order list only contains the caller’s own orders', async () => {
|
||
const mine = await userWithOrders(1)
|
||
const theirs = await userWithOrders(1)
|
||
const res = await list(mine.token)
|
||
expect(res.data.every((o: any) => Number(o.id) === mine.ids[0])).toBe(true)
|
||
expect(res.data.some((o: any) => Number(o.id) === theirs.ids[0])).toBe(false)
|
||
})
|
||
|
||
it('UORD-83: region fields come from the order’s region', async () => {
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const d = await detail(fx.user.token, Number(placed[0].id))
|
||
expect(d.regionId).toBe(REGION_1)
|
||
expect(d.regionName).toEqual(expect.any(String))
|
||
})
|
||
|
||
it('UORD-84: a flash order reports isFlashDelivery', async () => {
|
||
const store = await createStore()
|
||
const product = await createProduct({
|
||
storeId: store.storeId,
|
||
price: 199,
|
||
isFlashAvailable: true,
|
||
flashPrice: 149,
|
||
})
|
||
const user = await registerThrowawayUser()
|
||
const address = await createAddress(user.token)
|
||
const res = await placeCall(user.token, {
|
||
addressId: address.id,
|
||
skuId: product.skuId,
|
||
slotId: null,
|
||
isFlashDelivery: true,
|
||
})
|
||
const d = await detail(user.token, Number(res.data[0].id))
|
||
expect(d.isFlashDelivery).toBe(true)
|
||
})
|
||
|
||
it('UORD-85: getOrderById with a numeric string returns the matching detail', async () => {
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const id = Number(placed[0].id)
|
||
const d = await detail(fx.user.token, id)
|
||
expect(d.id).toBe(id)
|
||
expect(d.orderId).toBe(`ORD${id}`)
|
||
})
|
||
|
||
it('UORD-86 (current): ORD123 / ORD-123 fail with INTERNAL_SERVER_ERROR (parseInt -> NaN)', async () => {
|
||
const fx = await createCatalogOrderFixture()
|
||
await expectTrpcError(
|
||
() => apiClient(fx.user.token).user.order.getOrderById.query({ orderId: 'ORD123' }),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'Order not found'
|
||
)
|
||
await expectTrpcError(
|
||
() => apiClient(fx.user.token).user.order.getOrderById.query({ orderId: 'ORD-123' }),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'Order not found'
|
||
)
|
||
})
|
||
|
||
it('UORD-87: an unknown order id is not found', async () => {
|
||
const fx = await createCatalogOrderFixture()
|
||
await expectTrpcError(
|
||
() => apiClient(fx.user.token).user.order.getOrderById.query({ orderId: '999999999' }),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'Order not found'
|
||
)
|
||
})
|
||
|
||
it("UORD-88: another user's order is not found", async () => {
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const other = await registerThrowawayUser()
|
||
await expectTrpcError(
|
||
() => apiClient(other.token).user.order.getOrderById.query({ orderId: String(placed[0].id) }),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'Order not found'
|
||
)
|
||
})
|
||
|
||
it('UORD-89: a coupon order exposes the code and the stored discount', async () => {
|
||
const fx = await createCatalogOrderFixture({ price: 199 })
|
||
const c = await makeCoupon({ flatDiscount: 40, minOrder: 1 })
|
||
const res = await placeCall(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
couponIds: [c.id],
|
||
})
|
||
const d = await detail(fx.user.token, Number(res.data[0].id))
|
||
expect(d.couponCode).toBe(c.couponCode)
|
||
expect(d.discountAmount).toBe(40)
|
||
})
|
||
|
||
it('UORD-90: detail totals expose orderAmount/totalAmount/deliveryCharge', async () => {
|
||
const fx = await createCatalogOrderFixture({ price: 199 })
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const d = await detail(fx.user.token, Number(placed[0].id))
|
||
expect(d.orderAmount).toBe(199 + 20)
|
||
expect(d.totalAmount).toBe(199 + 20)
|
||
expect(d.deliveryCharge).toBe(20)
|
||
})
|
||
|
||
it('UORD-91: a cancelled detail carries the cancelReason', async () => {
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const id = Number(placed[0].id)
|
||
await apiClient(fx.user.token).user.order.cancelOrder.mutate({ id, reason: 'changed my mind' })
|
||
|
||
const d = await detail(fx.user.token, id)
|
||
expect(d.cancelReason).toBe('changed my mind')
|
||
expect(d.deliveryStatus).toBe('cancelled')
|
||
})
|
||
|
||
it('UORD-92: a flash detail reports isFlashDelivery', async () => {
|
||
const store = await createStore()
|
||
const product = await createProduct({
|
||
storeId: store.storeId,
|
||
price: 199,
|
||
isFlashAvailable: true,
|
||
flashPrice: 149,
|
||
})
|
||
const user = await registerThrowawayUser()
|
||
const address = await createAddress(user.token)
|
||
const res = await placeCall(user.token, {
|
||
addressId: address.id,
|
||
skuId: product.skuId,
|
||
slotId: null,
|
||
isFlashDelivery: true,
|
||
})
|
||
const d = await detail(user.token, Number(res.data[0].id))
|
||
expect(d.isFlashDelivery).toBe(true)
|
||
expect(d.deliveryStatus).toBe('pending')
|
||
})
|
||
})
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* 4. Notes + cancellation (UORD-93..103) */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('user.order updateUserNotes (UORD-93..97)', () => {
|
||
it('UORD-93: notes on a pending order are persisted', async () => {
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const id = Number(placed[0].id)
|
||
const note = `call before delivery ${uniqueSuffix()}`
|
||
const res: any = await apiClient(fx.user.token).user.order.updateUserNotes.mutate({ id, userNotes: note })
|
||
expect(res.success).toBe(true)
|
||
const d = await detail(fx.user.token, id)
|
||
expect(d.userNotes).toBe(note)
|
||
})
|
||
|
||
it('UORD-94: notes on a delivered order are BAD_REQUEST', async () => {
|
||
const admin = await staffClient()
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const id = Number(placed[0].id)
|
||
await admin.admin.order.updateDelivered.mutate({ orderId: String(id), isDelivered: true })
|
||
|
||
await expectTrpcError(
|
||
() => apiClient(fx.user.token).user.order.updateUserNotes.mutate({ id, userNotes: 'x' }),
|
||
'BAD_REQUEST',
|
||
'delivered'
|
||
)
|
||
})
|
||
|
||
it('UORD-95: notes on a cancelled order are BAD_REQUEST', async () => {
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const id = Number(placed[0].id)
|
||
await apiClient(fx.user.token).user.order.cancelOrder.mutate({ id, reason: 'x' })
|
||
|
||
await expectTrpcError(
|
||
() => apiClient(fx.user.token).user.order.updateUserNotes.mutate({ id, userNotes: 'y' }),
|
||
'BAD_REQUEST',
|
||
'cancelled'
|
||
)
|
||
})
|
||
|
||
it("UORD-96: notes on another user's order are NOT_FOUND", async () => {
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const other = await registerThrowawayUser()
|
||
await expectTrpcError(
|
||
() =>
|
||
apiClient(other.token).user.order.updateUserNotes.mutate({ id: Number(placed[0].id), userNotes: 'x' }),
|
||
'NOT_FOUND'
|
||
)
|
||
})
|
||
|
||
it('UORD-97: empty notes are accepted (stored as null)', async () => {
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const id = Number(placed[0].id)
|
||
const res: any = await apiClient(fx.user.token).user.order.updateUserNotes.mutate({ id, userNotes: '' })
|
||
expect(res.success).toBe(true)
|
||
const d = await detail(fx.user.token, id)
|
||
expect(d.userNotes).toBeNull()
|
||
})
|
||
})
|
||
|
||
describe('user.order cancelOrder (UORD-98..103, 107)', () => {
|
||
it('UORD-98: cancelling a pending own order succeeds with the reason stored', async () => {
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const id = Number(placed[0].id)
|
||
const res: any = await apiClient(fx.user.token).user.order.cancelOrder.mutate({ id, reason: 'ordered by mistake' })
|
||
expect(res.success).toBe(true)
|
||
|
||
const d = await detail(fx.user.token, id)
|
||
expect(d.deliveryStatus).toBe('cancelled')
|
||
expect(d.cancelReason).toBe('ordered by mistake')
|
||
})
|
||
|
||
it('UORD-99: cancelling without / with a blank reason is BAD_REQUEST', async () => {
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const id = Number(placed[0].id)
|
||
await expectTrpcError(
|
||
() => apiClient(fx.user.token).user.order.cancelOrder.mutate({ id, reason: '' }),
|
||
'BAD_REQUEST'
|
||
)
|
||
await expectTrpcError(
|
||
() => apiClient(fx.user.token).user.order.cancelOrder.mutate({ id } as any),
|
||
'BAD_REQUEST'
|
||
)
|
||
})
|
||
|
||
it('UORD-100 (current): cancelling a missing order is a generic INTERNAL_SERVER_ERROR', async () => {
|
||
const fx = await createCatalogOrderFixture()
|
||
await expectTrpcError(
|
||
() => apiClient(fx.user.token).user.order.cancelOrder.mutate({ id: 999_999_999, reason: 'x' }),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'failed to cancel order'
|
||
)
|
||
})
|
||
|
||
it("UORD-101 (current): cancelling another user's order is a generic INTERNAL_SERVER_ERROR", async () => {
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const other = await registerThrowawayUser()
|
||
await expectTrpcError(
|
||
() =>
|
||
apiClient(other.token).user.order.cancelOrder.mutate({ id: Number(placed[0].id), reason: 'x' }),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'failed to cancel order'
|
||
)
|
||
// untouched
|
||
const d = await detail(fx.user.token, Number(placed[0].id))
|
||
expect(d.deliveryStatus).toBe('pending')
|
||
})
|
||
|
||
it('UORD-102 (current): cancelling an already-cancelled order is a generic INTERNAL_SERVER_ERROR', async () => {
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const id = Number(placed[0].id)
|
||
await apiClient(fx.user.token).user.order.cancelOrder.mutate({ id, reason: 'first' })
|
||
|
||
await expectTrpcError(
|
||
() => apiClient(fx.user.token).user.order.cancelOrder.mutate({ id, reason: 'second' }),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'failed to cancel order'
|
||
)
|
||
})
|
||
|
||
it('UORD-103 (current): cancelling a delivered order is a generic INTERNAL_SERVER_ERROR', async () => {
|
||
const admin = await staffClient()
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const id = Number(placed[0].id)
|
||
await admin.admin.order.updateDelivered.mutate({ orderId: String(id), isDelivered: true })
|
||
|
||
await expectTrpcError(
|
||
() => apiClient(fx.user.token).user.order.cancelOrder.mutate({ id, reason: 'x' }),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'failed to cancel order'
|
||
)
|
||
})
|
||
|
||
it('UORD-107: a double-cancel race has exactly one success and leaves a clean cancelled status', async () => {
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const id = Number(placed[0].id)
|
||
|
||
const results = await Promise.allSettled([
|
||
apiClient(fx.user.token).user.order.cancelOrder.mutate({ id, reason: 'a' }),
|
||
apiClient(fx.user.token).user.order.cancelOrder.mutate({ id, reason: 'b' }),
|
||
])
|
||
const fulfilled = results.filter((r) => r.status === 'fulfilled')
|
||
const rejected = results.filter((r) => r.status === 'rejected')
|
||
expect(fulfilled).toHaveLength(1)
|
||
expect(rejected).toHaveLength(1)
|
||
|
||
const d = await detail(fx.user.token, id)
|
||
expect(d.deliveryStatus).toBe('cancelled')
|
||
expect(['a', 'b']).toContain(d.cancelReason)
|
||
})
|
||
})
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* 5. Cancellation side-effects (UORD-104..106, 108, 108B) */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('user.order cancellation side-effects (UORD-104..106, 108, 108B)', () => {
|
||
it('UORD-104: cancelling releases the coupon usage so it can be used again', async () => {
|
||
const fx = await createCatalogOrderFixture({ price: 199 })
|
||
const c = await makeCoupon({ flatDiscount: 20, minOrder: 1, maxLimitForUser: 1 })
|
||
|
||
const first = await placeCall(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
couponIds: [c.id],
|
||
})
|
||
expect(Number(first.data[0].discountAmount)).toBe(20)
|
||
|
||
await apiClient(fx.user.token).user.order.cancelOrder.mutate({ id: Number(first.data[0].id), reason: 'release' })
|
||
|
||
// Re-usable: with the usage row released the same coupon applies again.
|
||
const second = await placeCall(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
couponIds: [c.id],
|
||
})
|
||
expect(second.success).toBe(true)
|
||
expect(Number(second.data[0].discountAmount)).toBe(20)
|
||
})
|
||
|
||
it('UORD-105: cancelling the first order makes the user first-order eligible again', async () => {
|
||
const fx = await createCatalogOrderFixture({ price: 199 })
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
await apiClient(fx.user.token).user.order.cancelOrder.mutate({ id: Number(placed[0].id), reason: 'x' })
|
||
|
||
const eligible: any = await apiClient(fx.user.token).user.coupon.getEligible.query({ regionId: REGION_1 })
|
||
expect(eligible.isFirstOrder).toBe(true)
|
||
})
|
||
|
||
it('UORD-106 (current): a cancellation persists a cancelled status (publish is best-effort)', async () => {
|
||
// publishCancellation is awaited; its failure is not separately observable, so
|
||
// the pinned contract is: cancel success implies a persisted cancelled status.
|
||
const fx = await createCatalogOrderFixture()
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const id = Number(placed[0].id)
|
||
const res: any = await apiClient(fx.user.token).user.order.cancelOrder.mutate({ id, reason: 'publish check' })
|
||
expect(res.success).toBe(true)
|
||
const d = await detail(fx.user.token, id)
|
||
expect(d.deliveryStatus).toBe('cancelled')
|
||
expect(d.cancelReason).toBe('publish check')
|
||
})
|
||
|
||
it('UORD-108: cancelling a grouped order updates each sub-order consistently', async () => {
|
||
const fx = await createCatalogOrderFixture({ price: 199 })
|
||
const slot2 = await createFutureSlot([fx.product.skuId])
|
||
const c = await makeCoupon({ flatDiscount: 50, minOrder: 1 })
|
||
const res = await placeCall(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
items: [
|
||
{ skuId: fx.product.skuId, quantity: 1, slotId: fx.slot.slotId },
|
||
{ skuId: fx.product.skuId, quantity: 1, slotId: slot2.slotId },
|
||
],
|
||
couponIds: [c.id],
|
||
})
|
||
expect(res.data).toHaveLength(2)
|
||
const ids = res.data.map((o: any) => Number(o.id))
|
||
|
||
// Cancel one: only that sibling is cancelled, the coupon still resolves via the group.
|
||
await apiClient(fx.user.token).user.order.cancelOrder.mutate({ id: ids[0], reason: 'first' })
|
||
const d0 = await detail(fx.user.token, ids[0])
|
||
const d1 = await detail(fx.user.token, ids[1])
|
||
expect(d0.deliveryStatus).toBe('cancelled')
|
||
expect(d1.deliveryStatus).toBe('pending')
|
||
expect(d1.couponCode).toBe(c.couponCode)
|
||
|
||
// Cancel the sibling: both cancelled and the group coupon usage is released.
|
||
await apiClient(fx.user.token).user.order.cancelOrder.mutate({ id: ids[1], reason: 'second' })
|
||
const e0 = await detail(fx.user.token, ids[0])
|
||
const e1 = await detail(fx.user.token, ids[1])
|
||
expect(e0.deliveryStatus).toBe('cancelled')
|
||
expect(e1.deliveryStatus).toBe('cancelled')
|
||
expect(e0.couponCode).toBeNull()
|
||
expect(e1.couponCode).toBeNull()
|
||
})
|
||
|
||
it('UORD-108B: a grouped coupon is restored only after every sibling is cancelled', async () => {
|
||
const fx = await createCatalogOrderFixture({ price: 199 })
|
||
const slot2 = await createFutureSlot([fx.product.skuId])
|
||
const c = await makeCoupon({ flatDiscount: 50, minOrder: 1, maxLimitForUser: 1 })
|
||
const res = await placeCall(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
items: [
|
||
{ skuId: fx.product.skuId, quantity: 1, slotId: fx.slot.slotId },
|
||
{ skuId: fx.product.skuId, quantity: 1, slotId: slot2.slotId },
|
||
],
|
||
couponIds: [c.id],
|
||
})
|
||
const ids = res.data.map((o: any) => Number(o.id))
|
||
|
||
// Cancel one sibling: the group still has a live order, so the coupon stays used.
|
||
await apiClient(fx.user.token).user.order.cancelOrder.mutate({ id: ids[0], reason: 'first' })
|
||
await expectTrpcError(
|
||
() =>
|
||
placeCall(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
couponIds: [c.id],
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'usage limit exceeded'
|
||
)
|
||
|
||
// Cancel the last sibling: the group is fully cancelled, so the usage is released.
|
||
await apiClient(fx.user.token).user.order.cancelOrder.mutate({ id: ids[1], reason: 'second' })
|
||
const after = await placeCall(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
couponIds: [c.id],
|
||
})
|
||
expect(after.success).toBe(true)
|
||
expect(Number(after.data[0].discountAmount)).toBe(50)
|
||
})
|
||
})
|