@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
99 lines (98 loc) • 5.18 kB
JavaScript
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 { REOPTIMIZE, ItemAlreadyAssigned, ShipmentNotFound, InvalidParameter } from "../../models";
import { ShipmentStrategyFactory } from "./strategies";
import { RouteResultEditorBase } from "./route-result-editor-base";
/**
* Editor for managing shipments in a route planner result
*/
export class RouteResultShipmentEditor extends RouteResultEditorBase {
assignShipments(agentIndex, shipmentIndexes, options = {}) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
this.validateAgent(agentIndex);
this.validateShipments(shipmentIndexes, agentIndex);
const strategy = ShipmentStrategyFactory.createAssignStrategy((_a = options.strategy) !== null && _a !== void 0 ? _a : REOPTIMIZE);
const result = yield strategy.execute(this, agentIndex, shipmentIndexes, options);
this.updateIssues();
return result;
});
}
removeShipments(shipmentIndexes, options = {}) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
this.validateShipments(shipmentIndexes);
const strategy = ShipmentStrategyFactory.createRemoveStrategy((_a = options.strategy) !== null && _a !== void 0 ? _a : REOPTIMIZE);
const result = yield strategy.execute(this, shipmentIndexes, options);
this.updateIssues();
return result;
});
}
addNewShipments(agentIndex, shipments, options = {}) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const shipmentsRaw = shipments.map(s => s.getRaw());
this.validateAgent(agentIndex);
this.ensureNewItemsValid(shipmentsRaw, "shipments");
this.validateNewShipmentsHaveLocations(shipmentsRaw);
const newShipmentIndexes = this.appendShipmentsToInput(shipmentsRaw);
const strategy = ShipmentStrategyFactory.createAssignStrategy((_a = options.strategy) !== null && _a !== void 0 ? _a : REOPTIMIZE);
const result = yield strategy.execute(this, agentIndex, newShipmentIndexes, options);
this.updateIssues();
return result;
});
}
validateShipments(shipmentIndexes, agentIndex) {
this.ensureItemsProvided(shipmentIndexes, "shipments");
this.ensureItemsUnique(shipmentIndexes, "shipments");
for (const shipmentIndex of shipmentIndexes) {
this.validateShipmentAssignment(shipmentIndex, agentIndex);
}
}
validateShipmentAssignment(shipmentIndex, agentIndex) {
const realAgentIndexForShipment = this.getAgentIndexForShipment(shipmentIndex);
if (realAgentIndexForShipment === undefined) {
this.validateShipmentExists(shipmentIndex);
}
if (agentIndex !== undefined && realAgentIndexForShipment === agentIndex) {
throw new ItemAlreadyAssigned(`Shipment with index ${shipmentIndex} already assigned to agent with index ${agentIndex}`, 'shipment', shipmentIndex, agentIndex);
}
}
validateShipmentExists(shipmentIndex) {
const shipmentFound = this.rawData.properties.params.shipments[shipmentIndex];
if (!shipmentFound) {
throw new ShipmentNotFound(`Shipment with index ${shipmentIndex} not found`, shipmentIndex);
}
}
appendShipmentsToInput(shipmentsRaw) {
const params = this.rawData.properties.params;
if (!params.shipments) {
params.shipments = [];
}
const startIndex = params.shipments.length;
params.shipments.push(...shipmentsRaw);
return shipmentsRaw.map((_, i) => startIndex + i);
}
validateNewShipmentsHaveLocations(shipmentsRaw) {
for (let i = 0; i < shipmentsRaw.length; i++) {
const shipment = shipmentsRaw[i];
this.validateShipmentStepLocation(shipment.pickup, i, "pickup");
this.validateShipmentStepLocation(shipment.delivery, i, "delivery");
}
}
validateShipmentStepLocation(step, shipmentPosition, stepName) {
if (!step) {
throw new InvalidParameter(`New shipment at position ${shipmentPosition} must have ${stepName} step`, "shipments");
}
if (step.location === undefined && step.location_index === undefined) {
throw new InvalidParameter(`New shipment at position ${shipmentPosition} has ${stepName} step without location or location_index`, "shipments");
}
}
}