import {z} from 'zod' export type GetApiCabinetAdminWheelStatisticsArgs = {date_from?: (string | null) | undefined, date_to?: (string | null) | undefined}; export type GetApiCabinetAdminWheelStatisticsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const wheelStatisticsResponse = z.object({total_spins: z.number().int(), total_revenue_kopeks: z.number().int(), total_payout_kopeks: z.number().int(), actual_rtp_percent: z.number(), configured_rtp_percent: z.number().int(), spins_by_payment_type: z.record(z.string(), z.unknown()), prizes_distribution: z.array(z.record(z.string(), z.unknown())), top_wins: z.array(z.record(z.string(), z.unknown())), period_from: z.string().nullable().optional(), period_to: z.string().nullable().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get Statistics * * GET /cabinet/admin/wheel/statistics */ export const getApiCabinetAdminWheelStatistics = async (args: GetApiCabinetAdminWheelStatisticsArgs, options: GetApiCabinetAdminWheelStatisticsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.date_from != null) query.append("date_from", String(args.date_from)) if (args.date_to != null) query.append("date_to", String(args.date_to)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/cabinet/admin/wheel/statistics${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(wheelStatisticsResponse, await res.json()) };