diff --git a/src/modules/ad/adapters/primaries/ad-messager.controller.ts b/src/modules/ad/adapters/primaries/ad-messager.controller.ts index 7dc19ce..bbc4685 100644 --- a/src/modules/ad/adapters/primaries/ad-messager.controller.ts +++ b/src/modules/ad/adapters/primaries/ad-messager.controller.ts @@ -24,14 +24,21 @@ export class AdMessagerController { const parsedMessage: CreateAdRequest = JSON.parse(message); console.log(parsedMessage); // create a real instance of CreateAdRequest from parsed message - const createAdRequest: CreateAdRequest = this.mapper.map( - parsedMessage, - CreateAdRequest, - CreateAdRequest, - ); + // const createAdRequest: CreateAdRequest = this.mapper.map( + // parsedMessage, + // CreateAdRequest, + // CreateAdRequest, + // ); + const createAdRequest = new CreateAdRequest(); + createAdRequest.originType = parsedMessage.originType; + createAdRequest.destinationType = parsedMessage.destinationType; + createAdRequest.waypoints = parsedMessage.waypoints.map((waypoint) => ({ + lon: waypoint.lon, + lat: waypoint.lat, + })); console.log(createAdRequest); // validate instance - await validateOrReject(createAdRequest); + await validateOrReject(createAdRequest.waypoints[0]); const ad: Ad = await this.commandBus.execute( new CreateAdCommand(createAdRequest), ); diff --git a/src/modules/ad/domain/dtos/create-ad.request.ts b/src/modules/ad/domain/dtos/create-ad.request.ts index f05c67f..0e71c3d 100644 --- a/src/modules/ad/domain/dtos/create-ad.request.ts +++ b/src/modules/ad/domain/dtos/create-ad.request.ts @@ -8,10 +8,12 @@ import { IsNumber, IsOptional, IsString, + ValidateNested, } from 'class-validator'; import { PointType } from '../../../geography/domain/types/point-type.enum'; import { Frequency } from '../types/frequency.enum'; import { Coordinates } from '../../../geography/domain/types/coordinates.type'; +import { Type } from 'class-transformer'; export class CreateAdRequest { @IsString() @@ -112,7 +114,9 @@ export class CreateAdRequest { destinationType: PointType; @IsArray() + @ValidateNested({ each: true }) @ArrayMinSize(2) + @Type(() => Coordinates) @AutoMap(() => [Coordinates]) waypoints: Coordinates[]; diff --git a/src/modules/ad/mappers/ad.profile.ts b/src/modules/ad/mappers/ad.profile.ts index 09a247f..460068c 100644 --- a/src/modules/ad/mappers/ad.profile.ts +++ b/src/modules/ad/mappers/ad.profile.ts @@ -4,6 +4,7 @@ import { Injectable } from '@nestjs/common'; import { Ad } from '../domain/entities/ad'; import { AdPresenter } from '../adapters/primaries/ad.presenter'; import { CreateAdRequest } from '../domain/dtos/create-ad.request'; +import { Coordinates } from 'src/modules/geography/domain/types/coordinates.type'; @Injectable() export class AdProfile extends AutomapperProfile { @@ -14,7 +15,25 @@ export class AdProfile extends AutomapperProfile { override get profile() { return (mapper: any) => { createMap(mapper, Ad, AdPresenter); - createMap(mapper, CreateAdRequest, CreateAdRequest); + createMap( + mapper, + CreateAdRequest, + CreateAdRequest, + forMember( + (dest) => dest.waypoints, + mapFrom( + (source) => + source.waypoints.map( + (waypoint) => + new Coordinates( + waypoint.lon ?? undefined, + waypoint.lat ?? undefined, + ), + ), + // .filter((waypoint) => waypoint), + ), + ), + ); createMap( mapper, CreateAdRequest, diff --git a/src/modules/ad/mappers/coordinates.profile.ts b/src/modules/ad/mappers/coordinates.profile.ts new file mode 100644 index 0000000..69a4a47 --- /dev/null +++ b/src/modules/ad/mappers/coordinates.profile.ts @@ -0,0 +1,17 @@ +import { createMap, Mapper } from '@automapper/core'; +import { AutomapperProfile, InjectMapper } from '@automapper/nestjs'; +import { Injectable } from '@nestjs/common'; +import { Coordinates } from '../../geography/domain/types/coordinates.type'; + +@Injectable() +export class CoordinatesProfile extends AutomapperProfile { + constructor(@InjectMapper() mapper: Mapper) { + super(mapper); + } + + override get profile() { + return (mapper: any) => { + createMap(mapper, Coordinates, Coordinates); + }; + } +} diff --git a/src/modules/geography/domain/types/coordinates.type.ts b/src/modules/geography/domain/types/coordinates.type.ts index 80872c6..05769c2 100644 --- a/src/modules/geography/domain/types/coordinates.type.ts +++ b/src/modules/geography/domain/types/coordinates.type.ts @@ -1,5 +1,5 @@ import { AutoMap } from '@automapper/classes'; -import { IsNumber, Max, Min } from 'class-validator'; +import { IsLatitude, IsLongitude, IsNumber } from 'class-validator'; export class Coordinates { constructor(lon: number, lat: number) { @@ -8,14 +8,12 @@ export class Coordinates { } @IsNumber() - @Min(-180) - @Max(180) + @IsLongitude() @AutoMap() lon: number; @IsNumber() - @Min(-90) - @Max(90) + @IsLatitude() @AutoMap() lat: number; }