import {z} from 'zod' export type GetApiCalendarsCalendarIdAppointmentsArgs = {calendarId: string, eventType?: string | undefined, pageSize?: number | undefined, recordStartIndex?: number | undefined, startDate: string, endDate: string, jobId?: string | undefined}; export type GetApiCalendarsCalendarIdAppointmentsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const companyUserLink = z.object({id: z.string().optional(), _link: z.string().optional()}); export const jobLink = z.object({id: z.string().optional(), _link: z.string().optional()}); export const calendarSearchEvent = z.object({id: z.string().optional(), eventType: z.enum(["All", "Personal", "InitialAppointment", "MaterialDelivery", "CrewLabor"]).optional(), title: z.string().optional(), attendees: z.array(companyUserLink).optional(), job: jobLink.optional(), allDay: z.boolean().optional(), start: z.string().optional(), end: z.string().optional(), jobName: z.string().optional(), location: z.string().optional(), sharedWithCustomerPortal: z.boolean().optional(), _link: z.string().optional()}); export const calendarSearchEventCollection = z.object({count: z.number().int().optional(), pageSize: z.number().int().optional(), pageStartIndex: z.number().int().optional(), items: z.array(calendarSearchEvent).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 Calendar Appointments * * GET /calendars/{calendarId}/appointments */ export const getApiCalendarsCalendarIdAppointments = async (args: GetApiCalendarsCalendarIdAppointmentsArgs, options?: GetApiCalendarsCalendarIdAppointmentsOptions) => { const {baseUrl = "https://api.acculynx.com/api/v2", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.eventType != null) query.append("eventType", String(args.eventType)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.recordStartIndex != null) query.append("recordStartIndex", String(args.recordStartIndex)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.jobId != null) query.append("jobId", String(args.jobId)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/calendars/${encodeURIComponent(String(args.calendarId))}/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(calendarSearchEventCollection, await res.json()) };