From 1dd44b14258a5b66ce2caef42f4462b879e04914 Mon Sep 17 00:00:00 2001 From: sbriat Date: Mon, 21 Aug 2023 15:24:34 +0200 Subject: [PATCH] simplify ad --- src/modules/ad/ad.mapper.ts | 18 +++++++----------- .../application/ports/route-provider.port.ts | 8 ++++---- .../core/application/types/coordinates.type.ts | 4 ++++ .../types/georouter-settings.type.ts | 5 +++++ .../ad/core/application/types/route.type.ts | 11 +++++++++++ .../ad/core/application/types/waypoint.type.ts | 7 ++----- src/modules/ad/core/domain/ad.types.ts | 8 -------- .../value-objects/waypoint.value-object.ts | 6 ------ .../ad/interface/message-handlers/ad.types.ts | 3 +-- src/modules/ad/tests/unit/ad.mapper.spec.ts | 15 ++++++++++++++- .../unit/core/waypoint.value-object.spec.ts | 16 +--------------- 11 files changed, 49 insertions(+), 52 deletions(-) create mode 100644 src/modules/ad/core/application/types/coordinates.type.ts create mode 100644 src/modules/ad/core/application/types/georouter-settings.type.ts create mode 100644 src/modules/ad/core/application/types/route.type.ts diff --git a/src/modules/ad/ad.mapper.ts b/src/modules/ad/ad.mapper.ts index 4412b1e..0f38427 100644 --- a/src/modules/ad/ad.mapper.ts +++ b/src/modules/ad/ad.mapper.ts @@ -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, diff --git a/src/modules/ad/core/application/ports/route-provider.port.ts b/src/modules/ad/core/application/ports/route-provider.port.ts index a7a60a6..7ac93be 100644 --- a/src/modules/ad/core/application/ports/route-provider.port.ts +++ b/src/modules/ad/core/application/ports/route-provider.port.ts @@ -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; } diff --git a/src/modules/ad/core/application/types/coordinates.type.ts b/src/modules/ad/core/application/types/coordinates.type.ts new file mode 100644 index 0000000..8e149ed --- /dev/null +++ b/src/modules/ad/core/application/types/coordinates.type.ts @@ -0,0 +1,4 @@ +export type Coordinates = { + lon: number; + lat: number; +}; diff --git a/src/modules/ad/core/application/types/georouter-settings.type.ts b/src/modules/ad/core/application/types/georouter-settings.type.ts new file mode 100644 index 0000000..ae5b391 --- /dev/null +++ b/src/modules/ad/core/application/types/georouter-settings.type.ts @@ -0,0 +1,5 @@ +export type GeorouterSettings = { + points: boolean; + detailedDuration: boolean; + detailedDistance: boolean; +}; diff --git a/src/modules/ad/core/application/types/route.type.ts b/src/modules/ad/core/application/types/route.type.ts new file mode 100644 index 0000000..971e2a9 --- /dev/null +++ b/src/modules/ad/core/application/types/route.type.ts @@ -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[]; +}; diff --git a/src/modules/ad/core/application/types/waypoint.type.ts b/src/modules/ad/core/application/types/waypoint.type.ts index f3e4a99..ba91158 100644 --- a/src/modules/ad/core/application/types/waypoint.type.ts +++ b/src/modules/ad/core/application/types/waypoint.type.ts @@ -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; diff --git a/src/modules/ad/core/domain/ad.types.ts b/src/modules/ad/core/domain/ad.types.ts index 77fc02e..ad2c7ef 100644 --- a/src/modules/ad/core/domain/ad.types.ts +++ b/src/modules/ad/core/domain/ad.types.ts @@ -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', diff --git a/src/modules/ad/core/domain/value-objects/waypoint.value-object.ts b/src/modules/ad/core/domain/value-objects/waypoint.value-object.ts index 7dcadc2..353f51d 100644 --- a/src/modules/ad/core/domain/value-objects/waypoint.value-object.ts +++ b/src/modules/ad/core/domain/value-objects/waypoint.value-object.ts @@ -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 { @@ -30,10 +28,6 @@ export class Waypoint extends ValueObject { return this.props.lat; } - get context(): PointContext { - return this.props.context; - } - protected validate(props: WaypointProps): void { if (props.position < 0) throw new ArgumentInvalidException( diff --git a/src/modules/ad/interface/message-handlers/ad.types.ts b/src/modules/ad/interface/message-handlers/ad.types.ts index f795309..1deb105 100644 --- a/src/modules/ad/interface/message-handlers/ad.types.ts +++ b/src/modules/ad/interface/message-handlers/ad.types.ts @@ -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; }; diff --git a/src/modules/ad/tests/unit/ad.mapper.spec.ts b/src/modules/ad/tests/unit/ad.mapper.spec.ts index 36f6beb..a854e3d 100644 --- a/src/modules/ad/tests/unit/ad.mapper.spec.ts +++ b/src/modules/ad/tests/unit/ad.mapper.spec.ts @@ -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, + }, + ], })), }; diff --git a/src/modules/ad/tests/unit/core/waypoint.value-object.spec.ts b/src/modules/ad/tests/unit/core/waypoint.value-object.spec.ts index 1d6e334..a2f363f 100644 --- a/src/modules/ad/tests/unit/core/waypoint.value-object.spec.ts +++ b/src/modules/ad/tests/unit/core/waypoint.value-object.spec.ts @@ -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 {