import {z} from 'zod' export type FundsUpdatedReasons = 'free_gift' | 'host_cut' | 'referrer_cut' | 'parent_cut' | 'transaction_voided' | 'game_played' | 'game_outcome' | 'pending_outcome' | 'purchase' | 'redeem' | 'refund' | 'payout' | 'admin_update' | 'distributor_cut' | 'achievement'; export type ValidUpdateFunds = 'black' | 'white' | 'coin' | 'point'; export type ValidUpdateFund = {type: ValidUpdateFunds, amount: number}; export type AdminUpdateFundsDto = {reason: FundsUpdatedReasons, walletVersion?: number | undefined, funds: Array, adminDescription?: string | undefined, distributorId?: string | undefined, gameCorrectAnswer?: boolean | undefined, gameDeclared?: boolean | undefined}; export type UpdateApiV1AdminWalletsUserIdArgs = {userId: string, body: AdminUpdateFundsDto}; export type UpdateApiV1AdminWalletsUserIdOptions = {baseUrl: string, accessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const updateWalletResponse = z.object({version: z.number().optional(), updatedAt: z.string()}); 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 }; /** * PUT /v1/admin/wallets/{userId} */ export const updateApiV1AdminWalletsUserId = async (args: UpdateApiV1AdminWalletsUserIdArgs, options: UpdateApiV1AdminWalletsUserIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, accessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (accessToken !== undefined) headers.set('authorization', `Bearer ${accessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/admin/wallets/${encodeURIComponent(String(args.userId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(updateWalletResponse, await res.json()) };