@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
151 lines (150 loc) • 6.97 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 { RoutePlannerResult } from "./models/entities/route-planner-result";
import { RouteResultJobEditor } from "./tools/route-editor/route-result-job-editor";
import { RouteResultShipmentEditor } from "./tools/route-editor/route-result-shipment-editor";
import { Utils } from "./tools/utils";
export class RoutePlannerResultEditor {
constructor(result) {
this.result = new RoutePlannerResult(Utils.cloneObject(result.getOptions()), Utils.cloneObject(result.getRawData()));
}
/**
* Assigns a job to an agent. Removes the job if it's currently assigned to another agent
* @param agentIdOrIndex - The index/id of the agent.
* @param jobIndexesOrIds - Indexes/ids of the jobs.
* @param newPriority - The new priority of the job.
* @returns {boolean} - Returns true if the job was successfully assigned.
*/
assignJobs(agentIdOrIndex, jobIndexesOrIds, newPriority) {
return __awaiter(this, void 0, void 0, function* () {
this.assertArray(jobIndexesOrIds, "jobIndexesOrIds");
let agentIndex = this.convertAgentToIndex(agentIdOrIndex);
let jobIndexes = this.convertJobsToIndexes(jobIndexesOrIds);
return new RouteResultJobEditor(this.result).assignJobs(agentIndex, jobIndexes, newPriority);
});
}
/**
* Assigns a shipment to an agent. Removes the shipment if it's currently assigned to another agent
* @param shipmentIndexesOrIds - The indexes/ids of the shipments.
* @param agentIdOrIndex - The index/id of the agent.
* @param newPriority - The new priority of the shipment.
* @returns {boolean} - Returns true if the shipment was successfully assigned.
*/
assignShipments(agentIdOrIndex, shipmentIndexesOrIds, newPriority) {
return __awaiter(this, void 0, void 0, function* () {
this.assertArray(shipmentIndexesOrIds, "shipmentIndexesOrIds");
let shipmentIndexes = this.convertShipmentsToIndexes(shipmentIndexesOrIds);
let agentIndex = this.convertAgentToIndex(agentIdOrIndex);
return new RouteResultShipmentEditor(this.result).assignShipments(agentIndex, shipmentIndexes, newPriority);
});
}
/**
* Removes a job from the plan.
* @param jobIndexesOrIds - The indexes/ids of the jobs to remove.
* @returns {boolean} - Returns true if the job was successfully removed.
*/
removeJobs(jobIndexesOrIds) {
return __awaiter(this, void 0, void 0, function* () {
this.assertArray(jobIndexesOrIds, "jobIndexesOrIds");
let jobIndexes = this.convertJobsToIndexes(jobIndexesOrIds);
return new RouteResultJobEditor(this.result).removeJobs(jobIndexes);
});
}
/**
* Removes a shipment from the plan.
* @param shipmentIndexesOrIds - The indexes/ids of the shipments to remove.
* @returns {boolean} - Returns true if the shipment was successfully removed.
*/
removeShipments(shipmentIndexesOrIds) {
return __awaiter(this, void 0, void 0, function* () {
this.assertArray(shipmentIndexesOrIds, "shipmentIndexes");
let shipmentIndexes = this.convertShipmentsToIndexes(shipmentIndexesOrIds);
return new RouteResultShipmentEditor(this.result).removeShipments(shipmentIndexes);
});
}
/**
* Adds new jobs to an agent's schedule.
* @param jobs - An array of job objects to be added.
* @param agentIdOrIndex - The index/id of the agent.
* @returns {boolean} - Returns true if jobs were successfully added.
*/
addNewJobs(agentIdOrIndex, jobs) {
this.assertArray(jobs, "jobs");
let agentIndex = this.convertAgentToIndex(agentIdOrIndex);
return new RouteResultJobEditor(this.result).addNewJobs(agentIndex, jobs);
}
/**
* Adds new shipments to an agent's schedule.
* @param shipments - An array of shipment objects to be added.
* @param agentIdOrIndex - The index/id of the agent.
* @returns {boolean} - Returns true if shipments were successfully added.
*/
addNewShipments(agentIdOrIndex, shipments) {
this.assertArray(shipments, "shipments");
let agentIndex = this.convertAgentToIndex(agentIdOrIndex);
return new RouteResultShipmentEditor(this.result).addNewShipments(agentIndex, shipments);
}
/**
* Returns the modified result.
* @returns {RoutePlannerResult} - The modified result object.
*/
getModifiedResult() {
return this.result;
}
assertArray(array, name) {
if (!Array.isArray(array)) {
throw new Error("Type error: " + name + " must be an array");
}
}
convertJobsToIndexes(jobIndexesOrIds) {
if (typeof jobIndexesOrIds[0] === "number") {
return jobIndexesOrIds;
}
let jobIndexes = [];
jobIndexesOrIds.forEach(jobId => {
let jobIndex = this.result.getData().inputData.jobs.findIndex(job => job.id === jobId);
if (jobIndex < 0) {
throw new Error(`Job with id ${jobId} not found`);
}
else {
jobIndexes.push(jobIndex);
}
});
return jobIndexes;
}
convertShipmentsToIndexes(shipmentIds) {
if (typeof shipmentIds[0] === "number") {
return shipmentIds;
}
let shipmentIndexes = [];
shipmentIds.forEach(shipmentId => {
let shipmentIndex = this.result.getData().inputData.shipments.findIndex(shipment => shipment.id === shipmentId);
if (shipmentIndex < 0) {
throw new Error(`Shipment with id ${shipmentId} not found`);
}
else {
shipmentIndexes.push(shipmentIndex);
}
});
return shipmentIndexes;
}
convertAgentToIndex(agentIdOrIndex) {
if (typeof agentIdOrIndex === "number") {
return agentIdOrIndex;
}
let index = this.result.getData().inputData.agents.findIndex(agent => agent.id === agentIdOrIndex);
if (index === -1) {
throw new Error(`Agent with id ${agentIdOrIndex} not found`);
}
else {
return index;
}
}
}