import {z} from 'zod' export type AppointmentCreateRequest = {title: string, description?: (string | null) | undefined, appt_date: number, duration_minutes?: (number | null) | undefined, type?: (string | null) | undefined, outcome?: (string | null) | undefined, status?: (string | null) | undefined, location?: (string | null) | undefined, people_contact_ids?: (Array | null) | undefined, team?: (string | null) | undefined, contact_lead_source?: (string | null) | undefined, marketing_source?: (string | null) | undefined, property_id?: (string | null) | undefined, opportunity_id?: (string | null) | undefined, userid?: (string | null) | undefined}; export type CreateApiReportsAppointmentsArgs = {body: AppointmentCreateRequest}; export type CreateApiReportsAppointmentsOptions = {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 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 }; /** * Post Appointment * * POST /reports/appointments */ export const createApiReportsAppointments = async (args: CreateApiReportsAppointmentsArgs, options: CreateApiReportsAppointmentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("Authorization", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/reports/appointments${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(appointmentResponse, await res.json()) };