add route provider in ad module

This commit is contained in:
sbriat
2023-08-21 14:40:04 +02:00
parent 88326dcf6f
commit bff199557a
20 changed files with 330 additions and 46 deletions

View File

@@ -0,0 +1,19 @@
import { IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import { GetRouteQuery } from './get-route.query';
import { RouteEntity } from '@modules/geography/core/domain/route.entity';
import { Inject } from '@nestjs/common';
import { GEOROUTER } from '@modules/geography/geography.di-tokens';
import { GeorouterPort } from '../../ports/georouter.port';
@QueryHandler(GetRouteQuery)
export class GetRouteQueryHandler implements IQueryHandler {
constructor(@Inject(GEOROUTER) private readonly georouter: GeorouterPort) {}
execute = async (query: GetRouteQuery): Promise<RouteEntity> =>
await RouteEntity.create({
roles: query.roles,
waypoints: query.waypoints,
georouter: this.georouter,
georouterSettings: query.georouterSettings,
});
}

View File

@@ -0,0 +1,21 @@
import { QueryBase } from '@mobicoop/ddd-library';
import { Role } from '@modules/geography/core/domain/route.types';
import { Waypoint } from '../../types/waypoint.type';
import { GeorouterSettings } from '../../types/georouter-settings.type';
export class GetRouteQuery extends QueryBase {
readonly roles: Role[];
readonly waypoints: Waypoint[];
readonly georouterSettings: GeorouterSettings;
constructor(
roles: Role[],
waypoints: Waypoint[],
georouterSettings: GeorouterSettings,
) {
super();
this.roles = roles;
this.waypoints = waypoints;
this.georouterSettings = georouterSettings;
}
}

View File

@@ -1,11 +1,14 @@
import { PathType } from '../../domain/route.types';
import { Point } from './point.type';
import { SpacetimePoint } from './spacetime-point.type';
export type Route = {
name: string;
type: PathType;
distance: number;
duration: number;
fwdAzimuth: number;
backAzimuth: number;
distanceAzimuth: number;
points: Point[];
spacetimePoints: SpacetimePoint[];
};

View File

@@ -0,0 +1,6 @@
import { Point } from './point.type';
export type SpacetimePoint = Point & {
duration: number;
distance: number;
};

View File

@@ -0,0 +1,5 @@
import { Point } from './point.type';
export type Waypoint = Point & {
position: number;
};

View File

@@ -5,19 +5,61 @@ import {
Role,
RouteProps,
PathType,
Direction,
} from './route.types';
import { WaypointProps } from './value-objects/waypoint.value-object';
import { Route } from '../application/types/route.type';
import { v4 } from 'uuid';
export class RouteEntity extends AggregateRoot<RouteProps> {
protected readonly _id: AggregateID;
static create = async (create: CreateRouteProps): Promise<RouteEntity> => {
const props: RouteProps = await create.georouter.routes(
const directions: Direction[] = await create.georouter.routes(
this.getPaths(create.roles, create.waypoints),
create.georouterSettings,
);
const route = new RouteEntity({ props });
return route;
let driverRoute: Route;
let passengerRoute: Route;
if (directions.some((route: Route) => route.type == PathType.GENERIC)) {
driverRoute = passengerRoute = directions.find(
(route: Route) => route.type == PathType.GENERIC,
);
} else {
driverRoute = directions.some(
(route: Route) => route.type == PathType.DRIVER,
)
? directions.find((route: Route) => route.type == PathType.DRIVER)
: undefined;
passengerRoute = directions.some(
(route: Route) => route.type == PathType.PASSENGER,
)
? directions.find((route: Route) => route.type == PathType.PASSENGER)
: undefined;
}
const routeProps: RouteProps = {
driverDistance: driverRoute?.distance,
driverDuration: driverRoute?.duration,
passengerDistance: passengerRoute?.distance,
passengerDuration: passengerRoute?.duration,
fwdAzimuth: driverRoute
? driverRoute.fwdAzimuth
: passengerRoute.fwdAzimuth,
backAzimuth: driverRoute
? driverRoute.backAzimuth
: passengerRoute.backAzimuth,
distanceAzimuth: driverRoute
? driverRoute.distanceAzimuth
: passengerRoute.distanceAzimuth,
waypoints: create.waypoints,
spacetimePoints: driverRoute
? driverRoute.spacetimePoints
: passengerRoute.spacetimePoints,
};
return new RouteEntity({
id: v4(),
props: routeProps,
});
};
validate(): void {
@@ -32,39 +74,40 @@ export class RouteEntity extends AggregateRoot<RouteProps> {
if (roles.includes(Role.DRIVER) && roles.includes(Role.PASSENGER)) {
if (waypoints.length == 2) {
// 2 points => same route for driver and passenger
const commonPath: Path = {
type: PathType.COMMON,
points: waypoints,
};
paths.push(commonPath);
paths.push(this.createGenericPath(waypoints));
} else {
const driverPath: Path = RouteEntity.createDriverPath(waypoints);
const passengerPath: Path = RouteEntity.createPassengerPath(waypoints);
paths.push(driverPath, passengerPath);
paths.push(
this.createDriverPath(waypoints),
this.createPassengerPath(waypoints),
);
}
} else if (roles.includes(Role.DRIVER)) {
const driverPath: Path = RouteEntity.createDriverPath(waypoints);
paths.push(driverPath);
paths.push(this.createDriverPath(waypoints));
} else if (roles.includes(Role.PASSENGER)) {
const passengerPath: Path = RouteEntity.createPassengerPath(waypoints);
paths.push(passengerPath);
paths.push(this.createPassengerPath(waypoints));
}
return paths;
};
private static createDriverPath = (waypoints: WaypointProps[]): Path => {
return {
type: PathType.DRIVER,
points: waypoints,
};
};
private static createGenericPath = (waypoints: WaypointProps[]): Path =>
this.createPath(waypoints, PathType.GENERIC);
private static createPassengerPath = (waypoints: WaypointProps[]): Path => {
return {
type: PathType.PASSENGER,
points: [waypoints[0], waypoints[waypoints.length - 1]],
};
};
private static createDriverPath = (waypoints: WaypointProps[]): Path =>
this.createPath(waypoints, PathType.DRIVER);
private static createPassengerPath = (waypoints: WaypointProps[]): Path =>
this.createPath(
[waypoints[0], waypoints[waypoints.length - 1]],
PathType.PASSENGER,
);
private static createPath = (
points: WaypointProps[],
type: PathType,
): Path => ({
type,
points,
});
}
// import { IGeodesic } from '../interfaces/geodesic.interface';

View File

@@ -5,9 +5,10 @@ import { WaypointProps } from './value-objects/waypoint.value-object';
// All properties that a Route has
export interface RouteProps {
name: string;
distance: number;
duration: number;
driverDistance?: number;
driverDuration?: number;
passengerDistance?: number;
passengerDuration?: number;
fwdAzimuth: number;
backAzimuth: number;
distanceAzimuth: number;
@@ -23,6 +24,17 @@ export interface CreateRouteProps {
georouterSettings: GeorouterSettings;
}
export type Direction = {
type: PathType;
distance: number;
duration: number;
fwdAzimuth: number;
backAzimuth: number;
distanceAzimuth: number;
points: Point[];
spacetimePoints: SpacetimePoint[];
};
export type Path = {
type: PathType;
points: Point[];
@@ -34,6 +46,11 @@ export type Point = {
context?: PointContext;
};
export type SpacetimePoint = Point & {
duration: number;
distance: number;
};
export enum Role {
DRIVER = 'DRIVER',
PASSENGER = 'PASSENGER',
@@ -48,7 +65,7 @@ export enum PointContext {
}
export enum PathType {
COMMON = 'common',
GENERIC = 'generic',
DRIVER = 'driver',
PASSENGER = 'passenger',
}