import {z} from 'zod' export type WeekDayInputModel = {startTime?: number | undefined, endTime?: number | undefined}; export type AvailabilityInputModel = {sun?: WeekDayInputModel | undefined, mon?: WeekDayInputModel | undefined, tue?: WeekDayInputModel | undefined, wed?: WeekDayInputModel | undefined, thu?: WeekDayInputModel | undefined, fri?: WeekDayInputModel | undefined, sat?: WeekDayInputModel | undefined}; export type ServiceSettingsInputModel = {bookAheadUnit?: (number | null) | undefined, bookAheadValue?: (number | null) | undefined, bookInAdvance?: (number | null) | undefined}; export type ServiceOptionsInputModel = {defaultService?: (boolean | null) | undefined, durationSelect?: (boolean | null) | undefined, durationInterval?: (number | null) | undefined, durationMin?: (number | null) | undefined, durationMax?: (number | null) | undefined, padding?: (number | null) | undefined, consumerPadding?: (boolean | null) | undefined}; export type ServiceFeesInputModel = {feeAmount?: number | undefined, feeTaxable?: boolean | undefined, cancellationFeeAmount?: number | undefined, cancellationFeeTaxable?: boolean | undefined, nonRefundable?: boolean | undefined}; export type CustomFieldInputModel = {field1?: (string | null) | undefined, field2?: (string | null) | undefined, field3?: (string | null) | undefined, field4?: (string | null) | undefined, field5?: (string | null) | undefined, field6?: (string | null) | undefined, field7?: (string | null) | undefined, field8?: (string | null) | undefined, field9?: (string | null) | undefined, field10?: (string | null) | undefined}; export type ServiceInputModel = {locationId?: (string | null) | undefined, name?: (string | null) | undefined, description?: (string | null) | undefined, type?: (string | null) | undefined, duration?: number | undefined, bookingLimit?: number | undefined, bookingInterval?: number | undefined, maxCapacity?: number | undefined, maxGroupSize?: number | undefined, serviceGroupId?: (string | null) | undefined, public?: boolean | undefined, mediaPageUrl?: (string | null) | undefined, availability?: AvailabilityInputModel | undefined, settings?: ServiceSettingsInputModel | undefined, options?: ServiceOptionsInputModel | undefined, fees?: ServiceFeesInputModel | undefined, customFields?: CustomFieldInputModel | undefined}; export type CreateApiSetupV1ServicesArgs = {body: ServiceInputModel}; export type CreateApiSetupV1ServicesOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceHourViewModel = z.object({startTime: z.number().int().optional(), endTime: z.number().int().optional()}); export const serviceHoursViewModel = z.object({mon: serviceHourViewModel.optional(), tue: serviceHourViewModel.optional(), wed: serviceHourViewModel.optional(), thu: serviceHourViewModel.optional(), fri: serviceHourViewModel.optional(), sat: serviceHourViewModel.optional(), sun: serviceHourViewModel.optional()}); export const customFieldInputModel = z.object({field1: z.string().nullable().optional(), field2: z.string().nullable().optional(), field3: z.string().nullable().optional(), field4: z.string().nullable().optional(), field5: z.string().nullable().optional(), field6: z.string().nullable().optional(), field7: z.string().nullable().optional(), field8: z.string().nullable().optional(), field9: z.string().nullable().optional(), field10: z.string().nullable().optional()}); export const serviceViewModel = z.object({object: z.string().nullable().optional(), id: z.string().nullable().optional(), companyId: z.string().nullable().optional(), locationId: z.string().nullable().optional(), name: z.string().nullable().optional(), description: z.string().nullable().optional(), type: z.string().nullable().optional(), imageUrl: z.string().nullable().optional(), mediaPageUrl: z.string().nullable().optional(), serviceGroupId: z.number().int().nullable().optional(), serviceGroupName: z.string().nullable().optional(), calendarId: z.string().nullable().optional(), calendarResourceGroupId: z.string().nullable().optional(), bookingLimit: z.number().int().optional(), bookingInterval: z.number().int().optional(), bookAheadUnit: z.number().int().optional(), bookAheadValue: z.number().int().optional(), bookInAdvance: z.number().int().optional(), roundRobin: z.number().int().optional(), showOnline: z.boolean().optional(), defaultService: z.boolean().optional(), duration: z.number().int().optional(), durationSelect: z.boolean().optional(), durationMin: z.number().int().optional(), durationMax: z.number().int().optional(), durationInterval: z.number().int().optional(), padding: z.number().int().optional(), consumerPadding: z.boolean().optional(), feeAmount: z.number().optional(), feeTaxable: z.boolean().optional(), cancellationFeeAmount: z.number().optional(), cancellationFeeTaxable: z.boolean().optional(), nonRefundable: z.boolean().optional(), dailyBookingLimitCount: z.number().int().optional(), dailyBookingLimitMinutes: z.number().int().optional(), maxBookingLimit: z.number().int().optional(), maxResourceBookingLimit: z.number().int().optional(), maxCapacity: z.number().int().optional(), maxGroupSize: z.number().int().optional(), availability: serviceHoursViewModel.optional(), customFields: customFieldInputModel.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 }; /** * Create Service * * POST /setup/v1/services */ export const createApiSetupV1Services = async (args: CreateApiSetupV1ServicesArgs, options?: CreateApiSetupV1ServicesOptions) => { const {baseUrl = "https://sandbox-api.onsched.com", 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(/\/+$/, '')}/setup/v1/services${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(serviceViewModel, await res.json()) };