import {z} from 'zod' export type ViewType = 0 | 1; export type GetApiV1UsersAvailabilityOverviewArgs = {Id?: number | undefined, DisplayName?: string | undefined, Department?: string | undefined, GroupIds?: string | undefined, State?: number | undefined, ViewType?: ViewType | undefined, Page?: number | undefined, ItemsPerPage?: number | undefined, ReturnTotalItems?: boolean | undefined}; export type GetApiV1UsersAvailabilityOverviewOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const userAvailabilityOverviewResponseModel = z.object({userId: z.number().int().optional(), displayName: z.string().nullable().optional(), state: z.number().int().optional(), callAvailability: z.boolean().optional(), dealAvailability: z.boolean().optional(), ticketAvailability: z.boolean().optional(), chatAvailability: z.boolean().optional(), startTime: z.string().optional(), duration: z.number().int().optional()}); export const userAvailabilityOverviewResponseModelQueryResult = z.object({totalItems: z.number().int().optional(), items: z.array(userAvailabilityOverviewResponseModel).nullable().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 }; /** * Get all users with states and availabilities. * * GET /v1/users/availability/overview * * Also accepts auth: API Key */ export const getApiV1UsersAvailabilityOverview = async (args: GetApiV1UsersAvailabilityOverviewArgs, options: GetApiV1UsersAvailabilityOverviewOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Id != null) query.append("Id", String(args.Id)) if (args.DisplayName != null) query.append("DisplayName", String(args.DisplayName)) if (args.Department != null) query.append("Department", String(args.Department)) if (args.GroupIds != null) query.append("GroupIds", String(args.GroupIds)) if (args.State != null) query.append("State", String(args.State)) if (args.ViewType != null) query.append("ViewType", String(args.ViewType)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.ItemsPerPage != null) query.append("ItemsPerPage", String(args.ItemsPerPage)) if (args.ReturnTotalItems != null) query.append("ReturnTotalItems", String(args.ReturnTotalItems)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/users/availability/overview${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(userAvailabilityOverviewResponseModelQueryResult, await res.json()) };