import {z} from 'zod' export type UpdateSubscriptionRequest = {action: string, days?: (number | null) | undefined, end_date?: (string | null) | undefined, tariff_id?: (number | null) | undefined, traffic_limit_gb?: (number | null) | undefined, traffic_used_gb?: (number | null) | undefined, autopay_enabled?: (boolean | null) | undefined, traffic_gb?: (number | null) | undefined, traffic_purchase_id?: (number | null) | undefined, is_trial?: (boolean | null) | undefined, device_limit?: (number | null) | undefined}; export type CreateApiCabinetAdminUsersUserIdSubscriptionArgs = {user_id: number, body: UpdateSubscriptionRequest}; export type CreateApiCabinetAdminUsersUserIdSubscriptionOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const trafficPurchaseItem = z.object({id: z.number().int(), traffic_gb: z.number().int(), expires_at: z.string(), created_at: z.string(), days_remaining: z.number().int(), is_expired: z.boolean()}); export const userSubscriptionInfo = z.object({id: z.number().int(), status: z.string(), is_trial: z.boolean(), start_date: z.string().nullable().optional(), end_date: z.string().nullable().optional(), traffic_limit_gb: z.number().int().optional(), traffic_used_gb: z.number().optional(), device_limit: z.number().int().optional(), tariff_id: z.number().int().nullable().optional(), tariff_name: z.string().nullable().optional(), autopay_enabled: z.boolean().optional(), is_active: z.boolean().optional(), days_remaining: z.number().int().optional(), purchased_traffic_gb: z.number().int().optional(), traffic_purchases: z.array(trafficPurchaseItem).optional()}); export const updateSubscriptionResponse = z.object({success: z.boolean(), message: z.string(), subscription: userSubscriptionInfo.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 }; /** * Update User Subscription * * POST /cabinet/admin/users/{user_id}/subscription */ export const createApiCabinetAdminUsersUserIdSubscription = async (args: CreateApiCabinetAdminUsersUserIdSubscriptionArgs, options: CreateApiCabinetAdminUsersUserIdSubscriptionOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/cabinet/admin/users/${encodeURIComponent(String(args.user_id))}/subscription${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(updateSubscriptionResponse, await res.json()) };