mirror of
https://gitlab.com/mobicoop/v3/service/matcher.git
synced 2026-01-01 14:02:39 +00:00
add route provider in ad module
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
export const AD_REPOSITORY = Symbol('AD_REPOSITORY');
|
||||
export const GEOROUTER = Symbol('GEOROUTER');
|
||||
export const AD_DIRECTION_ENCODER = Symbol('AD_DIRECTION_ENCODER');
|
||||
export const AD_MESSAGE_PUBLISHER = Symbol('AD_MESSAGE_PUBLISHER');
|
||||
export const AD_ROUTE_PROVIDER = Symbol('AD_ROUTE_PROVIDER');
|
||||
|
||||
@@ -6,11 +6,13 @@ import {
|
||||
AdReadModel,
|
||||
ScheduleItemModel,
|
||||
} from './infrastructure/ad.repository';
|
||||
import { Frequency } from './core/domain/ad.types';
|
||||
import { Frequency, Role } 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 } from './ad.di-tokens';
|
||||
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';
|
||||
|
||||
/**
|
||||
* Mapper constructs objects that are used in different layers:
|
||||
@@ -26,11 +28,25 @@ 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: RouteResponseDto = this.routeProvider.get(
|
||||
roles,
|
||||
copy.waypoints,
|
||||
{
|
||||
withDistance: true,
|
||||
withPoints: true,
|
||||
withTime: false,
|
||||
},
|
||||
);
|
||||
const record: AdWriteModel = {
|
||||
uuid: copy.id,
|
||||
driver: copy.driver,
|
||||
@@ -57,14 +73,14 @@ export class AdMapper
|
||||
seatsProposed: copy.seatsProposed,
|
||||
seatsRequested: copy.seatsRequested,
|
||||
strict: copy.strict,
|
||||
driverDuration: copy.driverDuration,
|
||||
driverDistance: copy.driverDistance,
|
||||
passengerDuration: copy.passengerDuration,
|
||||
passengerDistance: copy.passengerDistance,
|
||||
driverDuration: route.driverDuration,
|
||||
driverDistance: route.driverDistance,
|
||||
passengerDuration: route.passengerDuration,
|
||||
passengerDistance: route.passengerDistance,
|
||||
waypoints: this.directionEncoder.encode(copy.waypoints),
|
||||
direction: '',
|
||||
fwdAzimuth: copy.fwdAzimuth,
|
||||
backAzimuth: copy.backAzimuth,
|
||||
direction: this.directionEncoder.encode(route.spacetimePoints),
|
||||
fwdAzimuth: route.fwdAzimuth,
|
||||
backAzimuth: route.backAzimuth,
|
||||
createdAt: copy.createdAt,
|
||||
updatedAt: copy.updatedAt,
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
AD_MESSAGE_PUBLISHER,
|
||||
AD_REPOSITORY,
|
||||
AD_DIRECTION_ENCODER,
|
||||
AD_ROUTE_PROVIDER,
|
||||
} from './ad.di-tokens';
|
||||
import { MessageBrokerPublisher } from '@mobicoop/message-broker-module';
|
||||
import { AdRepository } from './infrastructure/ad.repository';
|
||||
@@ -11,6 +12,7 @@ import { PrismaService } from './infrastructure/prisma.service';
|
||||
import { AdMapper } from './ad.mapper';
|
||||
import { AdCreatedMessageHandler } from './interface/message-handlers/ad-created.message-handler';
|
||||
import { PostgresDirectionEncoder } from '@modules/geography/infrastructure/postgres-direction-encoder';
|
||||
import { GetRouteController } from '@modules/geography/interface/controllers/get-route.controller';
|
||||
|
||||
const messageHandlers = [AdCreatedMessageHandler];
|
||||
|
||||
@@ -37,6 +39,10 @@ const adapters: Provider[] = [
|
||||
provide: AD_DIRECTION_ENCODER,
|
||||
useClass: PostgresDirectionEncoder,
|
||||
},
|
||||
{
|
||||
provide: AD_ROUTE_PROVIDER,
|
||||
useClass: GetRouteController,
|
||||
},
|
||||
];
|
||||
|
||||
@Module({
|
||||
|
||||
12
src/modules/ad/core/application/ports/route-provider.port.ts
Normal file
12
src/modules/ad/core/application/ports/route-provider.port.ts
Normal file
@@ -0,0 +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 { Role } from '../../domain/ad.types';
|
||||
|
||||
export interface RouteProviderPort {
|
||||
get(
|
||||
roles: Role[],
|
||||
waypoints: Waypoint[],
|
||||
georouterSettings: GeorouterSettings,
|
||||
): RouteResponseDto;
|
||||
}
|
||||
@@ -48,3 +48,8 @@ export enum PointContext {
|
||||
VENUE = 'VENUE',
|
||||
OTHER = 'OTHER',
|
||||
}
|
||||
|
||||
export enum Role {
|
||||
DRIVER = 'DRIVER',
|
||||
PASSENGER = 'PASSENGER',
|
||||
}
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { AD_DIRECTION_ENCODER } from '@modules/ad/ad.di-tokens';
|
||||
import {
|
||||
AD_DIRECTION_ENCODER,
|
||||
AD_ROUTE_PROVIDER,
|
||||
} 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 {
|
||||
@@ -81,11 +85,28 @@ const adReadModel: AdReadModel = {
|
||||
const mockDirectionEncoder: DirectionEncoderPort = {
|
||||
encode: jest
|
||||
.fn()
|
||||
.mockImplementation(
|
||||
.mockImplementationOnce(
|
||||
() => "'LINESTRING(6.1765102 48.689445,2.3522 48.8566)'",
|
||||
)
|
||||
.mockImplementationOnce(
|
||||
() =>
|
||||
"'LINESTRING(6.1765102 48.689445,4.984578 48.725687,2.3522 48.8566)'",
|
||||
),
|
||||
};
|
||||
|
||||
const mockRouteProvider: RouteProviderPort = {
|
||||
get: jest.fn().mockImplementation(() => ({
|
||||
driverDistance: 350101,
|
||||
driverDuration: 14422,
|
||||
passengerDistance: 350101,
|
||||
passengerDuration: 14422,
|
||||
fwdAzimuth: 273,
|
||||
backAzimuth: 93,
|
||||
distanceAzimuth: 336544,
|
||||
spacetimePoints: [],
|
||||
})),
|
||||
};
|
||||
|
||||
describe('Ad Mapper', () => {
|
||||
let adMapper: AdMapper;
|
||||
|
||||
@@ -97,6 +118,10 @@ describe('Ad Mapper', () => {
|
||||
provide: AD_DIRECTION_ENCODER,
|
||||
useValue: mockDirectionEncoder,
|
||||
},
|
||||
{
|
||||
provide: AD_ROUTE_PROVIDER,
|
||||
useValue: mockRouteProvider,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
adMapper = module.get<AdMapper>(AdMapper);
|
||||
@@ -112,6 +137,11 @@ describe('Ad Mapper', () => {
|
||||
expect(mapped.waypoints).toBe(
|
||||
"'LINESTRING(6.1765102 48.689445,2.3522 48.8566)'",
|
||||
);
|
||||
expect(mapped.direction).toBe(
|
||||
"'LINESTRING(6.1765102 48.689445,4.984578 48.725687,2.3522 48.8566)'",
|
||||
);
|
||||
expect(mapped.driverDuration).toBe(14422);
|
||||
expect(mapped.fwdAzimuth).toBe(273);
|
||||
});
|
||||
|
||||
it('should map persisted data to domain entity', async () => {
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { AD_DIRECTION_ENCODER } from '@modules/ad/ad.di-tokens';
|
||||
import {
|
||||
AD_DIRECTION_ENCODER,
|
||||
AD_ROUTE_PROVIDER,
|
||||
} 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 { AdRepository } from '@modules/ad/infrastructure/ad.repository';
|
||||
import { PrismaService } from '@modules/ad/infrastructure/prisma.service';
|
||||
import { DirectionEncoderPort } from '@modules/geography/core/application/ports/direction-encoder.port';
|
||||
@@ -14,6 +18,10 @@ const mockDirectionEncoder: DirectionEncoderPort = {
|
||||
encode: jest.fn(),
|
||||
};
|
||||
|
||||
const mockRouteProvider: RouteProviderPort = {
|
||||
get: jest.fn(),
|
||||
};
|
||||
|
||||
describe('Ad repository', () => {
|
||||
let prismaService: PrismaService;
|
||||
let adMapper: AdMapper;
|
||||
@@ -29,6 +37,10 @@ describe('Ad repository', () => {
|
||||
provide: AD_DIRECTION_ENCODER,
|
||||
useValue: mockDirectionEncoder,
|
||||
},
|
||||
{
|
||||
provide: AD_ROUTE_PROVIDER,
|
||||
useValue: mockRouteProvider,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user