diff --git a/src/modules/ad/adapters/primaries/ad.controller.ts b/src/modules/ad/adapters/primaries/ad.controller.ts index a53df3c..3b462a2 100644 --- a/src/modules/ad/adapters/primaries/ad.controller.ts +++ b/src/modules/ad/adapters/primaries/ad.controller.ts @@ -25,7 +25,6 @@ export class AdController { @GrpcMethod('AdsService', 'FindOneByUuid') async findOnebyUuid(data: FindAdByUuidRequest): Promise { try { - console.log('ici'); const ad = await this.queryBus.execute(new FindAdByUuidQuery(data)); return this._mapper.map(ad, Ad, AdPresenter); } catch (e) { diff --git a/src/modules/ad/domain/dtos/create-ad.request.ts b/src/modules/ad/domain/dtos/create-ad.request.ts index f7cbd04..bc33e1f 100644 --- a/src/modules/ad/domain/dtos/create-ad.request.ts +++ b/src/modules/ad/domain/dtos/create-ad.request.ts @@ -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
; +} diff --git a/src/modules/ad/domain/entities/address.ts b/src/modules/ad/domain/entities/address.ts index e69de29..e527215 100644 --- a/src/modules/ad/domain/entities/address.ts +++ b/src/modules/ad/domain/entities/address.ts @@ -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; +} diff --git a/src/modules/ad/domain/entities/addressEnum.ts b/src/modules/ad/domain/entities/addressEnum.ts new file mode 100644 index 0000000..3aac4a4 --- /dev/null +++ b/src/modules/ad/domain/entities/addressEnum.ts @@ -0,0 +1,7 @@ +export enum AddressType { + HOUSE_NUMBER = 'HOUSE_NUMBER', + STREET_ADDRESS = 'STREET_ADDRESS', + LOCALITY = 'LOCALITY', + VENUE = 'VENUE', + OTHER = 'OTHER', +} diff --git a/src/modules/ad/domain/entities/frequencyEnum.ts b/src/modules/ad/domain/entities/frequencyEnum.ts new file mode 100644 index 0000000..996c2c0 --- /dev/null +++ b/src/modules/ad/domain/entities/frequencyEnum.ts @@ -0,0 +1,4 @@ +export enum FrequencyType { + PUNCTUAL = 'PUNCTUAL', + RECURRENT = 'RECURRENT', +} diff --git a/src/modules/ad/tests/unit/create-ad.usecase.spec.ts b/src/modules/ad/tests/unit/create-ad.usecase.spec.ts index 9d7d89b..97964b8 100644 --- a/src/modules/ad/tests/unit/create-ad.usecase.spec.ts +++ b/src/modules/ad/tests/unit/create-ad.usecase.spec.ts @@ -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 { 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 = { position: 0, - lon: '48.68944505415954', - lat: '6.176510296462267', - houseNumber: 5, + lon: 48.68944505415954, + lat: 6.176510296462267, + houseNumber: '5', street: 'Avenue Foch', locality: 'Nancy', postalCode: '75000', country: 'France', - type: 'HOUSE_NUMBER', + type: AddressType.HOUSE_NUMBER, }; const mockAddress2: Address = { position: 1, - lon: '48.8566', - lat: '2.3522', + lon: 48.8566, + lat: 2.3522, locality: 'Paris', postalCode: '75000', country: 'France', - type: 'LOCALITY', + type: AddressType.LOCALITY, }; const newAdRequest: CreateAdRequest = { - userUuid: 'a83e804a-e910-4c10-917f-5274533022b0', + userUuid: '113e0000-0000-4000-a000-000000000000', driver: true, passenger: false, - frequency: 'RECURRENT', + frequency: FrequencyType.RECURRENT, fromDate: new Date('01-05-2023'), toDate: new Date('20-08-2023'), tueTime: new Date(''), wedTime: new Date(''), seatsPassenger: 2, - adresses: [mockAddress1], + adresses: [mockAddress1, mockAddress2], }; const mockMessager = { 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', () => { - it('should be defined', () => {}); let createAdUseCase: CreateAdUseCase; beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ - imports: [], + imports: [AutomapperModule.forRoot({ strategyInitializer: classes() })], providers: [ { provide: AdsRepository, @@ -62,6 +82,10 @@ describe('CreateAdUseCase', () => { createAdUseCase = module.get(CreateAdUseCase); }); + it('should be defined', () => { + expect(createAdUseCase).toBeDefined(); + }); + describe('execution', () => { it('should create an new ad', () => {}); it('should throw an error if the ad already exists', () => {}); diff --git a/src/modules/ad/tests/unit/find-ad-by-uuid.usecase.spec.ts b/src/modules/ad/tests/unit/find-ad-by-uuid.usecase.spec.ts index df3375b..d176d96 100644 --- a/src/modules/ad/tests/unit/find-ad-by-uuid.usecase.spec.ts +++ b/src/modules/ad/tests/unit/find-ad-by-uuid.usecase.spec.ts @@ -18,7 +18,7 @@ const mockAdRepository = { return Promise.resolve(mockAd); }) .mockImplementation(() => { - return Promise.resolve(undefined); + return Promise.resolve(null); }), };