1510 lines
54 KiB
TypeScript
1510 lines
54 KiB
TypeScript
/**
|
|
* UORD-01..70 — `user.order.placeOrder` (catalogue §6.11).
|
|
*
|
|
* Live HTTP/tRPC suite against the running backend. Every `it()` maps to one
|
|
* catalogue row. Where the implementation diverges from the catalogue's intent
|
|
* the row is prefixed `(current)` and the comment records the observed contract.
|
|
*
|
|
* Notable observed contracts (do not "fix" here):
|
|
* - The order's delivery address coordinates are authoritative for the region;
|
|
* a client `regionId`/`boundId` cannot move the order.
|
|
* - Coupon validation errors are thrown as raw `Error`s -> INTERNAL_SERVER_ERROR,
|
|
* not BAD_REQUEST (see `validateAndGetCoupons`).
|
|
* - There is no stock reservation/price re-check between validation and write,
|
|
* and no idempotency key.
|
|
*/
|
|
|
|
import { apiClient, expectTrpcError, uniqueSuffix } from '../helpers/api'
|
|
import { staffClient, superadminClient } from '../helpers/sessions'
|
|
import {
|
|
COORDS,
|
|
createAddress,
|
|
createFutureSlot,
|
|
createProduct,
|
|
createStore,
|
|
placeOrder,
|
|
registerThrowawayUser,
|
|
withBoundSetting,
|
|
} from '../helpers/fixtures'
|
|
import { TEST_BOUND_ID, TEST_OTHER_REGION_ID } from '../helpers/env'
|
|
|
|
jest.setTimeout(180_000)
|
|
|
|
const REGION_1 = 1
|
|
const OTHER_BOUND = 3
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* Local helpers */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
interface Item {
|
|
skuId: number
|
|
quantity: number
|
|
slotId: number | null
|
|
}
|
|
|
|
interface PlaceOpts {
|
|
addressId: number
|
|
items?: Item[]
|
|
skuId?: number
|
|
quantity?: number
|
|
slotId?: number | null
|
|
regionId?: number
|
|
couponIds?: number[]
|
|
userNotes?: string
|
|
isFlashDelivery?: boolean
|
|
boundId?: number
|
|
}
|
|
|
|
/** Direct placeOrder call (edge shapes the shared fixture cannot express). */
|
|
function placeCall(token: string, o: PlaceOpts): Promise<any> {
|
|
const items = o.items ?? [{ skuId: o.skuId as number, 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 } : {}),
|
|
...(o.boundId ? { boundId: o.boundId } : {}),
|
|
} as any) as Promise<any>
|
|
}
|
|
|
|
async function detail(token: string, orderId: number): Promise<any> {
|
|
return apiClient(token).user.order.getOrderById.query({ orderId: String(orderId) })
|
|
}
|
|
|
|
/** Create a coupon in the staff (region 1) region, only sending provided fields. */
|
|
async function makeCoupon(options: Record<string, any> = {}): Promise<any> {
|
|
const admin = await staffClient()
|
|
const code = `UORD${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 product + future slot + matching skuId (region 1). */
|
|
async function productWithSlot(
|
|
price = 199,
|
|
opts: Record<string, any> = {}
|
|
): Promise<{ skuId: number; slotId: number; productId: number }> {
|
|
const store = await createStore()
|
|
const product = await createProduct({ storeId: store.storeId, price, ...opts })
|
|
const slot = await createFutureSlot([product.skuId])
|
|
return { skuId: product.skuId, slotId: slot.slotId, productId: product.productId }
|
|
}
|
|
|
|
/** A region-2 staff + store + product + slot, built once per process. */
|
|
let region2Promise: Promise<{ token: string; skuId: number; slotId: number }> | null = null
|
|
function region2Catalog() {
|
|
region2Promise ??= (async () => {
|
|
const sa = await superadminClient()
|
|
const password = 'FreshyoTest!2026'
|
|
const name = `e2e_r2_${uniqueSuffix().replace(/[^a-z0-9]/gi, '')}`
|
|
await sa.superadmin.staff.create.mutate({ name, password, roleId: 2, regionId: TEST_OTHER_REGION_ID })
|
|
const login: any = await apiClient().admin.staffUser.login.mutate({ name, password })
|
|
const client = apiClient(login.token)
|
|
const storeRes: any = await client.admin.store.createStore.mutate({
|
|
name: `R2 Store ${uniqueSuffix()}`,
|
|
description: 'region-2 isolation fixture',
|
|
})
|
|
const storeId = Number(storeRes.store.id)
|
|
const prodRes: any = await client.admin.product.createProduct.mutate({
|
|
name: `R2 Product ${uniqueSuffix()}`,
|
|
shortDescription: 'region-2 isolation fixture',
|
|
storeId,
|
|
productType: 'item',
|
|
skus: [
|
|
{
|
|
name: '1pc',
|
|
price: 250,
|
|
marketPrice: 300,
|
|
images: [],
|
|
isFlashAvailable: false,
|
|
flashPrice: null,
|
|
isOutOfStock: false,
|
|
isOffer: false,
|
|
isComboOnly: false,
|
|
isSuspended: false,
|
|
features: [{ featureName: 'quantity', featureValue: '1pc' }],
|
|
},
|
|
],
|
|
})
|
|
const skuId = Number(prodRes.product.skus[0].id)
|
|
const slotRes: any = await client.admin.slots.createSlot.mutate({
|
|
deliveryTime: new Date(Date.now() + 3 * 3_600_000).toISOString(),
|
|
freezeTime: new Date(Date.now() + 2 * 3_600_000).toISOString(),
|
|
isActive: true,
|
|
skuIds: [skuId],
|
|
})
|
|
return { token: login.token, skuId, slotId: Number(slotRes.slot.id) }
|
|
})()
|
|
return region2Promise
|
|
}
|
|
|
|
async function suspendUser(userId: number, isSuspended: boolean): Promise<void> {
|
|
const admin = await staffClient()
|
|
await admin.admin.user.updateUserSuspension.mutate({ userId, isSuspended })
|
|
}
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* 1. Guards + input validation (UORD-01..24) */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* Shared catalog fixture */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
* `createFutureSlot` and `createStore` are the expensive fixture calls (~1 s
|
|
* each: the slot create rebuilds the whole region slots file and uploads it to
|
|
* S3). Most tests only need "a valid SKU + a slot that carries it + an address",
|
|
* so build one store and one slot containing a few priced SKUs in `beforeAll`
|
|
* and reuse them. Each test still gets a fresh user + address (order history,
|
|
* first-order and coupon usage are per-user).
|
|
*
|
|
* Tests that mutate the product or the slot opt into `isolatedProduct` /
|
|
* `isolatedSlot` and get their own.
|
|
*/
|
|
const SHARED_PRICES = [199, 349, 320, 250]
|
|
let sharedStore: any
|
|
let sharedSlot: any
|
|
const sharedProducts = new Map<number, any>()
|
|
|
|
beforeAll(async () => {
|
|
sharedStore = await createStore({ name: `E2E UORD Shared Store ${uniqueSuffix()}` })
|
|
const skuIds: number[] = []
|
|
for (const price of SHARED_PRICES) {
|
|
const product = await createProduct({ storeId: sharedStore.storeId, price })
|
|
sharedProducts.set(price, product)
|
|
skuIds.push(product.skuId)
|
|
}
|
|
sharedSlot = await createFutureSlot(skuIds)
|
|
}, 180_000)
|
|
|
|
async function createCatalogOrderFixture(
|
|
options: { price?: number; isolatedProduct?: boolean; isolatedSlot?: boolean } = {}
|
|
): Promise<any> {
|
|
const price = options.price ?? 199
|
|
const user = await registerThrowawayUser()
|
|
const address = await createAddress(user.token)
|
|
|
|
let product = sharedProducts.get(price)
|
|
const needsOwnProduct = options.isolatedProduct || !product
|
|
if (needsOwnProduct) product = await createProduct({ storeId: sharedStore.storeId, price })
|
|
|
|
// A slot only carries the SKUs it was created with, so a fresh product needs a
|
|
// fresh slot too. `isolatedSlot` keeps the shared products but gets a new slot.
|
|
const slot =
|
|
options.isolatedSlot || needsOwnProduct
|
|
? await createFutureSlot([product.skuId])
|
|
: sharedSlot
|
|
|
|
return { user, store: sharedStore, product, slot, address }
|
|
}
|
|
|
|
describe('user.order.placeOrder — guards + input (UORD-01..24)', () => {
|
|
it('UORD-01: one SKU / one slot / COD creates one order with persisted items', async () => {
|
|
const fx = await createCatalogOrderFixture({ price: 349 })
|
|
const placed = await placeOrder(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
})
|
|
expect(placed).toHaveLength(1)
|
|
|
|
const d = await detail(fx.user.token, Number(placed[0].id))
|
|
expect(d.items).toHaveLength(1)
|
|
expect(d.items[0].quantity).toBe(1)
|
|
expect(d.totalAmount).toBeGreaterThan(0)
|
|
expect(d.orderStatus).toBe('success')
|
|
})
|
|
|
|
it('UORD-02: no token is UNAUTHORIZED', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
await expectTrpcError(
|
|
() =>
|
|
apiClient().user.order.placeOrder.mutate({
|
|
selectedItems: [{ skuId: fx.product.skuId, quantity: 1, slotId: fx.slot.slotId }],
|
|
addressId: fx.address.id,
|
|
regionId: REGION_1,
|
|
} as any),
|
|
'UNAUTHORIZED'
|
|
)
|
|
})
|
|
|
|
it('UORD-03: a suspended user is rejected with 403 before the order is written', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
await suspendUser(fx.user.userId, true)
|
|
try {
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
}),
|
|
'FORBIDDEN',
|
|
'Unable to place order'
|
|
)
|
|
} finally {
|
|
await suspendUser(fx.user.userId, false)
|
|
}
|
|
})
|
|
|
|
it('UORD-04 (current): empty selectedItems is an INTERNAL_SERVER_ERROR, never success', async () => {
|
|
// The schema allows an empty array; placeOrderTransaction then calls
|
|
// drizzle `.values([])` -> "values() must be called with at least one value".
|
|
const fx = await createCatalogOrderFixture()
|
|
await expectTrpcError(
|
|
() => placeCall(fx.user.token, { addressId: fx.address.id, items: [] }),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'at least one value'
|
|
)
|
|
})
|
|
|
|
it('UORD-05: quantity 0 is BAD_REQUEST', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
quantity: 0,
|
|
slotId: fx.slot.slotId,
|
|
}),
|
|
'BAD_REQUEST'
|
|
)
|
|
})
|
|
|
|
it('UORD-06: a negative quantity is BAD_REQUEST', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
quantity: -1,
|
|
slotId: fx.slot.slotId,
|
|
}),
|
|
'BAD_REQUEST'
|
|
)
|
|
})
|
|
|
|
it('UORD-07 (current): a huge quantity is accepted — there is no quantity cap', async () => {
|
|
const fx = await createCatalogOrderFixture({ price: 199 })
|
|
const res = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
quantity: 1_000_000_000,
|
|
slotId: fx.slot.slotId,
|
|
})
|
|
expect(res.success).toBe(true)
|
|
expect(Number(res.data[0].totalAmount)).toBe(199 * 1_000_000_000)
|
|
})
|
|
|
|
it('UORD-08: an unknown SKU is rejected as not found', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: 999_999_999,
|
|
slotId: fx.slot.slotId,
|
|
}),
|
|
'BAD_REQUEST',
|
|
'not found'
|
|
)
|
|
})
|
|
|
|
it('UORD-09: a SKU from another region is rejected with CONFLICT', async () => {
|
|
const r2 = await region2Catalog()
|
|
const fx = await createCatalogOrderFixture()
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: r2.skuId,
|
|
slotId: fx.slot.slotId,
|
|
}),
|
|
'CONFLICT',
|
|
'not available in this region'
|
|
)
|
|
})
|
|
|
|
it('UORD-10: a suspended SKU is rejected as unavailable', async () => {
|
|
const store = await createStore()
|
|
const product = await createProduct({ storeId: store.storeId, price: 199, isSuspended: true })
|
|
const slot = await createFutureSlot([product.skuId])
|
|
const user = await registerThrowawayUser()
|
|
const address = await createAddress(user.token)
|
|
|
|
await expectTrpcError(
|
|
() => placeCall(user.token, { addressId: address.id, skuId: product.skuId, slotId: slot.slotId }),
|
|
'BAD_REQUEST',
|
|
'unavailable'
|
|
)
|
|
})
|
|
|
|
it('UORD-11: an out-of-stock SKU is rejected as unavailable', async () => {
|
|
const store = await createStore()
|
|
const product = await createProduct({ storeId: store.storeId, price: 199, isOutOfStock: true })
|
|
const slot = await createFutureSlot([product.skuId])
|
|
const user = await registerThrowawayUser()
|
|
const address = await createAddress(user.token)
|
|
|
|
await expectTrpcError(
|
|
() => placeCall(user.token, { addressId: address.id, skuId: product.skuId, slotId: slot.slotId }),
|
|
'BAD_REQUEST',
|
|
'unavailable'
|
|
)
|
|
})
|
|
|
|
it('UORD-12: a missing address id is rejected with Invalid address', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: 999_999_999,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
}),
|
|
'BAD_REQUEST',
|
|
'Invalid address'
|
|
)
|
|
})
|
|
|
|
it("UORD-13: another user's address is rejected with Invalid address", async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
const other = await registerThrowawayUser()
|
|
const otherAddress = await createAddress(other.token)
|
|
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: otherAddress.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
}),
|
|
'BAD_REQUEST',
|
|
'Invalid address'
|
|
)
|
|
})
|
|
|
|
it('UORD-14 (current): an out-of-region address cannot be created, so placeOrder never sees one', async () => {
|
|
// createAddress validates coordinates against the region and returns 422,
|
|
// and no API can persist an out-of-region latitude on an address, so the
|
|
// "coords outside region" branch is unreachable from the public API.
|
|
const user = await registerThrowawayUser()
|
|
await expectTrpcError(
|
|
() =>
|
|
apiClient(user.token).user.address.createAddress.mutate({
|
|
regionId: REGION_1,
|
|
name: 'Out of region',
|
|
phone: '9876543210',
|
|
addressLine1: `Out ${uniqueSuffix()}`,
|
|
city: 'Mahabubnagar',
|
|
state: 'Telangana',
|
|
isDefault: true,
|
|
latitude: 0,
|
|
longitude: 0,
|
|
} as any),
|
|
'UNPROCESSABLE_CONTENT',
|
|
'outside this region'
|
|
)
|
|
})
|
|
|
|
it('UORD-15: the address coordinates, not the client regionId, decide the region', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
const res = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
regionId: TEST_OTHER_REGION_ID,
|
|
})
|
|
expect(res.success).toBe(true)
|
|
expect(Number(res.data[0].regionId)).toBe(REGION_1)
|
|
})
|
|
|
|
it('UORD-16: an address without coordinates falls back to its stored region', async () => {
|
|
const store = await createStore()
|
|
const product = await createProduct({ storeId: store.storeId, price: 199 })
|
|
const slot = await createFutureSlot([product.skuId])
|
|
const user = await registerThrowawayUser()
|
|
const address = await apiClient(user.token).user.address.createAddress.mutate({
|
|
regionId: REGION_1,
|
|
name: 'No coords',
|
|
phone: '9876543210',
|
|
addressLine1: `NoCoords ${uniqueSuffix()}`,
|
|
city: 'Mahabubnagar',
|
|
state: 'Telangana',
|
|
isDefault: true,
|
|
} as any)
|
|
const addressId = Number((address as any).data.id)
|
|
|
|
const res = await placeCall(user.token, {
|
|
addressId,
|
|
skuId: product.skuId,
|
|
slotId: slot.slotId,
|
|
regionId: TEST_OTHER_REGION_ID,
|
|
})
|
|
expect(res.success).toBe(true)
|
|
expect(Number(res.data[0].regionId)).toBe(REGION_1)
|
|
})
|
|
|
|
it('UORD-17 (current): a client-supplied boundId is ignored — the address-derived bound wins', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
const res = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
boundId: OTHER_BOUND,
|
|
})
|
|
expect(res.success).toBe(true)
|
|
expect(Number(res.data[0].regionId)).toBe(REGION_1)
|
|
})
|
|
|
|
it('UORD-18: a missing regionId is BAD_REQUEST', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
await expectTrpcError(
|
|
() =>
|
|
apiClient(fx.user.token).user.order.placeOrder.mutate({
|
|
selectedItems: [{ skuId: fx.product.skuId, quantity: 1, slotId: fx.slot.slotId }],
|
|
addressId: fx.address.id,
|
|
} as any),
|
|
'BAD_REQUEST'
|
|
)
|
|
})
|
|
|
|
it('UORD-19: an invalid regionId is BAD_REQUEST', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
await expectTrpcError(
|
|
() => placeCall(fx.user.token, { addressId: fx.address.id, skuId: fx.product.skuId, slotId: fx.slot.slotId, regionId: 0 }),
|
|
'BAD_REQUEST'
|
|
)
|
|
await expectTrpcError(
|
|
() => placeCall(fx.user.token, { addressId: fx.address.id, skuId: fx.product.skuId, slotId: fx.slot.slotId, regionId: -1 }),
|
|
'BAD_REQUEST'
|
|
)
|
|
})
|
|
|
|
it('UORD-20: a regular item with a null slotId is rejected', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
await expectTrpcError(
|
|
() => placeCall(fx.user.token, { addressId: fx.address.id, skuId: fx.product.skuId, slotId: null }),
|
|
'BAD_REQUEST',
|
|
'delivery slot is required'
|
|
)
|
|
})
|
|
|
|
it('UORD-21: a slot from another region is rejected with CONFLICT', async () => {
|
|
const r2 = await region2Catalog()
|
|
const fx = await createCatalogOrderFixture()
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: r2.slotId,
|
|
}),
|
|
'CONFLICT',
|
|
'different region'
|
|
)
|
|
})
|
|
|
|
it('UORD-22: a slot that does not carry the SKU is rejected', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
const other = await productWithSlot(120)
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: other.slotId,
|
|
}),
|
|
'BAD_REQUEST',
|
|
'not available in the selected delivery slot'
|
|
)
|
|
})
|
|
|
|
it('UORD-23: a slot whose freeze time has passed is rejected', async () => {
|
|
const admin = await staffClient()
|
|
const fx = await createCatalogOrderFixture()
|
|
const past: any = await admin.admin.slots.createSlot.mutate({
|
|
deliveryTime: new Date(Date.now() + 3 * 3_600_000).toISOString(),
|
|
freezeTime: new Date(Date.now() - 3_600_000).toISOString(),
|
|
isActive: true,
|
|
skuIds: [fx.product.skuId],
|
|
})
|
|
const pastSlotId = Number(past.slot.id)
|
|
|
|
await expectTrpcError(
|
|
() => placeCall(fx.user.token, { addressId: fx.address.id, skuId: fx.product.skuId, slotId: pastSlotId }),
|
|
'BAD_REQUEST',
|
|
'closed for orders'
|
|
)
|
|
})
|
|
|
|
it('UORD-24: a slot at full capacity is rejected with 403', async () => {
|
|
const admin = await staffClient()
|
|
const fx = await createCatalogOrderFixture({ isolatedSlot: true })
|
|
await admin.admin.slots.updateSlotCapacity.mutate({ slotId: fx.slot.slotId, isCapacityFull: true })
|
|
|
|
await expectTrpcError(
|
|
() => placeCall(fx.user.token, { addressId: fx.address.id, skuId: fx.product.skuId, slotId: fx.slot.slotId }),
|
|
'FORBIDDEN',
|
|
'full capacity'
|
|
)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* 2. Splitting, flash, combos, duplicates (UORD-25..38) */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('user.order.placeOrder — splitting/flash/combo (UORD-25..38)', () => {
|
|
it('UORD-25: two items in the same slot produce one order with both items', async () => {
|
|
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: 2, slotId: slot.slotId },
|
|
],
|
|
})
|
|
expect(res.data).toHaveLength(1)
|
|
const d = await detail(user.token, Number(res.data[0].id))
|
|
expect(d.items).toHaveLength(2)
|
|
})
|
|
|
|
it('UORD-26: two slots produce one order per slot, with delivery charged once', async () => {
|
|
const store = await createStore()
|
|
const a = await createProduct({ storeId: store.storeId, price: 100 })
|
|
const b = await createProduct({ storeId: store.storeId, price: 100 })
|
|
const slot1 = await createFutureSlot([a.skuId])
|
|
const slot2 = await createFutureSlot([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: slot1.slotId },
|
|
{ skuId: b.skuId, quantity: 1, slotId: slot2.slotId },
|
|
],
|
|
})
|
|
expect(res.data).toHaveLength(2)
|
|
|
|
const charges = res.data.map((o: any) => Number(o.deliveryCharge))
|
|
// goods total 200 < 300 -> one delivery charge, attached to the primary sub-order only.
|
|
expect(charges.filter((c: number) => c > 0)).toHaveLength(1)
|
|
expect(charges.reduce((s: number, c: number) => s + c, 0)).toBe(20)
|
|
})
|
|
|
|
it('UORD-27: a flash order ignores slot ids (slotId stored null)', async () => {
|
|
const store = await createStore()
|
|
const product = await createProduct({
|
|
storeId: store.storeId,
|
|
price: 199,
|
|
isFlashAvailable: true,
|
|
flashPrice: 149,
|
|
})
|
|
const slot = await createFutureSlot([product.skuId])
|
|
const user = await registerThrowawayUser()
|
|
const address = await createAddress(user.token)
|
|
|
|
const res = await placeCall(user.token, {
|
|
addressId: address.id,
|
|
skuId: product.skuId,
|
|
slotId: slot.slotId,
|
|
isFlashDelivery: true,
|
|
})
|
|
expect(res.success).toBe(true)
|
|
expect(res.data[0].slotId).toBeNull()
|
|
expect(res.data[0].isFlashDelivery).toBe(true)
|
|
})
|
|
|
|
it('UORD-28: flash disabled on the bound is rejected with 403', 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)
|
|
|
|
await withBoundSetting(TEST_BOUND_ID, 'isFlashDeliveryEnabled', 'false', async () => {
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(user.token, {
|
|
addressId: address.id,
|
|
skuId: product.skuId,
|
|
slotId: null,
|
|
isFlashDelivery: true,
|
|
}),
|
|
'FORBIDDEN',
|
|
'Flash delivery is currently unavailable'
|
|
)
|
|
})
|
|
})
|
|
|
|
it('UORD-29: flash enabled + eligible SKU creates a flash order with a null slot', 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,
|
|
})
|
|
expect(res.success).toBe(true)
|
|
expect(res.data[0].isFlashDelivery).toBe(true)
|
|
expect(res.data[0].slotId).toBeNull()
|
|
expect(Number(res.data[0].totalAmount)).toBe(149 + 69) // flash price + flash delivery charge
|
|
|
|
// The line stores the flash price, not the regular 199.
|
|
const d = await detail(user.token, Number(res.data[0].id))
|
|
expect(d.items[0]).toEqual(
|
|
expect.objectContaining({
|
|
quantity: 1,
|
|
price: 149,
|
|
discountedPrice: 149,
|
|
amount: 149,
|
|
})
|
|
)
|
|
})
|
|
|
|
it('UORD-30: flash enabled but an ineligible SKU is rejected', async () => {
|
|
const fx = await createCatalogOrderFixture({ price: 199 })
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: null,
|
|
isFlashDelivery: true,
|
|
}),
|
|
'BAD_REQUEST',
|
|
'flash delivery'
|
|
)
|
|
})
|
|
|
|
it('UORD-31: a flash-eligible but out-of-stock SKU is rejected as unavailable', async () => {
|
|
const store = await createStore()
|
|
const product = await createProduct({
|
|
storeId: store.storeId,
|
|
price: 199,
|
|
isFlashAvailable: true,
|
|
flashPrice: 149,
|
|
isOutOfStock: true,
|
|
})
|
|
const user = await registerThrowawayUser()
|
|
const address = await createAddress(user.token)
|
|
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(user.token, {
|
|
addressId: address.id,
|
|
skuId: product.skuId,
|
|
slotId: null,
|
|
isFlashDelivery: true,
|
|
}),
|
|
'BAD_REQUEST',
|
|
'unavailable'
|
|
)
|
|
})
|
|
|
|
it('UORD-32: a combo product line is accepted and priced', async () => {
|
|
const store = await createStore()
|
|
const base = await createProduct({ storeId: store.storeId, price: 100 })
|
|
const combo = await createProduct({
|
|
storeId: store.storeId,
|
|
price: 250,
|
|
productType: 'combo',
|
|
comboItems: [{ skuId: base.skuId }],
|
|
})
|
|
const slot = await createFutureSlot([combo.skuId])
|
|
const user = await registerThrowawayUser()
|
|
const address = await createAddress(user.token)
|
|
|
|
const res = await placeCall(user.token, { addressId: address.id, skuId: combo.skuId, slotId: slot.slotId })
|
|
expect(res.success).toBe(true)
|
|
expect(Number(res.data[0].totalAmount)).toBe(250 + 20)
|
|
|
|
// The combo line keeps its own SKU price; child items are not expanded.
|
|
const d = await detail(user.token, Number(res.data[0].id))
|
|
expect(d.items).toHaveLength(1)
|
|
expect(d.items[0]).toEqual(
|
|
expect.objectContaining({ quantity: 1, price: 250, amount: 250 })
|
|
)
|
|
})
|
|
|
|
it('UORD-33 (current): a combo-only SKU ordered directly is accepted (no isComboOnly guard)', async () => {
|
|
const store = await createStore()
|
|
const admin = await staffClient()
|
|
const created: any = await admin.admin.product.createProduct.mutate({
|
|
name: `ComboOnly ${uniqueSuffix()}`,
|
|
shortDescription: 'combo-only fixture',
|
|
storeId: store.storeId,
|
|
productType: 'item',
|
|
skus: [
|
|
{
|
|
name: '1pc',
|
|
price: 180,
|
|
marketPrice: 200,
|
|
images: [],
|
|
isFlashAvailable: false,
|
|
flashPrice: null,
|
|
isOutOfStock: false,
|
|
isOffer: false,
|
|
isComboOnly: true,
|
|
isSuspended: false,
|
|
features: [{ featureName: 'quantity', featureValue: '1pc' }],
|
|
},
|
|
],
|
|
})
|
|
const skuId = Number(created.product.skus[0].id)
|
|
const slot = await createFutureSlot([skuId])
|
|
const user = await registerThrowawayUser()
|
|
const address = await createAddress(user.token)
|
|
|
|
const res = await placeCall(user.token, { addressId: address.id, skuId, slotId: slot.slotId })
|
|
expect(res.success).toBe(true)
|
|
})
|
|
|
|
it('UORD-34 (current): duplicate lines for the same SKU are kept as separate rows, amount correct', async () => {
|
|
const fx = await createCatalogOrderFixture({ price: 199 })
|
|
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: 2, slotId: fx.slot.slotId },
|
|
],
|
|
})
|
|
expect(res.data).toHaveLength(1)
|
|
const d = await detail(fx.user.token, Number(res.data[0].id))
|
|
expect(d.items).toHaveLength(2)
|
|
expect(d.orderAmount).toBe(199 * 3)
|
|
})
|
|
|
|
it('UORD-35: two slots with the same SKU produce separate orders with correct quantities', async () => {
|
|
const fx = await createCatalogOrderFixture({ price: 199 })
|
|
const slot2 = await createFutureSlot([fx.product.skuId])
|
|
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: 3, slotId: slot2.slotId },
|
|
],
|
|
})
|
|
expect(res.data).toHaveLength(2)
|
|
|
|
const bySlot = new Map<number, any>()
|
|
for (const o of res.data) bySlot.set(Number(o.slotId), o)
|
|
|
|
const d1 = await detail(fx.user.token, Number(bySlot.get(fx.slot.slotId).id))
|
|
const d2 = await detail(fx.user.token, Number(bySlot.get(slot2.slotId).id))
|
|
expect(d1.items[0].quantity).toBe(1)
|
|
expect(d2.items[0].quantity).toBe(3)
|
|
})
|
|
|
|
it('UORD-36: userNotes are persisted', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
const note = `Ring the bell ${uniqueSuffix()}`
|
|
const res = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
userNotes: note,
|
|
})
|
|
const d = await detail(fx.user.token, Number(res.data[0].id))
|
|
expect(d.userNotes).toBe(note)
|
|
})
|
|
|
|
it('UORD-37: a long note is accepted (no max declared)', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
const note = 'x'.repeat(5000)
|
|
const res = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
userNotes: note,
|
|
})
|
|
const d = await detail(fx.user.token, Number(res.data[0].id))
|
|
expect(d.userNotes).toBe(note)
|
|
})
|
|
|
|
it('UORD-38: special characters/emoji notes round-trip', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
const note = 'Leave at door 🚪🏠 — "careful" & <tags>'
|
|
const res = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
userNotes: note,
|
|
})
|
|
const d = await detail(fx.user.token, Number(res.data[0].id))
|
|
expect(d.userNotes).toBe(note)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* 3. Coupons (UORD-39..56) */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('user.order.placeOrder — coupons (UORD-39..56)', () => {
|
|
it('UORD-39: without coupons no coupon data is recorded', async () => {
|
|
const fx = await createCatalogOrderFixture({ price: 349 })
|
|
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.couponCode).toBeNull()
|
|
expect(d.discountAmount).toBeNull()
|
|
})
|
|
|
|
it('UORD-40: one coupon is applied and recorded', async () => {
|
|
const fx = await createCatalogOrderFixture({ price: 199 })
|
|
const c = await makeCoupon({ flatDiscount: 50, minOrder: 1 })
|
|
const res = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
couponIds: [c.id],
|
|
})
|
|
expect(Number(res.data[0].discountAmount)).toBe(50)
|
|
const d = await detail(fx.user.token, Number(res.data[0].id))
|
|
expect(d.couponCode).toBe(c.couponCode)
|
|
expect(d.discountAmount).toBe(50)
|
|
expect(d.totalAmount).toBe(199 - 50 + 20)
|
|
})
|
|
|
|
it('UORD-41: multiple coupons stack sequentially', async () => {
|
|
const fx = await createCatalogOrderFixture({ price: 199 })
|
|
const a = await makeCoupon({ flatDiscount: 50, minOrder: 1 })
|
|
const b = await makeCoupon({ flatDiscount: 30, minOrder: 1 })
|
|
const res = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
couponIds: [a.id, b.id],
|
|
})
|
|
expect(Number(res.data[0].discountAmount)).toBe(80)
|
|
const d = await detail(fx.user.token, Number(res.data[0].id))
|
|
expect(d.discountAmount).toBe(80)
|
|
expect(d.totalAmount).toBe(199 - 80 + 20)
|
|
})
|
|
|
|
it('UORD-42: a coupon from another region is rejected', async () => {
|
|
const r2 = await region2Catalog()
|
|
const r2Client = apiClient(r2.token)
|
|
const couponCode = `R2${uniqueSuffix().replace(/[^a-z0-9]/gi, '').toUpperCase()}`
|
|
const created: any = await r2Client.admin.coupon.create.mutate({
|
|
couponCode,
|
|
skuIds: [],
|
|
isApplyForAll: true,
|
|
flatDiscount: 20,
|
|
minOrder: 1,
|
|
})
|
|
const coupon = Array.isArray(created) ? created[0] : created?.coupons?.[0]
|
|
|
|
const fx = await createCatalogOrderFixture({ price: 199 })
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
couponIds: [coupon.id],
|
|
}),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'not valid in this region'
|
|
)
|
|
})
|
|
|
|
it("UORD-43: another user's personal coupon is rejected", async () => {
|
|
const other = await registerThrowawayUser()
|
|
const c = await makeCoupon({
|
|
isUserBased: true,
|
|
applicableUsers: [other.userId],
|
|
isApplyForAll: false,
|
|
flatDiscount: 20,
|
|
})
|
|
const fx = await createCatalogOrderFixture({ price: 199 })
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
couponIds: [c.id],
|
|
}),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'not applicable to your account'
|
|
)
|
|
})
|
|
|
|
it('UORD-44: an invalidated coupon is rejected', async () => {
|
|
const admin = await staffClient()
|
|
const c = await makeCoupon({ flatDiscount: 20 })
|
|
await admin.admin.coupon.delete.mutate({ id: c.id })
|
|
const fx = await createCatalogOrderFixture({ price: 199 })
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
couponIds: [c.id],
|
|
}),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'no longer valid'
|
|
)
|
|
})
|
|
|
|
it('UORD-45: a coupon whose per-user usage limit is reached is rejected', 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(first.success).toBe(true)
|
|
|
|
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'
|
|
)
|
|
})
|
|
|
|
it('UORD-46: a coupon whose minimum order is not met is rejected', async () => {
|
|
const fx = await createCatalogOrderFixture({ price: 199 })
|
|
const c = await makeCoupon({ flatDiscount: 50, minOrder: 100_000 })
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
couponIds: [c.id],
|
|
}),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'minimum requirement'
|
|
)
|
|
})
|
|
|
|
it('UORD-47: a targeted coupon discounts only its SKU', async () => {
|
|
const store = await createStore()
|
|
const target = await createProduct({ storeId: store.storeId, price: 100 })
|
|
const other = await createProduct({ storeId: store.storeId, price: 300 })
|
|
const slot = await createFutureSlot([target.skuId, other.skuId])
|
|
// stock a large cart so delivery stays 0 and the discount is easy to reason about.
|
|
const quantityOther = 10
|
|
const c = await makeCoupon({ flatDiscount: 50, skuIds: [target.skuId], minOrder: 1 })
|
|
|
|
const user = await registerThrowawayUser()
|
|
const address = await createAddress(user.token)
|
|
const res = await placeCall(user.token, {
|
|
addressId: address.id,
|
|
items: [
|
|
{ skuId: target.skuId, quantity: 1, slotId: slot.slotId },
|
|
{ skuId: other.skuId, quantity: quantityOther, slotId: slot.slotId },
|
|
],
|
|
couponIds: [c.id],
|
|
})
|
|
// eligible subtotal = 100 -> flat 50 fully applies
|
|
expect(Number(res.data[0].discountAmount)).toBe(50)
|
|
expect(Number(res.data[0].totalAmount)).toBe(100 + 300 * quantityOther - 50)
|
|
})
|
|
|
|
it('UORD-47B: a targeted coupon measures minOrder on the whole cart, discounts only its SKU', async () => {
|
|
const store = await createStore()
|
|
const target = await createProduct({ storeId: store.storeId, price: 100 })
|
|
const other = await createProduct({ storeId: store.storeId, price: 300 })
|
|
const slot = await createFutureSlot([target.skuId, other.skuId])
|
|
const quantityOther = 10
|
|
// Target value is ₹100 (< ₹500), but the cart is ₹3,100.
|
|
const c = await makeCoupon({ flatDiscount: 50, skuIds: [target.skuId], minOrder: 500 })
|
|
|
|
const user = await registerThrowawayUser()
|
|
const address = await createAddress(user.token)
|
|
const res = await placeCall(user.token, {
|
|
addressId: address.id,
|
|
items: [
|
|
{ skuId: target.skuId, quantity: 1, slotId: slot.slotId },
|
|
{ skuId: other.skuId, quantity: quantityOther, slotId: slot.slotId },
|
|
],
|
|
couponIds: [c.id],
|
|
})
|
|
// The discount base stays the targeted ₹100, so the ₹50 flat fully applies.
|
|
expect(Number(res.data[0].discountAmount)).toBe(50)
|
|
expect(Number(res.data[0].totalAmount)).toBe(100 + 300 * quantityOther - 50)
|
|
})
|
|
|
|
it('UORD-48: a first-order-only coupon works once and is rejected afterwards', async () => {
|
|
const fx = await createCatalogOrderFixture({ price: 199 })
|
|
const c = await makeCoupon({ flatDiscount: 20, minOrder: 1, isFirstOrderOnly: true })
|
|
const first = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
couponIds: [c.id],
|
|
})
|
|
expect(first.success).toBe(true)
|
|
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
couponIds: [c.id],
|
|
}),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'first order only'
|
|
)
|
|
})
|
|
|
|
it('UORD-49: an exclusive coupon cannot be combined, but works alone', async () => {
|
|
const fx = await createCatalogOrderFixture({ price: 199 })
|
|
const exclusive = await makeCoupon({ flatDiscount: 20, minOrder: 1, exclusiveApply: true })
|
|
const other = await makeCoupon({ flatDiscount: 30, minOrder: 1 })
|
|
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
couponIds: [exclusive.id, other.id],
|
|
}),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'exclusive coupon cannot be combined'
|
|
)
|
|
|
|
const alone = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
couponIds: [exclusive.id],
|
|
})
|
|
expect(alone.success).toBe(true)
|
|
expect(Number(alone.data[0].discountAmount)).toBe(20)
|
|
})
|
|
|
|
it('UORD-50: a >100% coupon clamps the discount to the goods subtotal', async () => {
|
|
const fx = await createCatalogOrderFixture({ price: 199 })
|
|
const c = await makeCoupon({ discountPercent: 150, minOrder: 1 })
|
|
const res = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
couponIds: [c.id],
|
|
})
|
|
expect(Number(res.data[0].discountAmount)).toBe(199)
|
|
expect(Number(res.data[0].totalAmount)).toBe(20) // delivery only, never negative
|
|
})
|
|
|
|
it('UORD-51: a flat discount greater than the goods subtotal is capped at the goods', async () => {
|
|
const fx = await createCatalogOrderFixture({ price: 199 })
|
|
const c = await makeCoupon({ flatDiscount: 10_000_000, minOrder: 1 })
|
|
const res = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
couponIds: [c.id],
|
|
})
|
|
expect(Number(res.data[0].discountAmount)).toBe(199)
|
|
expect(Number(res.data[0].totalAmount)).toBe(20)
|
|
})
|
|
|
|
it('UORD-52: a fractional percentage rounds to the nearest rupee', async () => {
|
|
const fx = await createCatalogOrderFixture({ price: 199 })
|
|
const c = await makeCoupon({ discountPercent: 33.33, minOrder: 1 })
|
|
const res = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
couponIds: [c.id],
|
|
})
|
|
// 199 * 0.3333 = 66.33 -> 66
|
|
expect(Number(res.data[0].discountAmount)).toBe(66)
|
|
expect(Number(res.data[0].totalAmount)).toBe(199 - 66 + 20)
|
|
})
|
|
|
|
it('UORD-53: delivery is re-added when the discount drops the cart below the threshold', async () => {
|
|
const fx = await createCatalogOrderFixture({ price: 320 }) // >= 300 -> normally free delivery
|
|
const without = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
})
|
|
expect(Number(without.data[0].deliveryCharge)).toBe(0)
|
|
|
|
const c = await makeCoupon({ flatDiscount: 100, minOrder: 1 })
|
|
const withCoupon = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
couponIds: [c.id],
|
|
})
|
|
expect(Number(withCoupon.data[0].deliveryCharge)).toBe(20) // 320-100 = 220 < 300
|
|
expect(Number(withCoupon.data[0].totalAmount)).toBe(220 + 20)
|
|
})
|
|
|
|
it('UORD-54: flash orders use the flash threshold/charge', 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,
|
|
})
|
|
// flash goods 149 < flashFreeDeliveryThreshold 500 -> flash charge 69 (not the regular 20)
|
|
expect(Number(res.data[0].deliveryCharge)).toBe(69)
|
|
})
|
|
|
|
it('UORD-55: placing with a coupon creates a usage row resolvable from the order', async () => {
|
|
const fx = await createCatalogOrderFixture({ price: 199 })
|
|
const c = await makeCoupon({ flatDiscount: 20, 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.couponDescription).toMatch(/coupon/)
|
|
})
|
|
|
|
it('UORD-56: the first-order flag is cleared after the first order', async () => {
|
|
const fx = await createCatalogOrderFixture({ price: 199 })
|
|
const before: any = await apiClient(fx.user.token).user.coupon.getEligible.query({ regionId: REGION_1 })
|
|
expect(before.isFirstOrder).toBe(true)
|
|
|
|
await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
})
|
|
|
|
const after: any = await apiClient(fx.user.token).user.coupon.getEligible.query({ regionId: REGION_1 })
|
|
expect(after.isFirstOrder).toBe(false)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* 4. Atomicity, grouping, region, misc (UORD-57..70) */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('user.order.placeOrder — atomicity/region/misc (UORD-57..70)', () => {
|
|
it('UORD-57 (current): there is no idempotency — a double submit creates two orders', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
const first = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
})
|
|
const second = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
})
|
|
expect(Number(first.data[0].id)).not.toBe(Number(second.data[0].id))
|
|
const list: any = await apiClient(fx.user.token).user.order.getOrders.query({ page: 1, pageSize: 10 })
|
|
expect(list.pagination.totalCount).toBe(2)
|
|
})
|
|
|
|
it('UORD-58: two concurrent placeOrder calls both persist without corruption', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
const [a, b] = await Promise.all([
|
|
placeCall(fx.user.token, { addressId: fx.address.id, skuId: fx.product.skuId, slotId: fx.slot.slotId }),
|
|
placeCall(fx.user.token, { addressId: fx.address.id, skuId: fx.product.skuId, slotId: fx.slot.slotId }),
|
|
])
|
|
expect(Number(a.data[0].id)).not.toBe(Number(b.data[0].id))
|
|
|
|
const list: any = await apiClient(fx.user.token).user.order.getOrders.query({ page: 1, pageSize: 10 })
|
|
expect(list.pagination.totalCount).toBe(2)
|
|
for (const o of [a, b]) {
|
|
const d = await detail(fx.user.token, Number(o.data[0].id))
|
|
expect(d.orderStatus).toBe('success')
|
|
}
|
|
})
|
|
|
|
it('UORD-59 (current): a successful placeOrder returns after publish; the order is readable', async () => {
|
|
// The publish step (publishFormattedOrder) is awaited and its failure is not
|
|
// separately observable, so the contract pinned here is: success implies a
|
|
// persisted, immediately readable order.
|
|
const fx = await createCatalogOrderFixture()
|
|
const res = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
})
|
|
expect(res.success).toBe(true)
|
|
const d = await detail(fx.user.token, Number(res.data[0].id))
|
|
expect(d.id).toBe(Number(res.data[0].id))
|
|
})
|
|
|
|
it('UORD-60: split orders share an order group (coupon resolved on every sibling)', 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)
|
|
|
|
// The coupon usage row is attached to the primary sub-order only, but both
|
|
// siblings resolve it via the shared orderGroupId.
|
|
for (const o of res.data) {
|
|
const d = await detail(fx.user.token, Number(o.id))
|
|
expect(d.couponCode).toBe(c.couponCode)
|
|
}
|
|
})
|
|
|
|
it('UORD-61: a placed order is immediately visible in getOrders and getOrderById', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
const res = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
})
|
|
const id = Number(res.data[0].id)
|
|
const list: any = await apiClient(fx.user.token).user.order.getOrders.query({ page: 1, pageSize: 10 })
|
|
expect(list.data.some((o: any) => Number(o.id) === id)).toBe(true)
|
|
const d = await detail(fx.user.token, id)
|
|
expect(d.id).toBe(id)
|
|
})
|
|
|
|
it("UORD-62: an order appears only in its own region's admin order queries", async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
const res = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
})
|
|
const id = Number(res.data[0].id)
|
|
|
|
const admin = await staffClient()
|
|
const region1: any = await admin.admin.order.getAll.query({ limit: 50 })
|
|
expect(region1.orders.some((o: any) => Number(o.id) === id)).toBe(true)
|
|
|
|
const r2 = await region2Catalog()
|
|
const region2: any = await apiClient(r2.token).admin.order.getAll.query({ limit: 50 })
|
|
expect(region2.orders.some((o: any) => Number(o.id) === id)).toBe(false)
|
|
})
|
|
|
|
it("UORD-63: an address that does not belong to the user cannot be used", async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
const other = await registerThrowawayUser()
|
|
const otherAddress = await createAddress(other.token)
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: otherAddress.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
}),
|
|
'BAD_REQUEST',
|
|
'Invalid address'
|
|
)
|
|
})
|
|
|
|
it('UORD-64: a user with no address at all gets a 400', async () => {
|
|
const store = await createStore()
|
|
const product = await createProduct({ storeId: store.storeId, price: 199 })
|
|
const slot = await createFutureSlot([product.skuId])
|
|
const user = await registerThrowawayUser()
|
|
|
|
await expectTrpcError(
|
|
() => placeCall(user.token, { addressId: 999_999_999, skuId: product.skuId, slotId: slot.slotId }),
|
|
'BAD_REQUEST',
|
|
'Invalid address'
|
|
)
|
|
})
|
|
|
|
it('UORD-65 (current): the stock check is read-time; a later toggle is not re-checked in-transaction', async () => {
|
|
// placeOrder reads each SKU and rejects out-of-stock SKUs, but the write
|
|
// transaction does not re-check stock. The observable contract: once the
|
|
// SKU is flagged out of stock, every subsequent placement is rejected.
|
|
const admin = await staffClient()
|
|
const store = await createStore()
|
|
const product = await createProduct({ storeId: store.storeId, price: 199 })
|
|
const slot = await createFutureSlot([product.skuId])
|
|
const user = await registerThrowawayUser()
|
|
const address = await createAddress(user.token)
|
|
|
|
await admin.admin.product.toggleOutOfStock.mutate({ id: product.productId })
|
|
await expectTrpcError(
|
|
() => placeCall(user.token, { addressId: address.id, skuId: product.skuId, slotId: slot.slotId }),
|
|
'BAD_REQUEST',
|
|
'unavailable'
|
|
)
|
|
})
|
|
|
|
it('UORD-66 (current): capacity is checked at read-time; a full slot rejects the write', async () => {
|
|
const admin = await staffClient()
|
|
const fx = await createCatalogOrderFixture({ isolatedSlot: true })
|
|
await admin.admin.slots.updateSlotCapacity.mutate({ slotId: fx.slot.slotId, isCapacityFull: true })
|
|
await expectTrpcError(
|
|
() =>
|
|
placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
}),
|
|
'FORBIDDEN',
|
|
'full capacity'
|
|
)
|
|
})
|
|
|
|
it('UORD-67: the server price at write time is what the order stores', async () => {
|
|
const admin = await staffClient()
|
|
const fx = await createCatalogOrderFixture({ price: 250, isolatedProduct: true })
|
|
const res = await placeCall(fx.user.token, {
|
|
addressId: fx.address.id,
|
|
skuId: fx.product.skuId,
|
|
slotId: fx.slot.slotId,
|
|
})
|
|
const orderId = Number(res.data[0].id)
|
|
expect(Number(res.data[0].totalAmount)).toBe(250 + 20)
|
|
|
|
// Change the SKU price after the order: the stored order item/price must not move.
|
|
const details: any = await admin.admin.product.getProductById.query({ id: fx.product.productId })
|
|
const sku = details.product.skus[0]
|
|
await admin.admin.product.updateProduct.mutate({
|
|
id: fx.product.productId,
|
|
name: details.product.name,
|
|
shortDescription: details.product.shortDescription ?? 'x',
|
|
storeId: fx.store.storeId,
|
|
productType: 'item',
|
|
skus: [
|
|
{
|
|
id: sku.id,
|
|
name: sku.name,
|
|
price: 400,
|
|
marketPrice: 450,
|
|
images: [],
|
|
isFlashAvailable: false,
|
|
flashPrice: null,
|
|
isOutOfStock: false,
|
|
isOffer: false,
|
|
isComboOnly: false,
|
|
isSuspended: false,
|
|
features: (sku.features ?? []).map((f: any) => ({
|
|
featureName: f.featureName,
|
|
featureValue: f.featureValue,
|
|
})),
|
|
},
|
|
],
|
|
} as any)
|
|
|
|
const d = await detail(fx.user.token, orderId)
|
|
expect(d.items[0].price).toBe(250)
|
|
expect(d.totalAmount).toBe(270)
|
|
})
|
|
|
|
it('UORD-68 (current): unknown payment/isCod fields are stripped; only the COD contract exists', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
const res: any = await apiClient(fx.user.token).user.order.placeOrder.mutate({
|
|
selectedItems: [{ skuId: fx.product.skuId, quantity: 1, slotId: fx.slot.slotId }],
|
|
addressId: fx.address.id,
|
|
regionId: REGION_1,
|
|
isCod: true,
|
|
paymentMode: 'online',
|
|
paymentStatus: 'paid',
|
|
} as any)
|
|
expect(res.success).toBe(true)
|
|
expect(res.data[0].paymentMode).toBeUndefined()
|
|
expect(res.data[0].isCod).toBeUndefined()
|
|
})
|
|
|
|
it('UORD-69: malformed selectedItems is BAD_REQUEST', async () => {
|
|
const fx = await createCatalogOrderFixture()
|
|
await expectTrpcError(
|
|
() =>
|
|
apiClient(fx.user.token).user.order.placeOrder.mutate({
|
|
selectedItems: 'nope',
|
|
addressId: fx.address.id,
|
|
regionId: REGION_1,
|
|
} as any),
|
|
'BAD_REQUEST'
|
|
)
|
|
})
|
|
|
|
it('UORD-70 (current): an address with only one coordinate falls back to its stored region', async () => {
|
|
const store = await createStore()
|
|
const product = await createProduct({ storeId: store.storeId, price: 199 })
|
|
const slot = await createFutureSlot([product.skuId])
|
|
const user = await registerThrowawayUser()
|
|
const created: any = await apiClient(user.token).user.address.createAddress.mutate({
|
|
regionId: REGION_1,
|
|
name: 'One coord',
|
|
phone: '9876543210',
|
|
addressLine1: `OneCoord ${uniqueSuffix()}`,
|
|
city: 'Mahabubnagar',
|
|
state: 'Telangana',
|
|
isDefault: true,
|
|
latitude: COORDS.region1Bound1.lat,
|
|
} as any)
|
|
expect(created.data.longitude).toBeNull()
|
|
const addressId = Number(created.data.id)
|
|
|
|
const res = await placeCall(user.token, {
|
|
addressId,
|
|
skuId: product.skuId,
|
|
slotId: slot.slotId,
|
|
regionId: TEST_OTHER_REGION_ID,
|
|
})
|
|
expect(res.success).toBe(true)
|
|
expect(Number(res.data[0].regionId)).toBe(REGION_1)
|
|
})
|
|
})
|