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

51 lines (50 loc) 2.54 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { AgentPlanRecalculator, WaypointBuilder } from "../preserve-order"; import { RouteViolationValidator } from "../../validations"; /** * Strategy that removes shipments while preserving the order of remaining items. * Also removes waypoints and rebuilds legs. */ export class ShipmentRemovePreserveOrderStrategy { execute(context, shipmentIndexes, options) { return __awaiter(this, void 0, void 0, function* () { const impactedAgentIndexes = new Set(); for (const shipmentIndex of shipmentIndexes) { const agentIndex = this.removeShipmentFromResult(context, shipmentIndex); if (agentIndex !== -1) { impactedAgentIndexes.add(agentIndex); } } for (const agentIndex of impactedAgentIndexes) { yield AgentPlanRecalculator.recalculate(context, agentIndex); RouteViolationValidator.validate(context, agentIndex); } return true; }); } removeShipmentFromResult(context, shipmentIndex) { const agentIndex = context.getAgentIndexForShipment(shipmentIndex); if (agentIndex === undefined) { return -1; } const feature = context.getAgentFeature(agentIndex); const waypoints = feature.properties.waypoints; const legs = feature.properties.legs || []; WaypointBuilder.removeShipmentsFromWaypoints(waypoints, shipmentIndex); const legDataMap = WaypointBuilder.buildLegDataMap(waypoints, legs); const cleanupResult = WaypointBuilder.removeEmptyWaypoints(waypoints, legDataMap); feature.properties.waypoints = cleanupResult.waypoints; if (cleanupResult.legs) { feature.properties.legs = cleanupResult.legs; } return agentIndex; } }