UNPKG

@geoapify/route-planner-sdk

Version:

TypeScript SDK for the Geoapify Route Planner API. Supports route optimization, delivery planning, and timeline visualization in browser and Node.js

84 lines (83 loc) 3.5 kB
/** * Helper for route editing: create actions, retrieve job/shipment data, remove items from agents */ export class RouteEditorHelper { static createJobAction(context, jobIndex, waypointIndex) { const job = this.getJobByIndex(context, jobIndex); return { index: 0, type: 'job', start_time: 0, duration: job.duration || 0, job_index: jobIndex, job_id: job.id, waypoint_index: waypointIndex }; } static createShipmentAction(context, shipmentIndex, type, waypointIndex) { var _a, _b; const shipment = this.getShipmentByIndex(context, shipmentIndex); const duration = type === 'pickup' ? (((_a = shipment.pickup) === null || _a === void 0 ? void 0 : _a.duration) || 0) : (((_b = shipment.delivery) === null || _b === void 0 ? void 0 : _b.duration) || 0); return { index: 0, type: type, start_time: 0, duration: duration, shipment_index: shipmentIndex, shipment_id: shipment.id, waypoint_index: waypointIndex }; } static getJobByIndex(context, jobIndex) { return context.getRawData().properties.params.jobs[jobIndex]; } static getShipmentByIndex(context, shipmentIndex) { return context.getRawData().properties.params.shipments[shipmentIndex]; } static resolveShipmentStepLocation(context, step) { return this.resolveShipmentStepWaypointLocationData(context, step).location; } static resolveShipmentStepWaypointLocationData(context, step) { if (step.location) { return { location: step.location }; } if (step.location_index !== undefined) { const rawData = context.getRawData(); const locations = rawData.properties.params.locations; const indexedLocation = locations[step.location_index]; if (!(indexedLocation === null || indexedLocation === void 0 ? void 0 : indexedLocation.location)) { throw new Error(`Shipment step has invalid location_index ${step.location_index}`); } return { location: indexedLocation.location, locationIndex: step.location_index, locationId: indexedLocation.id }; } throw new Error('Shipment step must have either location or location_index'); } static resolveJobLocation(context, job) { return this.resolveJobWaypointLocationData(context, job).location; } static resolveJobWaypointLocationData(context, job) { if (job.location) { return { location: job.location }; } if (job.location_index !== undefined) { const rawData = context.getRawData(); const locations = rawData.properties.params.locations; const indexedLocation = locations[job.location_index]; if (!(indexedLocation === null || indexedLocation === void 0 ? void 0 : indexedLocation.location)) { throw new Error(`Job has invalid location_index ${job.location_index}`); } return { location: indexedLocation.location, locationIndex: job.location_index, locationId: indexedLocation.id }; } throw new Error('Job must have either location or location_index'); } }