import {z} from 'zod' export type MilestonesSchema = {milestones?: (Array<{event?: ('ARRIVED' | 'START' | 'END' | 'DEPARTED' | 'CHECKED_IN' | 'CHECKED_OUT') | undefined, scheduledDateTime?: string | undefined, actualDateTime?: string | undefined}>) | undefined}; export type UpdateApiServicesAppointmentsV1AppointmentAppointmentIdMilestonesArgs = {appointmentId: number, body: MilestonesSchema}; export type UpdateApiServicesAppointmentsV1AppointmentAppointmentIdMilestonesOptions = {baseUrl?: string | undefined, oAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const appointment = z.object({appointmentId: z.number().int().optional(), scheduleStartDateTime: z.string().optional(), scheduleEndDateTime: z.string().optional(), slotDurationInMinutes: z.number().int().optional()}); export const ymsLocation = z.object({identifier: z.array(z.string()).optional(), siteCode: z.string().optional(), siteName: z.string().optional()}); export const milestones = z.object({actualDateTime: z.string().optional(), event: z.enum(["ARRIVED", "START", "END", "DEPARTED", "CHECKED_IN", "CHECKED_OUT"]).optional(), scheduledDateTime: z.string().optional()}); export const loadInformation = z.object({productType: z.string().optional(), quantity: z.number().int().optional(), sku: z.string().optional()}); export const yardShipment = z.object({carrier: z.string().optional(), cabNo: z.string().optional(), customer: z.string().optional(), comment: z.string().optional(), commodity: z.string().optional(), id: z.string().optional(), loadType: z.enum(["live", "drop"]).optional(), licensePlateNo: z.string().optional(), loadInformation: z.array(loadInformation).optional(), orderIds: z.array(z.string()).optional(), pallets: z.number().int().optional(), reference1: z.string().optional(), reference2: z.string().optional(), serviceType: z.enum(["pickup", "delivery"]).optional(), status: z.string().optional(), trailer: z.string().optional(), units: z.number().int().optional(), vendor: z.string().optional(), weight: z.number().int().optional()}); export const appointmentsResponse = z.object({appointment: appointment.optional(), location: ymsLocation.optional(), milestones: z.array(milestones).optional(), status: z.string().optional(), yardShipment: yardShipment.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 }; /** * Update appointment milestones * * PUT /services/appointments/v1/appointment/{appointmentId}/milestones */ export const updateApiServicesAppointmentsV1AppointmentAppointmentIdMilestones = async (args: UpdateApiServicesAppointmentsV1AppointmentAppointmentIdMilestonesArgs, options?: UpdateApiServicesAppointmentsV1AppointmentAppointmentIdMilestonesOptions) => { const {baseUrl = "https://na12.api.project44.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(/\/+$/, '')}/services/appointments/v1/appointment/${encodeURIComponent(String(args.appointmentId))}/milestones${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(appointmentsResponse, await res.json()) };