import {z} from 'zod' export type GetApiCalendarEventsArgs = {filterCalendarEventType?: ('JOB_PHASE' | 'QUOTE' | 'ESTIMATE' | 'OTHER') | undefined, filterUserId?: number | undefined, filterJobId?: number | undefined, filterJobPhaseId?: number | undefined, filterJobEventsOnly?: boolean | undefined, filterNonJobEventsOnly?: boolean | undefined, filterUnassignedEventsOnly?: boolean | undefined, filterActiveOnly?: boolean | undefined, filterDateFrom?: string | undefined, filterCalendarRange?: ('DAY' | 'THREE_DAY' | 'WEEK' | 'FORTNIGHT' | 'MONTH') | undefined}; export type GetApiCalendarEventsOptions = {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 }; /** * Returns a list of CalendarEvents. The list can be filtered by user name, event type or active events only. * * GET /calendarEvents * * Also accepts auth: PersonalAccessToken */ export const getApiCalendarEvents = async (args: GetApiCalendarEventsArgs, options?: GetApiCalendarEventsOptions) => { const {baseUrl = "https://api.fergus.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filterCalendarEventType != null) query.append("filterCalendarEventType", String(args.filterCalendarEventType)) if (args.filterUserId != null) query.append("filterUserId", String(args.filterUserId)) if (args.filterJobId != null) query.append("filterJobId", String(args.filterJobId)) if (args.filterJobPhaseId != null) query.append("filterJobPhaseId", String(args.filterJobPhaseId)) if (args.filterJobEventsOnly != null) query.append("filterJobEventsOnly", String(args.filterJobEventsOnly)) if (args.filterNonJobEventsOnly != null) query.append("filterNonJobEventsOnly", String(args.filterNonJobEventsOnly)) if (args.filterUnassignedEventsOnly != null) query.append("filterUnassignedEventsOnly", String(args.filterUnassignedEventsOnly)) if (args.filterActiveOnly != null) query.append("filterActiveOnly", String(args.filterActiveOnly)) if (args.filterDateFrom != null) query.append("filterDateFrom", String(args.filterDateFrom)) if (args.filterCalendarRange != null) query.append("filterCalendarRange", String(args.filterCalendarRange)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/calendarEvents${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(calendarEventsResponse, await res.json()) };