import {z} from 'zod' export type GetApiGeotagsArgs = {order_handle?: (string | null) | undefined, worker_handle?: (string | null) | undefined, driver_handle?: (string | null) | undefined, ops_group_handles?: (string | null) | undefined, search_term?: (string | null) | undefined, device_id?: (string | null) | undefined, device_name?: (string | null) | undefined, driver_profile_filter?: (unknown | null) | undefined, metadata_filter?: (unknown | null) | undefined, system_generated_order_handle?: (boolean | null) | undefined, include_system_generated_order_handle?: (boolean | null) | undefined, region?: (unknown | null) | undefined, pagination_token?: (string | null) | undefined, from_time?: (string | null) | undefined, to_time?: (string | null) | undefined, from_date?: (string | null) | undefined, to_date?: (string | null) | undefined, from_geotag_type?: (string | null) | undefined, to_geotag_type?: (string | null) | undefined, fetch_attachment_links?: boolean | undefined, include_geotags_with_orders?: (boolean | null) | undefined, aggregate?: boolean | undefined, limit?: (number | null) | undefined}; export type GetApiGeotagsOptions = {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 locationWithoutRadius = z.object({address: z.string().nullable().optional(), geometry: z.union([locationGeometry, z.unknown()]).optional()}); export const region = z.object({country: z.string(), city: z.string().nullable().optional(), state: z.string().nullable().optional()}); 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 activitySummary = z.object({duration: z.number().int(), count: z.number().int()}); export const driveSummary = z.object({duration: z.number().int(), count: z.number().int(), distance: z.number().int()}); export const geotagTrackingSummary = z.object({stop: z.union([activitySummary, z.unknown()]).optional(), walk: z.union([activitySummary, z.unknown()]).optional(), drive: z.union([driveSummary, z.unknown()]).optional()}); export const geotag = z.object({geotag_id: z.string(), driver_handle: z.string().optional(), worker_handle: z.string().optional(), recorded_at: z.string(), device_id: z.string(), metadata: z.record(z.string(), z.unknown()).optional(), location: locationWithoutRadius, place_handle: z.string().optional(), expected_location: locationWithoutRadius.optional(), start_location: locationWithoutRadius.optional(), region: region.optional(), distance: z.number().int().optional(), duration: z.number().int().optional(), deviation: z.number().int().optional(), tracking_rate: z.number().optional(), geotag_type: z.string().optional(), order_handle: z.string().optional(), driver_name: z.string().optional(), ops_group_handle: z.string().optional(), system_generated_order_handle: z.boolean().optional(), timezone: z.string().nullable().optional(), attachments: z.array(z.union([imageAttachmentResponse, noteAttachmentResponse, signatureAttachmentResponse])).nullable().optional(), tracking_summary: z.union([geotagTrackingSummary, z.unknown()]).optional()}); export const geotagOrder = z.object({order_handle: z.string(), last_geotag_recorded_at: z.string(), distance: z.number().int(), duration: z.number().int(), driver_handle: z.string().optional(), worker_handle: z.string().optional(), driver_name: z.string().optional(), worker_name: z.string().optional(), device_id: z.string(), tracking_rate: z.number().optional(), ops_group_handle: z.string().optional(), system_generated_order_handle: z.boolean(), geotags: z.array(geotag).optional()}); export const geotagStats = z.object({mean: z.number().nullable().optional(), min: z.number().nullable().optional(), max: z.number().nullable().optional(), stdev: z.number().nullable().optional(), mean_by_driver: z.number().nullable().optional(), sum: z.number()}); export const geotagAggregate = z.object({count: z.number().int().optional(), count_mean: z.number().optional()}); export const driversAggregate = z.object({count: z.number().int().optional()}); export const orderOutageSummary = z.object({duration: z.number().int(), distance: z.number().int().optional(), code: z.string(), label: z.string(), description: z.string(), category: z.enum(["behavioural", "adversarial", "reachability", "os", "sporadic", "null"]).nullable().optional()}); export const geotagOrderAggregate = z.object({count: z.number().int(), distance: geotagStats.optional(), duration: geotagStats.optional(), active_duration: geotagStats.optional(), outage_duration: geotagStats.optional(), tracking_rate: z.number().optional(), deviation: geotagStats.optional(), geotags: geotagAggregate.optional(), drivers: driversAggregate.optional(), workers: driversAggregate.optional(), outage_summary: z.array(orderOutageSummary).nullable().optional()}); export const getGeotagsResponse = z.object({orders: z.array(geotagOrder).nullable().optional(), pagination_token: z.string().nullable().optional(), aggregate: z.union([geotagOrderAggregate, z.unknown()]).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 Geotags * * GET /geotags/ * * Also accepts auth: TokenAuth */ export const getApiGeotags = async (args: GetApiGeotagsArgs, options?: GetApiGeotagsOptions) => { const {baseUrl = "https://v3.api.hypertrack.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.order_handle != null) query.append("order_handle", String(args.order_handle)) if (args.worker_handle != null) query.append("worker_handle", String(args.worker_handle)) if (args.driver_handle != null) query.append("driver_handle", String(args.driver_handle)) if (args.ops_group_handles != null) query.append("ops_group_handles", String(args.ops_group_handles)) if (args.search_term != null) query.append("search_term", String(args.search_term)) if (args.device_id != null) query.append("device_id", String(args.device_id)) if (args.device_name != null) query.append("device_name", String(args.device_name)) if (args.driver_profile_filter != null) query.append("driver_profile_filter", String(args.driver_profile_filter)) if (args.metadata_filter != null) query.append("metadata_filter", String(args.metadata_filter)) if (args.system_generated_order_handle != null) query.append("system_generated_order_handle", String(args.system_generated_order_handle)) if (args.include_system_generated_order_handle != null) query.append("include_system_generated_order_handle", String(args.include_system_generated_order_handle)) if (args.region != null) query.append("region", String(args.region)) if (args.pagination_token != null) query.append("pagination_token", String(args.pagination_token)) if (args.from_time != null) query.append("from_time", String(args.from_time)) if (args.to_time != null) query.append("to_time", String(args.to_time)) if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (args.from_geotag_type != null) query.append("from_geotag_type", String(args.from_geotag_type)) if (args.to_geotag_type != null) query.append("to_geotag_type", String(args.to_geotag_type)) if (args.fetch_attachment_links != null) query.append("fetch_attachment_links", String(args.fetch_attachment_links)) if (args.include_geotags_with_orders != null) query.append("include_geotags_with_orders", String(args.include_geotags_with_orders)) if (args.aggregate != null) query.append("aggregate", String(args.aggregate)) if (args.limit != null) query.append("limit", String(args.limit)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/geotags/${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(getGeotagsResponse, await res.json()) };