805 lines
28 KiB
TypeScript
805 lines
28 KiB
TypeScript
/**
|
||
* AVEND-01..32 — `admin.vendorSnippets` (catalogue §7.11).
|
||
*
|
||
* `create` is `staffProcedure`; `getAll`/`update`/`delete` are
|
||
* `protectedProcedure`. The vendor link endpoints — `getOrdersBySnippet`,
|
||
* `getUpcomingSlots`, `getOrdersBySnippetAndSlot` and (notably) the
|
||
* `updateOrderItemPackaging` mutation — are `publicProcedure` by design and do a
|
||
* GLOBAL lookup (`regionId = null`), which is the security-review item in §13
|
||
* #10. Raw `Error`s thrown by the resolvers flatten to 500, so the "not found"
|
||
* cases assert INTERNAL_SERVER_ERROR, not 404.
|
||
*/
|
||
|
||
import { apiClient, expectTrpcError, uniqueSuffix, type ApiClient } from '../helpers/api'
|
||
import { anonClient, staffClient } from '../helpers/sessions'
|
||
import {
|
||
PASSWORD,
|
||
createAddress,
|
||
createFutureSlot,
|
||
createProduct,
|
||
createStore,
|
||
placeOrder,
|
||
registerThrowawayUser,
|
||
type CreatedProduct,
|
||
} from '../helpers/fixtures'
|
||
import { TEST_OTHER_REGION_ID } from '../helpers/env'
|
||
|
||
jest.setTimeout(300_000)
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* Helpers */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
interface SnippetInput {
|
||
snippetCode?: string
|
||
slotId?: number
|
||
skuIds?: number[]
|
||
validTill?: string
|
||
isPermanent?: boolean
|
||
}
|
||
|
||
async function createSnippet(admin: ApiClient, input: SnippetInput): Promise<any> {
|
||
return admin.admin.vendorSnippets.create.mutate({
|
||
snippetCode: input.snippetCode ?? `E2E VS ${uniqueSuffix()}`,
|
||
skuIds: input.skuIds ?? [],
|
||
...(input.slotId !== undefined ? { slotId: input.slotId } : {}),
|
||
...(input.validTill !== undefined ? { validTill: input.validTill } : {}),
|
||
...(input.isPermanent !== undefined ? { isPermanent: input.isPermanent } : {}),
|
||
} as any)
|
||
}
|
||
|
||
/** A region-1 store + product + future slot for snippet creation. */
|
||
let catalogPromise: Promise<{ product: CreatedProduct; slotId: number }> | null = null
|
||
function catalog(): Promise<{ product: CreatedProduct; slotId: number }> {
|
||
catalogPromise ??= (async () => {
|
||
const store = await createStore()
|
||
const product = await createProduct({ storeId: store.storeId, price: 175 })
|
||
const slot = await createFutureSlot([product.skuId])
|
||
return { product, slotId: slot.slotId }
|
||
})()
|
||
return catalogPromise
|
||
}
|
||
|
||
async function createRegionStaff(regionId: number): Promise<ApiClient> {
|
||
const { superadminClient } = await import('../helpers/sessions')
|
||
const superadmin = await superadminClient()
|
||
const roles: any = await superadmin.superadmin.staff.roles.query()
|
||
const roleId = Number(roles.roles.find((r: any) => r.name === 'admin')?.id ?? roles.roles[0].id)
|
||
const name = `e2e_avend_${uniqueSuffix()}`
|
||
await superadmin.superadmin.staff.create.mutate({ name, password: PASSWORD, roleId, regionId })
|
||
const login: any = await apiClient().admin.staffUser.login.mutate({ name, password: PASSWORD })
|
||
return apiClient(login.token)
|
||
}
|
||
|
||
interface ForeignRegion {
|
||
client: ApiClient
|
||
skuId: number
|
||
slotId: number
|
||
}
|
||
|
||
/** A private region with its own store/product/slot (snippets live there). */
|
||
let foreignPromise: Promise<ForeignRegion> | null = null
|
||
function foreignRegion(): Promise<ForeignRegion> {
|
||
foreignPromise ??= (async () => {
|
||
const client = await createRegionStaff(TEST_OTHER_REGION_ID)
|
||
const storeRes: any = await client.admin.store.createStore.mutate({
|
||
name: `E2E Foreign Store ${uniqueSuffix()}`,
|
||
})
|
||
const storeId = Number(storeRes.store.id)
|
||
const productRes: any = await client.admin.product.createProduct.mutate({
|
||
name: `E2E Foreign Product ${uniqueSuffix()}`,
|
||
shortDescription: 'foreign region product',
|
||
storeId,
|
||
productType: 'item',
|
||
skus: [
|
||
{
|
||
name: '1pc',
|
||
price: 200,
|
||
marketPrice: 250,
|
||
images: [],
|
||
isFlashAvailable: false,
|
||
flashPrice: null,
|
||
isOutOfStock: false,
|
||
isOffer: false,
|
||
isComboOnly: false,
|
||
isSuspended: false,
|
||
features: [{ featureName: 'quantity', featureValue: '1pc' }],
|
||
},
|
||
],
|
||
} as any)
|
||
const skuId = Number(productRes.product.skus[0].id)
|
||
const slotRes: any = await client.admin.slots.createSlot.mutate({
|
||
deliveryTime: new Date(Date.now() + 4 * 3_600_000).toISOString(),
|
||
freezeTime: new Date(Date.now() + 3 * 3_600_000).toISOString(),
|
||
skuIds: [skuId],
|
||
})
|
||
return { client, skuId, slotId: Number(slotRes.slot.id) }
|
||
})()
|
||
return foreignPromise
|
||
}
|
||
|
||
interface OrderSetup {
|
||
snippetCode: string
|
||
slotId: number
|
||
product: CreatedProduct
|
||
user: { name: string; token: string }
|
||
orderA: any
|
||
orderB: any
|
||
orderC: any
|
||
otherSlotId: number
|
||
}
|
||
|
||
/**
|
||
* Region-1 store + two products + slot + user/address, with:
|
||
* orderA = productA on the snippet slot
|
||
* orderB = productB on the same slot (no snippet SKU)
|
||
* orderC = productA on the snippet slot, then cancelled
|
||
*/
|
||
let orderSetupPromise: Promise<OrderSetup> | null = null
|
||
function orderSetup(): Promise<OrderSetup> {
|
||
orderSetupPromise ??= (async () => {
|
||
const admin = await staffClient()
|
||
const store = await createStore()
|
||
const productA = await createProduct({ storeId: store.storeId, price: 111 })
|
||
const productB = await createProduct({ storeId: store.storeId, price: 222 })
|
||
const slot = await createFutureSlot([productA.skuId, productB.skuId])
|
||
const otherSlot = await createFutureSlot([productA.skuId])
|
||
const user = await registerThrowawayUser()
|
||
const address = await createAddress(user.token)
|
||
|
||
const [orderA] = await placeOrder(user.token, {
|
||
addressId: address.id,
|
||
skuId: productA.skuId,
|
||
slotId: slot.slotId,
|
||
})
|
||
const [orderB] = await placeOrder(user.token, {
|
||
addressId: address.id,
|
||
skuId: productB.skuId,
|
||
slotId: slot.slotId,
|
||
})
|
||
const [orderC] = await placeOrder(user.token, {
|
||
addressId: address.id,
|
||
skuId: productA.skuId,
|
||
slotId: slot.slotId,
|
||
})
|
||
|
||
const snippet: any = await createSnippet(admin, {
|
||
snippetCode: `E2E ORD ${uniqueSuffix()}`,
|
||
slotId: slot.slotId,
|
||
skuIds: [productA.skuId],
|
||
isPermanent: true,
|
||
})
|
||
|
||
await admin.admin.order.cancelOrder.mutate({
|
||
orderId: Number(orderC.id),
|
||
reason: 'E2E AVEND-21 cancellation',
|
||
})
|
||
|
||
return {
|
||
snippetCode: snippet.snippetCode,
|
||
slotId: slot.slotId,
|
||
product: productA,
|
||
user,
|
||
orderA,
|
||
orderB,
|
||
orderC,
|
||
otherSlotId: otherSlot.slotId,
|
||
}
|
||
})()
|
||
return orderSetupPromise
|
||
}
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* create */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('admin.vendorSnippets.create (AVEND-01..08)', () => {
|
||
it('AVEND-01: create with a slot and SKUs returns the snippet', async () => {
|
||
const admin = await staffClient()
|
||
const { product, slotId } = await catalog()
|
||
const code = `E2E VS ${uniqueSuffix()}`
|
||
|
||
const snippet: any = await createSnippet(admin, { snippetCode: code, slotId, skuIds: [product.skuId] })
|
||
|
||
expect(snippet).toEqual(
|
||
expect.objectContaining({
|
||
id: expect.any(Number),
|
||
snippetCode: code,
|
||
slotId,
|
||
skuIds: [product.skuId],
|
||
isPermanent: false,
|
||
validTill: null,
|
||
})
|
||
)
|
||
})
|
||
|
||
it('AVEND-02: a missing snippet code is BAD_REQUEST', async () => {
|
||
const admin = await staffClient()
|
||
const { product, slotId } = await catalog()
|
||
await expectTrpcError(
|
||
() => admin.admin.vendorSnippets.create.mutate({ slotId, skuIds: [product.skuId] } as any),
|
||
'BAD_REQUEST'
|
||
)
|
||
})
|
||
|
||
it('AVEND-03: an empty SKU list is BAD_REQUEST', async () => {
|
||
const admin = await staffClient()
|
||
const { slotId } = await catalog()
|
||
await expectTrpcError(
|
||
() =>
|
||
admin.admin.vendorSnippets.create.mutate({
|
||
snippetCode: `E2E VS ${uniqueSuffix()}`,
|
||
slotId,
|
||
skuIds: [],
|
||
}),
|
||
'BAD_REQUEST'
|
||
)
|
||
})
|
||
|
||
it('AVEND-04: an unknown slot id is a 500 “Invalid slot ID”', async () => {
|
||
const admin = await staffClient()
|
||
const { product } = await catalog()
|
||
await expectTrpcError(
|
||
() =>
|
||
createSnippet(admin, {
|
||
snippetCode: `E2E VS ${uniqueSuffix()}`,
|
||
slotId: 999_999_999,
|
||
skuIds: [product.skuId],
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'Invalid slot ID'
|
||
)
|
||
})
|
||
|
||
it('AVEND-05: invalid and foreign SKUs are 500s', async () => {
|
||
const admin = await staffClient()
|
||
const { product, slotId } = await catalog()
|
||
|
||
await expectTrpcError(
|
||
() =>
|
||
createSnippet(admin, {
|
||
snippetCode: `E2E VS ${uniqueSuffix()}`,
|
||
slotId,
|
||
skuIds: [999_999_999],
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'invalid product'
|
||
)
|
||
|
||
const foreign = await foreignRegion()
|
||
await expectTrpcError(
|
||
() =>
|
||
createSnippet(admin, {
|
||
snippetCode: `E2E VS ${uniqueSuffix()}`,
|
||
slotId,
|
||
skuIds: [foreign.skuId],
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'invalid product'
|
||
)
|
||
expect(product.skuId).toBeGreaterThan(0)
|
||
})
|
||
|
||
it('AVEND-06: a duplicate snippet code is a 500', async () => {
|
||
const admin = await staffClient()
|
||
const { product, slotId } = await catalog()
|
||
const code = `E2E DUP ${uniqueSuffix()}`
|
||
await createSnippet(admin, { snippetCode: code, slotId, skuIds: [product.skuId] })
|
||
|
||
await expectTrpcError(
|
||
() => createSnippet(admin, { snippetCode: code, slotId, skuIds: [product.skuId] }),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'already exists'
|
||
)
|
||
})
|
||
|
||
it('AVEND-07: a snippet with no slot is allowed but the orders endpoint errors', async () => {
|
||
const admin = await staffClient()
|
||
const { product } = await catalog()
|
||
const snippet: any = await createSnippet(admin, {
|
||
snippetCode: `E2E NOSLOT ${uniqueSuffix()}`,
|
||
skuIds: [product.skuId],
|
||
})
|
||
expect(snippet.slotId).toBeNull()
|
||
|
||
await expectTrpcError(
|
||
() =>
|
||
anonClient().admin.vendorSnippets.getOrdersBySnippet.query({
|
||
snippetCode: snippet.snippetCode,
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'not associated with a slot'
|
||
)
|
||
})
|
||
|
||
it('AVEND-08: validTill and isPermanent are stored', async () => {
|
||
const admin = await staffClient()
|
||
const { product, slotId } = await catalog()
|
||
const validTill = new Date(Date.now() + 7 * 24 * 3_600_000).toISOString()
|
||
|
||
const snippet: any = await createSnippet(admin, {
|
||
snippetCode: `E2E PERM ${uniqueSuffix()}`,
|
||
slotId,
|
||
skuIds: [product.skuId],
|
||
validTill,
|
||
isPermanent: true,
|
||
})
|
||
expect(snippet.isPermanent).toBe(true)
|
||
expect(new Date(snippet.validTill).toISOString()).toBe(validTill)
|
||
|
||
const list: any = await admin.admin.vendorSnippets.getAll.query()
|
||
const listed = list.find((entry: any) => entry.id === snippet.id)
|
||
expect(listed.isPermanent).toBe(true)
|
||
expect(new Date(listed.validTill).toISOString()).toBe(validTill)
|
||
})
|
||
})
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* getAll */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('admin.vendorSnippets.getAll (AVEND-09..10)', () => {
|
||
it('AVEND-09: getAll returns snippets with products, slot and access URL', async () => {
|
||
const admin = await staffClient()
|
||
const { product, slotId } = await catalog()
|
||
const code = `E2E ALL ${uniqueSuffix()}`
|
||
const snippet: any = await createSnippet(admin, { snippetCode: code, slotId, skuIds: [product.skuId] })
|
||
|
||
const list: any = await admin.admin.vendorSnippets.getAll.query()
|
||
const listed = list.find((entry: any) => entry.id === snippet.id)
|
||
expect(listed).toBeTruthy()
|
||
expect(listed.accessUrl).toContain('/vendor-order-list?id=')
|
||
expect(listed.accessUrl).toContain(code)
|
||
expect(listed.slot).toEqual(expect.objectContaining({ id: slotId }))
|
||
expect(listed.products).toEqual(
|
||
expect.arrayContaining([expect.objectContaining({ id: product.skuId })])
|
||
)
|
||
})
|
||
|
||
it('AVEND-10: a region with no snippets returns []', async () => {
|
||
// A brand-new region has no vendor snippets yet, so getAll is genuinely
|
||
// empty (the resolver also swallows errors and returns []).
|
||
const { superadminClient } = await import('../helpers/sessions')
|
||
const superadmin = await superadminClient()
|
||
const created: any = await superadmin.superadmin.regions.create.mutate({
|
||
name: `E2E Empty Snippets ${uniqueSuffix()}`,
|
||
description: 'AVEND-10',
|
||
center: { lat: 19.1, lng: 80.1 },
|
||
bounds: [
|
||
{
|
||
description: 'b',
|
||
geojson: {
|
||
type: 'FeatureCollection',
|
||
features: [
|
||
{
|
||
type: 'Feature',
|
||
properties: {},
|
||
geometry: {
|
||
type: 'Polygon',
|
||
coordinates: [
|
||
[
|
||
[80.0, 19.0],
|
||
[80.2, 19.0],
|
||
[80.2, 19.2],
|
||
[80.0, 19.2],
|
||
[80.0, 19.0],
|
||
],
|
||
],
|
||
},
|
||
},
|
||
],
|
||
},
|
||
priority: '1',
|
||
},
|
||
],
|
||
})
|
||
const freshClient = await createRegionStaff(Number(created.id))
|
||
const list: any = await freshClient.admin.vendorSnippets.getAll.query()
|
||
expect(list).toEqual([])
|
||
})
|
||
})
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* update / delete */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('admin.vendorSnippets.update/delete (AVEND-11..16)', () => {
|
||
it('AVEND-11: update changes code, slot, SKUs and flags', async () => {
|
||
const admin = await staffClient()
|
||
const { product, slotId } = await catalog()
|
||
const snippet: any = await createSnippet(admin, {
|
||
snippetCode: `E2E UPD ${uniqueSuffix()}`,
|
||
slotId,
|
||
skuIds: [product.skuId],
|
||
})
|
||
|
||
const newCode = `E2E UPD ${uniqueSuffix()}`
|
||
const updated: any = await admin.admin.vendorSnippets.update.mutate({
|
||
id: snippet.id,
|
||
updates: {
|
||
snippetCode: newCode,
|
||
slotId,
|
||
skuIds: [product.skuId],
|
||
isPermanent: true,
|
||
},
|
||
})
|
||
expect(updated).toEqual(
|
||
expect.objectContaining({
|
||
id: snippet.id,
|
||
snippetCode: newCode,
|
||
slotId,
|
||
skuIds: [product.skuId],
|
||
isPermanent: true,
|
||
})
|
||
)
|
||
})
|
||
|
||
it('AVEND-12: updating an unknown snippet is a 500', async () => {
|
||
const admin = await staffClient()
|
||
await expectTrpcError(
|
||
() =>
|
||
admin.admin.vendorSnippets.update.mutate({
|
||
id: 999_999_999,
|
||
updates: { snippetCode: 'ghost' },
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'not found'
|
||
)
|
||
})
|
||
|
||
it('AVEND-13: update with an invalid slot or SKU is a 500', async () => {
|
||
const admin = await staffClient()
|
||
const { product, slotId } = await catalog()
|
||
const snippet: any = await createSnippet(admin, {
|
||
snippetCode: `E2E UPD ${uniqueSuffix()}`,
|
||
slotId,
|
||
skuIds: [product.skuId],
|
||
})
|
||
|
||
await expectTrpcError(
|
||
() =>
|
||
admin.admin.vendorSnippets.update.mutate({
|
||
id: snippet.id,
|
||
updates: { slotId: 999_999_999 },
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'Invalid slot ID'
|
||
)
|
||
await expectTrpcError(
|
||
() =>
|
||
admin.admin.vendorSnippets.update.mutate({
|
||
id: snippet.id,
|
||
updates: { skuIds: [999_999_999] },
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'invalid product'
|
||
)
|
||
})
|
||
|
||
it('AVEND-14: update to a duplicate code is a 500', async () => {
|
||
const admin = await staffClient()
|
||
const { product, slotId } = await catalog()
|
||
const code = `E2E DUPD ${uniqueSuffix()}`
|
||
await createSnippet(admin, { snippetCode: code, slotId, skuIds: [product.skuId] })
|
||
const other: any = await createSnippet(admin, {
|
||
snippetCode: `E2E DUPD2 ${uniqueSuffix()}`,
|
||
slotId,
|
||
skuIds: [product.skuId],
|
||
})
|
||
|
||
await expectTrpcError(
|
||
() =>
|
||
admin.admin.vendorSnippets.update.mutate({
|
||
id: other.id,
|
||
updates: { snippetCode: code },
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'already exists'
|
||
)
|
||
})
|
||
|
||
it('AVEND-15: delete removes the snippet', async () => {
|
||
const admin = await staffClient()
|
||
const { product, slotId } = await catalog()
|
||
const snippet: any = await createSnippet(admin, {
|
||
snippetCode: `E2E DEL ${uniqueSuffix()}`,
|
||
slotId,
|
||
skuIds: [product.skuId],
|
||
})
|
||
|
||
const res: any = await admin.admin.vendorSnippets.delete.mutate({ id: snippet.id })
|
||
expect(res.message).toBe('Vendor snippet deleted successfully')
|
||
|
||
const list: any = await admin.admin.vendorSnippets.getAll.query()
|
||
expect(list.some((entry: any) => entry.id === snippet.id)).toBe(false)
|
||
})
|
||
|
||
it('AVEND-16: deleting an unknown snippet is a 500', async () => {
|
||
const admin = await staffClient()
|
||
await expectTrpcError(
|
||
() => admin.admin.vendorSnippets.delete.mutate({ id: 999_999_999 }),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'not found'
|
||
)
|
||
})
|
||
})
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* public vendor link: orders */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('admin.vendorSnippets public order lookups (AVEND-17..28)', () => {
|
||
it('AVEND-17: getOrdersBySnippet returns only orders with the snippet’s SKUs', async () => {
|
||
const setup = await orderSetup()
|
||
const res: any = await anonClient().admin.vendorSnippets.getOrdersBySnippet.query({
|
||
snippetCode: setup.snippetCode,
|
||
})
|
||
|
||
expect(res.success).toBe(true)
|
||
expect(res.snippet).toEqual(
|
||
expect.objectContaining({ snippetCode: setup.snippetCode, slotId: setup.slotId })
|
||
)
|
||
|
||
const ids = res.data.map((order: any) => order.orderId)
|
||
expect(ids).toContain(`ORD${setup.orderA.id}`)
|
||
// orderB is on the same slot but has no snippet SKU.
|
||
expect(ids).not.toContain(`ORD${setup.orderB.id}`)
|
||
|
||
const orderA = res.data.find((order: any) => order.orderId === `ORD${setup.orderA.id}`)
|
||
expect(orderA.products.map((p: any) => Number(p.productId))).toEqual([setup.product.skuId])
|
||
expect(orderA.products[0]).toEqual(
|
||
expect.objectContaining({
|
||
orderItemId: expect.any(Number),
|
||
quantity: expect.any(Number),
|
||
is_packaged: expect.any(Boolean),
|
||
})
|
||
)
|
||
})
|
||
|
||
it('AVEND-18: an unknown snippet code is a 500', async () => {
|
||
await expectTrpcError(
|
||
() =>
|
||
anonClient().admin.vendorSnippets.getOrdersBySnippet.query({
|
||
snippetCode: `NOPE-${uniqueSuffix()}`,
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'not found'
|
||
)
|
||
})
|
||
|
||
it('AVEND-19: an expired snippet is a 500', async () => {
|
||
const admin = await staffClient()
|
||
const { product, slotId } = await catalog()
|
||
const snippet: any = await createSnippet(admin, {
|
||
snippetCode: `E2E EXP ${uniqueSuffix()}`,
|
||
slotId,
|
||
skuIds: [product.skuId],
|
||
validTill: new Date(Date.now() - 3_600_000).toISOString(),
|
||
isPermanent: false,
|
||
})
|
||
|
||
await expectTrpcError(
|
||
() =>
|
||
anonClient().admin.vendorSnippets.getOrdersBySnippet.query({
|
||
snippetCode: snippet.snippetCode,
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'expired'
|
||
)
|
||
})
|
||
|
||
it('AVEND-20: a snippet with no slot is a 500 “not associated with a slot”', async () => {
|
||
const admin = await staffClient()
|
||
const { product } = await catalog()
|
||
const snippet: any = await createSnippet(admin, {
|
||
snippetCode: `E2E NOSLOT ${uniqueSuffix()}`,
|
||
skuIds: [product.skuId],
|
||
})
|
||
|
||
await expectTrpcError(
|
||
() =>
|
||
anonClient().admin.vendorSnippets.getOrdersBySnippet.query({
|
||
snippetCode: snippet.snippetCode,
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'not associated with a slot'
|
||
)
|
||
})
|
||
|
||
it('AVEND-21: cancelled orders are excluded', async () => {
|
||
const setup = await orderSetup()
|
||
const res: any = await anonClient().admin.vendorSnippets.getOrdersBySnippet.query({
|
||
snippetCode: setup.snippetCode,
|
||
})
|
||
const ids = res.data.map((order: any) => order.orderId)
|
||
expect(ids).not.toContain(`ORD${setup.orderC.id}`)
|
||
})
|
||
|
||
it('AVEND-22: orders without a snippet SKU are excluded', async () => {
|
||
const setup = await orderSetup()
|
||
const res: any = await anonClient().admin.vendorSnippets.getOrdersBySnippet.query({
|
||
snippetCode: setup.snippetCode,
|
||
})
|
||
const ids = res.data.map((order: any) => order.orderId)
|
||
expect(ids).toContain(`ORD${setup.orderA.id}`)
|
||
expect(ids).not.toContain(`ORD${setup.orderB.id}`)
|
||
})
|
||
|
||
it('AVEND-23 (current): customerName falls back to "" only for null-name rows', async () => {
|
||
// The resolver writes `order.user.name || ''`; every API-registered user has
|
||
// a non-empty name, so the empty-string branch is only reachable for legacy
|
||
// rows with a null `users.name`.
|
||
const setup = await orderSetup()
|
||
const res: any = await anonClient().admin.vendorSnippets.getOrdersBySnippet.query({
|
||
snippetCode: setup.snippetCode,
|
||
})
|
||
const orderA = res.data.find((order: any) => order.orderId === `ORD${setup.orderA.id}`)
|
||
expect(orderA.customerName).toBe(setup.user.name)
|
||
expect(orderA.customerName.length).toBeGreaterThan(0)
|
||
})
|
||
|
||
it('AVEND-24: the vendor order lookup is public (no auth needed)', async () => {
|
||
const setup = await orderSetup()
|
||
const res: any = await anonClient().admin.vendorSnippets.getOrdersBySnippet.query({
|
||
snippetCode: setup.snippetCode,
|
||
})
|
||
expect(res.success).toBe(true)
|
||
})
|
||
|
||
it('AVEND-25: getUpcomingSlots returns slots after now-3h, publicly', async () => {
|
||
const setup = await orderSetup()
|
||
const res: any = await anonClient().admin.vendorSnippets.getUpcomingSlots.query()
|
||
|
||
expect(res.success).toBe(true)
|
||
expect(Array.isArray(res.data)).toBe(true)
|
||
const slot = res.data.find((entry: any) => Number(entry.id) === setup.slotId)
|
||
expect(slot).toBeTruthy()
|
||
expect(slot).toEqual(
|
||
expect.objectContaining({
|
||
deliveryTime: expect.any(String),
|
||
freezeTime: expect.any(String),
|
||
})
|
||
)
|
||
})
|
||
|
||
it('AVEND-26 (current): getUpcomingSlots is global, so the empty case is unreachable here', async () => {
|
||
// The resolver ignores region entirely (`getSlotsAfterDate(null, now-3h)`),
|
||
// so a seeded DB always has rows; the pinned contract is the envelope shape.
|
||
const res: any = await anonClient().admin.vendorSnippets.getUpcomingSlots.query()
|
||
expect(res.success).toBe(true)
|
||
expect(res.data.length).toBeGreaterThan(0)
|
||
for (const slot of res.data) {
|
||
expect(new Date(slot.deliveryTime).getTime()).toBeGreaterThan(Date.now() - 3 * 3_600_000)
|
||
}
|
||
})
|
||
|
||
it('AVEND-27: getOrdersBySnippetAndSlot filters by the given slot', async () => {
|
||
const setup = await orderSetup()
|
||
const res: any = await anonClient().admin.vendorSnippets.getOrdersBySnippetAndSlot.query({
|
||
snippetCode: setup.snippetCode,
|
||
slotId: setup.slotId,
|
||
})
|
||
|
||
expect(res.success).toBe(true)
|
||
expect(res.selectedSlot.id).toBe(setup.slotId)
|
||
expect(res.data.map((order: any) => order.orderId)).toContain(`ORD${setup.orderA.id}`)
|
||
|
||
// A different, valid slot has no matching orders.
|
||
const other: any = await anonClient().admin.vendorSnippets.getOrdersBySnippetAndSlot.query({
|
||
snippetCode: setup.snippetCode,
|
||
slotId: setup.otherSlotId,
|
||
})
|
||
expect(other.data).toEqual([])
|
||
})
|
||
|
||
it('AVEND-28: an unknown snippet or slot is a 500', async () => {
|
||
const setup = await orderSetup()
|
||
await expectTrpcError(
|
||
() =>
|
||
anonClient().admin.vendorSnippets.getOrdersBySnippetAndSlot.query({
|
||
snippetCode: `NOPE-${uniqueSuffix()}`,
|
||
slotId: setup.slotId,
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'not found'
|
||
)
|
||
await expectTrpcError(
|
||
() =>
|
||
anonClient().admin.vendorSnippets.getOrdersBySnippetAndSlot.query({
|
||
snippetCode: setup.snippetCode,
|
||
slotId: 999_999_999,
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'Slot not found'
|
||
)
|
||
})
|
||
})
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* public vendor link: packaging */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('admin.vendorSnippets.updateOrderItemPackaging (AVEND-29..31)', () => {
|
||
it('AVEND-29: updateOrderItemPackaging flips is_packaged', async () => {
|
||
const setup = await orderSetup()
|
||
const before: any = await anonClient().admin.vendorSnippets.getOrdersBySnippet.query({
|
||
snippetCode: setup.snippetCode,
|
||
})
|
||
const orderA = before.data.find((order: any) => order.orderId === `ORD${setup.orderA.id}`)
|
||
const orderItemId = Number(orderA.products[0].orderItemId)
|
||
|
||
const res: any = await anonClient().admin.vendorSnippets.updateOrderItemPackaging.mutate({
|
||
orderItemId,
|
||
is_packaged: true,
|
||
})
|
||
expect(res).toEqual(expect.objectContaining({ success: true, orderItemId, is_packaged: true }))
|
||
|
||
const after: any = await anonClient().admin.vendorSnippets.getOrdersBySnippet.query({
|
||
snippetCode: setup.snippetCode,
|
||
})
|
||
const refreshed = after.data
|
||
.find((order: any) => order.orderId === `ORD${setup.orderA.id}`)
|
||
.products.find((p: any) => Number(p.orderItemId) === orderItemId)
|
||
expect(refreshed.is_packaged).toBe(true)
|
||
})
|
||
|
||
it('AVEND-30: an unknown order item is a 500', async () => {
|
||
await expectTrpcError(
|
||
() =>
|
||
anonClient().admin.vendorSnippets.updateOrderItemPackaging.mutate({
|
||
orderItemId: 999_999_999,
|
||
is_packaged: true,
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'Order item not found'
|
||
)
|
||
})
|
||
|
||
it('AVEND-31 (current): the packaging mutation is public — a security gap', async () => {
|
||
// `updateOrderItemPackaging` is a `publicProcedure` with the auth check
|
||
// commented out, and it is callable anonymously (§13 #10).
|
||
const setup = await orderSetup()
|
||
const view: any = await anonClient().admin.vendorSnippets.getOrdersBySnippet.query({
|
||
snippetCode: setup.snippetCode,
|
||
})
|
||
const orderA = view.data.find((order: any) => order.orderId === `ORD${setup.orderA.id}`)
|
||
const orderItemId = Number(orderA.products[0].orderItemId)
|
||
|
||
const res: any = await anonClient().admin.vendorSnippets.updateOrderItemPackaging.mutate({
|
||
orderItemId,
|
||
is_packaged: true,
|
||
})
|
||
expect(res.success).toBe(true)
|
||
expect(res.is_packaged).toBe(true)
|
||
})
|
||
})
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* cross-region */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('admin.vendorSnippets cross-region (AVEND-32)', () => {
|
||
it('AVEND-32 (current): the public lookup is global, so another region’s snippet resolves', async () => {
|
||
const foreign = await foreignRegion()
|
||
const code = `E2E FOREIGN ${uniqueSuffix()}`
|
||
const created: any = await foreign.client.admin.vendorSnippets.create.mutate({
|
||
snippetCode: code,
|
||
slotId: foreign.slotId,
|
||
skuIds: [foreign.skuId],
|
||
isPermanent: true,
|
||
})
|
||
|
||
// Region-1 staff cannot see it...
|
||
const admin = await staffClient()
|
||
const list: any = await admin.admin.vendorSnippets.getAll.query()
|
||
expect(list.some((entry: any) => entry.id === created.id)).toBe(false)
|
||
|
||
// ...but the public vendor link resolves it with no region filter.
|
||
const res: any = await anonClient().admin.vendorSnippets.getOrdersBySnippet.query({
|
||
snippetCode: code,
|
||
})
|
||
expect(res.success).toBe(true)
|
||
expect(res.snippet.id).toBe(created.id)
|
||
expect(res.data).toEqual([])
|
||
})
|
||
})
|