import {z} from 'zod' export type EventType = 'APPOINTMENT' | 'SHIFT' | 'BLOCK'; export type EventStatus = 'PENDING' | 'RESERVED' | 'CONFIRMED' | 'CANCELLED' | 'COMPLETED' | 'OVERRIDABLE' | 'NO_SHOW'; export type GetApiCalendarEventListArgs = {TENANT: string, 'API-KEY': string, user_ids?: (Array | null) | undefined, types?: (Array | null) | undefined, statuses?: (Array | null) | undefined, start?: (string | null) | undefined, end?: (string | null) | undefined, location_ids?: (Array | null) | undefined}; export type GetApiCalendarEventListOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const eventType = z.enum(["APPOINTMENT", "SHIFT", "BLOCK"]); export const locationTiny = z.object({id: z.string(), isVirtual: z.boolean()}); export const userType = z.enum(["PROVIDER", "PATIENT", "ASSISTANT"]); export const userTiny = z.object({id: z.string(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), phoneNumber: z.string().nullable().optional(), email: z.string().nullable().optional(), type: userType, locationId: z.string().nullable().optional()}); export const inviteStatus = z.enum(["PENDING", "ACCEPTED", "REJECTED"]); export const attendeeDetailOutput = z.object({user: userTiny, inviteStatus: inviteStatus}); export const eventStatus = z.enum(["PENDING", "RESERVED", "CONFIRMED", "CANCELLED", "COMPLETED", "OVERRIDABLE", "NO_SHOW"]); export const recurringEventDetail = z.object({id: z.string(), rrule: z.string(), startDate: z.string(), durationMinutes: z.number().int(), timezone: z.string(), serviceIds: z.array(z.string()).nullable().optional(), capacity: z.number().int().nullable().optional(), type: eventType, title: z.string(), attendeeIds: z.array(z.string()).nullable().optional(), locationId: z.string().nullable().optional(), organizerId: z.string()}); export const serviceTiny = z.object({id: z.string(), duration: z.number().int(), name: z.string(), locationIds: z.array(z.string()).nullable().optional(), color: z.string()}); export const eventServiceBookable = z.object({eventId: z.string(), serviceId: z.string(), bookableId: z.string(), start: z.string(), end: z.string()}); export const eventServiceTiny = z.object({service: serviceTiny, providerId: z.string().nullable().optional(), bookables: z.array(eventServiceBookable).nullable().optional(), start: z.string().nullable().optional(), end: z.string().nullable().optional(), externalId: z.string().nullable().optional(), isArchived: z.boolean().nullable().optional()}); export const eventIcon = z.enum(["BOOKING_FEE_PAID", "NEW_PATIENT", "SCHEDULED_BY_AI", "FORM_COMPLETED", "CHECKED_IN"]); export const eventTinyOutput = z.object({id: z.string(), title: z.string(), start: z.string(), end: z.string(), type: eventType, location: locationTiny.nullable().optional(), attendees: z.array(attendeeDetailOutput).optional(), status: eventStatus, recurringEventId: z.string().nullable().optional(), recurringEvent: recurringEventDetail.nullable().optional(), eventServices: z.array(eventServiceTiny).optional(), icons: z.array(eventIcon).optional(), capacity: z.number().int().nullable().optional()}); export const getApiCalendarEventListResponse = z.array(eventTinyOutput); 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 }; /** * List Events * * GET /calendar/event/list */ export const getApiCalendarEventList = async (args: GetApiCalendarEventListArgs, options: GetApiCalendarEventListOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.TENANT != null) headers.set("TENANT", String(args.TENANT)) if (args['API-KEY'] != null) headers.set("API-KEY", String(args['API-KEY'])) for (const item of args.user_ids ?? []) query.append("user_ids", String(item)) for (const item of args.types ?? []) query.append("types", String(item)) for (const item of args.statuses ?? []) query.append("statuses", String(item)) if (args.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) for (const item of args.location_ids ?? []) query.append("location_ids", String(item)) const url = `${baseUrl.replace(/\/+$/, '')}/calendar/event/list${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(getApiCalendarEventListResponse, await res.json()) };