42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
/**
|
|
* key_val_store constants (delivery charge, min order value, flash gate, …).
|
|
*
|
|
* `admin.const.getConstants` wraps them in an unknown shape, so this normalizes
|
|
* whichever form it returns: `{ constants: [{key,value}] }`, `{ constants: {k:v} }`
|
|
* or a bare map.
|
|
*/
|
|
|
|
import { apiClient } from './api'
|
|
import { staffSession } from './sessions'
|
|
|
|
export type Constants = Record<string, unknown>
|
|
|
|
export async function getConstants(): Promise<Constants> {
|
|
const staff = await staffSession()
|
|
const res = await apiClient(staff.token).admin.const.getConstants.query()
|
|
|
|
const raw =
|
|
(res as { constants?: unknown })?.constants ??
|
|
(res as { data?: unknown })?.data ??
|
|
res
|
|
|
|
if (Array.isArray(raw)) {
|
|
return Object.fromEntries(
|
|
raw.map((entry: { key: string; value: unknown }) => [entry.key, entry.value])
|
|
)
|
|
}
|
|
return (raw ?? {}) as Constants
|
|
}
|
|
|
|
/** Update one constant (used to flip the flash gate, always restored by the caller). */
|
|
export async function setConst(key: string, value: unknown): Promise<void> {
|
|
const staff = await staffSession()
|
|
await apiClient(staff.token).admin.const.updateConstants.mutate({
|
|
constants: [{ key, value }],
|
|
})
|
|
}
|
|
|
|
export function asNumber(value: unknown, fallback = 0): number {
|
|
const parsed = Number(value)
|
|
return Number.isFinite(parsed) ? parsed : fallback
|
|
}
|