add direction decoder

This commit is contained in:
sbriat 2023-08-21 16:00:37 +02:00
parent 1dd44b1425
commit e93d8b0c9d
5 changed files with 29 additions and 1 deletions

View File

@ -112,7 +112,12 @@ export class AdMapper
driverDistance: record.driverDistance, driverDistance: record.driverDistance,
passengerDuration: record.passengerDuration, passengerDuration: record.passengerDuration,
passengerDistance: record.passengerDistance, passengerDistance: record.passengerDistance,
waypoints: [], waypoints: this.directionEncoder
.decode(record.waypoints)
.map((coordinates, index) => ({
position: index,
...coordinates,
})),
fwdAzimuth: record.fwdAzimuth, fwdAzimuth: record.fwdAzimuth,
backAzimuth: record.backAzimuth, backAzimuth: record.backAzimuth,
}, },

View File

@ -92,6 +92,16 @@ const mockDirectionEncoder: DirectionEncoderPort = {
() => () =>
"'LINESTRING(6.1765102 48.689445,4.984578 48.725687,2.3522 48.8566)'", "'LINESTRING(6.1765102 48.689445,4.984578 48.725687,2.3522 48.8566)'",
), ),
decode: jest.fn().mockImplementation(() => [
{
lon: 6.1765102,
lat: 48.689445,
},
{
lon: 2.3522,
lat: 48.8566,
},
]),
}; };
const mockRouteProvider: RouteProviderPort = { const mockRouteProvider: RouteProviderPort = {
@ -161,6 +171,7 @@ describe('Ad Mapper', () => {
const mapped: AdEntity = adMapper.toDomain(adReadModel); const mapped: AdEntity = adMapper.toDomain(adReadModel);
expect(mapped.getProps().schedule.length).toBe(1); expect(mapped.getProps().schedule.length).toBe(1);
expect(mapped.getProps().schedule[0].time).toBe('07:05'); expect(mapped.getProps().schedule[0].time).toBe('07:05');
expect(mapped.getProps().waypoints.length).toBe(2);
}); });
it('should map domain entity to response', async () => { it('should map domain entity to response', async () => {

View File

@ -16,6 +16,7 @@ const mockMessagePublisher = {
const mockDirectionEncoder: DirectionEncoderPort = { const mockDirectionEncoder: DirectionEncoderPort = {
encode: jest.fn(), encode: jest.fn(),
decode: jest.fn(),
}; };
const mockRouteProvider: RouteProviderPort = { const mockRouteProvider: RouteProviderPort = {

View File

@ -2,4 +2,5 @@ import { Coordinates } from '../types/coordinates.type';
export interface DirectionEncoderPort { export interface DirectionEncoderPort {
encode(coordinates: Coordinates[]): string; encode(coordinates: Coordinates[]): string;
decode(direction: string): Coordinates[];
} }

View File

@ -10,4 +10,14 @@ export class PostgresDirectionEncoder implements DirectionEncoderPort {
coordinates.map((point) => [point.lon, point.lat].join(' ')).join(), coordinates.map((point) => [point.lon, point.lat].join(' ')).join(),
")'", ")'",
].join(''); ].join('');
decode = (direction: string): Coordinates[] =>
direction
.split('(')[1]
.split(')')[0]
.split(',')
.map((coordinates) => coordinates.split(' '))
.map((point) => ({
lon: parseFloat(point[0]),
lat: parseFloat(point[1]),
}));
} }