diff --git a/src/modules/ad/domain/dtos/create-ad.request.ts b/src/modules/ad/domain/dtos/create-ad.request.ts index 34a0067..a8b01b5 100644 --- a/src/modules/ad/domain/dtos/create-ad.request.ts +++ b/src/modules/ad/domain/dtos/create-ad.request.ts @@ -7,19 +7,17 @@ import { IsInt, IsEnum, ValidateNested, - IsLongitude, - IsLatitude, ValidateIf, - IsArray, ArrayMinSize, + IsNotEmptyObject, } from 'class-validator'; import { Frequency } from '../entities/frequency.enum'; import { Transform, Type } from 'class-transformer'; import { mappingKeyToFrequency } from './utils/frequency.mapping'; import { MarginDTO } from './create.margin.dto'; import { ScheduleDTO } from './create.schedule.dto'; +import { AddressDTO } from './create.address.dto'; -// TODO mapping schedule and margin (proto object to margin and schedule fields ) export class CreateAdRequest { @IsString() @AutoMap() @@ -60,14 +58,17 @@ export class CreateAdRequest { @AutoMap() toDate?: Date; - @ValidateNested({ each: true }) + @ValidateIf((ad) => ad.frequency === 'RECURRENT') @Type(() => ScheduleDTO) - @AutoMap(() => ScheduleDTO) + @ValidateNested({ each: true }) + @IsNotEmptyObject() + @AutoMap() schedule: ScheduleDTO; - @ValidateNested({ each: true }) + @IsOptional() @Type(() => MarginDTO) - @AutoMap(() => MarginDTO) + @ValidateNested({ each: true }) + @AutoMap() marginDurations: MarginDTO; @ValidateIf((ad) => ad.driver) @@ -85,53 +86,9 @@ export class CreateAdRequest { @AutoMap() strict?: boolean; - @IsArray() + @Type(() => AddressDTO) @ArrayMinSize(2) @ValidateNested({ each: true }) - @Type(() => AddressDTO) @AutoMap(() => [AddressDTO]) addresses: AddressDTO[]; } - -class AddressDTO { - @IsInt() - @AutoMap() - position: number; - - @IsLongitude() - @AutoMap() - lon: number; - - @IsLatitude() - @AutoMap() - lat: number; - - @IsString() - @IsOptional() - @AutoMap() - name?: string; - - @IsString() - @IsOptional() - @AutoMap() - houseNumber?: string; - - @IsString() - @IsOptional() - @AutoMap() - street?: string; - - @IsString() - @IsOptional() - @AutoMap() - locality?: string; - - @IsString() - @IsOptional() - @AutoMap() - postalCode?: string; - - @IsString() - @AutoMap() - country: string; -} diff --git a/src/modules/ad/domain/dtos/create.address.dto.ts b/src/modules/ad/domain/dtos/create.address.dto.ts index 6c0f933..378af10 100644 --- a/src/modules/ad/domain/dtos/create.address.dto.ts +++ b/src/modules/ad/domain/dtos/create.address.dto.ts @@ -20,28 +20,27 @@ export class AddressDTO { @AutoMap() lat: number; - @IsString() @IsOptional() @AutoMap() name?: string; - @IsString() @IsOptional() + @IsString() @AutoMap() houseNumber?: string; - @IsString() @IsOptional() + @IsString() @AutoMap() street?: string; - @IsString() @IsOptional() + @IsString() @AutoMap() locality?: string; - @IsString() @IsOptional() + @IsString() @AutoMap() postalCode?: string; diff --git a/src/modules/ad/domain/dtos/create.margin.dto.ts b/src/modules/ad/domain/dtos/create.margin.dto.ts index 698afaa..a6d7e61 100644 --- a/src/modules/ad/domain/dtos/create.margin.dto.ts +++ b/src/modules/ad/domain/dtos/create.margin.dto.ts @@ -1,40 +1,39 @@ import { AutoMap } from '@automapper/classes'; -import { Transform } from 'class-transformer'; import { IsInt, IsOptional } from 'class-validator'; export class MarginDTO { - @IsInt() @IsOptional() + @IsInt() @AutoMap() mon?: number; - @IsInt() @IsOptional() + @IsInt() @AutoMap() tue?: number; - @IsInt() @IsOptional() + @IsInt() @AutoMap() wed?: number; - @IsInt() @IsOptional() + @IsInt() @AutoMap() thu?: number; - @IsInt() @IsOptional() + @IsInt() @AutoMap() fri?: number; - @IsInt() @IsOptional() + @IsInt() @AutoMap() sat?: number; - @IsInt() @IsOptional() + @IsInt() @AutoMap() sun?: number; } diff --git a/src/modules/ad/domain/dtos/create.schedule.dto.ts b/src/modules/ad/domain/dtos/create.schedule.dto.ts index 7ef0ad2..3918c57 100644 --- a/src/modules/ad/domain/dtos/create.schedule.dto.ts +++ b/src/modules/ad/domain/dtos/create.schedule.dto.ts @@ -1,39 +1,39 @@ import { AutoMap } from '@automapper/classes'; -import { IsOptional, IsString } from 'class-validator'; +import { IsOptional, IsMilitaryTime } from 'class-validator'; export class ScheduleDTO { - @IsString() @IsOptional() + @IsMilitaryTime() @AutoMap() - monTime?: string; + mon?: string; - @IsString() @IsOptional() + @IsMilitaryTime() @AutoMap() - tueTime?: string; + tue?: string; - @IsString() @IsOptional() + @IsMilitaryTime() @AutoMap() - wedTime?: string; + wed?: string; - @IsString() @IsOptional() + @IsMilitaryTime() @AutoMap() - thuTime?: string; + thu?: string; - @IsString() @IsOptional() + @IsMilitaryTime() @AutoMap() - friTime?: string; + fri?: string; - @IsString() @IsOptional() + @IsMilitaryTime() @AutoMap() - satTime?: string; + sat?: string; - @IsString() @IsOptional() + @IsMilitaryTime() @AutoMap() - sunTime?: string; + sun?: string; } diff --git a/src/modules/ad/domain/entities/ad.ts b/src/modules/ad/domain/entities/ad.ts index 0b56f68..58015a6 100644 --- a/src/modules/ad/domain/entities/ad.ts +++ b/src/modules/ad/domain/entities/ad.ts @@ -39,90 +39,82 @@ export class Ad { @AutoMap() toDate: Date; - @IsDate() @IsOptional() + @IsDate() @AutoMap() monTime?: string; - @IsString() @IsOptional() + @IsString() @AutoMap() tueTime?: string; - @IsString() @IsOptional() + @IsString() @AutoMap() wedTime?: string; - @IsString() @IsOptional() + @IsString() @AutoMap() thuTime?: string; - @IsString() @IsOptional() + @IsString() @AutoMap() friTime?: string; - @IsString() @IsOptional() + @IsString() @AutoMap() satTime?: string; - @IsString() @IsOptional() + @IsString() @AutoMap() sunTime?: string; @IsInt() - @IsOptional() @AutoMap() - monMargin?: number; - - @IsInt() - @IsOptional() - @AutoMap() - tueMargin?: number; - - @IsInt() - @IsOptional() - @AutoMap() - wedMargin?: number; - - @IsInt() - @IsOptional() - @AutoMap() - thuMargin?: number; - - @IsInt() - @IsOptional() - @AutoMap() - friMargin?: number; - - @IsInt() - @IsOptional() - @AutoMap() - satMargin?: number; - - @IsInt() - @IsOptional() - @AutoMap() - sunMargin?: number; + monMargin: number; @IsInt() @AutoMap() - seatsDriver?: number; + tueMargin: number; @IsInt() @AutoMap() - seatsPassenger?: number; + wedMargin: number; + + @IsInt() + @AutoMap() + thuMargin: number; + + @IsInt() + @AutoMap() + friMargin: number; + + @IsInt() + @AutoMap() + satMargin: number; + + @IsInt() + @AutoMap() + sunMargin: number; + + @IsInt() + @AutoMap() + seatsDriver: number; + + @IsInt() + @AutoMap() + seatsPassenger: number; @IsDate() @AutoMap() createdAt: Date; @IsDate() - @IsOptional() @AutoMap() updatedAt?: Date; diff --git a/src/modules/ad/mappers/ad.profile.ts b/src/modules/ad/mappers/ad.profile.ts index c501072..cb7a430 100644 --- a/src/modules/ad/mappers/ad.profile.ts +++ b/src/modules/ad/mappers/ad.profile.ts @@ -1,4 +1,4 @@ -import { createMap, Mapper } from '@automapper/core'; +import { createMap, forMember, mapFrom, Mapper } from '@automapper/core'; import { AutomapperProfile, InjectMapper } from '@automapper/nestjs'; import { Injectable } from '@nestjs/common'; import { Ad } from '../domain/entities/ad'; @@ -14,7 +14,67 @@ export class AdProfile extends AutomapperProfile { override get profile() { return (mapper) => { createMap(mapper, Ad, AdPresenter); - createMap(mapper, CreateAdRequest, Ad); + createMap( + mapper, + CreateAdRequest, + Ad, + forMember( + (destination) => destination.monMargin, + mapFrom((source) => source.marginDurations.mon), + ), + forMember( + (destination) => destination.tueMargin, + mapFrom((source) => source.marginDurations.tue), + ), + forMember( + (destination) => destination.wedMargin, + mapFrom((source) => source.marginDurations.wed), + ), + forMember( + (destination) => destination.thuMargin, + mapFrom((source) => source.marginDurations.thu), + ), + forMember( + (destination) => destination.friMargin, + mapFrom((source) => source.marginDurations.fri), + ), + forMember( + (destination) => destination.satMargin, + mapFrom((source) => source.marginDurations.sat), + ), + forMember( + (destination) => destination.sunMargin, + mapFrom((source) => source.marginDurations.sun), + ), + forMember( + (destination) => destination.monTime, + mapFrom((source) => source.schedule.mon), + ), + forMember( + (destination) => destination.tueTime, + mapFrom((source) => source.schedule.tue), + ), + forMember( + (destination) => destination.wedTime, + mapFrom((source) => source.schedule.wed), + ), + forMember( + (destination) => destination.thuTime, + mapFrom((source) => source.schedule.thu), + ), + forMember( + (destination) => destination.friTime, + mapFrom((source) => source.schedule.fri), + ), + forMember( + (destination) => destination.satTime, + mapFrom((source) => source.schedule.sat), + ), + forMember( + (destination) => destination.sunTime, + mapFrom((source) => source.schedule.sun), + ), + ); }; } }