import {z} from 'zod' export type RepeatsEnum = 'Monthly' | 'Weekly'; export type RecurringEventSchedule = {created_date?: (string | null) | undefined, description?: string | undefined, dow_friday?: boolean | undefined, dow_monday?: boolean | undefined, dow_saturday?: boolean | undefined, dow_sunday?: boolean | undefined, dow_thursday?: boolean | undefined, dow_tuesday?: boolean | undefined, dow_wednesday?: boolean | undefined, duration: number, event_time: string, id?: number | undefined, physician: number, repeats?: RepeatsEnum | undefined, series_start: string, series_stop?: (string | null) | undefined}; export type TimeSlotTypeEnum = 'appointment' | 'appointment_slot' | 'event'; export type RecurringEventGroup = {created_date?: (string | null) | undefined, deleted_date?: (string | null) | undefined, id?: number | undefined, is_blocking?: boolean | undefined, practice: number, reason?: (string | null) | undefined, schedules: Array, time_slot_type: TimeSlotTypeEnum}; export type CreateApiApi20RecurringEventGroupsArgs = {body: RecurringEventGroup}; export type CreateApiApi20RecurringEventGroupsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const repeatsEnum = z.enum(["Monthly", "Weekly"]); export const recurringEventSchedule = z.object({created_date: z.string().nullable().optional(), description: z.string().optional(), dow_friday: z.boolean().optional(), dow_monday: z.boolean().optional(), dow_saturday: z.boolean().optional(), dow_sunday: z.boolean().optional(), dow_thursday: z.boolean().optional(), dow_tuesday: z.boolean().optional(), dow_wednesday: z.boolean().optional(), duration: z.number().int(), event_time: z.string(), id: z.number().int().optional(), physician: z.number().int(), repeats: repeatsEnum.optional(), series_start: z.string(), series_stop: z.string().nullable().optional()}); export const timeSlotTypeEnum = z.enum(["appointment", "appointment_slot", "event"]); export const recurringEventGroup = z.object({created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), id: z.number().int().optional(), is_blocking: z.boolean().optional(), practice: z.number().int(), reason: z.string().nullable().optional(), schedules: z.array(recurringEventSchedule), time_slot_type: timeSlotTypeEnum}); 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 }; /** * Create Recurring Event Group * * POST /api/2.0/recurring_event_groups/ */ export const createApiApi20RecurringEventGroups = async (args: CreateApiApi20RecurringEventGroupsArgs, options?: CreateApiApi20RecurringEventGroupsOptions) => { const {baseUrl = "https://sandbox.elationemr.com/api/2.0", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/2.0/recurring_event_groups/${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(recurringEventGroup, await res.json()) };