matcher/src/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-05-11 15:47:55 +00:00
if (routes.some((route) => route.key == RouteKey.COMMON)) {
this.driverRoute = routes.find(
(route) => route.key == RouteKey.COMMON,
).route;
this.passengerRoute = routes.find(
(route) => route.key == RouteKey.COMMON,
).route;
} else {
if (routes.some((route) => route.key == RouteKey.DRIVER)) {
this.driverRoute = routes.find(
(route) => route.key == RouteKey.DRIVER,
).route;
}
if (routes.some((route) => route.key == RouteKey.PASSENGER)) {
this.passengerRoute = routes.find(
(route) => route.key == RouteKey.PASSENGER,
).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 = {
key: RouteKey.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: RouteKey.DRIVER,
points: this.coordinates,
};
};
private createPassengerPath = (): Path => {
return {
key: RouteKey.PASSENGER,
points: [
this.coordinates[0],
this.coordinates[this.coordinates.length - 1],
],
};
2023-05-11 15:47:55 +00:00
};
}
export enum RouteKey {
COMMON = 'common',
DRIVER = 'driver',
PASSENGER = 'passenger',
}