WIP setting default values

This commit is contained in:
Grégoire Chevalier 2023-05-11 17:42:43 +02:00
parent c35961cbd4
commit b035655bc6
12 changed files with 89 additions and 91 deletions

View File

@ -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")
}

View File

@ -41,6 +41,9 @@ export class AdController {
@GrpcMethod('AdsService', 'Create')
async createAd(data: CreateAdRequest): Promise<AdPresenter> {
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);

View File

@ -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 {

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -0,0 +1,4 @@
import { Frequency } from '../../entities/frequency.enum';
export const mappingKeyToFrequency = (index: number): string => {
return Frequency[index - 1];
};

View File

@ -1,7 +0,0 @@
export enum AddressType {
HOUSE_NUMBER,
STREET_ADDRESS,
LOCALITY,
VENUE,
OTHER,
}

View File

@ -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;
}

View File

@ -16,7 +16,7 @@ export class CreateAdUseCase {
) {}
async execute(command: CreateAdCommand): Promise<Ad> {
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';

View File

@ -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);
};
}
}

View File

@ -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],
};

View File

@ -80,6 +80,8 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
// TODO : using any is not good, but needed for nested entities
// TODO : Refactor for good clean architecture ?
async create(entity: Partial<T> | any, include?: any): Promise<T> {
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<T> implements IRepository<T> {
return res;
} catch (e) {
console.log('repo error ');
console.log(e);
if (e instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseException(
Prisma.PrismaClientKnownRequestError.name,