import {z} from 'zod' export type Id = number; export type CustomFields = Record | null; export type CommentsAttributes = {title?: string | undefined, comment?: string | undefined}; export type PatchApiVehicleAssignmentsIdArgs = {id: string, body: {contact_employee_number?: string | undefined, contact_id?: Id | undefined, vehicle_id?: Id | undefined, started_at?: string | undefined, ended_at?: string | undefined, custom_fields?: CustomFields | undefined, starting_meter_entry_attributes?: ({value?: number | undefined, void?: boolean | undefined}) | undefined, ending_meter_entry_attributes?: ({value?: number | undefined, void?: boolean | undefined}) | undefined, comments_attributes?: Array | undefined}}; export type PatchApiVehicleAssignmentsIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, accountToken?: string | undefined, fetch?: typeof fetch | undefined}; export const id = z.number().int().gte(1); export const createdAt = z.string(); export const updatedAt = z.string(); export const customFields = z.object({}).nullable(); export const attachmentPermissions = z.object({read_photos: z.boolean().optional(), manage_photos: z.boolean().optional(), read_documents: z.boolean().optional(), manage_documents: z.boolean().optional()}).nullable(); export const commentableType = z.enum(["Contact", "FleetcorCard", "FuelEntry", "InventoryJournalEntry", "Issue", "Part", "PurchaseOrder", "ServiceEntry", "ServiceReminder", "SubmittedInspectionItem", "Vehicle", "VehicleAssignment", "Vendor", "WexCard", "WorkOrder"]); export const comment = z.object({id: id, created_at: createdAt.optional(), updated_at: updatedAt.optional(), commentable_type: commentableType, commentable_id: id, user_id: id, title: z.string().optional(), comment: z.string(), user_full_name: z.string(), user_image_url: z.string().nullable(), rich_content: z.object({}).nullable(), html_content: z.string().nullable(), with_mentions: z.boolean()}).nullable(); export const vehicleAssignment = z.object({id: id, created_at: createdAt.optional(), updated_at: updatedAt.optional(), vehicle_id: id, contact_id: id, started_at: z.string().nullable(), ended_at: z.string().nullable(), current: z.boolean(), future: z.boolean(), custom_fields: customFields, starting_meter_entry_value: z.string().nullable(), ending_meter_entry_value: z.string().nullable(), comments_count: z.number().int(), contact_full_name: z.string().nullable().optional(), contact_image_url: z.string().nullable().optional(), attachment_permissions: attachmentPermissions.optional(), comments: z.array(comment)}).nullable(); 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 Vehicle Assignment * * PATCH /vehicle_assignments/{id} */ export const patchApiVehicleAssignmentsId = async (args: PatchApiVehicleAssignmentsIdArgs, options?: PatchApiVehicleAssignmentsIdOptions) => { const {baseUrl = "https://secure.fleetio.com/api", fetch: fetchFn = fetch, apiKey, accountToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) if (accountToken !== undefined) headers.set("Account-Token", accountToken) const url = `${baseUrl.replace(/\/+$/, '')}/vehicle_assignments/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(vehicleAssignment, await res.json()) };