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