matcher/old/modules/ad/domain/entities/geography.ts

93 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-05-22 09:25:09 +00:00
import { Coordinate } from '../../../geography/domain/entities/coordinate';
2023-05-11 15:47:55 +00:00
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';
2023-05-12 14:23:42 +00:00
import { GeorouterSettings } from '../../../geography/domain/types/georouter-settings.type';
2023-05-11 15:47:55 +00:00
export class Geography {
2023-05-22 09:25:09 +00:00
private coordinates: Coordinate[];
2023-05-11 15:47:55 +00:00
driverRoute: Route;
passengerRoute: Route;
2023-05-22 09:25:09 +00:00
constructor(coordinates: Coordinate[]) {
this.coordinates = coordinates;
2023-05-11 15:47:55 +00:00
}
createRoutes = async (
roles: Role[],
georouter: IGeorouter,
2023-05-12 14:23:42 +00:00
settings: GeorouterSettings,
2023-05-11 15:47:55 +00:00
): Promise<void> => {
2023-05-22 09:25:09 +00:00
const paths: Path[] = this.getPaths(roles);
2023-05-12 14:23:42 +00:00
const routes = await georouter.route(paths, settings);
2023-08-16 10:28:20 +00:00
if (routes.some((route) => route.key == RouteType.COMMON)) {
2023-05-11 15:47:55 +00:00
this.driverRoute = routes.find(
2023-08-16 10:28:20 +00:00
(route) => route.key == RouteType.COMMON,
2023-05-11 15:47:55 +00:00
).route;
this.passengerRoute = routes.find(
2023-08-16 10:28:20 +00:00
(route) => route.key == RouteType.COMMON,
2023-05-11 15:47:55 +00:00
).route;
} else {
2023-08-16 10:28:20 +00:00
if (routes.some((route) => route.key == RouteType.DRIVER)) {
2023-05-11 15:47:55 +00:00
this.driverRoute = routes.find(
2023-08-16 10:28:20 +00:00
(route) => route.key == RouteType.DRIVER,
2023-05-11 15:47:55 +00:00
).route;
}
2023-08-16 10:28:20 +00:00
if (routes.some((route) => route.key == RouteType.PASSENGER)) {
2023-05-11 15:47:55 +00:00
this.passengerRoute = routes.find(
2023-08-16 10:28:20 +00:00
(route) => route.key == RouteType.PASSENGER,
2023-05-11 15:47:55 +00:00
).route;
}
}
};
2023-05-22 09:25:09 +00:00
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 = {
2023-08-16 10:28:20 +00:00
key: RouteType.COMMON,
2023-05-22 09:25:09 +00:00
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 {
2023-08-16 10:28:20 +00:00
key: RouteType.DRIVER,
2023-05-22 09:25:09 +00:00
points: this.coordinates,
};
};
private createPassengerPath = (): Path => {
return {
2023-08-16 10:28:20 +00:00
key: RouteType.PASSENGER,
2023-05-22 09:25:09 +00:00
points: [
this.coordinates[0],
this.coordinates[this.coordinates.length - 1],
],
};
2023-05-11 15:47:55 +00:00
};
}
2023-08-16 10:28:20 +00:00
export enum RouteType {
2023-05-11 15:47:55 +00:00
COMMON = 'common',
DRIVER = 'driver',
PASSENGER = 'passenger',
}