WIP create adresses

This commit is contained in:
Grégoire Chevalier 2023-05-05 17:33:08 +02:00
parent 393105d525
commit cac8e90c9b
7 changed files with 218 additions and 18 deletions

View File

@ -25,7 +25,6 @@ export class AdController {
@GrpcMethod('AdsService', 'FindOneByUuid') @GrpcMethod('AdsService', 'FindOneByUuid')
async findOnebyUuid(data: FindAdByUuidRequest): Promise<AdPresenter> { async findOnebyUuid(data: FindAdByUuidRequest): Promise<AdPresenter> {
try { try {
console.log('ici');
const ad = await this.queryBus.execute(new FindAdByUuidQuery(data)); const ad = await this.queryBus.execute(new FindAdByUuidQuery(data));
return this._mapper.map(ad, Ad, AdPresenter); return this._mapper.map(ad, Ad, AdPresenter);
} catch (e) { } catch (e) {

View File

@ -1 +1,133 @@
export class CreateAdRequest {} import { AutoMap } from '@automapper/classes';
import {
IsOptional,
IsString,
IsBoolean,
IsDate,
IsInt,
MinLength,
} from 'class-validator';
import { FrequencyType } from '../entities/frequencyEnum';
import { Address } from '../entities/address';
export class CreateAdRequest {
@IsString()
@IsOptional()
@AutoMap()
uuid?: string;
@IsString()
@AutoMap()
userUuid: string;
//@ValidateIf(o => o.passenger? false : true )
@IsBoolean()
@AutoMap()
driver: boolean;
//@ValidateIf(o => o.driver? false : true )
@IsBoolean()
@AutoMap()
passenger?: boolean;
@AutoMap()
frequency: FrequencyType;
@AutoMap()
fromDate: Date;
@AutoMap()
toDate: Date;
@IsDate()
@IsOptional()
@AutoMap()
monTime: Date;
@IsDate()
@IsOptional()
@AutoMap()
tueTime: Date;
@IsDate()
@IsOptional()
@AutoMap()
wedTime: Date;
@IsDate()
@IsOptional()
@AutoMap()
thuTime: Date;
@IsDate()
@IsOptional()
@AutoMap()
friTime: Date;
@IsDate()
@IsOptional()
@AutoMap()
satTime: Date;
@IsDate()
@IsOptional()
@AutoMap()
sunTime: Date;
@IsInt()
@IsOptional()
@AutoMap()
Margin: number;
@IsInt()
@IsOptional()
@AutoMap()
tueMargin: number;
@IsInt()
@IsOptional()
@AutoMap()
wedMargin: number;
@IsInt()
@IsOptional()
@AutoMap()
thuMargin: number;
@IsInt()
@IsOptional()
@AutoMap()
friMargin: number;
@IsInt()
@IsOptional()
@AutoMap()
satMargin: number;
@IsInt()
@IsOptional()
@AutoMap()
sunMargin: number;
//@ValidateIf(o => o.passenger? false : true )
@IsInt()
@AutoMap()
seatsDriver?: number;
//@ValidateIf(o => o.driver? false : true )
@IsInt()
@AutoMap()
seatsPassenger?: number;
@IsDate()
@AutoMap()
createdAt: Date;
@IsDate()
@AutoMap()
updatedAt: Date;
@MinLength(2)
@AutoMap()
addresses: Array<Address>;
}

View File

@ -0,0 +1,34 @@
import { AutoMap } from '@automapper/classes';
import { AddressType } from './addressEnum';
export class Address {
@AutoMap()
uuid?: string;
@AutoMap()
position: number;
@AutoMap()
lon: number;
@AutoMap()
lat: number;
@AutoMap()
houseNumber?: string;
@AutoMap()
street?: string;
@AutoMap()
locality: string;
@AutoMap()
postalCode: string;
@AutoMap()
country: string;
@AutoMap()
type: AddressType;
}

View File

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

View File

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

View File

@ -1,52 +1,72 @@
import { classes } from '@automapper/classes';
import { AutomapperModule } from '@automapper/nestjs';
import { Ad } from '../../domain/entities/ad';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { CreateAdUseCase } from '../../domain/usecases/create-ad.usecase'; import { CreateAdUseCase } from '../../domain/usecases/create-ad.usecase';
import { CreateAdRequest } from '../../domain/dtos/create-ad.request';
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/addressEnum';
import { AutomapperModule } from '@automapper/nestjs';
import { classes } from '@automapper/classes';
import { FrequencyType } from '../../domain/entities/frequencyEnum';
const mockAddress1: Address = { const mockAddress1: Address = {
position: 0, position: 0,
lon: '48.68944505415954', lon: 48.68944505415954,
lat: '6.176510296462267', lat: 6.176510296462267,
houseNumber: 5, houseNumber: '5',
street: 'Avenue Foch', street: 'Avenue Foch',
locality: 'Nancy', locality: 'Nancy',
postalCode: '75000', postalCode: '75000',
country: 'France', country: 'France',
type: 'HOUSE_NUMBER', type: AddressType.HOUSE_NUMBER,
}; };
const mockAddress2: Address = { const mockAddress2: Address = {
position: 1, position: 1,
lon: '48.8566', lon: 48.8566,
lat: '2.3522', lat: 2.3522,
locality: 'Paris', locality: 'Paris',
postalCode: '75000', postalCode: '75000',
country: 'France', country: 'France',
type: 'LOCALITY', type: AddressType.LOCALITY,
}; };
const newAdRequest: CreateAdRequest = { const newAdRequest: CreateAdRequest = {
userUuid: 'a83e804a-e910-4c10-917f-5274533022b0', userUuid: '113e0000-0000-4000-a000-000000000000',
driver: true, driver: true,
passenger: false, passenger: false,
frequency: 'RECURRENT', frequency: FrequencyType.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: new Date(''),
wedTime: new Date(''), wedTime: new Date(''),
seatsPassenger: 2, seatsPassenger: 2,
adresses: [mockAddress1], adresses: [mockAddress1, mockAddress2],
}; };
const mockMessager = { const mockMessager = {
publish: jest.fn().mockImplementation(), publish: jest.fn().mockImplementation(),
}; };
const mockAdRepository = {
findOneByUuid: jest
.fn()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementationOnce((command?: CreateAdCommand) => {
return Promise.resolve({
...newAdRequest,
uuid: 'ad000000-0000-4000-a000-000000000000',
});
})
.mockImplementation(() => {
throw new Error('Already exists');
}),
};
describe('CreateAdUseCase', () => { describe('CreateAdUseCase', () => {
it('should be defined', () => {});
let createAdUseCase: CreateAdUseCase; let createAdUseCase: CreateAdUseCase;
beforeAll(async () => { beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({ const module: TestingModule = await Test.createTestingModule({
imports: [], imports: [AutomapperModule.forRoot({ strategyInitializer: classes() })],
providers: [ providers: [
{ {
provide: AdsRepository, provide: AdsRepository,
@ -62,6 +82,10 @@ describe('CreateAdUseCase', () => {
createAdUseCase = module.get<CreateAdUseCase>(CreateAdUseCase); createAdUseCase = module.get<CreateAdUseCase>(CreateAdUseCase);
}); });
it('should be defined', () => {
expect(createAdUseCase).toBeDefined();
});
describe('execution', () => { describe('execution', () => {
it('should create an new ad', () => {}); it('should create an new ad', () => {});
it('should throw an error if the ad already exists', () => {}); it('should throw an error if the ad already exists', () => {});

View File

@ -18,7 +18,7 @@ const mockAdRepository = {
return Promise.resolve(mockAd); return Promise.resolve(mockAd);
}) })
.mockImplementation(() => { .mockImplementation(() => {
return Promise.resolve(undefined); return Promise.resolve(null);
}), }),
}; };