import { Coordinate } from '../../../geography/domain/entities/coordinate'; import { Route } from '../../../geography/domain/entities/route'; import { Role } from '../types/role.enum'; import { IGeorouter } from '../../../geography/domain/interfaces/georouter.interface'; import { Path } from '../../../geography/domain/types/path.type'; import { GeorouterSettings } from '../../../geography/domain/types/georouter-settings.type'; export class Geography { private coordinates: Coordinate[]; driverRoute: Route; passengerRoute: Route; constructor(coordinates: Coordinate[]) { this.coordinates = coordinates; } createRoutes = async ( roles: Role[], georouter: IGeorouter, settings: GeorouterSettings, ): Promise => { const paths: Path[] = this.getPaths(roles); const routes = await georouter.route(paths, settings); if (routes.some((route) => route.key == RouteType.COMMON)) { this.driverRoute = routes.find( (route) => route.key == RouteType.COMMON, ).route; this.passengerRoute = routes.find( (route) => route.key == RouteType.COMMON, ).route; } else { if (routes.some((route) => route.key == RouteType.DRIVER)) { this.driverRoute = routes.find( (route) => route.key == RouteType.DRIVER, ).route; } if (routes.some((route) => route.key == RouteType.PASSENGER)) { this.passengerRoute = routes.find( (route) => route.key == RouteType.PASSENGER, ).route; } } }; private getPaths = (roles: Role[]): Path[] => { const paths: Path[] = []; if (roles.includes(Role.DRIVER) && roles.includes(Role.PASSENGER)) { if (this.coordinates.length == 2) { // 2 points => same route for driver and passenger const commonPath: Path = { key: RouteType.COMMON, points: this.coordinates, }; paths.push(commonPath); } else { const driverPath: Path = this.createDriverPath(); const passengerPath: Path = this.createPassengerPath(); paths.push(driverPath, passengerPath); } } else if (roles.includes(Role.DRIVER)) { const driverPath: Path = this.createDriverPath(); paths.push(driverPath); } else if (roles.includes(Role.PASSENGER)) { const passengerPath: Path = this.createPassengerPath(); paths.push(passengerPath); } return paths; }; private createDriverPath = (): Path => { return { key: RouteType.DRIVER, points: this.coordinates, }; }; private createPassengerPath = (): Path => { return { key: RouteType.PASSENGER, points: [ this.coordinates[0], this.coordinates[this.coordinates.length - 1], ], }; }; } export enum RouteType { COMMON = 'common', DRIVER = 'driver', PASSENGER = 'passenger', }