use strict null checks

This commit is contained in:
sbriat
2023-08-25 15:16:33 +02:00
parent effe51b9a2
commit f15e7d11b1
16 changed files with 91 additions and 165 deletions

View File

@@ -1,4 +1,4 @@
export type DefaultParams = {
GEOROUTER_TYPE: string;
GEOROUTER_URL: string;
GEOROUTER_TYPE?: string;
GEOROUTER_URL?: string;
};

View File

@@ -25,8 +25,9 @@ export class RouteEntity extends AggregateRoot<RouteProps> {
} catch (e: any) {
throw e;
}
let driverRoute: Route;
let passengerRoute: Route;
let baseRoute: Route;
let driverRoute: Route | undefined;
let passengerRoute: Route | undefined;
if (routes.some((route: Route) => route.type == PathType.GENERIC)) {
driverRoute = passengerRoute = routes.find(
(route: Route) => route.type == PathType.GENERIC,
@@ -41,22 +42,21 @@ export class RouteEntity extends AggregateRoot<RouteProps> {
? routes.find((route: Route) => route.type == PathType.PASSENGER)
: undefined;
}
if (driverRoute) {
baseRoute = driverRoute;
} else {
baseRoute = passengerRoute as Route;
}
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,
fwdAzimuth: baseRoute.fwdAzimuth,
backAzimuth: baseRoute.backAzimuth,
distanceAzimuth: baseRoute.distanceAzimuth,
waypoints: create.waypoints,
points: driverRoute ? driverRoute.points : passengerRoute.points,
points: baseRoute.points,
};
return new RouteEntity({
id: v4(),
@@ -111,52 +111,3 @@ export class RouteEntity extends AggregateRoot<RouteProps> {
points,
});
}
// import { IGeodesic } from '../interfaces/geodesic.interface';
// import { Point } from '../types/point.type';
// import { SpacetimePoint } from './spacetime-point';
// export class Route {
// distance: number;
// duration: number;
// fwdAzimuth: number;
// backAzimuth: number;
// distanceAzimuth: number;
// points: Point[];
// spacetimePoints: SpacetimePoint[];
// private geodesic: IGeodesic;
// constructor(geodesic: IGeodesic) {
// this.distance = undefined;
// this.duration = undefined;
// this.fwdAzimuth = undefined;
// this.backAzimuth = undefined;
// this.distanceAzimuth = undefined;
// this.points = [];
// this.spacetimePoints = [];
// this.geodesic = geodesic;
// }
// setPoints = (points: Point[]): void => {
// this.points = points;
// this.setAzimuth(points);
// };
// setSpacetimePoints = (spacetimePoints: SpacetimePoint[]): void => {
// this.spacetimePoints = spacetimePoints;
// };
// protected setAzimuth = (points: Point[]): void => {
// const inverse = this.geodesic.inverse(
// points[0].lon,
// points[0].lat,
// points[points.length - 1].lon,
// points[points.length - 1].lat,
// );
// this.fwdAzimuth =
// inverse.azimuth >= 0 ? inverse.azimuth : 360 - Math.abs(inverse.azimuth);
// this.backAzimuth =
// this.fwdAzimuth > 180 ? this.fwdAzimuth - 180 : this.fwdAzimuth + 180;
// this.distanceAzimuth = inverse.distance;
// };
// }

View File

@@ -52,7 +52,7 @@ export type Waypoint = Coordinates & {
export type SpacetimePoint = Coordinates & {
duration: number;
distance: number;
distance?: number;
};
export enum Role {

View File

@@ -22,6 +22,7 @@ export class Geodesic implements GeodesicPort {
lat2,
lon2,
);
if (!azimuth || !distance) throw new Error('Azimuth not found');
return { azimuth, distance };
};
}

View File

@@ -72,11 +72,12 @@ export class GraphhopperGeorouter implements GeorouterPort {
.map((point) => [point.lat, point.lon].join('%2C'))
.join('&point='),
].join('');
const route = await lastValueFrom(
return await lastValueFrom(
this.httpService.get(url).pipe(
map((res) =>
res.data ? this.createRoute(res, path.type) : undefined,
),
map((response) => {
if (response.data) return this.createRoute(response, path.type);
throw new Error();
}),
catchError((error: AxiosError) => {
if (error.code == AxiosError.ERR_BAD_REQUEST) {
throw new RouteNotFoundException(
@@ -88,7 +89,6 @@ export class GraphhopperGeorouter implements GeorouterPort {
}),
),
);
return route;
}),
);
return routes;
@@ -156,12 +156,20 @@ export class GraphhopperGeorouter implements GeorouterPort {
const indices = this.getIndices(points, snappedWaypoints);
const times = this.getTimes(durations, indices);
const distances = this.getDistances(instructions, indices);
return indices.map((index) => ({
lon: points[index][1],
lat: points[index][0],
distance: distances.find((distance) => distance.index == index)?.distance,
duration: times.find((time) => time.index == index)?.duration,
}));
return indices.map((index) => {
const duration = times.find((time) => time.index == index);
if (!duration)
throw new Error(`Duration not found for waypoint #${index}`);
const distance = distances.find((distance) => distance.index == index);
if (!distance && instructions.length > 0)
throw new Error(`Distance not found for waypoint #${index}`);
return {
lon: points[index][1],
lat: points[index][0],
distance: distance?.distance,
duration: duration.duration,
};
});
};
private getIndices = (
@@ -182,7 +190,7 @@ export class GraphhopperGeorouter implements GeorouterPort {
index: number;
originIndex: number;
waypoint: number[];
nearest: number;
nearest?: number;
distance: number;
}
>{
@@ -209,7 +217,7 @@ export class GraphhopperGeorouter implements GeorouterPort {
}
}
for (const missedWaypoint of missedWaypoints) {
indices[missedWaypoint.originIndex] = missedWaypoint.nearest;
indices[missedWaypoint.originIndex] = missedWaypoint.nearest as number;
}
return indices;
};

View File

@@ -14,26 +14,20 @@ import { RouteResponseDto } from './interface/dtos/route.response.dto';
export class RouteMapper
implements Mapper<RouteEntity, undefined, undefined, RouteResponseDto>
{
toPersistence = (): undefined => {
return undefined;
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toDomain = (): undefined => {
return undefined;
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toResponse = (entity: RouteEntity): RouteResponseDto => {
const response = new RouteResponseDto();
response.driverDistance = Math.round(entity.getProps().driverDistance);
response.driverDuration = Math.round(entity.getProps().driverDuration);
response.passengerDistance = Math.round(
entity.getProps().passengerDistance,
);
response.passengerDuration = Math.round(
entity.getProps().passengerDuration,
);
response.driverDistance = entity.getProps().driverDistance
? Math.round(entity.getProps().driverDistance as number)
: undefined;
response.driverDuration = entity.getProps().driverDuration
? Math.round(entity.getProps().driverDuration as number)
: undefined;
response.passengerDistance = entity.getProps().passengerDistance
? Math.round(entity.getProps().passengerDistance as number)
: undefined;
response.passengerDuration = entity.getProps().passengerDuration
? Math.round(entity.getProps().passengerDuration as number)
: undefined;
response.fwdAzimuth = Math.round(entity.getProps().fwdAzimuth);
response.backAzimuth = Math.round(entity.getProps().backAzimuth);
response.distanceAzimuth = Math.round(entity.getProps().distanceAzimuth);

View File

@@ -8,7 +8,7 @@ describe('Matcher geodesic', () => {
it('should get inverse values', () => {
const geodesic: Geodesic = new Geodesic();
const inv = geodesic.inverse(0, 0, 1, 1);
expect(Math.round(inv.azimuth)).toBe(45);
expect(Math.round(inv.distance)).toBe(156900);
expect(Math.round(inv.azimuth as number)).toBe(45);
expect(Math.round(inv.distance as number)).toBe(156900);
});
});

View File

@@ -16,14 +16,6 @@ describe('Route Mapper', () => {
expect(routeMapper).toBeDefined();
});
it('should map domain entity to persistence data', async () => {
expect(routeMapper.toPersistence()).toBeUndefined();
});
it('should map persisted data to domain entity', async () => {
expect(routeMapper.toDomain()).toBeUndefined();
});
it('should map domain entity to response', async () => {
const now = new Date();
const routeEntity: RouteEntity = new RouteEntity({