From b035655bc644f5844bbad3e7d154d763f074c714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Chevalier?= Date: Thu, 11 May 2023 17:42:43 +0200 Subject: [PATCH] WIP setting default values --- prisma/schema.prisma | 23 ++++---- .../ad/adapters/primaries/ad.controller.ts | 3 + src/modules/ad/adapters/primaries/ad.proto | 55 ++++++++++--------- src/modules/ad/commands/create-ad.command.ts | 8 +++ .../ad/domain/dtos/create-ad.request.ts | 44 ++++++++------- .../ad/domain/dtos/utils/frequency.mapping.ts | 4 ++ .../ad/domain/entities/address.enum.ts | 7 --- src/modules/ad/domain/entities/address.ts | 4 -- .../ad/domain/usecases/create-ad.usecase.ts | 6 +- src/modules/ad/mappers/ad.profile.ts | 15 +---- .../ad/tests/unit/create-ad.usecase.spec.ts | 7 +-- .../secondaries/prisma-repository.abstract.ts | 4 ++ 12 files changed, 89 insertions(+), 91 deletions(-) create mode 100644 src/modules/ad/domain/dtos/utils/frequency.mapping.ts delete mode 100644 src/modules/ad/domain/entities/address.enum.ts diff --git a/prisma/schema.prisma b/prisma/schema.prisma index da9ddb6..202d909 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -13,9 +13,9 @@ datasource db { model Ad { uuid String @id @default(uuid()) @db.Uuid userUuid String @db.Uuid - driver Boolean - passenger Boolean - frequency Frequency + driver Boolean + passenger Boolean + frequency Frequency fromDate DateTime @db.Date toDate DateTime @db.Date monTime DateTime? @db.Timestamptz() @@ -34,7 +34,7 @@ model Ad { sunMargin Int seatsDriver Int @db.SmallInt seatsPassenger Int @db.SmallInt - strict Boolean + strict Boolean createdAt DateTime @default(now()) updatedAt DateTime @default(now()) @updatedAt addresses Address[] @@ -43,9 +43,9 @@ model Ad { } model Address { - uuid String @id @default(uuid()) @db.Uuid - adUuid String @db.Uuid - position Int @db.SmallInt + uuid String @id @default(uuid()) @db.Uuid + adUuid String @db.Uuid + position Int @db.SmallInt lon Float lat Float name String? @@ -53,11 +53,10 @@ model Address { street String? locality String? postalCode String? - country String? - countryCode String? - createdAt DateTime @default(now()) - updatedAt DateTime @default(now()) @updatedAt - Ad Ad @relation(fields: [adUuid], references: [uuid], onDelete: Cascade) + country String + createdAt DateTime @default(now()) + updatedAt DateTime @default(now()) @updatedAt + Ad Ad @relation(fields: [adUuid], references: [uuid], onDelete: Cascade) @@map("address") } diff --git a/src/modules/ad/adapters/primaries/ad.controller.ts b/src/modules/ad/adapters/primaries/ad.controller.ts index ec2ca65..77960b7 100644 --- a/src/modules/ad/adapters/primaries/ad.controller.ts +++ b/src/modules/ad/adapters/primaries/ad.controller.ts @@ -41,6 +41,9 @@ export class AdController { @GrpcMethod('AdsService', 'Create') async createAd(data: CreateAdRequest): Promise { + console.log('presenter'); + console.log(data); + console.log('*******************************'); try { const ad = await this._commandBus.execute(new CreateAdCommand(data)); return this._mapper.map(ad, Ad, AdPresenter); diff --git a/src/modules/ad/adapters/primaries/ad.proto b/src/modules/ad/adapters/primaries/ad.proto index 666811f..63a07d1 100644 --- a/src/modules/ad/adapters/primaries/ad.proto +++ b/src/modules/ad/adapters/primaries/ad.proto @@ -20,24 +20,25 @@ message Ad { bool driver = 3; bool passenger = 4; Frequency frequency = 5; - string fromDate = 6; - string toDate = 7; - Schedule schedule = 8; - MarginDurations marginDurations = 9; - optional int32 seatsPassenger = 10; - optional int32 seatsDriver = 11; - optional bool strict = 12; - repeated Address addresses = 13; + optional string departure = 6; + string fromDate = 7; + string toDate = 8; + Schedule schedule = 9; + MarginDurations marginDurations = 10; + int32 seatsPassenger = 11; + int32 seatsDriver = 12; + bool strict = 13; + repeated Address addresses = 14; } message Schedule { - string mon = 1; - string tue = 2; - string wed = 3; - string thu = 4; - string fri = 5; - string sat = 6; - string sun = 7; + optional string mon = 1; + optional string tue = 2; + optional string wed = 3; + optional string thu = 4; + optional string fri = 5; + optional string sat = 6; + optional string sun = 7; } message MarginDurations { @@ -51,16 +52,16 @@ message MarginDurations { } message Address { - int32 position =1; - float lon = 2; - float lat = 3; - optional string name =4; - optional string houseNumber = 5; - optional string street = 6; - optional string locality = 7; - optional string postalCode = 8; - optional string country = 9; - optional string countryCode = 10; + string uuid = 1; + int32 position = 2; + float lon = 3; + float lat = 4; + optional string name = 5; + optional string houseNumber = 6; + optional string street = 7; + optional string locality = 8; + optional string postalCode = 9; + string country = 10; } @@ -70,8 +71,8 @@ enum Frequency { } message AdFilter { - optional int32 page = 1; - optional int32 perPage = 2; + int32 page = 1; + int32 perPage = 2; } message Ads { diff --git a/src/modules/ad/commands/create-ad.command.ts b/src/modules/ad/commands/create-ad.command.ts index b4f1e8d..b1a8eba 100644 --- a/src/modules/ad/commands/create-ad.command.ts +++ b/src/modules/ad/commands/create-ad.command.ts @@ -5,5 +5,13 @@ export class CreateAdCommand { constructor(request: CreateAdRequest) { this.createAdRequest = request; + console.log('req creation'); + console.log(this.createAdRequest.monMargin); + console.log(this.createAdRequest.tueMargin); + console.log(this.createAdRequest.wedMargin); + console.log(this.createAdRequest.thuMargin); + console.log(this.createAdRequest.friMargin); + console.log(this.createAdRequest.satMargin); + console.log(this.createAdRequest.sunMargin); } } diff --git a/src/modules/ad/domain/dtos/create-ad.request.ts b/src/modules/ad/domain/dtos/create-ad.request.ts index 81ebe4e..5f52944 100644 --- a/src/modules/ad/domain/dtos/create-ad.request.ts +++ b/src/modules/ad/domain/dtos/create-ad.request.ts @@ -9,10 +9,15 @@ import { ValidateNested, IsLongitude, IsLatitude, + ValidateIf, + IsArray, + ArrayMinSize, } from 'class-validator'; import { Frequency } from '../entities/frequency.enum'; import { Transform, Type } from 'class-transformer'; +import { mappingKeyToFrequency } from './utils/frequency.mapping'; +// TODO mapping schedule and margin (proto object to margin and schedule fields ) export class CreateAdRequest { @IsString() @AutoMap() @@ -28,26 +33,28 @@ export class CreateAdRequest { @AutoMap() passenger?: boolean; - @Transform(({ value }) => Frequency[value - 1], { toClassOnly: true }) + @Transform(({ value }) => mappingKeyToFrequency(value), { + toClassOnly: true, + }) @IsEnum(Frequency) @AutoMap() frequency: Frequency; + @ValidateIf((ad) => ad.frequency === 'PUNCTUAL') @Type(() => Date) @IsDate() - @IsOptional() @AutoMap() departure?: Date; + @ValidateIf((ad) => ad.frequency === 'RECURRENT') @Type(() => Date) @IsDate() - @IsOptional() @AutoMap() fromDate?: Date; + @ValidateIf((ad) => ad.frequency === 'RECURRENT') @Type(() => Date) @IsDate() - @IsOptional() @AutoMap() toDate?: Date; @@ -86,6 +93,15 @@ export class CreateAdRequest { @AutoMap() sunTime?: string; + @Transform( + ({ value }) => { + console.log(value); + return value; + }, + { + toClassOnly: true, + }, + ) @IsInt() @IsOptional() @AutoMap() @@ -121,7 +137,7 @@ export class CreateAdRequest { @AutoMap() sunMargin?: number; - @IsOptional() + @ValidateIf((ad) => ad.driver) @IsInt() @AutoMap() seatsDriver?: number; @@ -136,15 +152,9 @@ export class CreateAdRequest { @AutoMap() strict?: boolean; + @IsArray() + @ArrayMinSize(2) @ValidateNested({ each: true }) - @Transform( - ({ value }) => { - console.log('in dto'); - console.log(value); - return value; - }, - { toClassOnly: true }, - ) @Type(() => AddressDTO) @AutoMap(() => [AddressDTO]) addresses: AddressDTO[]; @@ -189,12 +199,6 @@ class AddressDTO { postalCode?: string; @IsString() - @IsOptional() @AutoMap() - country?: string; - - @IsString() - @IsOptional() - @AutoMap() - countryCode?: string; + country: string; } diff --git a/src/modules/ad/domain/dtos/utils/frequency.mapping.ts b/src/modules/ad/domain/dtos/utils/frequency.mapping.ts new file mode 100644 index 0000000..8c41025 --- /dev/null +++ b/src/modules/ad/domain/dtos/utils/frequency.mapping.ts @@ -0,0 +1,4 @@ +import { Frequency } from '../../entities/frequency.enum'; +export const mappingKeyToFrequency = (index: number): string => { + return Frequency[index - 1]; +}; diff --git a/src/modules/ad/domain/entities/address.enum.ts b/src/modules/ad/domain/entities/address.enum.ts deleted file mode 100644 index 53a5a38..0000000 --- a/src/modules/ad/domain/entities/address.enum.ts +++ /dev/null @@ -1,7 +0,0 @@ -export enum AddressType { - HOUSE_NUMBER, - STREET_ADDRESS, - LOCALITY, - VENUE, - OTHER, -} diff --git a/src/modules/ad/domain/entities/address.ts b/src/modules/ad/domain/entities/address.ts index 6b9a783..abda857 100644 --- a/src/modules/ad/domain/entities/address.ts +++ b/src/modules/ad/domain/entities/address.ts @@ -1,5 +1,4 @@ import { AutoMap } from '@automapper/classes'; -import { AddressType } from './address.enum'; export class Address { @AutoMap() @@ -31,7 +30,4 @@ export class Address { @AutoMap() country: string; - - @AutoMap() - type: AddressType; } diff --git a/src/modules/ad/domain/usecases/create-ad.usecase.ts b/src/modules/ad/domain/usecases/create-ad.usecase.ts index bf2d3d3..5b4faa0 100644 --- a/src/modules/ad/domain/usecases/create-ad.usecase.ts +++ b/src/modules/ad/domain/usecases/create-ad.usecase.ts @@ -16,7 +16,7 @@ export class CreateAdUseCase { ) {} async execute(command: CreateAdCommand): Promise { - const entity = this._mapper.map( + const entity: Ad = this._mapper.map( command.createAdRequest, CreateAdRequest, Ad, @@ -24,8 +24,8 @@ export class CreateAdUseCase { try { const ad = await this._repository.create(entity); - this._messager.publish('ad.create', JSON.stringify(ad)); - this._messager.publish('logging.ad.create.info', JSON.stringify(ad)); + // this._messager.publish('ad.create', JSON.stringify(ad)); + // this._messager.publish('logging.ad.create.info', JSON.stringify(ad)); return ad; } catch (error) { let key = 'logging.ad.create.crit'; diff --git a/src/modules/ad/mappers/ad.profile.ts b/src/modules/ad/mappers/ad.profile.ts index abd117a..c501072 100644 --- a/src/modules/ad/mappers/ad.profile.ts +++ b/src/modules/ad/mappers/ad.profile.ts @@ -1,10 +1,9 @@ -import { createMap, forMember, mapFrom, Mapper } from '@automapper/core'; +import { createMap, Mapper } from '@automapper/core'; import { AutomapperProfile, InjectMapper } from '@automapper/nestjs'; 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 { Frequency } from '../domain/entities/frequency.enum'; @Injectable() export class AdProfile extends AutomapperProfile { @@ -15,17 +14,7 @@ export class AdProfile extends AutomapperProfile { override get profile() { return (mapper) => { createMap(mapper, Ad, AdPresenter); - createMap( - mapper, - CreateAdRequest, - Ad, - // forMember( - // (dest) => dest.frequency, - // mapFrom((source) => - // source.frequency == Frequency.PUNCTUAL ? 'PUNCTUAL' : 'RECCURENT', - // ), - // ), - ); + createMap(mapper, CreateAdRequest, Ad); }; } } diff --git a/src/modules/ad/tests/unit/create-ad.usecase.spec.ts b/src/modules/ad/tests/unit/create-ad.usecase.spec.ts index 1102ad1..a32e541 100644 --- a/src/modules/ad/tests/unit/create-ad.usecase.spec.ts +++ b/src/modules/ad/tests/unit/create-ad.usecase.spec.ts @@ -5,7 +5,6 @@ import { Address } from '../../domain/entities/address'; import { Messager } from '../../adapters/secondaries/messager'; import { AdsRepository } from '../../adapters/secondaries/ads.repository'; import { CreateAdCommand } from '../../commands/create-ad.command'; -import { AddressType } from '../../domain/entities/address.enum'; import { AutomapperModule } from '@automapper/nestjs'; import { classes } from '@automapper/classes'; import { Frequency } from '../../domain/entities/frequency.enum'; @@ -21,7 +20,6 @@ const mockAddress1: Address = { locality: 'Nancy', postalCode: '54000', country: 'France', - type: AddressType.HOUSE_NUMBER, }; const mockAddress2: Address = { position: 1, @@ -30,7 +28,6 @@ const mockAddress2: Address = { locality: 'Paris', postalCode: '75000', country: 'France', - type: AddressType.LOCALITY, }; const newAdRequest: CreateAdRequest = { @@ -40,8 +37,8 @@ const newAdRequest: CreateAdRequest = { frequency: Frequency.RECURRENT, fromDate: new Date('01-05-2023'), toDate: new Date('20-08-2023'), - tueTime: new Date(''), - wedTime: new Date(''), + tueTime: '08:00', + wedTime: '08:30', seatsPassenger: 2, addresses: [mockAddress1, mockAddress2], }; diff --git a/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts b/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts index dee142c..b18b2b5 100644 --- a/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts +++ b/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts @@ -80,6 +80,8 @@ export abstract class PrismaRepository implements IRepository { // TODO : using any is not good, but needed for nested entities // TODO : Refactor for good clean architecture ? async create(entity: Partial | any, include?: any): Promise { + console.log('repo entity '); + console.log(entity); try { const res = await this._prisma[this._model].create({ data: entity, @@ -88,6 +90,8 @@ export abstract class PrismaRepository implements IRepository { return res; } catch (e) { + console.log('repo error '); + console.log(e); if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( Prisma.PrismaClientKnownRequestError.name,