From def8caa6e174ea8d007d56333dc9378de89b856c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Chevalier?= Date: Thu, 11 May 2023 10:45:44 +0200 Subject: [PATCH] update and sync prisma schema and create ad dto --- prisma/schema.prisma | 36 ++++-------- .../ad/domain/dtos/create-ad.request.ts | 56 ++++++++----------- src/modules/ad/domain/entities/ad.ts | 40 +++++++------ .../ad/domain/entities/address.enum.ts | 2 +- .../ad/domain/entities/frequency.enum.ts | 2 +- 5 files changed, 55 insertions(+), 81 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 5cbb3e6..56a63e8 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -17,7 +17,7 @@ model Ad { passenger Boolean frequency Frequency fromDate DateTime @db.Date - toDate DateTime @db.Date + toDate DateTime @db.Date monTime DateTime? @db.Timestamptz() tueTime DateTime? @db.Timestamptz() wedTime DateTime? @db.Timestamptz() @@ -25,23 +25,19 @@ model Ad { friTime DateTime? @db.Timestamptz() satTime DateTime? @db.Timestamptz() sunTime DateTime? @db.Timestamptz() - monMargin Int? - tueMargin Int? - wedMargin Int? - thuMargin Int? - friMargin Int? - satMargin Int? - sunMargin Int? - seatsDriver Int @db.SmallInt - seatsPassenger Int @db.SmallInt + monMargin Int + tueMargin Int + wedMargin Int + thuMargin Int + friMargin Int + satMargin Int + sunMargin Int + seatsDriver Int @db.SmallInt + seatsPassenger Int @db.SmallInt createdAt DateTime @default(now()) updatedAt DateTime @default(now()) @updatedAt addresses Address[] - @@index([driver]) - @@index([passenger]) - @@index([fromDate]) - @@index([toDate]) @@map("ad") } @@ -56,8 +52,8 @@ model Address { street String? locality String? postalCode String? - country String? - type AddressType? + country String? + countryCode String? createdAt DateTime @default(now()) updatedAt DateTime @default(now()) @updatedAt Ad Ad @relation(fields: [adUuid], references: [uuid], onDelete: Cascade) @@ -69,11 +65,3 @@ enum Frequency { PUNCTUAL RECURRENT } - -enum AddressType { - HOUSE_NUMBER - STREET_ADDRESS - LOCALITY - VENUE - OTHER -} diff --git a/src/modules/ad/domain/dtos/create-ad.request.ts b/src/modules/ad/domain/dtos/create-ad.request.ts index 7181670..0ef4480 100644 --- a/src/modules/ad/domain/dtos/create-ad.request.ts +++ b/src/modules/ad/domain/dtos/create-ad.request.ts @@ -6,51 +6,53 @@ import { IsDate, IsInt, ValidateIf, - IsArray, IsEnum, IsNumber, ValidateNested, + IsLongitude, + IsLatitude, } from 'class-validator'; import { Frequency } from '../entities/frequency.enum'; import { Transform, Type } from 'class-transformer'; import { AddressType } from '../entities/address.enum'; export class CreateAdRequest { - @IsString() - @IsOptional() - @AutoMap() - uuid?: string; - @IsString() @AutoMap() userUuid: string; - @ValidateIf((ad) => (ad.passenger ? false : true)) @IsOptional() @IsBoolean() @AutoMap() driver?: boolean; - @ValidateIf((ad) => (ad.driver ? false : true)) @IsOptional() @IsBoolean() @AutoMap() passenger?: boolean; - @Transform(({ value }) => Frequency[value], { toClassOnly: true }) + @Transform(({ value }) => Frequency[value - 1], { toClassOnly: true }) @IsEnum(Frequency) @AutoMap() frequency: Frequency; @Type(() => Date) @IsDate() + @IsOptional() @AutoMap() - fromDate: Date; + departure?: Date; @Type(() => Date) @IsDate() + @IsOptional() @AutoMap() - toDate: Date; + fromDate?: Date; + + @Type(() => Date) + @IsDate() + @IsOptional() + @AutoMap() + toDate?: Date; @IsDate() @IsOptional() @@ -122,14 +124,11 @@ export class CreateAdRequest { @AutoMap() sunMargin?: number; - @ValidateIf((ad) => (ad.passenger ? false : true)) @IsOptional() @IsInt() @AutoMap() seatsDriver?: number; - @ValidateIf((ad) => (ad.driver ? false : true)) - //@Transform(({ value }) => (value ? value : 0), { toClassOnly: true }) @IsOptional() @IsInt() @AutoMap() @@ -140,18 +139,6 @@ export class CreateAdRequest { @AutoMap() strict?: boolean; - @Type(() => Date) - @IsDate() - @IsOptional() - @AutoMap() - createdAt?: Date; - - @Type(() => Date) - @IsDate() - @IsOptional() - @AutoMap() - updatedAt?: Date; - @ValidateNested({ each: true }) @Transform( ({ value }) => { @@ -167,15 +154,15 @@ export class CreateAdRequest { } class AddressDTO { - @IsNumber() + @IsInt() @AutoMap() position: number; - @IsNumber() + @IsLongitude() @AutoMap() lon: number; - @IsNumber() + @IsLatitude() @AutoMap() lat: number; @@ -197,19 +184,20 @@ class AddressDTO { @IsString() @IsOptional() @AutoMap() - locality: string; + locality?: string; @IsString() @IsOptional() @AutoMap() - postalCode: string; + postalCode?: string; @IsString() @IsOptional() @AutoMap() - country: string; + country?: string; - @Transform(({ value }) => AddressType[value], { toClassOnly: true }) + @IsString() + @IsOptional() @AutoMap() - type: AddressType; + countryCode?: string; } diff --git a/src/modules/ad/domain/entities/ad.ts b/src/modules/ad/domain/entities/ad.ts index 013dfc0..0b56f68 100644 --- a/src/modules/ad/domain/entities/ad.ts +++ b/src/modules/ad/domain/entities/ad.ts @@ -5,10 +5,10 @@ import { IsBoolean, IsDate, IsInt, - MinLength, - ValidateIf, + IsEnum, } from 'class-validator'; import { Address } from '../entities/address'; +import { Frequency } from './frequency.enum'; export class Ad { @IsString() @@ -19,59 +19,60 @@ export class Ad { @AutoMap() userUuid: string; - @ValidateIf((ad) => (ad.passenger ? false : true)) @IsBoolean() @AutoMap() driver?: boolean; - @ValidateIf((ad) => (ad.driver ? false : true)) @IsBoolean() @AutoMap() passenger?: boolean; + @IsEnum(Frequency) @AutoMap() - frequency: string; + frequency: Frequency; + @IsDate() @AutoMap() fromDate: Date; + @IsDate() @AutoMap() toDate: Date; @IsDate() @IsOptional() @AutoMap() - monTime?: Date; + monTime?: string; - @IsDate() + @IsString() @IsOptional() @AutoMap() - tueTime?: Date; + tueTime?: string; - @IsDate() + @IsString() @IsOptional() @AutoMap() - wedTime?: Date; + wedTime?: string; - @IsDate() + @IsString() @IsOptional() @AutoMap() - thuTime?: Date; + thuTime?: string; - @IsDate() + @IsString() @IsOptional() @AutoMap() - friTime?: Date; + friTime?: string; - @IsDate() + @IsString() @IsOptional() @AutoMap() - satTime?: Date; + satTime?: string; - @IsDate() + @IsString() @IsOptional() @AutoMap() - sunTime?: Date; + sunTime?: string; @IsInt() @IsOptional() @@ -108,12 +109,10 @@ export class Ad { @AutoMap() sunMargin?: number; - @ValidateIf((ad) => (ad.passenger ? false : true)) @IsInt() @AutoMap() seatsDriver?: number; - @ValidateIf((ad) => (ad.driver ? false : true)) @IsInt() @AutoMap() seatsPassenger?: number; @@ -127,7 +126,6 @@ export class Ad { @AutoMap() updatedAt?: Date; - @MinLength(2) @AutoMap() addresses?: Array
; } diff --git a/src/modules/ad/domain/entities/address.enum.ts b/src/modules/ad/domain/entities/address.enum.ts index 934012f..53a5a38 100644 --- a/src/modules/ad/domain/entities/address.enum.ts +++ b/src/modules/ad/domain/entities/address.enum.ts @@ -1,5 +1,5 @@ export enum AddressType { - HOUSE_NUMBER = 1, + HOUSE_NUMBER, STREET_ADDRESS, LOCALITY, VENUE, diff --git a/src/modules/ad/domain/entities/frequency.enum.ts b/src/modules/ad/domain/entities/frequency.enum.ts index 629b865..6c571eb 100644 --- a/src/modules/ad/domain/entities/frequency.enum.ts +++ b/src/modules/ad/domain/entities/frequency.enum.ts @@ -1,4 +1,4 @@ export enum Frequency { - PUNCTUAL = 1, + PUNCTUAL, RECURRENT, }