import {z} from 'zod' export type GetApiV2AvailabilityBlockingEntitiesArgs = {date: string, slotLength?: number | undefined, jobTypes?: string | undefined, availableBased?: number | undefined, totalSpots?: number | undefined, scheduleOffset?: number | undefined, showHolidays?: number | undefined, ignoreUserAvailability?: boolean | undefined, Authorization: string}; export type GetApiV2AvailabilityBlockingEntitiesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const blockingEntitiesV2Dto = z.object({type: z.enum(["job", "lead", "task", "event", "recurrence", "timeOff"]).optional(), id: z.string().optional(), name: z.string().optional()}); export const responseBlockingEntityV2Dto = z.object({startTime: z.string().optional(), startTimestamp: z.number().optional(), blockingEntities: z.array(blockingEntitiesV2Dto).optional()}); export const responseBlockingEntitiesV2Dto = z.object({totalResults: z.number().optional(), data: z.array(responseBlockingEntityV2Dto).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 }; /** * Retrieve blocking entities for a date * * GET /v2/availability/blockingEntities */ export const getApiV2AvailabilityBlockingEntities = async (args: GetApiV2AvailabilityBlockingEntitiesArgs, options?: GetApiV2AvailabilityBlockingEntitiesOptions) => { const {baseUrl = "https://api.workiz.com/crm/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.date != null) query.append("date", String(args.date)) if (args.slotLength != null) query.append("slotLength", String(args.slotLength)) if (args.jobTypes != null) query.append("jobTypes", String(args.jobTypes)) if (args.availableBased != null) query.append("availableBased", String(args.availableBased)) if (args.totalSpots != null) query.append("totalSpots", String(args.totalSpots)) if (args.scheduleOffset != null) query.append("scheduleOffset", String(args.scheduleOffset)) if (args.showHolidays != null) query.append("showHolidays", String(args.showHolidays)) if (args.ignoreUserAvailability != null) query.append("ignoreUserAvailability", String(args.ignoreUserAvailability)) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/availability/blockingEntities${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(responseBlockingEntitiesV2Dto, await res.json()) };