fix dto frequency transformation

This commit is contained in:
Grégoire Chevalier 2023-05-10 15:44:25 +02:00
parent 5095082f2e
commit b83baef1ee
5 changed files with 27 additions and 33 deletions

View File

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

View File

@ -7,6 +7,7 @@ import {
IsInt, IsInt,
ValidateIf, ValidateIf,
IsArray, IsArray,
IsEnum,
} from 'class-validator'; } from 'class-validator';
import { Frequency } from '../entities/frequency.enum'; import { Frequency } from '../entities/frequency.enum';
import { Address } from '../entities/address'; import { Address } from '../entities/address';
@ -34,7 +35,11 @@ export class CreateAdRequest {
@AutoMap() @AutoMap()
passenger?: boolean; passenger?: boolean;
@Transform(({ value }) => Frequency[value]) @Transform(
({ value }) => (value == 1 ? Frequency.PUNCTUAL : Frequency.RECURRENT),
{ toClassOnly: true },
)
@IsEnum(Frequency)
@AutoMap() @AutoMap()
frequency: Frequency; frequency: Frequency;
@ -48,47 +53,40 @@ export class CreateAdRequest {
@AutoMap() @AutoMap()
toDate: Date; toDate: Date;
@Type(() => Date)
@IsDate() @IsDate()
@IsOptional() @IsOptional()
@AutoMap() @AutoMap()
monTime?: Date; monTime?: string;
@Type(() => Date) @IsString()
@IsDate()
@IsOptional() @IsOptional()
@AutoMap() @AutoMap()
tueTime?: Date; tueTime?: string;
@Type(() => Date) @IsString()
@IsDate()
@IsOptional() @IsOptional()
@AutoMap() @AutoMap()
wedTime?: Date; wedTime?: string;
@Type(() => Date) @IsString()
@IsDate()
@IsOptional() @IsOptional()
@AutoMap() @AutoMap()
thuTime?: Date; thuTime?: string;
@Type(() => Date) @IsString()
@IsDate()
@IsOptional() @IsOptional()
@AutoMap() @AutoMap()
friTime?: Date; friTime?: string;
@Type(() => Date) @IsString()
@IsDate()
@IsOptional() @IsOptional()
@AutoMap() @AutoMap()
satTime?: Date; satTime?: string;
@Type(() => Date) @IsString()
@IsDate()
@IsOptional() @IsOptional()
@AutoMap() @AutoMap()
sunTime?: Date; sunTime?: string;
@IsInt() @IsInt()
@IsOptional() @IsOptional()

View File

@ -1,4 +1,4 @@
export enum Frequency { export enum Frequency {
PUNCTUAL, PUNCTUAL = 'PUNCTUAL',
RECURRENT, RECURRENT = 'RECURRENT',
} }

View File

@ -19,12 +19,12 @@ export class AdProfile extends AutomapperProfile {
mapper, mapper,
CreateAdRequest, CreateAdRequest,
Ad, Ad,
forMember( // forMember(
(dest) => dest.frequency, // (dest) => dest.frequency,
mapFrom((source) => // mapFrom((source) =>
source.frequency == Frequency.PUNCTUAL ? 'PUNCTUAL' : 'RECCURENT', // source.frequency == Frequency.PUNCTUAL ? 'PUNCTUAL' : 'RECCURENT',
), // ),
), // ),
); );
}; };
} }

View File

@ -88,8 +88,6 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
return res; return res;
} catch (e) { } catch (e) {
console.log('repository');
console.log(e);
if (e instanceof Prisma.PrismaClientKnownRequestError) { if (e instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseException( throw new DatabaseException(
Prisma.PrismaClientKnownRequestError.name, Prisma.PrismaClientKnownRequestError.name,