import {z} from 'zod' export type GetApiCabinetAdminTicketsArgs = {page?: number | undefined, per_page?: number | undefined, status?: (string | null) | undefined, priority?: (string | null) | undefined, user_id?: (number | null) | undefined}; export type GetApiCabinetAdminTicketsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const adminTicketUserInfo = z.object({id: z.number().int(), telegram_id: z.number().int().nullable().optional(), email: z.string().nullable().optional(), username: z.string().nullable().optional(), first_name: z.string().nullable().optional(), last_name: z.string().nullable().optional()}); export const appCabinetSchemasTicketsTicketMessageResponse = z.object({id: z.number().int(), message_text: z.string(), is_from_admin: z.boolean(), has_media: z.boolean().optional(), media_type: z.string().nullable().optional(), media_file_id: z.string().nullable().optional(), media_caption: z.string().nullable().optional(), created_at: z.string()}); export const adminTicketResponse = z.object({id: z.number().int(), title: z.string(), status: z.string(), priority: z.string(), created_at: z.string(), updated_at: z.string(), closed_at: z.string().nullable().optional(), messages_count: z.number().int().optional(), user: adminTicketUserInfo.nullable().optional(), last_message: appCabinetSchemasTicketsTicketMessageResponse.nullable().optional()}); export const adminTicketListResponse = z.object({items: z.array(adminTicketResponse), total: z.number().int(), page: z.number().int(), per_page: z.number().int(), pages: z.number().int()}); 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 All Tickets * * GET /cabinet/admin/tickets */ export const getApiCabinetAdminTickets = async (args: GetApiCabinetAdminTicketsArgs, options: GetApiCabinetAdminTicketsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.status != null) query.append("status", String(args.status)) if (args.priority != null) query.append("priority", String(args.priority)) if (args.user_id != null) query.append("user_id", String(args.user_id)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/cabinet/admin/tickets${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(adminTicketListResponse, await res.json()) };