@geoapify/route-planner-sdk
Version:
A TypeScript SDK for the Geoapify Route Planner API that simplifies route optimization requests, and helps visualize and edit resulting routes.
162 lines (161 loc) • 7.62 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 { RouteResultEditorBase } from "./route-result-editor-base";
export class RouteResultShipmentEditor extends RouteResultEditorBase {
assignShipments(agentId, shipmentIds) {
return __awaiter(this, void 0, void 0, function* () {
this.validateAgent(agentId);
this.validateShipments(shipmentIds, agentId);
for (const shipmentId of shipmentIds) {
yield this.assignShipment(shipmentId, agentId);
}
return true;
});
}
removeShipments(shipmentIds) {
return __awaiter(this, void 0, void 0, function* () {
this.validateShipments(shipmentIds);
for (const shipmentId of shipmentIds) {
yield this.removeShipment(shipmentId);
}
return true;
});
}
addNewShipments(agentId, shipments) {
return __awaiter(this, void 0, void 0, function* () {
let shipmentsRaw = shipments.map(shipment => shipment.getRaw());
this.validateAgent(agentId);
this.validateNewShipments(shipmentsRaw);
yield this.addNewShipmentsToAgent(agentId, shipmentsRaw);
return true;
});
}
assignShipment(shipmentId, agentId) {
return __awaiter(this, void 0, void 0, function* () {
let shipmentInfo = this.result.getShipmentInfo(shipmentId);
let newAgentSolution = this.result.getAgentSolution(agentId);
if (newAgentSolution && shipmentInfo) {
yield this.addShipmentToExistingAgent(agentId, shipmentId);
yield this.removeShipmentFromExistingAgent(shipmentInfo);
}
if (newAgentSolution && !shipmentInfo) {
yield this.addShipmentToExistingAgent(agentId, shipmentId);
}
if (!newAgentSolution && shipmentInfo) {
yield this.addShipmentToNonExistingAgent(agentId, shipmentId);
yield this.removeShipmentFromExistingAgent(shipmentInfo);
}
if (!newAgentSolution && !shipmentInfo) {
yield this.addShipmentToNonExistingAgent(agentId, shipmentId);
}
});
}
removeShipment(shipmentId) {
return __awaiter(this, void 0, void 0, function* () {
let shipmentInfo = this.result.getShipmentInfo(shipmentId);
if (shipmentInfo) {
yield this.removeShipmentFromExistingAgent(shipmentInfo);
}
else {
let shipmentInitialInfo = this.getInitialShipmentIndex(shipmentId);
this.result.getRaw().unassignedShipments =
this.result.getRaw().unassignedShipments.filter((shipmentIndex) => shipmentIndex !== shipmentInitialInfo);
}
});
}
addNewShipmentsToAgent(agentId, shipments) {
return __awaiter(this, void 0, void 0, function* () {
let existingAgentSolution = this.result.getAgentSolution(agentId);
this.result.getRaw().inputData.shipments.push(...shipments);
let newAgentInput = this.addShipmentsToAgent(agentId, shipments.map((shipment) => shipment.id), existingAgentSolution);
let optimizedRouterPlan = yield this.optimizeRoute(newAgentInput);
this.updateAgent(optimizedRouterPlan);
});
}
addShipmentToNonExistingAgent(agentId, shipmentId) {
return __awaiter(this, void 0, void 0, function* () {
let newAgentInput = this.addShipmentsToAgent(agentId, [shipmentId]);
let optimizedRouterPlan = yield this.optimizeRoute(newAgentInput);
this.updateAgent(optimizedRouterPlan);
});
}
addShipmentToExistingAgent(agentId, shipmentId) {
return __awaiter(this, void 0, void 0, function* () {
let existingAgentSolution = this.result.getAgentSolution(agentId);
let newAgentInput = this.addShipmentsToAgent(agentId, [shipmentId], existingAgentSolution);
let optimizedRouterPlan = yield this.optimizeRoute(newAgentInput);
this.updateAgent(optimizedRouterPlan);
});
}
removeShipmentFromExistingAgent(shipmentInfo) {
return __awaiter(this, void 0, void 0, function* () {
let existingAgentSolution = shipmentInfo.getAgent();
let newAgentInput = this.removeShipmentFromAgent(existingAgentSolution, shipmentInfo.getAction().getShipmentId());
let optimizedRouterPlan = yield this.optimizeRoute(newAgentInput);
this.updateAgent(optimizedRouterPlan);
});
}
addShipmentsToAgent(agentId, shipmentIds, existingAgent) {
let optimizedAgentInput = this.generateOptimizeAgentInput(agentId, existingAgent);
shipmentIds.forEach(shipmentId => {
optimizedAgentInput.agentShipmentIds.add(shipmentId);
});
return optimizedAgentInput;
}
removeShipmentFromAgent(existingAgent, shipmentId) {
let optimizedAgentInput = this.generateOptimizeAgentInput(existingAgent.getAgentId(), existingAgent);
optimizedAgentInput.agentShipmentIds.delete(shipmentId);
return optimizedAgentInput;
}
validateShipments(shipmentIds, agentId) {
if (shipmentIds.length == 0) {
throw new Error("No shipments provided");
}
if (!this.checkIfArrayIsUnique(shipmentIds)) {
throw new Error("Shipments are not unique");
}
shipmentIds.forEach((shipmentId) => {
let shipmentInfo = this.result.getShipmentInfo(shipmentId);
if (shipmentInfo == undefined) {
this.validateShipmentExists(shipmentId);
}
if (agentId) {
if ((shipmentInfo === null || shipmentInfo === void 0 ? void 0 : shipmentInfo.getAgentId()) == agentId) {
throw new Error(`Shipment with id ${shipmentId} already assigned to agent ${agentId}`);
}
}
});
}
validateShipmentExists(shipmentId) {
let shipmentIndex = this.getInitialShipmentIndex(shipmentId);
if (shipmentIndex == -1) {
throw new Error(`Shipment with id ${shipmentId} not found`);
}
else {
let isUnassignedShipment = this.result.getUnassignedShipments().includes(shipmentIndex);
if (!isUnassignedShipment) {
throw new Error(`Shipment with id ${shipmentId} not found`);
}
}
}
validateNewShipments(shipments) {
if (shipments.length == 0) {
throw new Error("No shipments provided");
}
if (!this.checkIfArrayIsUnique(shipments)) {
throw new Error("Shipments are not unique");
}
shipments.forEach((job) => {
if (job.id == undefined) {
throw new Error("Shipment id is undefined");
}
});
}
}