WIP ad creation address mapping troubleshooting

This commit is contained in:
Grégoire Chevalier 2023-05-16 14:43:52 +02:00
parent 5ed2c562d5
commit 8544b2f54e
8 changed files with 36 additions and 17 deletions

View File

@ -42,7 +42,7 @@ export class AdController {
@GrpcMethod('AdsService', 'Create') @GrpcMethod('AdsService', 'Create')
async createAd(data: CreateAdRequest): Promise<AdPresenter> { async createAd(data: CreateAdRequest): Promise<AdPresenter> {
try { try {
console.log('controler'); console.log('controler--------------------------------');
console.log(data); console.log(data);
console.log('-----------------------------------------'); console.log('-----------------------------------------');
const ad = await this._commandBus.execute(new CreateAdCommand(data)); const ad = await this._commandBus.execute(new CreateAdCommand(data));

View File

@ -10,6 +10,7 @@ import {
ValidateIf, ValidateIf,
ArrayMinSize, ArrayMinSize,
IsNotEmptyObject, IsNotEmptyObject,
IsUUID,
} from 'class-validator'; } from 'class-validator';
import { Frequency } from '../types/frequency.enum'; import { Frequency } from '../types/frequency.enum';
import { Transform, Type } from 'class-transformer'; import { Transform, Type } from 'class-transformer';
@ -21,7 +22,12 @@ import { HasProperPassengerSeats } from './utils/has-passenger-seats.validator';
import { HasProperDriverSeats } from './utils/has-driver-seats.validator'; import { HasProperDriverSeats } from './utils/has-driver-seats.validator';
export class CreateAdRequest { export class CreateAdRequest {
@IsString() @IsOptional()
@IsUUID(4)
@AutoMap()
uuid?: string;
@IsUUID(4)
@AutoMap() @AutoMap()
userUuid: string; userUuid: string;
@ -90,7 +96,6 @@ export class CreateAdRequest {
@AutoMap() @AutoMap()
strict?: boolean; strict?: boolean;
@Type(() => AddressDTO)
@ArrayMinSize(2) @ArrayMinSize(2)
@ValidateNested({ each: true }) @ValidateNested({ each: true })
@AutoMap(() => [AddressDTO]) @AutoMap(() => [AddressDTO])

View File

@ -5,9 +5,20 @@ import {
IsLongitude, IsLongitude,
IsOptional, IsOptional,
IsString, IsString,
IsUUID,
} from 'class-validator'; } from 'class-validator';
export class AddressDTO { export class AddressDTO {
@IsOptional()
@IsUUID(4)
@AutoMap()
uuid?: string;
@IsOptional()
@IsUUID(4)
@AutoMap()
adUuid?: string;
@IsInt() @IsInt()
@AutoMap() @AutoMap()
position: number; position: number;

View File

@ -125,9 +125,8 @@ export class Ad {
@AutoMap() @AutoMap()
updatedAt?: Date; updatedAt?: Date;
@Type(() => Address)
@ArrayMinSize(2) @ArrayMinSize(2)
@ValidateNested({ each: true }) @ValidateNested({ each: true })
@AutoMap() @AutoMap(() => [Address])
addresses: Address[]; addresses: Address[];
} }

View File

@ -1,9 +1,16 @@
import { AutoMap } from '@automapper/classes'; import { AutoMap } from '@automapper/classes';
import { IsInt, IsUUID } from 'class-validator';
export class Address { export class Address {
@IsUUID(4)
@AutoMap() @AutoMap()
uuid?: string; uuid: string;
@IsUUID(4)
@AutoMap()
adUuid: string;
@IsInt()
@AutoMap() @AutoMap()
position: number; position: number;

View File

@ -30,9 +30,6 @@ export class CreateAdUseCase {
CreateAdRequest, CreateAdRequest,
Ad, Ad,
); );
console.log('usecase');
console.log(entity.addresses[0]);
console.log('-----------------------------------------');
typeof entity.monMargin === 'number' typeof entity.monMargin === 'number'
? entity.monMargin ? entity.monMargin
: (entity.monMargin = this.defaultParams.MON_MARGIN); : (entity.monMargin = this.defaultParams.MON_MARGIN);
@ -58,6 +55,9 @@ export class CreateAdUseCase {
? entity.strict ? entity.strict
: (entity.strict = this.defaultParams.STRICT); : (entity.strict = this.defaultParams.STRICT);
try { try {
console.log('before ***********************************');
console.log(entity);
console.log('******************************************');
this.ad = await this._repository.create(entity); this.ad = await this._repository.create(entity);
// this._messager.publish('ad.create', JSON.stringify(ad)); // this._messager.publish('ad.create', JSON.stringify(ad));
// this._messager.publish('logging.ad.create.info', JSON.stringify(ad)); // this._messager.publish('logging.ad.create.info', JSON.stringify(ad));

View File

@ -4,6 +4,8 @@ import { Injectable } from '@nestjs/common';
import { Ad } from '../domain/entities/ad'; import { Ad } from '../domain/entities/ad';
import { AdPresenter } from '../adapters/primaries/ad.presenter'; import { AdPresenter } from '../adapters/primaries/ad.presenter';
import { CreateAdRequest } from '../domain/dtos/create-ad.request'; import { CreateAdRequest } from '../domain/dtos/create-ad.request';
import { Address } from '../domain/entities/address';
import { AddressDTO } from '../domain/dtos/create.address.dto';
@Injectable() @Injectable()
export class AdProfile extends AutomapperProfile { export class AdProfile extends AutomapperProfile {
@ -74,10 +76,6 @@ export class AdProfile extends AutomapperProfile {
(destination) => destination.sunTime, (destination) => destination.sunTime,
mapFrom((source) => source.schedule.sun), mapFrom((source) => source.schedule.sun),
), ),
forMember(
(destination) => destination.addresses,
mapFrom((source) => source.addresses),
),
); );
}; };
} }

View File

@ -1,7 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { CreateAdUseCase } from '../../domain/usecases/create-ad.usecase'; import { CreateAdUseCase } from '../../domain/usecases/create-ad.usecase';
import { CreateAdRequest } from '../../domain/dtos/create-ad.request'; import { CreateAdRequest } from '../../domain/dtos/create-ad.request';
import { Address } from '../../domain/entities/address';
import { Messager } from '../../adapters/secondaries/messager'; import { Messager } from '../../adapters/secondaries/messager';
import { AdsRepository } from '../../adapters/secondaries/ads.repository'; import { AdsRepository } from '../../adapters/secondaries/ads.repository';
import { CreateAdCommand } from '../../commands/create-ad.command'; import { CreateAdCommand } from '../../commands/create-ad.command';
@ -10,9 +9,9 @@ import { classes } from '@automapper/classes';
import { Frequency } from '../../domain/types/frequency.enum'; import { Frequency } from '../../domain/types/frequency.enum';
import { Ad } from '../../domain/entities/ad'; import { Ad } from '../../domain/entities/ad';
import { AdProfile } from '../../mappers/ad.profile'; import { AdProfile } from '../../mappers/ad.profile';
import { DefaultParams } from '../../domain/types/default-params.type'; import { AddressDTO } from '../../domain/dtos/create.address.dto';
const mockAddress1: Address = { const mockAddress1: AddressDTO = {
position: 0, position: 0,
lon: 48.68944505415954, lon: 48.68944505415954,
lat: 6.176510296462267, lat: 6.176510296462267,
@ -22,7 +21,7 @@ const mockAddress1: Address = {
postalCode: '54000', postalCode: '54000',
country: 'France', country: 'France',
}; };
const mockAddress2: Address = { const mockAddress2: AddressDTO = {
position: 1, position: 1,
lon: 48.8566, lon: 48.8566,
lat: 2.3522, lat: 2.3522,