import {z} from 'zod' export type ExtBookingCapacityData = {unit: 'hours' | 'percentage' | 'fraction', timeGranularity?: ('daily' | 'weekly' | 'monthly' | null) | undefined, capacity: number}; export type ExtBooking = {creatorEmail: string, createdAt: number, bookingId?: (number | null) | undefined, startDateIso?: string | undefined, endDateIso?: string | undefined, bookedCapacity?: (ExtBookingCapacityData | null) | undefined, lastModifiedAt?: (number | null) | undefined, endDate?: number | undefined, lastModifierEmail?: (string | null) | undefined, extBookingId: string, startDate?: number | undefined, extDemandId?: (string | null) | undefined, employeeId: string, bookingStatus: 'pending' | 'approved' | 'rejected', bookingType: 'demand' | 'leave' | 'optional_holiday' | 'blocker'}; export type BookingSchema = {createdAt?: (number | null) | undefined, updatedBy?: (string | null) | undefined, isArchived?: (boolean | null) | undefined, startDateIso?: string | undefined, extData?: (ExtBooking | null) | undefined, capacity: number, startDate?: number | undefined, archivedAt?: (number | null) | undefined, bookingStatus: 'pending' | 'approved' | 'rejected', bookingId?: (number | null) | undefined, demandId?: (number | null) | undefined, endDate?: number | undefined, updatedAt?: (number | null) | undefined, employeeProfileId?: (number | null) | undefined, bookingType: 'demand' | 'leave' | 'optional_holiday' | 'blocker', endDateIso?: string | undefined, createdBy?: (string | null) | undefined, employeeEmail: string}; export type CreateApiApiV2ResourceManagementBookingsArgs = {Authorization: string, body: BookingSchema}; export type CreateApiApiV2ResourceManagementBookingsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const extBookingCapacityData = z.object({unit: z.enum(["hours", "percentage", "fraction"]), timeGranularity: z.enum(["daily", "weekly", "monthly"]).nullable().optional(), capacity: z.number()}); export const extBooking = z.object({creatorEmail: z.string(), createdAt: z.number().int(), bookingId: z.number().int().nullable().optional(), startDateIso: z.string().optional(), endDateIso: z.string().optional(), bookedCapacity: extBookingCapacityData.nullable().optional(), lastModifiedAt: z.number().int().nullable().optional(), endDate: z.number().int().optional(), lastModifierEmail: z.string().nullable().optional(), extBookingId: z.string(), startDate: z.number().int().optional(), extDemandId: z.string().nullable().optional(), employeeId: z.string(), bookingStatus: z.enum(["pending", "approved", "rejected"]), bookingType: z.enum(["demand", "leave", "optional_holiday", "blocker"])}); export const bookingSchema = z.object({createdAt: z.number().int().nullable().optional(), updatedBy: z.string().nullable().optional(), isArchived: z.boolean().nullable().optional(), startDateIso: z.string().optional(), extData: extBooking.nullable().optional(), capacity: z.number(), startDate: z.number().int().optional(), archivedAt: z.number().int().nullable().optional(), bookingStatus: z.enum(["pending", "approved", "rejected"]), bookingId: z.number().int().nullable().optional(), demandId: z.number().int().nullable().optional(), endDate: z.number().int().optional(), updatedAt: z.number().int().nullable().optional(), employeeProfileId: z.number().int().nullable().optional(), bookingType: z.enum(["demand", "leave", "optional_holiday", "blocker"]), endDateIso: z.string().optional(), createdBy: z.string().nullable().optional(), employeeEmail: z.string()}); 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 Booking * * POST /api/v2/resource_management/bookings */ export const createApiApiV2ResourceManagementBookings = async (args: CreateApiApiV2ResourceManagementBookingsArgs, options?: CreateApiApiV2ResourceManagementBookingsOptions) => { const {baseUrl = "https://apiv2.eightfold.ai", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/resource_management/bookings${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(bookingSchema, await res.json()) };