WIP create add mapping tim and schedule

This commit is contained in:
Grégoire Chevalier 2023-05-12 16:18:24 +02:00
parent 7036a8be55
commit 7270ecb44b
6 changed files with 132 additions and 125 deletions

View File

@ -7,19 +7,17 @@ import {
IsInt, IsInt,
IsEnum, IsEnum,
ValidateNested, ValidateNested,
IsLongitude,
IsLatitude,
ValidateIf, ValidateIf,
IsArray,
ArrayMinSize, ArrayMinSize,
IsNotEmptyObject,
} from 'class-validator'; } from 'class-validator';
import { Frequency } from '../entities/frequency.enum'; import { Frequency } from '../entities/frequency.enum';
import { Transform, Type } from 'class-transformer'; import { Transform, Type } from 'class-transformer';
import { mappingKeyToFrequency } from './utils/frequency.mapping'; import { mappingKeyToFrequency } from './utils/frequency.mapping';
import { MarginDTO } from './create.margin.dto'; import { MarginDTO } from './create.margin.dto';
import { ScheduleDTO } from './create.schedule.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 { export class CreateAdRequest {
@IsString() @IsString()
@AutoMap() @AutoMap()
@ -60,14 +58,17 @@ export class CreateAdRequest {
@AutoMap() @AutoMap()
toDate?: Date; toDate?: Date;
@ValidateNested({ each: true }) @ValidateIf((ad) => ad.frequency === 'RECURRENT')
@Type(() => ScheduleDTO) @Type(() => ScheduleDTO)
@AutoMap(() => ScheduleDTO) @ValidateNested({ each: true })
@IsNotEmptyObject()
@AutoMap()
schedule: ScheduleDTO; schedule: ScheduleDTO;
@ValidateNested({ each: true }) @IsOptional()
@Type(() => MarginDTO) @Type(() => MarginDTO)
@AutoMap(() => MarginDTO) @ValidateNested({ each: true })
@AutoMap()
marginDurations: MarginDTO; marginDurations: MarginDTO;
@ValidateIf((ad) => ad.driver) @ValidateIf((ad) => ad.driver)
@ -85,53 +86,9 @@ export class CreateAdRequest {
@AutoMap() @AutoMap()
strict?: boolean; strict?: boolean;
@IsArray() @Type(() => AddressDTO)
@ArrayMinSize(2) @ArrayMinSize(2)
@ValidateNested({ each: true }) @ValidateNested({ each: true })
@Type(() => AddressDTO)
@AutoMap(() => [AddressDTO]) @AutoMap(() => [AddressDTO])
addresses: 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;
}

View File

@ -20,28 +20,27 @@ export class AddressDTO {
@AutoMap() @AutoMap()
lat: number; lat: number;
@IsString()
@IsOptional() @IsOptional()
@AutoMap() @AutoMap()
name?: string; name?: string;
@IsString()
@IsOptional() @IsOptional()
@IsString()
@AutoMap() @AutoMap()
houseNumber?: string; houseNumber?: string;
@IsString()
@IsOptional() @IsOptional()
@IsString()
@AutoMap() @AutoMap()
street?: string; street?: string;
@IsString()
@IsOptional() @IsOptional()
@IsString()
@AutoMap() @AutoMap()
locality?: string; locality?: string;
@IsString()
@IsOptional() @IsOptional()
@IsString()
@AutoMap() @AutoMap()
postalCode?: string; postalCode?: string;

View File

@ -1,40 +1,39 @@
import { AutoMap } from '@automapper/classes'; import { AutoMap } from '@automapper/classes';
import { Transform } from 'class-transformer';
import { IsInt, IsOptional } from 'class-validator'; import { IsInt, IsOptional } from 'class-validator';
export class MarginDTO { export class MarginDTO {
@IsInt()
@IsOptional() @IsOptional()
@IsInt()
@AutoMap() @AutoMap()
mon?: number; mon?: number;
@IsInt()
@IsOptional() @IsOptional()
@IsInt()
@AutoMap() @AutoMap()
tue?: number; tue?: number;
@IsInt()
@IsOptional() @IsOptional()
@IsInt()
@AutoMap() @AutoMap()
wed?: number; wed?: number;
@IsInt()
@IsOptional() @IsOptional()
@IsInt()
@AutoMap() @AutoMap()
thu?: number; thu?: number;
@IsInt()
@IsOptional() @IsOptional()
@IsInt()
@AutoMap() @AutoMap()
fri?: number; fri?: number;
@IsInt()
@IsOptional() @IsOptional()
@IsInt()
@AutoMap() @AutoMap()
sat?: number; sat?: number;
@IsInt()
@IsOptional() @IsOptional()
@IsInt()
@AutoMap() @AutoMap()
sun?: number; sun?: number;
} }

View File

@ -1,39 +1,39 @@
import { AutoMap } from '@automapper/classes'; import { AutoMap } from '@automapper/classes';
import { IsOptional, IsString } from 'class-validator'; import { IsOptional, IsMilitaryTime } from 'class-validator';
export class ScheduleDTO { export class ScheduleDTO {
@IsString()
@IsOptional() @IsOptional()
@IsMilitaryTime()
@AutoMap() @AutoMap()
monTime?: string; mon?: string;
@IsString()
@IsOptional() @IsOptional()
@IsMilitaryTime()
@AutoMap() @AutoMap()
tueTime?: string; tue?: string;
@IsString()
@IsOptional() @IsOptional()
@IsMilitaryTime()
@AutoMap() @AutoMap()
wedTime?: string; wed?: string;
@IsString()
@IsOptional() @IsOptional()
@IsMilitaryTime()
@AutoMap() @AutoMap()
thuTime?: string; thu?: string;
@IsString()
@IsOptional() @IsOptional()
@IsMilitaryTime()
@AutoMap() @AutoMap()
friTime?: string; fri?: string;
@IsString()
@IsOptional() @IsOptional()
@IsMilitaryTime()
@AutoMap() @AutoMap()
satTime?: string; sat?: string;
@IsString()
@IsOptional() @IsOptional()
@IsMilitaryTime()
@AutoMap() @AutoMap()
sunTime?: string; sun?: string;
} }

View File

@ -39,90 +39,82 @@ export class Ad {
@AutoMap() @AutoMap()
toDate: Date; toDate: Date;
@IsDate()
@IsOptional() @IsOptional()
@IsDate()
@AutoMap() @AutoMap()
monTime?: string; monTime?: string;
@IsString()
@IsOptional() @IsOptional()
@IsString()
@AutoMap() @AutoMap()
tueTime?: string; tueTime?: string;
@IsString()
@IsOptional() @IsOptional()
@IsString()
@AutoMap() @AutoMap()
wedTime?: string; wedTime?: string;
@IsString()
@IsOptional() @IsOptional()
@IsString()
@AutoMap() @AutoMap()
thuTime?: string; thuTime?: string;
@IsString()
@IsOptional() @IsOptional()
@IsString()
@AutoMap() @AutoMap()
friTime?: string; friTime?: string;
@IsString()
@IsOptional() @IsOptional()
@IsString()
@AutoMap() @AutoMap()
satTime?: string; satTime?: string;
@IsString()
@IsOptional() @IsOptional()
@IsString()
@AutoMap() @AutoMap()
sunTime?: string; sunTime?: string;
@IsInt() @IsInt()
@IsOptional()
@AutoMap() @AutoMap()
monMargin?: number; 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;
@IsInt() @IsInt()
@AutoMap() @AutoMap()
seatsDriver?: number; tueMargin: number;
@IsInt() @IsInt()
@AutoMap() @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() @IsDate()
@AutoMap() @AutoMap()
createdAt: Date; createdAt: Date;
@IsDate() @IsDate()
@IsOptional()
@AutoMap() @AutoMap()
updatedAt?: Date; updatedAt?: Date;

View File

@ -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 { AutomapperProfile, InjectMapper } from '@automapper/nestjs';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { Ad } from '../domain/entities/ad'; import { Ad } from '../domain/entities/ad';
@ -14,7 +14,67 @@ export class AdProfile extends AutomapperProfile {
override get profile() { override get profile() {
return (mapper) => { return (mapper) => {
createMap(mapper, Ad, AdPresenter); 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),
),
);
}; };
} }
} }