mirror of
https://gitlab.com/mobicoop/v3/service/matcher.git
synced 2026-01-01 13:52:40 +00:00
move route compute to create service as entity creation is not async
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
export const AD_REPOSITORY = Symbol('AD_REPOSITORY');
|
||||
export const AD_DIRECTION_ENCODER = Symbol('AD_DIRECTION_ENCODER');
|
||||
export const AD_MESSAGE_PUBLISHER = Symbol('AD_MESSAGE_PUBLISHER');
|
||||
export const AD_GET_BASIC_ROUTE_CONTROLLER = Symbol(
|
||||
'AD_GET_BASIC_ROUTE_CONTROLLER',
|
||||
);
|
||||
export const AD_ROUTE_PROVIDER = Symbol('AD_ROUTE_PROVIDER');
|
||||
|
||||
@@ -6,13 +6,11 @@ import {
|
||||
AdReadModel,
|
||||
ScheduleItemModel,
|
||||
} from './infrastructure/ad.repository';
|
||||
import { Frequency, Role } from './core/domain/ad.types';
|
||||
import { Frequency } from './core/domain/ad.types';
|
||||
import { v4 } from 'uuid';
|
||||
import { ScheduleItemProps } from './core/domain/value-objects/schedule-item.value-object';
|
||||
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 { Route } from './core/application/types/route.type';
|
||||
import { AD_DIRECTION_ENCODER } from './ad.di-tokens';
|
||||
|
||||
/**
|
||||
* Mapper constructs objects that are used in different layers:
|
||||
@@ -28,17 +26,11 @@ export class AdMapper
|
||||
constructor(
|
||||
@Inject(AD_DIRECTION_ENCODER)
|
||||
private readonly directionEncoder: DirectionEncoderPort,
|
||||
@Inject(AD_ROUTE_PROVIDER)
|
||||
private readonly routeProvider: RouteProviderPort,
|
||||
) {}
|
||||
|
||||
toPersistence = (entity: AdEntity): AdWriteModel => {
|
||||
const copy = entity.getProps();
|
||||
const now = new Date();
|
||||
const roles: Role[] = [];
|
||||
if (copy.driver) roles.push(Role.DRIVER);
|
||||
if (copy.passenger) roles.push(Role.PASSENGER);
|
||||
const route: Route = this.routeProvider.getBasic(roles, copy.waypoints);
|
||||
const record: AdWriteModel = {
|
||||
uuid: copy.id,
|
||||
driver: copy.driver,
|
||||
@@ -65,14 +57,14 @@ export class AdMapper
|
||||
seatsProposed: copy.seatsProposed,
|
||||
seatsRequested: copy.seatsRequested,
|
||||
strict: copy.strict,
|
||||
driverDuration: route.driverDuration,
|
||||
driverDistance: route.driverDistance,
|
||||
passengerDuration: route.passengerDuration,
|
||||
passengerDistance: route.passengerDistance,
|
||||
driverDuration: copy.driverDuration,
|
||||
driverDistance: copy.driverDistance,
|
||||
passengerDuration: copy.passengerDuration,
|
||||
passengerDistance: copy.passengerDistance,
|
||||
waypoints: this.directionEncoder.encode(copy.waypoints),
|
||||
direction: this.directionEncoder.encode(route.points),
|
||||
fwdAzimuth: route.fwdAzimuth,
|
||||
backAzimuth: route.backAzimuth,
|
||||
direction: this.directionEncoder.encode(copy.points),
|
||||
fwdAzimuth: copy.fwdAzimuth,
|
||||
backAzimuth: copy.backAzimuth,
|
||||
createdAt: copy.createdAt,
|
||||
updatedAt: copy.updatedAt,
|
||||
};
|
||||
@@ -116,6 +108,7 @@ export class AdMapper
|
||||
})),
|
||||
fwdAzimuth: record.fwdAzimuth,
|
||||
backAzimuth: record.backAzimuth,
|
||||
points: [],
|
||||
},
|
||||
});
|
||||
return entity;
|
||||
|
||||
@@ -1,20 +1,32 @@
|
||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
|
||||
import { CreateAdCommand } from './create-ad.command';
|
||||
import { Inject } from '@nestjs/common';
|
||||
import { AD_REPOSITORY } from '@modules/ad/ad.di-tokens';
|
||||
import { AD_REPOSITORY, AD_ROUTE_PROVIDER } from '@modules/ad/ad.di-tokens';
|
||||
import { AdEntity } from '@modules/ad/core/domain/ad.entity';
|
||||
import { AdRepositoryPort } from '../../ports/ad.repository.port';
|
||||
import { AggregateID, ConflictException } from '@mobicoop/ddd-library';
|
||||
import { AdAlreadyExistsException } from '@modules/ad/core/domain/ad.errors';
|
||||
import { RouteProviderPort } from '../../ports/route-provider.port';
|
||||
import { Role } from '@modules/ad/core/domain/ad.types';
|
||||
import { Route } from '../../types/route.type';
|
||||
|
||||
@CommandHandler(CreateAdCommand)
|
||||
export class CreateAdService implements ICommandHandler {
|
||||
constructor(
|
||||
@Inject(AD_REPOSITORY)
|
||||
private readonly repository: AdRepositoryPort,
|
||||
@Inject(AD_ROUTE_PROVIDER)
|
||||
private readonly routeProvider: RouteProviderPort,
|
||||
) {}
|
||||
|
||||
async execute(command: CreateAdCommand): Promise<AggregateID> {
|
||||
const roles: Role[] = [];
|
||||
if (command.driver) roles.push(Role.DRIVER);
|
||||
if (command.passenger) roles.push(Role.PASSENGER);
|
||||
const route: Route = await this.routeProvider.getBasic(
|
||||
roles,
|
||||
command.waypoints,
|
||||
);
|
||||
const ad = AdEntity.create({
|
||||
id: command.id,
|
||||
driver: command.driver,
|
||||
@@ -27,6 +39,13 @@ export class CreateAdService implements ICommandHandler {
|
||||
seatsRequested: command.seatsRequested,
|
||||
strict: command.strict,
|
||||
waypoints: command.waypoints,
|
||||
points: route.points,
|
||||
driverDistance: route.driverDistance,
|
||||
driverDuration: route.driverDuration,
|
||||
passengerDistance: route.passengerDistance,
|
||||
passengerDuration: route.passengerDuration,
|
||||
fwdAzimuth: route.fwdAzimuth,
|
||||
backAzimuth: route.backAzimuth,
|
||||
});
|
||||
|
||||
try {
|
||||
|
||||
@@ -6,5 +6,5 @@ export interface RouteProviderPort {
|
||||
/**
|
||||
* Get a basic route with points and overall duration / distance
|
||||
*/
|
||||
getBasic(roles: Role[], waypoints: Waypoint[]): Route;
|
||||
getBasic(roles: Role[], waypoints: Waypoint[]): Promise<Route>;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { PointProps } from './value-objects/point.value-object';
|
||||
import { ScheduleItemProps } from './value-objects/schedule-item.value-object';
|
||||
import { WaypointProps } from './value-objects/waypoint.value-object';
|
||||
|
||||
@@ -17,8 +18,9 @@ export interface AdProps {
|
||||
passengerDuration?: number;
|
||||
passengerDistance?: number;
|
||||
waypoints: WaypointProps[];
|
||||
fwdAzimuth?: number;
|
||||
backAzimuth?: number;
|
||||
points: PointProps[];
|
||||
fwdAzimuth: number;
|
||||
backAzimuth: number;
|
||||
}
|
||||
|
||||
// Properties that are needed for an Ad creation
|
||||
@@ -34,6 +36,13 @@ export interface CreateAdProps {
|
||||
seatsRequested: number;
|
||||
strict: boolean;
|
||||
waypoints: WaypointProps[];
|
||||
driverDuration?: number;
|
||||
driverDistance?: number;
|
||||
passengerDuration?: number;
|
||||
passengerDistance?: number;
|
||||
fwdAzimuth: number;
|
||||
backAzimuth: number;
|
||||
points: PointProps[];
|
||||
}
|
||||
|
||||
export enum Frequency {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import {
|
||||
ArgumentOutOfRangeException,
|
||||
ValueObject,
|
||||
} from '@mobicoop/ddd-library';
|
||||
|
||||
/** Note:
|
||||
* Value Objects with multiple properties can contain
|
||||
* other Value Objects inside if needed.
|
||||
* */
|
||||
|
||||
export interface PointProps {
|
||||
lon: number;
|
||||
lat: number;
|
||||
}
|
||||
|
||||
export class Point extends ValueObject<PointProps> {
|
||||
get lon(): number {
|
||||
return this.props.lon;
|
||||
}
|
||||
|
||||
get lat(): number {
|
||||
return this.props.lat;
|
||||
}
|
||||
|
||||
protected validate(props: PointProps): void {
|
||||
if (props.lon > 180 || props.lon < -180)
|
||||
throw new ArgumentOutOfRangeException('lon must be between -180 and 180');
|
||||
if (props.lat > 90 || props.lat < -90)
|
||||
throw new ArgumentOutOfRangeException('lat must be between -90 and 90');
|
||||
}
|
||||
}
|
||||
21
src/modules/ad/infrastructure/route-provider.ts
Normal file
21
src/modules/ad/infrastructure/route-provider.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { RouteProviderPort } from '../core/application/ports/route-provider.port';
|
||||
import { Route } from '../core/application/types/route.type';
|
||||
import { Waypoint } from '../core/application/types/waypoint.type';
|
||||
import { Role } from '../core/domain/ad.types';
|
||||
import { GetBasicRouteControllerPort } from '@modules/geography/core/application/ports/get-basic-route-controller.port';
|
||||
import { AD_GET_BASIC_ROUTE_CONTROLLER } from '../ad.di-tokens';
|
||||
|
||||
@Injectable()
|
||||
export class RouteProvider implements RouteProviderPort {
|
||||
constructor(
|
||||
@Inject(AD_GET_BASIC_ROUTE_CONTROLLER)
|
||||
private readonly getBasicRouteController: GetBasicRouteControllerPort,
|
||||
) {}
|
||||
|
||||
getBasic = async (roles: Role[], waypoints: Waypoint[]): Promise<Route> =>
|
||||
await this.getBasicRouteController.get({
|
||||
roles,
|
||||
waypoints,
|
||||
});
|
||||
}
|
||||
@@ -1,9 +1,5 @@
|
||||
import {
|
||||
AD_DIRECTION_ENCODER,
|
||||
AD_ROUTE_PROVIDER,
|
||||
} from '@modules/ad/ad.di-tokens';
|
||||
import { AD_DIRECTION_ENCODER } from '@modules/ad/ad.di-tokens';
|
||||
import { AdMapper } from '@modules/ad/ad.mapper';
|
||||
import { RouteProviderPort } from '@modules/ad/core/application/ports/route-provider.port';
|
||||
import { AdEntity } from '@modules/ad/core/domain/ad.entity';
|
||||
import { Frequency } from '@modules/ad/core/domain/ad.types';
|
||||
import {
|
||||
@@ -44,6 +40,26 @@ const adEntity: AdEntity = new AdEntity({
|
||||
strict: false,
|
||||
seatsProposed: 3,
|
||||
seatsRequested: 1,
|
||||
driverDistance: 350101,
|
||||
driverDuration: 14422,
|
||||
passengerDistance: 350101,
|
||||
passengerDuration: 14422,
|
||||
fwdAzimuth: 273,
|
||||
backAzimuth: 93,
|
||||
points: [
|
||||
{
|
||||
lon: 6.1765102,
|
||||
lat: 48.689445,
|
||||
},
|
||||
{
|
||||
lon: 4.984578,
|
||||
lat: 48.725687,
|
||||
},
|
||||
{
|
||||
lon: 2.3522,
|
||||
lat: 48.8566,
|
||||
},
|
||||
],
|
||||
},
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
@@ -104,32 +120,6 @@ const mockDirectionEncoder: DirectionEncoderPort = {
|
||||
]),
|
||||
};
|
||||
|
||||
const mockRouteProvider: RouteProviderPort = {
|
||||
getBasic: jest.fn().mockImplementation(() => ({
|
||||
driverDistance: 350101,
|
||||
driverDuration: 14422,
|
||||
passengerDistance: 350101,
|
||||
passengerDuration: 14422,
|
||||
fwdAzimuth: 273,
|
||||
backAzimuth: 93,
|
||||
distanceAzimuth: 336544,
|
||||
points: [
|
||||
{
|
||||
lon: 6.1765102,
|
||||
lat: 48.689445,
|
||||
},
|
||||
{
|
||||
lon: 4.984578,
|
||||
lat: 48.725687,
|
||||
},
|
||||
{
|
||||
lon: 2.3522,
|
||||
lat: 48.8566,
|
||||
},
|
||||
],
|
||||
})),
|
||||
};
|
||||
|
||||
describe('Ad Mapper', () => {
|
||||
let adMapper: AdMapper;
|
||||
|
||||
@@ -141,10 +131,6 @@ describe('Ad Mapper', () => {
|
||||
provide: AD_DIRECTION_ENCODER,
|
||||
useValue: mockDirectionEncoder,
|
||||
},
|
||||
{
|
||||
provide: AD_ROUTE_PROVIDER,
|
||||
useValue: mockRouteProvider,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
adMapper = module.get<AdMapper>(AdMapper);
|
||||
|
||||
@@ -31,6 +31,13 @@ const createAdProps: CreateAdProps = {
|
||||
seatsRequested: 1,
|
||||
strict: false,
|
||||
waypoints: [originWaypointProps, destinationWaypointProps],
|
||||
driverDistance: 23000,
|
||||
driverDuration: 900,
|
||||
passengerDistance: 23000,
|
||||
passengerDuration: 900,
|
||||
fwdAzimuth: 283,
|
||||
backAzimuth: 93,
|
||||
points: [],
|
||||
};
|
||||
|
||||
describe('Ad entity create', () => {
|
||||
@@ -42,6 +49,6 @@ describe('Ad entity create', () => {
|
||||
expect(ad.getProps().schedule[0].time).toBe('08:30');
|
||||
expect(ad.getProps().driver).toBeTruthy();
|
||||
expect(ad.getProps().passenger).toBeTruthy();
|
||||
expect(ad.getProps().driverDistance).toBeUndefined();
|
||||
expect(ad.getProps().driverDistance).toBe(23000);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AD_REPOSITORY } from '@modules/ad/ad.di-tokens';
|
||||
import { AD_REPOSITORY, AD_ROUTE_PROVIDER } from '@modules/ad/ad.di-tokens';
|
||||
import { AggregateID } from '@mobicoop/ddd-library';
|
||||
import { AdEntity } from '@modules/ad/core/domain/ad.entity';
|
||||
import { ConflictException } from '@mobicoop/ddd-library';
|
||||
@@ -8,6 +8,7 @@ import { CreateAdService } from '@modules/ad/core/application/commands/create-ad
|
||||
import { CreateAdCommand } from '@modules/ad/core/application/commands/create-ad/create-ad.command';
|
||||
import { AdAlreadyExistsException } from '@modules/ad/core/domain/ad.errors';
|
||||
import { WaypointProps } from '@modules/ad/core/domain/value-objects/waypoint.value-object';
|
||||
import { RouteProviderPort } from '@modules/ad/core/application/ports/route-provider.port';
|
||||
|
||||
const originWaypoint: WaypointProps = {
|
||||
position: 0,
|
||||
@@ -37,6 +38,13 @@ const createAdProps: CreateAdProps = {
|
||||
strict: false,
|
||||
frequency: Frequency.PUNCTUAL,
|
||||
waypoints: [originWaypoint, destinationWaypoint],
|
||||
driverDistance: 23000,
|
||||
driverDuration: 900,
|
||||
passengerDistance: 23000,
|
||||
passengerDuration: 900,
|
||||
fwdAzimuth: 283,
|
||||
backAzimuth: 93,
|
||||
points: [],
|
||||
};
|
||||
|
||||
const mockAdRepository = {
|
||||
@@ -51,6 +59,32 @@ const mockAdRepository = {
|
||||
}),
|
||||
};
|
||||
|
||||
const mockRouteProvider: RouteProviderPort = {
|
||||
getBasic: jest.fn().mockImplementation(() => ({
|
||||
driverDistance: 350101,
|
||||
driverDuration: 14422,
|
||||
passengerDistance: 350101,
|
||||
passengerDuration: 14422,
|
||||
fwdAzimuth: 273,
|
||||
backAzimuth: 93,
|
||||
distanceAzimuth: 336544,
|
||||
points: [
|
||||
{
|
||||
lon: 6.1765102,
|
||||
lat: 48.689445,
|
||||
},
|
||||
{
|
||||
lon: 4.984578,
|
||||
lat: 48.725687,
|
||||
},
|
||||
{
|
||||
lon: 2.3522,
|
||||
lat: 48.8566,
|
||||
},
|
||||
],
|
||||
})),
|
||||
};
|
||||
|
||||
describe('create-ad.service', () => {
|
||||
let createAdService: CreateAdService;
|
||||
|
||||
@@ -61,6 +95,10 @@ describe('create-ad.service', () => {
|
||||
provide: AD_REPOSITORY,
|
||||
useValue: mockAdRepository,
|
||||
},
|
||||
{
|
||||
provide: AD_ROUTE_PROVIDER,
|
||||
useValue: mockRouteProvider,
|
||||
},
|
||||
CreateAdService,
|
||||
],
|
||||
}).compile();
|
||||
|
||||
49
src/modules/ad/tests/unit/core/point.value-object.spec.ts
Normal file
49
src/modules/ad/tests/unit/core/point.value-object.spec.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { ArgumentOutOfRangeException } from '@mobicoop/ddd-library';
|
||||
import { Point } from '@modules/ad/core/domain/value-objects/point.value-object';
|
||||
|
||||
describe('Point value object', () => {
|
||||
it('should create a point value object', () => {
|
||||
const pointVO = new Point({
|
||||
lon: 48.689445,
|
||||
lat: 6.17651,
|
||||
});
|
||||
expect(pointVO.lon).toBe(48.689445);
|
||||
expect(pointVO.lat).toBe(6.17651);
|
||||
});
|
||||
it('should throw an exception if longitude is invalid', () => {
|
||||
try {
|
||||
new Point({
|
||||
lon: 348.689445,
|
||||
lat: 6.17651,
|
||||
});
|
||||
} catch (e: any) {
|
||||
expect(e).toBeInstanceOf(ArgumentOutOfRangeException);
|
||||
}
|
||||
try {
|
||||
new Point({
|
||||
lon: -348.689445,
|
||||
lat: 6.17651,
|
||||
});
|
||||
} catch (e: any) {
|
||||
expect(e).toBeInstanceOf(ArgumentOutOfRangeException);
|
||||
}
|
||||
});
|
||||
it('should throw an exception if latitude is invalid', () => {
|
||||
try {
|
||||
new Point({
|
||||
lon: 48.689445,
|
||||
lat: 96.17651,
|
||||
});
|
||||
} catch (e: any) {
|
||||
expect(e).toBeInstanceOf(ArgumentOutOfRangeException);
|
||||
}
|
||||
try {
|
||||
new Point({
|
||||
lon: 48.689445,
|
||||
lat: -96.17651,
|
||||
});
|
||||
} catch (e: any) {
|
||||
expect(e).toBeInstanceOf(ArgumentOutOfRangeException);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -46,7 +46,7 @@ describe('Waypoint value object', () => {
|
||||
expect(e).toBeInstanceOf(ArgumentOutOfRangeException);
|
||||
}
|
||||
});
|
||||
it('should throw an exception if longitude is invalid', () => {
|
||||
it('should throw an exception if latitude is invalid', () => {
|
||||
try {
|
||||
new Waypoint({
|
||||
position: 0,
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import { AD_GET_BASIC_ROUTE_CONTROLLER } from '@modules/ad/ad.di-tokens';
|
||||
import { Route } from '@modules/ad/core/application/types/route.type';
|
||||
import { Role } from '@modules/ad/core/domain/ad.types';
|
||||
import { RouteProvider } from '@modules/ad/infrastructure/route-provider';
|
||||
import { GetBasicRouteControllerPort } from '@modules/geography/core/application/ports/get-basic-route-controller.port';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
|
||||
const mockGetBasicRouteController: GetBasicRouteControllerPort = {
|
||||
get: jest.fn().mockImplementation(() => ({
|
||||
driverDistance: 23000,
|
||||
driverDuration: 900,
|
||||
passengerDistance: 23000,
|
||||
passengerDuration: 900,
|
||||
fwdAzimuth: 283,
|
||||
backAzimuth: 93,
|
||||
distanceAzimuth: 19840,
|
||||
points: [
|
||||
{
|
||||
lon: 6.1765103,
|
||||
lat: 48.689446,
|
||||
},
|
||||
{
|
||||
lon: 2.3523,
|
||||
lat: 48.8567,
|
||||
},
|
||||
],
|
||||
})),
|
||||
};
|
||||
|
||||
describe('Route provider', () => {
|
||||
let routeProvider: RouteProvider;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
RouteProvider,
|
||||
{
|
||||
provide: AD_GET_BASIC_ROUTE_CONTROLLER,
|
||||
useValue: mockGetBasicRouteController,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
routeProvider = module.get<RouteProvider>(RouteProvider);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(routeProvider).toBeDefined();
|
||||
});
|
||||
|
||||
it('should provide a route', async () => {
|
||||
const route: Route = await routeProvider.getBasic(
|
||||
[Role.DRIVER],
|
||||
[
|
||||
{
|
||||
position: 0,
|
||||
lat: 48.689445,
|
||||
lon: 6.1765102,
|
||||
},
|
||||
{
|
||||
position: 1,
|
||||
lat: 48.8566,
|
||||
lon: 2.3522,
|
||||
},
|
||||
],
|
||||
);
|
||||
expect(route.driverDistance).toBe(23000);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user