import {z} from 'zod' export type ImageAttachmentUpload = {type: 'image', caption: string, data: string, attachment_id?: (string | null) | undefined}; export type NoteAttachmentUpload = {type: 'note', data: string, attachment_id?: (string | null) | undefined}; export type SignatureAttachmentUpload = {type: 'signature', data: string, attachment_id?: (string | null) | undefined}; export type AddAttachmentRequest = {attachments?: (Array | null) | undefined}; export type CreateApiVisitsVisitIdAttachmentsArgs = {visit_id: string, body: AddAttachmentRequest}; export type CreateApiVisitsVisitIdAttachmentsOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const locationGeometry = z.object({type: z.literal("Point"), coordinates: z.array(z.number())}); export const locationInfo = z.object({location: z.union([locationGeometry, z.unknown()]).optional(), recorded_at: z.string(), location_accuracy: z.number().nullable().optional()}); export const enrouteStatsInfo = z.object({duration: z.number().int().nullable().optional(), distance: z.number().int().nullable().optional(), idle_time: z.number().int().nullable().optional(), outage_duration: z.number().int().nullable().optional(), outage_distance: z.number().int().nullable().optional(), outage: z.record(z.string(), z.unknown()).nullable().optional(), tracking_rate: z.number().nullable().optional()}); export const serviceStatsInfo = z.object({duration: z.number().int().nullable().optional(), distance: z.number().int().nullable().optional(), outage_duration: z.number().int().nullable().optional(), outage: z.record(z.string(), z.unknown()).nullable().optional(), tracking_rate: z.number().nullable().optional()}); export const anyLocationGeometry = z.object({type: z.enum(["Point", "Polygon"]), coordinates: z.array(z.unknown())}); export const visitGeometryModel = z.object({radius: z.number().int(), geometry: anyLocationGeometry}); export const imageAttachmentResponse = z.object({type: z.literal("image"), url: z.string(), caption: z.string().nullable().optional(), attachment_id: z.string().nullable().optional()}); export const noteAttachmentResponse = z.object({type: z.literal("note"), data: z.string(), attachment_id: z.string().nullable().optional()}); export const signatureAttachmentResponse = z.object({type: z.literal("signature"), url: z.string(), attachment_id: z.string().nullable().optional()}); export const addAttachmentResponse = z.object({visit_id: z.string(), created_at: z.string(), updated_at: z.string().nullable().optional(), geofence_id: z.string(), driver_handle: z.string().optional(), worker_handle: z.string().optional(), worker_name: z.string().nullable().optional(), worker_profile: z.record(z.string(), z.unknown()).nullable().optional(), device_id: z.string().nullable().optional(), order_handle: z.string(), place_handle: z.string().nullable().optional(), ops_group_handle: z.string(), arrival: z.union([locationInfo, z.unknown()]).optional(), exit: z.union([locationInfo, z.unknown()]).optional(), enroute: z.union([enrouteStatsInfo, z.unknown()]).optional(), service: z.union([serviceStatsInfo, z.unknown()]).optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), geofence_metadata: z.record(z.string(), z.unknown()).nullable().optional(), tracking_rate: z.number().optional(), system_generated_order_handle: z.boolean().nullable().optional(), visit_geometry: z.union([visitGeometryModel, z.unknown()]).optional(), geofence_address: z.string().nullable().optional(), timezone: z.string().nullable().optional(), attachments: z.array(z.union([imageAttachmentResponse, noteAttachmentResponse, signatureAttachmentResponse])).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 }; /** * Add Attachment to Visit * * POST /visits/{visit_id}/attachments * * Also accepts auth: TokenAuth */ export const createApiVisitsVisitIdAttachments = async (args: CreateApiVisitsVisitIdAttachmentsArgs, options?: CreateApiVisitsVisitIdAttachmentsOptions) => { const {baseUrl = "https://v3.api.hypertrack.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/visits/${encodeURIComponent(String(args.visit_id))}/attachments${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(addAttachmentResponse, await res.json()) };