import {z} from 'zod' export type GetApiReportsAppointmentsArgs = {start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, user_id?: (string | null) | undefined, type?: (string | null) | undefined, outcome?: (string | null) | undefined, page_size?: number | undefined, last_id?: (string | null) | undefined}; export type GetApiReportsAppointmentsOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const appointmentResponse = z.object({id: z.string(), title: z.string().nullable().optional(), description: z.string().nullable().optional(), appt_date: z.number().int().nullable().optional(), duration_minutes: z.number().int().nullable().optional(), type: z.string().nullable().optional(), outcome: z.string().nullable().optional(), status: z.string().nullable().optional(), location: z.string().nullable().optional(), people_contact_ids: z.array(z.unknown()).nullable().optional(), team: z.string().nullable().optional(), contact_lead_source: z.string().nullable().optional(), marketing_source: z.string().nullable().optional(), property_id: z.string().nullable().optional(), opportunity_id: z.string().nullable().optional(), userid: z.string().nullable().optional(), created_by_userid: z.string().nullable().optional(), created_ts: z.number().nullable().optional(), updated_ts: z.number().nullable().optional()}); export const appointmentListResponse = z.object({appointments: z.array(appointmentResponse), gross_count: z.number().int().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 Appointments * * GET /reports/appointments */ export const getApiReportsAppointments = async (args: GetApiReportsAppointmentsArgs, options: GetApiReportsAppointmentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.user_id != null) query.append("user_id", String(args.user_id)) if (args.type != null) query.append("type", String(args.type)) if (args.outcome != null) query.append("outcome", String(args.outcome)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.last_id != null) query.append("last_id", String(args.last_id)) if (APIKeyHeader !== undefined) headers.set("Authorization", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/reports/appointments${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(appointmentListResponse, await res.json()) };