freshyo/apps/backend/tests/helpers/consts.ts
2026-10-17 11:03:18 +05:30

85 lines
3.3 KiB
TypeScript

/**
* Constants the api tests read and flip.
*
* Constants live in three scope tables (`global_consts`, `region_consts`,
* `region_bound_consts`) plus the legacy `key_val_store`, and each scope has its
* own endpoint. These suites care about the delivery settings, which are
* BOUND-scoped, so they read/write the region's effective bound — the same bound
* `placeOrder` falls back to for an address with no coordinates (how these suites'
* addresses are created).
*
* Legacy constants are superadmin-only (`superadmin.const.getLegacyConstants`), and
* the test env has no superadmin credentials, so nothing here touches them anymore.
*/
import { apiClient } from './api'
import { staffSession } from './sessions'
export type Constants = Record<string, unknown>
/** The region's effective (lowest-priority) bound, as placeOrder resolves it. */
export async function effectiveBoundId(regionId = 1): Promise<number> {
const staff = await staffSession()
const bounds = await apiClient(staff.token).admin.region.bounds.query()
const bound = bounds.find((b) => b.regionId === regionId) ?? bounds[0]
if (!bound) {
throw new Error(`[api-tests] region ${regionId} has no configured bound`)
}
return bound.id
}
/** DB rows come back as strings; give the suites real booleans and numbers. */
const coerce = (raw: string | null | undefined): unknown => {
if (raw === null || raw === undefined) return undefined
if (raw === 'true') return true
if (raw === 'false') return false
const num = Number(raw)
return raw.trim() !== '' && Number.isFinite(num) ? num : raw
}
/**
* The region's effective bound settings (deliveryCharge, freeDeliveryThreshold,
* flashDeliveryCharge, isFlashDeliveryEnabled).
*/
export async function getConstants(regionId = 1): Promise<Constants> {
const staff = await staffSession()
const boundId = await effectiveBoundId(regionId)
const settings = await apiClient(staff.token).admin.region.getBoundSettings.query({ boundId })
const out: Constants = {}
for (const [key, value] of Object.entries(settings)) out[key] = coerce(value)
return out
}
/** Update one bound constant (used to flip the flash gate, always restored by the caller). */
export async function setConst(key: string, value: unknown, regionId = 1): Promise<void> {
const staff = await staffSession()
const boundId = await effectiveBoundId(regionId)
await apiClient(staff.token).admin.region.updateBoundSettings.mutate({
boundId,
settings: [{ key, value: String(value) }],
})
}
/** The caller's region constants (region_consts for the staff's own region). */
export async function getRegionConstants(): Promise<Constants> {
const staff = await staffSession()
const rows = await apiClient(staff.token).admin.region.getRegionConstants.query()
const out: Constants = {}
for (const { key, value } of rows) out[key] = coerce(value)
return out
}
/** Update one region constant. Null deletes the row. */
export async function setRegionConst(key: string, value: string | number | boolean | null): Promise<void> {
const staff = await staffSession()
await apiClient(staff.token).admin.region.updateRegionConstants.mutate({
constants: [{ key, value }],
})
}
export function asNumber(value: unknown, fallback = 0): number {
const parsed = Number(value)
return Number.isFinite(parsed) ? parsed : fallback
}