import {z} from 'zod' export type CreateApiCalendarEventsCalendarEventIdArgs = {calendarEventId: number, body: {startTime: string, endTime: string, eventTitle: string, description?: string | undefined, userId?: number | undefined, linkedUserIds?: Array | undefined, frequency?: ('DAILY' | 'WEEKLY' | 'MONTHLY' | 'YEARLY' | 'NEVER') | undefined, interval?: number | undefined, repeatEndType?: ('NEVER' | 'ON_DATE' | 'AFTER') | undefined, repeatEndDate?: (string | null) | undefined, repeatCount?: number | undefined, repeatSplitOnDate?: (string | null) | undefined, updateAllRecurring?: ('TRUE' | 'FALSE' | null) | undefined, updateAllGrouped?: ('TRUE' | 'FALSE' | null) | undefined}}; export type CreateApiCalendarEventsCalendarEventIdOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const recurringEventDetails = z.object({startDate: z.string(), endDate: z.string().nullable(), rRule: z.string(), text: z.string()}); export const links = z.object({href: z.string(), rel: z.string(), type: z.enum(["GET", "POST", "PUT", "PATCH"])}); export const calendarEvent = z.object({id: z.number(), companyId: z.number(), userId: z.number().nullable(), userName: z.string().nullable(), groupId: z.number().nullable(), title: z.string(), description: z.string().nullable(), eventType: z.enum(["JOB_PHASE", "QUOTE", "ESTIMATE", "OTHER"]), jobPhaseId: z.number().nullable(), jobId: z.number().nullable(), startTime: z.string(), endTime: z.string(), isActive: z.boolean(), isAllDay: z.boolean(), isRecurring: z.boolean(), recurringEventDetails: recurringEventDetails.nullable(), createdAt: z.string(), links: z.array(links)}); export const calendarEventsResponse = z.object({result: z.string(), data: z.array(calendarEvent)}); 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 }; /** * Updates a calendar event. * * POST /calendarEvents/{calendarEventId} * * Also accepts auth: PersonalAccessToken */ export const createApiCalendarEventsCalendarEventId = async (args: CreateApiCalendarEventsCalendarEventIdArgs, options?: CreateApiCalendarEventsCalendarEventIdOptions) => { const {baseUrl = "https://api.fergus.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(/\/+$/, '')}/calendarEvents/${encodeURIComponent(String(args.calendarEventId))}${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(calendarEventsResponse, await res.json()) };