import {z} from 'zod' export type GetApiOrdersArgs = {worker_handle?: (string | null) | undefined, place_handle?: (string | null) | undefined, search_term?: (string | null) | undefined, ops_group_handle?: (string | null) | undefined, order_metadata_filter?: (string | null) | undefined, risk_status?: (string | null) | undefined, region_filter?: (string | null) | undefined, status?: (string | null) | undefined, device_id?: (string | null) | undefined, destination_geohash?: (string | null) | undefined, plan_mode: unknown, plan_id?: (string | null) | undefined, route_handle?: (string | null) | undefined, track_mode: unknown, device_switch_mode: unknown, created_at_date?: (string | null) | undefined, created_at_from?: (string | null) | undefined, created_at_to?: (string | null) | undefined, planned_at_date?: (string | null) | undefined, planned_at_from?: (string | null) | undefined, planned_at_to?: (string | null) | undefined, scheduled_at_date?: (string | null) | undefined, scheduled_at_date_without_tz?: (string | null) | undefined, completed_at_date_without_tz?: (string | null) | undefined, scheduled_at_from?: (string | null) | undefined, scheduled_at_to?: (string | null) | undefined, scheduled_at_without_tz_from?: (string | null) | undefined, scheduled_at_without_tz_to?: (string | null) | undefined, cancelled_at_date?: (string | null) | undefined, cancelled_at_from?: (string | null) | undefined, cancelled_at_to?: (string | null) | undefined, completed_at_date?: (string | null) | undefined, completed_at_from?: (string | null) | undefined, completed_at_to?: (string | null) | undefined, rescheduled_marked_at_date?: (string | null) | undefined, rescheduled_marked_at_from?: (string | null) | undefined, rescheduled_marked_at_to?: (string | null) | undefined, route_started_at_date?: (string | null) | undefined, route_started_at_from?: (string | null) | undefined, route_started_at_to?: (string | null) | undefined, completed_at_without_tz_from?: (string | null) | undefined, completed_at_without_tz_to?: (string | null) | undefined, created_at_without_tz_from?: (string | null) | undefined, created_at_without_tz_to?: (string | null) | undefined, created_at_date_without_tz?: (string | null) | undefined, driver_handle?: (string | null) | undefined, sort_direction: unknown, fulfillment_attempt?: (number | null) | undefined, aggregate?: (boolean | null) | undefined, limit?: (number | null) | undefined, pagination_token?: (string | null) | undefined, send_outage?: (boolean | null) | undefined, tracking_status?: ('tracked' | 'untracked' | 'null' | null) | undefined, filter_outage_category?: (string | null) | undefined, worker_profile_filter?: (unknown | null) | undefined, worker_inside_destination?: (boolean | null) | undefined, order_handle?: (string | null) | undefined}; export type GetApiOrdersOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const pointGeometry = z.object({coordinates: z.array(z.number()), type: z.literal("Point").optional()}); export const polygonGeometry = z.object({type: z.literal("Polygon"), coordinates: z.array(z.array(z.array(z.number())))}); export const orderDestinationSpecialType = z.object({geofence_id: z.string().nullable().optional(), place_handle: z.string().nullable().optional(), address: z.string().nullable().optional(), geometry: z.union([pointGeometry, polygonGeometry]), place_id: z.string().nullable().optional(), radius: z.number().int().nullable().optional()}); export const region = z.object({country: z.string(), city: z.string().nullable().optional(), state: z.string().nullable().optional()}); export const locationGeometryResponse = z.object({type: z.string(), coordinates: z.array(z.unknown())}); export const driverLocation = z.object({geometry: locationGeometryResponse, bearing: z.number(), accuracy: z.number(), speed: z.number(), recorded_at: z.string()}); export const orderListEstimate = z.object({start_at: z.string().nullable().optional(), start_by: z.string().nullable().optional(), arrive_at: z.string().nullable().optional(), distance: z.number().int().nullable().optional(), duration: z.number().int().nullable().optional(), initial_arrive_at: z.string().nullable().optional(), current_worker_location: z.union([driverLocation, z.unknown()]).optional()}); export const source = z.object({source: z.string().nullable().optional(), timestamp: z.string().nullable().optional(), "ip-address": z.string().nullable().optional(), device_id: z.string().nullable().optional(), driver_handle: z.string().nullable().optional(), user_id: z.string().nullable().optional(), email: z.string().nullable().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 orderTask = z.object({label: z.string(), task_definition_id: z.string().nullable().optional(), description: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), priority: z.enum(["none", "low", "medium", "high", "null"]).nullable().optional(), task_id: z.string(), status: z.string().nullable().optional()}); export const breakEntry = z.object({type: z.string(), start_time: z.string(), end_time: z.string(), duration: z.number().int()}); export const hypertrackLocationTimesheet = z.object({source: z.string(), type: z.string(), start_time: z.string().nullable().optional(), end_time: z.string().nullable().optional(), total_duration_on_premises: z.number().int(), total_break_duration: z.number().int(), breaks: z.array(breakEntry).nullable().optional(), finalized: z.boolean()}); export const orderResponse = z.object({timezone: z.string().nullable().optional(), order_handle: z.string(), status: z.enum(["planned", "assigned", "dispatched", "ongoing", "disabled", "cancelled", "completed", "summarizing", "unplanned"]), destination: orderDestinationSpecialType, fulfillment_attempt: z.number().int().nullable().optional(), risk_status: z.boolean().nullable().optional(), ops_group_handle: z.string().nullable().optional(), plan_mode: z.unknown(), plan_id: z.string().nullable().optional(), planning_failure_reason: z.string().nullable().optional(), device_id: z.string().nullable().optional(), driver_handle: z.string().nullable().optional(), worker_handle: z.string().nullable().optional(), place_handle: z.string().nullable().optional(), created_at: z.string().nullable().optional(), started_at: z.string().nullable().optional(), assigned_at: z.string().nullable().optional(), driver_name: z.string().nullable().optional(), planned_at: z.string().nullable().optional(), scheduled_at: z.string().nullable().optional(), scheduled_after: z.string().nullable().optional(), completed_at: z.string().nullable().optional(), cancelled_at: z.string().nullable().optional(), disabled_at: z.string().nullable().optional(), enabled_at: z.string().nullable().optional(), arrived_at: z.string().nullable().optional(), exited_at: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), risks: z.record(z.string(), z.unknown()).nullable().optional(), expected_service_time: z.number().int().nullable().optional(), product_type: z.array(z.string()).nullable().optional(), type: z.unknown(), type_index: z.number().int().nullable().optional(), capacity_used: z.number().int().nullable().optional(), region: z.union([region, z.unknown()]).optional(), distance: z.number().int().nullable().optional(), duration: z.number().int().nullable().optional(), estimate: z.union([orderListEstimate, z.unknown()]).optional(), share_url: z.string().nullable().optional(), route_handle: z.string().nullable().optional(), track_mode: z.unknown(), actual_service_time: z.number().int().nullable().optional(), tracked_service_time: z.number().int().nullable().optional(), tracking_rate: z.number().nullable().optional(), time_to_first_event: z.number().nullable().optional(), time_to_first_location: z.number().nullable().optional(), created_by: z.union([source, z.unknown()]).optional(), outage_summary: z.array(orderOutageSummary).nullable().optional(), embed_url: z.string().nullable().optional(), device_switch_mode: z.unknown(), tasks: z.array(orderTask).nullable().optional(), priority: z.unknown(), worker_profile: z.record(z.string(), z.unknown()).nullable().optional(), rescheduled_marked_at: z.string().nullable().optional(), cancellation_source: z.record(z.string(), z.unknown()).nullable().optional(), cancellation_reason: z.string().nullable().optional(), timesheets: z.array(z.union([hypertrackLocationTimesheet, z.record(z.string(), z.unknown())])).nullable().optional()}); export const orderCountAggregateModel = z.object({total: z.number().int(), unplanned: z.number().int(), planned: z.number().int(), assigned: z.number().int(), ongoing: z.number().int(), disabled: z.number().int(), rescheduled: z.number().int(), completed: z.number().int(), cancelled: z.number().int(), tracked: z.number().int(), on_shift: z.number().int(), pre_shift: z.number().int(), full_shift: z.number().int(), arrived: z.number().int(), on_time: z.number().int(), worked: z.number().int(), ttfe30_orders_from_start: z.number().int(), ttfe30_orders_before_schedule_time: z.number().int()}); export const orderTaskAggregate = z.object({total: z.number().int(), status_counts: z.record(z.string(), z.number().int())}); export const baseStats = 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()}); export const driverTotal = z.object({total: z.number().int()}); export const routeTotal = z.object({total: z.number().int()}); export const orderOutageSummaryAgg = 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(), duration_in_tracked_orders: z.number().int().optional(), duration_in_untracked_orders: z.number().int().optional()}); export const ordersTotalWithTTFE = z.object({within_10_min: z.number().int(), within_20_min: z.number().int(), within_30_min: z.number().int(), within_40_min: z.number().int(), within_50_min: z.number().int(), within_60_min: z.number().int(), within_70_min: z.number().int(), within_80_min: z.number().int(), within_90_min: z.number().int()}); export const orderAggregate = z.object({orders: orderCountAggregateModel, tasks: z.union([orderTaskAggregate, z.unknown()]).optional(), distance: z.union([baseStats, z.unknown()]).optional(), duration: z.union([baseStats, z.unknown()]).optional(), tracked_duration: z.number().int().nullable().optional(), active_duration: z.number().int().nullable().optional(), deviation: z.union([baseStats, z.unknown()]).optional(), service_time: z.union([baseStats, z.unknown()]).optional(), completion_delay: z.union([baseStats, z.unknown()]).optional(), drivers: z.union([driverTotal, z.unknown()]).optional(), workers: z.union([driverTotal, z.unknown()]).optional(), tracking_rate: z.number().nullable().optional(), routes: z.union([routeTotal, z.unknown()]).optional(), outage_summary: z.array(orderOutageSummaryAgg).nullable().optional(), orders_count_with_ttfe: z.union([ordersTotalWithTTFE, z.unknown()]).optional()}); export const getOrdersResponse = z.object({orders: z.array(orderResponse).nullable().optional(), pagination_token: z.string().nullable().optional(), aggregate: z.union([orderAggregate, 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 Orders * * GET /orders/ * * Also accepts auth: TokenAuth */ export const getApiOrders = async (args: GetApiOrdersArgs, options?: GetApiOrdersOptions) => { 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.worker_handle != null) query.append("worker_handle", String(args.worker_handle)) if (args.place_handle != null) query.append("place_handle", String(args.place_handle)) if (args.search_term != null) query.append("search_term", String(args.search_term)) if (args.ops_group_handle != null) query.append("ops_group_handle", String(args.ops_group_handle)) if (args.order_metadata_filter != null) query.append("order_metadata_filter", String(args.order_metadata_filter)) if (args.risk_status != null) query.append("risk_status", String(args.risk_status)) if (args.region_filter != null) query.append("region_filter", String(args.region_filter)) if (args.status != null) query.append("status", String(args.status)) if (args.device_id != null) query.append("device_id", String(args.device_id)) if (args.destination_geohash != null) query.append("destination_geohash", String(args.destination_geohash)) if (args.plan_mode != null) query.append("plan_mode", String(args.plan_mode)) if (args.plan_id != null) query.append("plan_id", String(args.plan_id)) if (args.route_handle != null) query.append("route_handle", String(args.route_handle)) if (args.track_mode != null) query.append("track_mode", String(args.track_mode)) if (args.device_switch_mode != null) query.append("device_switch_mode", String(args.device_switch_mode)) if (args.created_at_date != null) query.append("created_at_date", String(args.created_at_date)) if (args.created_at_from != null) query.append("created_at_from", String(args.created_at_from)) if (args.created_at_to != null) query.append("created_at_to", String(args.created_at_to)) if (args.planned_at_date != null) query.append("planned_at_date", String(args.planned_at_date)) if (args.planned_at_from != null) query.append("planned_at_from", String(args.planned_at_from)) if (args.planned_at_to != null) query.append("planned_at_to", String(args.planned_at_to)) if (args.scheduled_at_date != null) query.append("scheduled_at_date", String(args.scheduled_at_date)) if (args.scheduled_at_date_without_tz != null) query.append("scheduled_at_date_without_tz", String(args.scheduled_at_date_without_tz)) if (args.completed_at_date_without_tz != null) query.append("completed_at_date_without_tz", String(args.completed_at_date_without_tz)) if (args.scheduled_at_from != null) query.append("scheduled_at_from", String(args.scheduled_at_from)) if (args.scheduled_at_to != null) query.append("scheduled_at_to", String(args.scheduled_at_to)) if (args.scheduled_at_without_tz_from != null) query.append("scheduled_at_without_tz_from", String(args.scheduled_at_without_tz_from)) if (args.scheduled_at_without_tz_to != null) query.append("scheduled_at_without_tz_to", String(args.scheduled_at_without_tz_to)) if (args.cancelled_at_date != null) query.append("cancelled_at_date", String(args.cancelled_at_date)) if (args.cancelled_at_from != null) query.append("cancelled_at_from", String(args.cancelled_at_from)) if (args.cancelled_at_to != null) query.append("cancelled_at_to", String(args.cancelled_at_to)) if (args.completed_at_date != null) query.append("completed_at_date", String(args.completed_at_date)) if (args.completed_at_from != null) query.append("completed_at_from", String(args.completed_at_from)) if (args.completed_at_to != null) query.append("completed_at_to", String(args.completed_at_to)) if (args.rescheduled_marked_at_date != null) query.append("rescheduled_marked_at_date", String(args.rescheduled_marked_at_date)) if (args.rescheduled_marked_at_from != null) query.append("rescheduled_marked_at_from", String(args.rescheduled_marked_at_from)) if (args.rescheduled_marked_at_to != null) query.append("rescheduled_marked_at_to", String(args.rescheduled_marked_at_to)) if (args.route_started_at_date != null) query.append("route_started_at_date", String(args.route_started_at_date)) if (args.route_started_at_from != null) query.append("route_started_at_from", String(args.route_started_at_from)) if (args.route_started_at_to != null) query.append("route_started_at_to", String(args.route_started_at_to)) if (args.completed_at_without_tz_from != null) query.append("completed_at_without_tz_from", String(args.completed_at_without_tz_from)) if (args.completed_at_without_tz_to != null) query.append("completed_at_without_tz_to", String(args.completed_at_without_tz_to)) if (args.created_at_without_tz_from != null) query.append("created_at_without_tz_from", String(args.created_at_without_tz_from)) if (args.created_at_without_tz_to != null) query.append("created_at_without_tz_to", String(args.created_at_without_tz_to)) if (args.created_at_date_without_tz != null) query.append("created_at_date_without_tz", String(args.created_at_date_without_tz)) if (args.driver_handle != null) query.append("driver_handle", String(args.driver_handle)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.fulfillment_attempt != null) query.append("fulfillment_attempt", String(args.fulfillment_attempt)) if (args.aggregate != null) query.append("aggregate", String(args.aggregate)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.pagination_token != null) query.append("pagination_token", String(args.pagination_token)) if (args.send_outage != null) query.append("send_outage", String(args.send_outage)) if (args.tracking_status != null) query.append("tracking_status", String(args.tracking_status)) if (args.filter_outage_category != null) query.append("filter_outage_category", String(args.filter_outage_category)) if (args.worker_profile_filter != null) query.append("worker_profile_filter", String(args.worker_profile_filter)) if (args.worker_inside_destination != null) query.append("worker_inside_destination", String(args.worker_inside_destination)) if (args.order_handle != null) query.append("order_handle", String(args.order_handle)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/orders/${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(getOrdersResponse, await res.json()) };