import {z} from 'zod' export type MindbodyRestApiDtoModelsRecurringAppointmentsRecurringOptions = {StartDate?: string | undefined, EndDate?: string | undefined, StartTime?: string | undefined, FrequencyInterval?: number | undefined, EndBy?: ('ByOccurrence' | 'ByDate') | undefined, MaxOccurrence?: number | undefined, SelectedDays?: (Array<'Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday'>) | undefined, IsSendConfirmationNotification?: boolean | undefined, BookingSource?: string | undefined, IsApplyPayment?: boolean | undefined}; export type MindbodyRestApiDtoModelsRecurringAppointmentsAttendee = {LegacyConsumerId?: number | undefined, ConsumerId?: string | undefined, Name?: string | undefined}; export type MindbodyRestApiDtoModelsRecurringAppointmentsOrganizer = {Attendee?: MindbodyRestApiDtoModelsRecurringAppointmentsAttendee | undefined}; export type MindbodyRestApiDtoModelsRecurringAppointmentsAppointmentResource = {Type?: ('Staff' | 'Room') | undefined, Id?: number | undefined}; export type MindbodyRestApiDtoModelsRecurringAppointmentsItineraryEvent = {ServiceTypeId?: number | undefined, IsAddOn?: boolean | undefined, ServiceGroupId?: number | undefined, DurationInMinutes?: number | undefined, Notes?: string | undefined, Resources?: Array | undefined}; export type MindbodyRestApiDtoModelsRecurringAppointmentsItinerary = {Attendee?: MindbodyRestApiDtoModelsRecurringAppointmentsAttendee | undefined, ItineraryEvents?: Array | undefined}; export type MindbodyRestApiDtoModelsRecurringAppointmentsRecurringRequest = {Options?: MindbodyRestApiDtoModelsRecurringAppointmentsRecurringOptions | undefined, Organizer?: MindbodyRestApiDtoModelsRecurringAppointmentsOrganizer | undefined, LocationId?: number | undefined, Itineraries?: Array | undefined}; export type CreateApiBusinessSchedulesAppointmentsRecurringArgs = {SiteId: number, body: MindbodyRestApiDtoModelsRecurringAppointmentsRecurringRequest}; export type CreateApiBusinessSchedulesAppointmentsRecurringOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const mindbodyRestApiDtoModelsRecurringAppointmentsValidateError = z.object({Date: z.string().optional(), Code: z.string().optional(), Description: z.string().optional()}); export const mindbodyRestApiDtoModelsRecurringAppointmentsBookResponse = z.object({ApptGroupId: z.number().int().optional(), AppointmentIds: z.array(z.number().int()).optional(), Errors: z.array(mindbodyRestApiDtoModelsRecurringAppointmentsValidateError).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 }; /** * Books a recurring appointment group based on the options and returns list of AppointmentIds * * POST /Business/Schedules/Appointments/Recurring */ export const createApiBusinessSchedulesAppointmentsRecurring = async (args: CreateApiBusinessSchedulesAppointmentsRecurringArgs, options?: CreateApiBusinessSchedulesAppointmentsRecurringOptions) => { const {baseUrl = "https://api.mindbodyonline.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.SiteId != null) headers.set("SiteId", String(args.SiteId)) const url = `${baseUrl.replace(/\/+$/, '')}/Business/Schedules/Appointments/Recurring${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(mindbodyRestApiDtoModelsRecurringAppointmentsBookResponse, await res.json()) };