WIP setting default values
This commit is contained in:
parent
c35961cbd4
commit
b035655bc6
|
@ -43,9 +43,9 @@ model Ad {
|
||||||
}
|
}
|
||||||
|
|
||||||
model Address {
|
model Address {
|
||||||
uuid String @id @default(uuid()) @db.Uuid
|
uuid String @id @default(uuid()) @db.Uuid
|
||||||
adUuid String @db.Uuid
|
adUuid String @db.Uuid
|
||||||
position Int @db.SmallInt
|
position Int @db.SmallInt
|
||||||
lon Float
|
lon Float
|
||||||
lat Float
|
lat Float
|
||||||
name String?
|
name String?
|
||||||
|
@ -53,11 +53,10 @@ model Address {
|
||||||
street String?
|
street String?
|
||||||
locality String?
|
locality String?
|
||||||
postalCode String?
|
postalCode String?
|
||||||
country String?
|
country String
|
||||||
countryCode String?
|
createdAt DateTime @default(now())
|
||||||
createdAt DateTime @default(now())
|
updatedAt DateTime @default(now()) @updatedAt
|
||||||
updatedAt DateTime @default(now()) @updatedAt
|
Ad Ad @relation(fields: [adUuid], references: [uuid], onDelete: Cascade)
|
||||||
Ad Ad @relation(fields: [adUuid], references: [uuid], onDelete: Cascade)
|
|
||||||
|
|
||||||
@@map("address")
|
@@map("address")
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,9 @@ export class AdController {
|
||||||
|
|
||||||
@GrpcMethod('AdsService', 'Create')
|
@GrpcMethod('AdsService', 'Create')
|
||||||
async createAd(data: CreateAdRequest): Promise<AdPresenter> {
|
async createAd(data: CreateAdRequest): Promise<AdPresenter> {
|
||||||
|
console.log('presenter');
|
||||||
|
console.log(data);
|
||||||
|
console.log('*******************************');
|
||||||
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);
|
||||||
|
|
|
@ -20,24 +20,25 @@ message Ad {
|
||||||
bool driver = 3;
|
bool driver = 3;
|
||||||
bool passenger = 4;
|
bool passenger = 4;
|
||||||
Frequency frequency = 5;
|
Frequency frequency = 5;
|
||||||
string fromDate = 6;
|
optional string departure = 6;
|
||||||
string toDate = 7;
|
string fromDate = 7;
|
||||||
Schedule schedule = 8;
|
string toDate = 8;
|
||||||
MarginDurations marginDurations = 9;
|
Schedule schedule = 9;
|
||||||
optional int32 seatsPassenger = 10;
|
MarginDurations marginDurations = 10;
|
||||||
optional int32 seatsDriver = 11;
|
int32 seatsPassenger = 11;
|
||||||
optional bool strict = 12;
|
int32 seatsDriver = 12;
|
||||||
repeated Address addresses = 13;
|
bool strict = 13;
|
||||||
|
repeated Address addresses = 14;
|
||||||
}
|
}
|
||||||
|
|
||||||
message Schedule {
|
message Schedule {
|
||||||
string mon = 1;
|
optional string mon = 1;
|
||||||
string tue = 2;
|
optional string tue = 2;
|
||||||
string wed = 3;
|
optional string wed = 3;
|
||||||
string thu = 4;
|
optional string thu = 4;
|
||||||
string fri = 5;
|
optional string fri = 5;
|
||||||
string sat = 6;
|
optional string sat = 6;
|
||||||
string sun = 7;
|
optional string sun = 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
message MarginDurations {
|
message MarginDurations {
|
||||||
|
@ -51,16 +52,16 @@ message MarginDurations {
|
||||||
}
|
}
|
||||||
|
|
||||||
message Address {
|
message Address {
|
||||||
int32 position =1;
|
string uuid = 1;
|
||||||
float lon = 2;
|
int32 position = 2;
|
||||||
float lat = 3;
|
float lon = 3;
|
||||||
optional string name =4;
|
float lat = 4;
|
||||||
optional string houseNumber = 5;
|
optional string name = 5;
|
||||||
optional string street = 6;
|
optional string houseNumber = 6;
|
||||||
optional string locality = 7;
|
optional string street = 7;
|
||||||
optional string postalCode = 8;
|
optional string locality = 8;
|
||||||
optional string country = 9;
|
optional string postalCode = 9;
|
||||||
optional string countryCode = 10;
|
string country = 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -70,8 +71,8 @@ enum Frequency {
|
||||||
}
|
}
|
||||||
|
|
||||||
message AdFilter {
|
message AdFilter {
|
||||||
optional int32 page = 1;
|
int32 page = 1;
|
||||||
optional int32 perPage = 2;
|
int32 perPage = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
message Ads {
|
message Ads {
|
||||||
|
|
|
@ -5,5 +5,13 @@ export class CreateAdCommand {
|
||||||
|
|
||||||
constructor(request: CreateAdRequest) {
|
constructor(request: CreateAdRequest) {
|
||||||
this.createAdRequest = request;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,10 +9,15 @@ import {
|
||||||
ValidateNested,
|
ValidateNested,
|
||||||
IsLongitude,
|
IsLongitude,
|
||||||
IsLatitude,
|
IsLatitude,
|
||||||
|
ValidateIf,
|
||||||
|
IsArray,
|
||||||
|
ArrayMinSize,
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
import { Frequency } from '../entities/frequency.enum';
|
import { Frequency } from '../entities/frequency.enum';
|
||||||
import { Transform, Type } from 'class-transformer';
|
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 {
|
export class CreateAdRequest {
|
||||||
@IsString()
|
@IsString()
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
|
@ -28,26 +33,28 @@ export class CreateAdRequest {
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
passenger?: boolean;
|
passenger?: boolean;
|
||||||
|
|
||||||
@Transform(({ value }) => Frequency[value - 1], { toClassOnly: true })
|
@Transform(({ value }) => mappingKeyToFrequency(value), {
|
||||||
|
toClassOnly: true,
|
||||||
|
})
|
||||||
@IsEnum(Frequency)
|
@IsEnum(Frequency)
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
frequency: Frequency;
|
frequency: Frequency;
|
||||||
|
|
||||||
|
@ValidateIf((ad) => ad.frequency === 'PUNCTUAL')
|
||||||
@Type(() => Date)
|
@Type(() => Date)
|
||||||
@IsDate()
|
@IsDate()
|
||||||
@IsOptional()
|
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
departure?: Date;
|
departure?: Date;
|
||||||
|
|
||||||
|
@ValidateIf((ad) => ad.frequency === 'RECURRENT')
|
||||||
@Type(() => Date)
|
@Type(() => Date)
|
||||||
@IsDate()
|
@IsDate()
|
||||||
@IsOptional()
|
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
fromDate?: Date;
|
fromDate?: Date;
|
||||||
|
|
||||||
|
@ValidateIf((ad) => ad.frequency === 'RECURRENT')
|
||||||
@Type(() => Date)
|
@Type(() => Date)
|
||||||
@IsDate()
|
@IsDate()
|
||||||
@IsOptional()
|
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
toDate?: Date;
|
toDate?: Date;
|
||||||
|
|
||||||
|
@ -86,6 +93,15 @@ export class CreateAdRequest {
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
sunTime?: string;
|
sunTime?: string;
|
||||||
|
|
||||||
|
@Transform(
|
||||||
|
({ value }) => {
|
||||||
|
console.log(value);
|
||||||
|
return value;
|
||||||
|
},
|
||||||
|
{
|
||||||
|
toClassOnly: true,
|
||||||
|
},
|
||||||
|
)
|
||||||
@IsInt()
|
@IsInt()
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
|
@ -121,7 +137,7 @@ export class CreateAdRequest {
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
sunMargin?: number;
|
sunMargin?: number;
|
||||||
|
|
||||||
@IsOptional()
|
@ValidateIf((ad) => ad.driver)
|
||||||
@IsInt()
|
@IsInt()
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
seatsDriver?: number;
|
seatsDriver?: number;
|
||||||
|
@ -136,15 +152,9 @@ export class CreateAdRequest {
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
strict?: boolean;
|
strict?: boolean;
|
||||||
|
|
||||||
|
@IsArray()
|
||||||
|
@ArrayMinSize(2)
|
||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
@Transform(
|
|
||||||
({ value }) => {
|
|
||||||
console.log('in dto');
|
|
||||||
console.log(value);
|
|
||||||
return value;
|
|
||||||
},
|
|
||||||
{ toClassOnly: true },
|
|
||||||
)
|
|
||||||
@Type(() => AddressDTO)
|
@Type(() => AddressDTO)
|
||||||
@AutoMap(() => [AddressDTO])
|
@AutoMap(() => [AddressDTO])
|
||||||
addresses: AddressDTO[];
|
addresses: AddressDTO[];
|
||||||
|
@ -189,12 +199,6 @@ class AddressDTO {
|
||||||
postalCode?: string;
|
postalCode?: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsOptional()
|
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
country?: string;
|
country: string;
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
@AutoMap()
|
|
||||||
countryCode?: string;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
import { Frequency } from '../../entities/frequency.enum';
|
||||||
|
export const mappingKeyToFrequency = (index: number): string => {
|
||||||
|
return Frequency[index - 1];
|
||||||
|
};
|
|
@ -1,7 +0,0 @@
|
||||||
export enum AddressType {
|
|
||||||
HOUSE_NUMBER,
|
|
||||||
STREET_ADDRESS,
|
|
||||||
LOCALITY,
|
|
||||||
VENUE,
|
|
||||||
OTHER,
|
|
||||||
}
|
|
|
@ -1,5 +1,4 @@
|
||||||
import { AutoMap } from '@automapper/classes';
|
import { AutoMap } from '@automapper/classes';
|
||||||
import { AddressType } from './address.enum';
|
|
||||||
|
|
||||||
export class Address {
|
export class Address {
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
|
@ -31,7 +30,4 @@ export class Address {
|
||||||
|
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
country: string;
|
country: string;
|
||||||
|
|
||||||
@AutoMap()
|
|
||||||
type: AddressType;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ export class CreateAdUseCase {
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async execute(command: CreateAdCommand): Promise<Ad> {
|
async execute(command: CreateAdCommand): Promise<Ad> {
|
||||||
const entity = this._mapper.map(
|
const entity: Ad = this._mapper.map(
|
||||||
command.createAdRequest,
|
command.createAdRequest,
|
||||||
CreateAdRequest,
|
CreateAdRequest,
|
||||||
Ad,
|
Ad,
|
||||||
|
@ -24,8 +24,8 @@ export class CreateAdUseCase {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const ad = await this._repository.create(entity);
|
const 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));
|
||||||
return ad;
|
return ad;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
let key = 'logging.ad.create.crit';
|
let key = 'logging.ad.create.crit';
|
||||||
|
|
|
@ -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 { AutomapperProfile, InjectMapper } from '@automapper/nestjs';
|
||||||
import { Injectable } from '@nestjs/common';
|
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 { Frequency } from '../domain/entities/frequency.enum';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AdProfile extends AutomapperProfile {
|
export class AdProfile extends AutomapperProfile {
|
||||||
|
@ -15,17 +14,7 @@ export class AdProfile extends AutomapperProfile {
|
||||||
override get profile() {
|
override get profile() {
|
||||||
return (mapper) => {
|
return (mapper) => {
|
||||||
createMap(mapper, Ad, AdPresenter);
|
createMap(mapper, Ad, AdPresenter);
|
||||||
createMap(
|
createMap(mapper, CreateAdRequest, Ad);
|
||||||
mapper,
|
|
||||||
CreateAdRequest,
|
|
||||||
Ad,
|
|
||||||
// forMember(
|
|
||||||
// (dest) => dest.frequency,
|
|
||||||
// mapFrom((source) =>
|
|
||||||
// source.frequency == Frequency.PUNCTUAL ? 'PUNCTUAL' : 'RECCURENT',
|
|
||||||
// ),
|
|
||||||
// ),
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ 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';
|
||||||
import { AddressType } from '../../domain/entities/address.enum';
|
|
||||||
import { AutomapperModule } from '@automapper/nestjs';
|
import { AutomapperModule } from '@automapper/nestjs';
|
||||||
import { classes } from '@automapper/classes';
|
import { classes } from '@automapper/classes';
|
||||||
import { Frequency } from '../../domain/entities/frequency.enum';
|
import { Frequency } from '../../domain/entities/frequency.enum';
|
||||||
|
@ -21,7 +20,6 @@ const mockAddress1: Address = {
|
||||||
locality: 'Nancy',
|
locality: 'Nancy',
|
||||||
postalCode: '54000',
|
postalCode: '54000',
|
||||||
country: 'France',
|
country: 'France',
|
||||||
type: AddressType.HOUSE_NUMBER,
|
|
||||||
};
|
};
|
||||||
const mockAddress2: Address = {
|
const mockAddress2: Address = {
|
||||||
position: 1,
|
position: 1,
|
||||||
|
@ -30,7 +28,6 @@ const mockAddress2: Address = {
|
||||||
locality: 'Paris',
|
locality: 'Paris',
|
||||||
postalCode: '75000',
|
postalCode: '75000',
|
||||||
country: 'France',
|
country: 'France',
|
||||||
type: AddressType.LOCALITY,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const newAdRequest: CreateAdRequest = {
|
const newAdRequest: CreateAdRequest = {
|
||||||
|
@ -40,8 +37,8 @@ const newAdRequest: CreateAdRequest = {
|
||||||
frequency: Frequency.RECURRENT,
|
frequency: Frequency.RECURRENT,
|
||||||
fromDate: new Date('01-05-2023'),
|
fromDate: new Date('01-05-2023'),
|
||||||
toDate: new Date('20-08-2023'),
|
toDate: new Date('20-08-2023'),
|
||||||
tueTime: new Date(''),
|
tueTime: '08:00',
|
||||||
wedTime: new Date(''),
|
wedTime: '08:30',
|
||||||
seatsPassenger: 2,
|
seatsPassenger: 2,
|
||||||
addresses: [mockAddress1, mockAddress2],
|
addresses: [mockAddress1, mockAddress2],
|
||||||
};
|
};
|
||||||
|
|
|
@ -80,6 +80,8 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
// TODO : using any is not good, but needed for nested entities
|
// TODO : using any is not good, but needed for nested entities
|
||||||
// TODO : Refactor for good clean architecture ?
|
// TODO : Refactor for good clean architecture ?
|
||||||
async create(entity: Partial<T> | any, include?: any): Promise<T> {
|
async create(entity: Partial<T> | any, include?: any): Promise<T> {
|
||||||
|
console.log('repo entity ');
|
||||||
|
console.log(entity);
|
||||||
try {
|
try {
|
||||||
const res = await this._prisma[this._model].create({
|
const res = await this._prisma[this._model].create({
|
||||||
data: entity,
|
data: entity,
|
||||||
|
@ -88,6 +90,8 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.log('repo error ');
|
||||||
|
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,
|
||||||
|
|
Loading…
Reference in New Issue