simplify ad

This commit is contained in:
sbriat 2023-08-21 15:24:34 +02:00
parent bff199557a
commit 1dd44b1425
11 changed files with 49 additions and 52 deletions

View File

@ -12,7 +12,7 @@ import { ScheduleItemProps } from './core/domain/value-objects/schedule-item.val
import { DirectionEncoderPort } from '@modules/geography/core/application/ports/direction-encoder.port';
import { AD_DIRECTION_ENCODER, AD_ROUTE_PROVIDER } from './ad.di-tokens';
import { RouteProviderPort } from './core/application/ports/route-provider.port';
import { RouteResponseDto } from '@modules/geography/interface/dtos/route.response.dto';
import { Route } from './core/application/types/route.type';
/**
* Mapper constructs objects that are used in different layers:
@ -38,15 +38,11 @@ export class AdMapper
const roles: Role[] = [];
if (copy.driver) roles.push(Role.DRIVER);
if (copy.passenger) roles.push(Role.PASSENGER);
const route: RouteResponseDto = this.routeProvider.get(
roles,
copy.waypoints,
{
withDistance: true,
withPoints: true,
withTime: false,
},
);
const route: Route = this.routeProvider.get(roles, copy.waypoints, {
points: true,
detailedDistance: false,
detailedDuration: false,
});
const record: AdWriteModel = {
uuid: copy.id,
driver: copy.driver,
@ -78,7 +74,7 @@ export class AdMapper
passengerDuration: route.passengerDuration,
passengerDistance: route.passengerDistance,
waypoints: this.directionEncoder.encode(copy.waypoints),
direction: this.directionEncoder.encode(route.spacetimePoints),
direction: this.directionEncoder.encode(route.points),
fwdAzimuth: route.fwdAzimuth,
backAzimuth: route.backAzimuth,
createdAt: copy.createdAt,

View File

@ -1,12 +1,12 @@
import { GeorouterSettings } from '@modules/geography/core/application/types/georouter-settings.type';
import { Waypoint } from '@modules/geography/core/application/types/waypoint.type';
import { RouteResponseDto } from '@modules/geography/interface/dtos/route.response.dto';
import { GeorouterSettings } from '../types/georouter-settings.type';
import { Role } from '../../domain/ad.types';
import { Waypoint } from '../types/waypoint.type';
import { Route } from '../types/route.type';
export interface RouteProviderPort {
get(
roles: Role[],
waypoints: Waypoint[],
georouterSettings: GeorouterSettings,
): RouteResponseDto;
): Route;
}

View File

@ -0,0 +1,4 @@
export type Coordinates = {
lon: number;
lat: number;
};

View File

@ -0,0 +1,5 @@
export type GeorouterSettings = {
points: boolean;
detailedDuration: boolean;
detailedDistance: boolean;
};

View File

@ -0,0 +1,11 @@
import { Coordinates } from './coordinates.type';
export type Route = {
driverDistance?: number;
driverDuration?: number;
passengerDistance?: number;
passengerDuration?: number;
fwdAzimuth: number;
backAzimuth: number;
points: Coordinates[];
};

View File

@ -1,8 +1,5 @@
import { PointContext } from '../../domain/ad.types';
import { Coordinates } from './coordinates.type';
export type Waypoint = {
position: number;
context?: PointContext;
lon: number;
lat: number;
};
} & Coordinates;

View File

@ -41,14 +41,6 @@ export enum Frequency {
RECURRENT = 'RECURRENT',
}
export enum PointContext {
HOUSE_NUMBER = 'HOUSE_NUMBER',
STREET_ADDRESS = 'STREET_ADDRESS',
LOCALITY = 'LOCALITY',
VENUE = 'VENUE',
OTHER = 'OTHER',
}
export enum Role {
DRIVER = 'DRIVER',
PASSENGER = 'PASSENGER',

View File

@ -3,7 +3,6 @@ import {
ArgumentOutOfRangeException,
ValueObject,
} from '@mobicoop/ddd-library';
import { PointContext } from '../ad.types';
/** Note:
* Value Objects with multiple properties can contain
@ -14,7 +13,6 @@ export interface WaypointProps {
position: number;
lon: number;
lat: number;
context?: PointContext;
}
export class Waypoint extends ValueObject<WaypointProps> {
@ -30,10 +28,6 @@ export class Waypoint extends ValueObject<WaypointProps> {
return this.props.lat;
}
get context(): PointContext {
return this.props.context;
}
protected validate(props: WaypointProps): void {
if (props.position < 0)
throw new ArgumentInvalidException(

View File

@ -1,4 +1,4 @@
import { Frequency, PointContext } from '@modules/ad/core/domain/ad.types';
import { Frequency } from '@modules/ad/core/domain/ad.types';
export type Ad = {
id: string;
@ -31,5 +31,4 @@ export type Waypoint = {
country: string;
lon: number;
lat: number;
context?: PointContext;
};

View File

@ -103,7 +103,20 @@ const mockRouteProvider: RouteProviderPort = {
fwdAzimuth: 273,
backAzimuth: 93,
distanceAzimuth: 336544,
spacetimePoints: [],
points: [
{
lon: 6.1765102,
lat: 48.689445,
},
{
lon: 4.984578,
lat: 48.725687,
},
{
lon: 2.3522,
lat: 48.8566,
},
],
})),
};

View File

@ -2,11 +2,10 @@ import {
ArgumentInvalidException,
ArgumentOutOfRangeException,
} from '@mobicoop/ddd-library';
import { PointContext } from '@modules/ad/core/domain/ad.types';
import { Waypoint } from '@modules/ad/core/domain/value-objects/waypoint.value-object';
describe('Waypoint value object', () => {
it('should create a waypoint value object without context', () => {
it('should create a waypoint value object', () => {
const waypointVO = new Waypoint({
position: 0,
lon: 48.689445,
@ -15,19 +14,6 @@ describe('Waypoint value object', () => {
expect(waypointVO.position).toBe(0);
expect(waypointVO.lon).toBe(48.689445);
expect(waypointVO.lat).toBe(6.17651);
expect(waypointVO.context).toBeUndefined();
});
it('should create a waypoint value object with context', () => {
const waypointVO = new Waypoint({
position: 0,
lon: 48.689445,
lat: 6.17651,
context: PointContext.HOUSE_NUMBER,
});
expect(waypointVO.position).toBe(0);
expect(waypointVO.lon).toBe(48.689445);
expect(waypointVO.lat).toBe(6.17651);
expect(waypointVO.context).toBe(PointContext.HOUSE_NUMBER);
});
it('should throw an exception if position is invalid', () => {
try {