From e2e3bd44077d671b9f77a204d07c2405d76b055d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Chevalier?= Date: Thu, 25 May 2023 13:39:28 +0200 Subject: [PATCH] refactor validators --- .../ad/domain/dtos/create-ad.request.ts | 14 +-- .../address-position.validator.ts | 0 .../frequency.mapping.ts | 0 .../has-driver-seats.validator.ts | 15 ++- .../has-passenger-seats.validator.ts | 13 ++- .../is-punctual-or-recurrent.validator.ts | 38 ++++++++ ...-normaliser.ts => recurrent-normaliser.ts} | 6 +- .../ad/domain/usecases/create-ad.usecase.ts | 1 + src/modules/ad/mappers/ad.profile.ts | 22 ++--- src/modules/ad/mappers/address.profile.ts | 18 ++++ .../tests/integration/ad.repository.spec.ts | 36 +++++++- .../unit/domain/create-ad.usecase.spec.ts | 16 ++-- .../unit/domain/frequency.mapping.spec.ts | 2 +- .../domain/has-driver-seats-validator.spec.ts | 42 ++++----- .../has-passenger-seats-validator.spec.ts | 42 ++++----- .../has-proper-addresses-indexes.spec.ts | 2 +- .../unit/domain/reccurent-normaliser.spec.ts | 92 ------------------- .../unit/domain/recurrent-normaliser.spec.ts | 92 +++++++++++++++++++ 18 files changed, 278 insertions(+), 173 deletions(-) rename src/modules/ad/domain/dtos/{utils => validators}/address-position.validator.ts (100%) rename src/modules/ad/domain/dtos/{utils => validators}/frequency.mapping.ts (100%) rename src/modules/ad/domain/dtos/{utils => validators}/has-driver-seats.validator.ts (64%) rename src/modules/ad/domain/dtos/{utils => validators}/has-passenger-seats.validator.ts (70%) create mode 100644 src/modules/ad/domain/dtos/validators/is-punctual-or-recurrent.validator.ts rename src/modules/ad/domain/entities/{reccurent-normaliser.ts => recurrent-normaliser.ts} (96%) create mode 100644 src/modules/ad/mappers/address.profile.ts delete mode 100644 src/modules/ad/tests/unit/domain/reccurent-normaliser.spec.ts create mode 100644 src/modules/ad/tests/unit/domain/recurrent-normaliser.spec.ts diff --git a/src/modules/ad/domain/dtos/create-ad.request.ts b/src/modules/ad/domain/dtos/create-ad.request.ts index b1d0ccd..9de8a83 100644 --- a/src/modules/ad/domain/dtos/create-ad.request.ts +++ b/src/modules/ad/domain/dtos/create-ad.request.ts @@ -13,13 +13,13 @@ import { } from 'class-validator'; import { Frequency } from '../types/frequency.enum'; import { Transform, Type } from 'class-transformer'; -import { mappingKeyToFrequency } from './utils/frequency.mapping'; +import { mappingKeyToFrequency } from './validators/frequency.mapping'; import { MarginDTO } from './create.margin.dto'; import { ScheduleDTO } from './create.schedule.dto'; import { AddressRequestDTO } from './create.address.request'; -import { HasProperPassengerSeats } from './utils/has-passenger-seats.validator'; -import { HasProperDriverSeats } from './utils/has-driver-seats.validator'; -import { HasProperPositionIndexes } from './utils/address-position.validator'; +import { HasProperPassengerSeats } from './validators/has-passenger-seats.validator'; +import { HasProperDriverSeats } from './validators/has-driver-seats.validator'; +import { HasProperPositionIndexes } from './validators/address-position.validator'; export class CreateAdRequest { @IsOptional() @@ -32,13 +32,11 @@ export class CreateAdRequest { userUuid: string; @IsOptional() - @HasProperDriverSeats() @IsBoolean() @AutoMap() driver?: boolean; @IsOptional() - @HasProperPassengerSeats() @IsBoolean() @AutoMap() passenger?: boolean; @@ -81,12 +79,14 @@ export class CreateAdRequest { @AutoMap() marginDurations?: MarginDTO; - @ValidateIf((ad) => ad.driver) + @IsOptional() + @HasProperDriverSeats() @IsInt() @AutoMap() seatsDriver?: number; @IsOptional() + @HasProperPassengerSeats() @IsInt() @AutoMap() seatsPassenger?: number; diff --git a/src/modules/ad/domain/dtos/utils/address-position.validator.ts b/src/modules/ad/domain/dtos/validators/address-position.validator.ts similarity index 100% rename from src/modules/ad/domain/dtos/utils/address-position.validator.ts rename to src/modules/ad/domain/dtos/validators/address-position.validator.ts diff --git a/src/modules/ad/domain/dtos/utils/frequency.mapping.ts b/src/modules/ad/domain/dtos/validators/frequency.mapping.ts similarity index 100% rename from src/modules/ad/domain/dtos/utils/frequency.mapping.ts rename to src/modules/ad/domain/dtos/validators/frequency.mapping.ts diff --git a/src/modules/ad/domain/dtos/utils/has-driver-seats.validator.ts b/src/modules/ad/domain/dtos/validators/has-driver-seats.validator.ts similarity index 64% rename from src/modules/ad/domain/dtos/utils/has-driver-seats.validator.ts rename to src/modules/ad/domain/dtos/validators/has-driver-seats.validator.ts index 0ad1316..9a6981c 100644 --- a/src/modules/ad/domain/dtos/utils/has-driver-seats.validator.ts +++ b/src/modules/ad/domain/dtos/validators/has-driver-seats.validator.ts @@ -5,11 +5,16 @@ import { buildMessage, } from 'class-validator'; -export function hasProperDriverSeats(value: any, args: ValidationArguments) { - if (value === true && typeof args.object['seatsDriver'] === 'number') +export function hasProperDriverSeats(args: ValidationArguments) { + if ( + args.object['driver'] === true && + typeof args.object['seatsDriver'] === 'number' + ) return args.object['seatsDriver'] > 0; else if ( - (value === false || value === null || value === undefined) && + (args.object['driver'] === false || + args.object['driver'] === null || + args.object['driver'] === undefined) && (args.object['seatsDriver'] === 0 || args.object['seatsDriver'] === null || args.object['seatsDriver'] === undefined) @@ -26,8 +31,8 @@ export function HasProperDriverSeats( name: '', constraints: [], validator: { - validate: (value, args: ValidationArguments): boolean => - hasProperDriverSeats(value, args), + validate: (value: any, args: ValidationArguments): boolean => + hasProperDriverSeats(args), defaultMessage: buildMessage( () => `driver and driver seats are not correct`, validationOptions, diff --git a/src/modules/ad/domain/dtos/utils/has-passenger-seats.validator.ts b/src/modules/ad/domain/dtos/validators/has-passenger-seats.validator.ts similarity index 70% rename from src/modules/ad/domain/dtos/utils/has-passenger-seats.validator.ts rename to src/modules/ad/domain/dtos/validators/has-passenger-seats.validator.ts index 9085016..ba6d521 100644 --- a/src/modules/ad/domain/dtos/utils/has-passenger-seats.validator.ts +++ b/src/modules/ad/domain/dtos/validators/has-passenger-seats.validator.ts @@ -5,11 +5,16 @@ import { buildMessage, } from 'class-validator'; -export function hasProperPassengerSeats(value: any, args: ValidationArguments) { - if (value === true && typeof args.object['seatsPassenger'] === 'number') +export function hasProperPassengerSeats(args: ValidationArguments) { + if ( + args.object['passenger'] === true && + typeof args.object['seatsPassenger'] === 'number' + ) return args.object['seatsPassenger'] > 0; else if ( - (value === false || value === null || value === undefined) && + (args.object['passenger'] === false || + args.object['passenger'] === null || + args.object['passenger'] === undefined) && (args.object['seatsPassenger'] === 0 || args.object['seatsPassenger'] === null || args.object['seatsPassenger'] === undefined) @@ -27,7 +32,7 @@ export function HasProperPassengerSeats( constraints: [], validator: { validate: (value, args: ValidationArguments): boolean => - hasProperPassengerSeats(value, args), + hasProperPassengerSeats(args), defaultMessage: buildMessage( () => `passenger and passenger seats are not correct`, validationOptions, diff --git a/src/modules/ad/domain/dtos/validators/is-punctual-or-recurrent.validator.ts b/src/modules/ad/domain/dtos/validators/is-punctual-or-recurrent.validator.ts new file mode 100644 index 0000000..4e0e56d --- /dev/null +++ b/src/modules/ad/domain/dtos/validators/is-punctual-or-recurrent.validator.ts @@ -0,0 +1,38 @@ +import { + ValidateBy, + ValidationArguments, + ValidationOptions, + buildMessage, +} from 'class-validator'; + +function isPunctual(): boolean { + throw new Error('Function not implemented.'); +} + +function isRecurrent(): boolean { + throw new Error('Function not implemented.'); +} + +export const isPunctualOrRecurrent = (): boolean => { + return isPunctual() || isRecurrent(); +}; +/* istanbul ignore next */ +// export function IsPunctualOrRecurrent( +// validationOptions?: ValidationOptions, +// ): PropertyDecorator { +// return ValidateBy( +// { +// name: '', +// constraints: [], +// validator: { +// validate: (value, args: ValidationArguments): boolean => +// isPunctualOrRecurrent(value, args), +// defaultMessage: buildMessage( +// () => `driver and driver seats are not correct`, +// validationOptions, +// ), +// }, +// }, +// validationOptions, +// ); +// } diff --git a/src/modules/ad/domain/entities/reccurent-normaliser.ts b/src/modules/ad/domain/entities/recurrent-normaliser.ts similarity index 96% rename from src/modules/ad/domain/entities/reccurent-normaliser.ts rename to src/modules/ad/domain/entities/recurrent-normaliser.ts index 2f9e7a2..40cbf68 100644 --- a/src/modules/ad/domain/entities/reccurent-normaliser.ts +++ b/src/modules/ad/domain/entities/recurrent-normaliser.ts @@ -2,8 +2,12 @@ import { CreateAdRequest } from '../dtos/create-ad.request'; import { Frequency } from '../types/frequency.enum'; -export class ReccurentNormaliser { +export class RecurrentNormaliser { + constructor() { + console.log('resolver call'); + } fromDateResolver(createAdRequest: CreateAdRequest): Date { + console.log('resolver call'); if (createAdRequest.frequency === Frequency.PUNCTUAL) return createAdRequest.departure; return createAdRequest.fromDate; diff --git a/src/modules/ad/domain/usecases/create-ad.usecase.ts b/src/modules/ad/domain/usecases/create-ad.usecase.ts index f9a0229..757081e 100644 --- a/src/modules/ad/domain/usecases/create-ad.usecase.ts +++ b/src/modules/ad/domain/usecases/create-ad.usecase.ts @@ -28,6 +28,7 @@ export class CreateAdUseCase { async execute(command: CreateAdCommand): Promise { console.log('usecase'); + console.log(command.createAdRequest); this.ad = this._mapper.map( command.createAdRequest, CreateAdRequest, diff --git a/src/modules/ad/mappers/ad.profile.ts b/src/modules/ad/mappers/ad.profile.ts index 4c0b701..c4de6e4 100644 --- a/src/modules/ad/mappers/ad.profile.ts +++ b/src/modules/ad/mappers/ad.profile.ts @@ -5,11 +5,11 @@ import { Ad } from '../domain/entities/ad'; import { AdPresenter } from '../adapters/primaries/ad.presenter'; import { CreateAdRequest } from '../domain/dtos/create-ad.request'; import { AdCreation } from '../domain/dtos/ad.creation'; -import { ReccurentNormaliser } from '../domain/entities/reccurent-normaliser'; +import { RecurrentNormaliser } from '../domain/entities/recurrent-normaliser'; @Injectable() export class AdProfile extends AutomapperProfile { - reccurentNormaliser = new ReccurentNormaliser(); + recurrentNormaliser = new RecurrentNormaliser(); constructor(@InjectMapper() mapper: Mapper) { super(mapper); } @@ -84,53 +84,53 @@ export class AdProfile extends AutomapperProfile { forMember( (destination) => destination.fromDate, mapFrom((source) => - this.reccurentNormaliser.fromDateResolver(source), + this.recurrentNormaliser.fromDateResolver(source), ), ), forMember( (destination) => destination.toDate, - mapFrom((source) => this.reccurentNormaliser.toDateResolver(source)), + mapFrom((source) => this.recurrentNormaliser.toDateResolver(source)), ), forMember( (destination) => destination.monTime, mapFrom((source) => - this.reccurentNormaliser.scheduleMonResolver(source), + this.recurrentNormaliser.scheduleMonResolver(source), ), ), forMember( (destination) => destination.tueTime, mapFrom((source) => - this.reccurentNormaliser.scheduleTueResolver(source), + this.recurrentNormaliser.scheduleTueResolver(source), ), ), forMember( (destination) => destination.wedTime, mapFrom((source) => - this.reccurentNormaliser.scheduleWedResolver(source), + this.recurrentNormaliser.scheduleWedResolver(source), ), ), forMember( (destination) => destination.thuTime, mapFrom((source) => - this.reccurentNormaliser.scheduleThuResolver(source), + this.recurrentNormaliser.scheduleThuResolver(source), ), ), forMember( (destination) => destination.friTime, mapFrom((source) => - this.reccurentNormaliser.scheduleFriResolver(source), + this.recurrentNormaliser.scheduleFriResolver(source), ), ), forMember( (destination) => destination.satTime, mapFrom((source) => - this.reccurentNormaliser.scheduleSatResolver(source), + this.recurrentNormaliser.scheduleSatResolver(source), ), ), forMember( (destination) => destination.sunTime, mapFrom((source) => - this.reccurentNormaliser.scheduleSunResolver(source), + this.recurrentNormaliser.scheduleSunResolver(source), ), ), ); diff --git a/src/modules/ad/mappers/address.profile.ts b/src/modules/ad/mappers/address.profile.ts new file mode 100644 index 0000000..12def6e --- /dev/null +++ b/src/modules/ad/mappers/address.profile.ts @@ -0,0 +1,18 @@ +import { Mapper, createMap } from '@automapper/core'; +import { AutomapperProfile, InjectMapper } from '@automapper/nestjs'; +import { Injectable } from '@nestjs/common'; +import { AddressRequestDTO } from '../domain/dtos/create.address.request'; +import { Address } from '../domain/entities/address'; + +@Injectable() +export class AdProfile extends AutomapperProfile { + constructor(@InjectMapper() mapper: Mapper) { + super(mapper); + } + + override get profile() { + return (mapper) => { + createMap(mapper, AddressRequestDTO, Address); + }; + } +} diff --git a/src/modules/ad/tests/integration/ad.repository.spec.ts b/src/modules/ad/tests/integration/ad.repository.spec.ts index 5e64dc2..e597cf9 100644 --- a/src/modules/ad/tests/integration/ad.repository.spec.ts +++ b/src/modules/ad/tests/integration/ad.repository.spec.ts @@ -402,7 +402,7 @@ describe('Ad Repository', () => { it('should Create an ad ', async () => {}); }); describe('create', () => { - it('should create an ad', async () => { + it('should create an punctual ad', async () => { const beforeCount = await prismaService.ad.count(); const adToCreate: AdCreation = new AdCreation(); @@ -434,5 +434,39 @@ describe('Ad Repository', () => { expect(afterCount - beforeCount).toBe(1); expect(ad.uuid).toBe('be459a29-7a41-4c0b-b371-abe90bfb6f00'); }); + it('should create an recurrent ad', async () => { + const beforeCount = await prismaService.ad.count(); + const adToCreate: AdCreation = new AdCreation(); + + adToCreate.uuid = '137a26fa-4b38-48ba-aecf-1a75f6b20f3d'; + adToCreate.userUuid = '4e52b54d-a729-4dbd-9283-f84a11bb2200'; + adToCreate.driver = true; + adToCreate.passenger = false; + adToCreate.frequency = Frequency.RECURRENT; + adToCreate.fromDate = new Date('01-15-2023 '); + adToCreate.toDate = new Date('10-31-2023'); + adToCreate.monTime = '07:30'; + adToCreate.friTime = '07:45'; + adToCreate.thuTime = '08:00'; + adToCreate.monMargin = 900; + adToCreate.tueMargin = 900; + adToCreate.wedMargin = 900; + adToCreate.thuMargin = 900; + adToCreate.friMargin = 900; + adToCreate.satMargin = 900; + adToCreate.sunMargin = 900; + adToCreate.seatsDriver = 2; + adToCreate.seatsPassenger = 0; + adToCreate.strict = false; + adToCreate.addresses = { + create: [address0 as Address, address1 as Address], + }; + const ad = await adsRepository.create(adToCreate); + + const afterCount = await prismaService.ad.count(); + + expect(afterCount - beforeCount).toBe(1); + expect(ad.uuid).toBe('137a26fa-4b38-48ba-aecf-1a75f6b20f3d'); + }); }); }); diff --git a/src/modules/ad/tests/unit/domain/create-ad.usecase.spec.ts b/src/modules/ad/tests/unit/domain/create-ad.usecase.spec.ts index ab7edfa..83c994a 100644 --- a/src/modules/ad/tests/unit/domain/create-ad.usecase.spec.ts +++ b/src/modules/ad/tests/unit/domain/create-ad.usecase.spec.ts @@ -45,7 +45,7 @@ const mockAddressWithoutPos2: AddressRequestDTO = { postalCode: '06000', country: 'France', }; -const minimalReccurentAdREquest: CreateAdRequest = { +const minimalRecurrentAdREquest: CreateAdRequest = { userUuid: '224e0000-0000-4000-a000-000000000000', frequency: Frequency.RECURRENT, fromDate: new Date('01-05-2023'), @@ -165,14 +165,14 @@ describe('CreateAdUseCase', () => { }); it('should define mimimal ad as 1 passager add', async () => { - const newAdCommand = new CreateAdCommand(minimalReccurentAdREquest); + const newAdCommand = new CreateAdCommand(minimalRecurrentAdREquest); await createAdUseCase.execute(newAdCommand); const expectedAdCreation = { - userUuid: minimalReccurentAdREquest.userUuid, - frequency: minimalReccurentAdREquest.frequency, - fromDate: minimalReccurentAdREquest.fromDate, - toDate: minimalReccurentAdREquest.toDate, - monTime: minimalReccurentAdREquest.schedule.mon, + userUuid: minimalRecurrentAdREquest.userUuid, + frequency: minimalRecurrentAdREquest.frequency, + fromDate: minimalRecurrentAdREquest.fromDate, + toDate: minimalRecurrentAdREquest.toDate, + monTime: minimalRecurrentAdREquest.schedule.mon, tueTime: undefined, wedTime: undefined, thuTime: undefined, @@ -192,7 +192,7 @@ describe('CreateAdUseCase', () => { seatsPassenger: mockDefaultParamsProvider.getParams().SEATS_REQUESTED, strict: mockDefaultParamsProvider.getParams().STRICT, addresses: { - create: minimalReccurentAdREquest.addresses as Address[], + create: minimalRecurrentAdREquest.addresses as Address[], }, createdAt: undefined, } as AdCreation; diff --git a/src/modules/ad/tests/unit/domain/frequency.mapping.spec.ts b/src/modules/ad/tests/unit/domain/frequency.mapping.spec.ts index d4c309f..ef5a72b 100644 --- a/src/modules/ad/tests/unit/domain/frequency.mapping.spec.ts +++ b/src/modules/ad/tests/unit/domain/frequency.mapping.spec.ts @@ -1,4 +1,4 @@ -import { mappingKeyToFrequency } from '../../../domain/dtos/utils/frequency.mapping'; +import { mappingKeyToFrequency } from '../../../domain/dtos/validators/frequency.mapping'; import { Frequency } from '../../../domain/types/frequency.enum'; describe('frequency mapping function ', () => { diff --git a/src/modules/ad/tests/unit/domain/has-driver-seats-validator.spec.ts b/src/modules/ad/tests/unit/domain/has-driver-seats-validator.spec.ts index 7542422..4da1d4e 100644 --- a/src/modules/ad/tests/unit/domain/has-driver-seats-validator.spec.ts +++ b/src/modules/ad/tests/unit/domain/has-driver-seats-validator.spec.ts @@ -1,98 +1,98 @@ -import { hasProperDriverSeats } from '../../../domain/dtos/utils/has-driver-seats.validator'; +import { hasProperDriverSeats } from '../../../domain/dtos/validators/has-driver-seats.validator'; describe('driver and/or driver seats validator', () => { it('should validate if driver and drivers seats is not provided ', () => { expect( - hasProperDriverSeats(undefined, { + hasProperDriverSeats({ value: undefined, constraints: [], targetName: '', - object: {}, + object: { driver: undefined }, property: '', }), ).toBe(true); expect( - hasProperDriverSeats(false, { + hasProperDriverSeats({ value: undefined, constraints: [], targetName: '', - object: {}, + object: { driver: false }, property: '', }), ).toBe(true); expect( - hasProperDriverSeats(null, { + hasProperDriverSeats({ value: undefined, constraints: [], targetName: '', - object: {}, + object: { driver: null }, property: '', }), ).toBe(true); }); it('should not validate if driver is set to true but not the related seats is not provided or 0', () => { expect( - hasProperDriverSeats(true, { + hasProperDriverSeats({ value: undefined, constraints: [], targetName: '', - object: {}, + object: { driver: true }, property: '', }), ).toBe(false); expect( - hasProperDriverSeats(true, { + hasProperDriverSeats({ value: undefined, constraints: [], targetName: '', - object: { seatsDriver: 0 }, + object: { driver: true, seatsDriver: 0 }, property: '', }), ).toBe(false); expect( - hasProperDriverSeats(true, { + hasProperDriverSeats({ value: undefined, constraints: [], targetName: '', - object: { seatsDriver: undefined }, + object: { driver: true, seatsDriver: undefined }, property: '', }), ).toBe(false); expect( - hasProperDriverSeats(true, { + hasProperDriverSeats({ value: undefined, constraints: [], targetName: '', - object: { seatsDriver: null }, + object: { driver: true, seatsDriver: null }, property: '', }), ).toBe(false); }); it('should not validate if driver seats are provided but driver is not set or set to false ', () => { expect( - hasProperDriverSeats(false, { + hasProperDriverSeats({ value: undefined, constraints: [], targetName: '', - object: { seatsDriver: 1 }, + object: { driver: false, seatsDriver: 1 }, property: '', }), ).toBe(false); expect( - hasProperDriverSeats(undefined, { + hasProperDriverSeats({ value: undefined, constraints: [], targetName: '', - object: { seatsDriver: 1 }, + object: { driver: undefined, seatsDriver: 1 }, property: '', }), ).toBe(false); expect( - hasProperDriverSeats(null, { + hasProperDriverSeats({ value: undefined, constraints: [], targetName: '', - object: { seatsDriver: 1 }, + object: { driver: null, seatsDriver: 1 }, property: '', }), ).toBe(false); diff --git a/src/modules/ad/tests/unit/domain/has-passenger-seats-validator.spec.ts b/src/modules/ad/tests/unit/domain/has-passenger-seats-validator.spec.ts index 2935eef..3befd78 100644 --- a/src/modules/ad/tests/unit/domain/has-passenger-seats-validator.spec.ts +++ b/src/modules/ad/tests/unit/domain/has-passenger-seats-validator.spec.ts @@ -1,98 +1,98 @@ -import { hasProperPassengerSeats } from '../../../domain/dtos/utils/has-passenger-seats.validator'; +import { hasProperPassengerSeats } from '../../../domain/dtos/validators/has-passenger-seats.validator'; describe('driver and/or passenger seats validator', () => { it('should validate if passenger and passengers seats is not provided ', () => { expect( - hasProperPassengerSeats(undefined, { + hasProperPassengerSeats({ value: undefined, constraints: [], targetName: '', - object: {}, + object: { passenger: undefined }, property: '', }), ).toBe(true); expect( - hasProperPassengerSeats(false, { + hasProperPassengerSeats({ value: undefined, constraints: [], targetName: '', - object: {}, + object: { passenger: false }, property: '', }), ).toBe(true); expect( - hasProperPassengerSeats(null, { + hasProperPassengerSeats({ value: undefined, constraints: [], targetName: '', - object: {}, + object: { passenger: null }, property: '', }), ).toBe(true); }); it('should not validate if passenger is set to true but not the related seats is not provided or 0', () => { expect( - hasProperPassengerSeats(true, { + hasProperPassengerSeats({ value: undefined, constraints: [], targetName: '', - object: {}, + object: { passenger: true }, property: '', }), ).toBe(false); expect( - hasProperPassengerSeats(true, { + hasProperPassengerSeats({ value: undefined, constraints: [], targetName: '', - object: { seatsPassenger: 0 }, + object: { passenger: true, seatsPassenger: 0 }, property: '', }), ).toBe(false); expect( - hasProperPassengerSeats(true, { + hasProperPassengerSeats({ value: undefined, constraints: [], targetName: '', - object: { seatsPassenger: undefined }, + object: { passenger: true, seatsPassenger: undefined }, property: '', }), ).toBe(false); expect( - hasProperPassengerSeats(true, { + hasProperPassengerSeats({ value: undefined, constraints: [], targetName: '', - object: { seatsPassenger: null }, + object: { passenger: true, seatsPassenger: null }, property: '', }), ).toBe(false); }); it('should not validate if passenger seats are provided but passenger is not set or set to false ', () => { expect( - hasProperPassengerSeats(false, { + hasProperPassengerSeats({ value: undefined, constraints: [], targetName: '', - object: { seatsPassenger: 1 }, + object: { passenger: false, seatsPassenger: 1 }, property: '', }), ).toBe(false); expect( - hasProperPassengerSeats(undefined, { + hasProperPassengerSeats({ value: undefined, constraints: [], targetName: '', - object: { seatsPassenger: 1 }, + object: { passenger: undefined, seatsPassenger: 1 }, property: '', }), ).toBe(false); expect( - hasProperPassengerSeats(null, { + hasProperPassengerSeats({ value: undefined, constraints: [], targetName: '', - object: { seatsPassenger: 1 }, + object: { passenger: null, seatsPassenger: 1 }, property: '', }), ).toBe(false); diff --git a/src/modules/ad/tests/unit/domain/has-proper-addresses-indexes.spec.ts b/src/modules/ad/tests/unit/domain/has-proper-addresses-indexes.spec.ts index edbc45b..2d9ddaa 100644 --- a/src/modules/ad/tests/unit/domain/has-proper-addresses-indexes.spec.ts +++ b/src/modules/ad/tests/unit/domain/has-proper-addresses-indexes.spec.ts @@ -1,5 +1,5 @@ import { AddressRequestDTO } from '../../../domain/dtos/create.address.request'; -import { hasProperPositionIndexes } from '../../../domain/dtos/utils/address-position.validator'; +import { hasProperPositionIndexes } from '../../../domain/dtos/validators/address-position.validator'; describe('addresses position validators', () => { const mockAddress1: AddressRequestDTO = { lon: 48.68944505415954, diff --git a/src/modules/ad/tests/unit/domain/reccurent-normaliser.spec.ts b/src/modules/ad/tests/unit/domain/reccurent-normaliser.spec.ts deleted file mode 100644 index f790b3c..0000000 --- a/src/modules/ad/tests/unit/domain/reccurent-normaliser.spec.ts +++ /dev/null @@ -1,92 +0,0 @@ -import { CreateAdRequest } from '../../../domain/dtos/create-ad.request'; -import { ScheduleDTO } from '../../../domain/dtos/create.schedule.dto'; -import { ReccurentNormaliser } from '../../../domain/entities/reccurent-normaliser'; -import { Frequency } from '../../../domain/types/frequency.enum'; -describe('reccurent normalizer transformer for punctual ad ', () => { - const reccurentNormaliser = new ReccurentNormaliser(); - it('should transform punctual ad into reccurent ad ', () => { - const punctualAd: CreateAdRequest = { - userUuid: '', - frequency: Frequency.PUNCTUAL, - departure: new Date('05-03-2023 12:39:39 '), - schedule: {} as ScheduleDTO, - addresses: [], - }; - expect(reccurentNormaliser.fromDateResolver(punctualAd)).toBe( - punctualAd.departure, - ); - expect(reccurentNormaliser.toDateResolver(punctualAd)).toBe( - punctualAd.departure, - ); - expect(reccurentNormaliser.scheduleMonResolver(punctualAd)).toBeUndefined(); - expect(reccurentNormaliser.scheduleTueResolver(punctualAd)).toBeUndefined(); - expect(reccurentNormaliser.scheduleWedResolver(punctualAd)).toBe('12:39'); - expect(reccurentNormaliser.scheduleThuResolver(punctualAd)).toBeUndefined(); - expect(reccurentNormaliser.scheduleFriResolver(punctualAd)).toBeUndefined(); - expect(reccurentNormaliser.scheduleSatResolver(punctualAd)).toBeUndefined(); - expect(reccurentNormaliser.scheduleSunResolver(punctualAd)).toBeUndefined(); - }); - it('should leave reccurent ad as is', () => { - const reccurentAd: CreateAdRequest = { - userUuid: '', - frequency: Frequency.RECURRENT, - schedule: { - mon: '08:30', - tue: '08:30', - wed: '09:00', - fri: '09:00', - }, - addresses: [], - }; - expect(reccurentNormaliser.fromDateResolver(reccurentAd)).toBe( - reccurentAd.departure, - ); - expect(reccurentNormaliser.toDateResolver(reccurentAd)).toBe( - reccurentAd.departure, - ); - expect(reccurentNormaliser.scheduleMonResolver(reccurentAd)).toBe( - reccurentAd.schedule.mon, - ); - expect(reccurentNormaliser.scheduleTueResolver(reccurentAd)).toBe( - reccurentAd.schedule.tue, - ); - expect(reccurentNormaliser.scheduleWedResolver(reccurentAd)).toBe( - reccurentAd.schedule.wed, - ); - expect(reccurentNormaliser.scheduleThuResolver(reccurentAd)).toBe( - reccurentAd.schedule.thu, - ); - expect(reccurentNormaliser.scheduleFriResolver(reccurentAd)).toBe( - reccurentAd.schedule.fri, - ); - expect(reccurentNormaliser.scheduleSatResolver(reccurentAd)).toBe( - reccurentAd.schedule.sat, - ); - expect(reccurentNormaliser.scheduleSunResolver(reccurentAd)).toBe( - reccurentAd.schedule.sun, - ); - }); - it('should pass for each day of the week of a deprarture ', () => { - const punctualAd: CreateAdRequest = { - userUuid: '', - frequency: Frequency.PUNCTUAL, - departure: undefined, - schedule: {} as ScheduleDTO, - addresses: [], - }; - punctualAd.departure = new Date('05-01-2023 '); - expect(reccurentNormaliser.scheduleMonResolver(punctualAd)).toBe('00:00'); - punctualAd.departure = new Date('05-02-2023 06:32:45'); - expect(reccurentNormaliser.scheduleTueResolver(punctualAd)).toBe('06:32'); - punctualAd.departure = new Date('05-03-2023 10:21'); - expect(reccurentNormaliser.scheduleWedResolver(punctualAd)).toBe('10:21'); - punctualAd.departure = new Date('05-04-2023 11:06:00'); - expect(reccurentNormaliser.scheduleThuResolver(punctualAd)).toBe('11:06'); - punctualAd.departure = new Date('05-05-2023 05:20'); - expect(reccurentNormaliser.scheduleFriResolver(punctualAd)).toBe('05:20'); - punctualAd.departure = new Date('05-06-2023'); - expect(reccurentNormaliser.scheduleSatResolver(punctualAd)).toBe('00:00'); - punctualAd.departure = new Date('05-07-2023'); - expect(reccurentNormaliser.scheduleSunResolver(punctualAd)).toBe('00:00'); - }); -}); diff --git a/src/modules/ad/tests/unit/domain/recurrent-normaliser.spec.ts b/src/modules/ad/tests/unit/domain/recurrent-normaliser.spec.ts new file mode 100644 index 0000000..f9cc3f0 --- /dev/null +++ b/src/modules/ad/tests/unit/domain/recurrent-normaliser.spec.ts @@ -0,0 +1,92 @@ +import { CreateAdRequest } from '../../../domain/dtos/create-ad.request'; +import { ScheduleDTO } from '../../../domain/dtos/create.schedule.dto'; +import { RecurrentNormaliser } from '../../../domain/entities/recurrent-normaliser'; +import { Frequency } from '../../../domain/types/frequency.enum'; +describe('recurrent normalizer transformer for punctual ad ', () => { + const recurrentNormaliser = new RecurrentNormaliser(); + it('should transform punctual ad into recurrent ad ', () => { + const punctualAd: CreateAdRequest = { + userUuid: '', + frequency: Frequency.PUNCTUAL, + departure: new Date('05-03-2023 12:39:39 '), + schedule: {} as ScheduleDTO, + addresses: [], + }; + expect(recurrentNormaliser.fromDateResolver(punctualAd)).toBe( + punctualAd.departure, + ); + expect(recurrentNormaliser.toDateResolver(punctualAd)).toBe( + punctualAd.departure, + ); + expect(recurrentNormaliser.scheduleMonResolver(punctualAd)).toBeUndefined(); + expect(recurrentNormaliser.scheduleTueResolver(punctualAd)).toBeUndefined(); + expect(recurrentNormaliser.scheduleWedResolver(punctualAd)).toBe('12:39'); + expect(recurrentNormaliser.scheduleThuResolver(punctualAd)).toBeUndefined(); + expect(recurrentNormaliser.scheduleFriResolver(punctualAd)).toBeUndefined(); + expect(recurrentNormaliser.scheduleSatResolver(punctualAd)).toBeUndefined(); + expect(recurrentNormaliser.scheduleSunResolver(punctualAd)).toBeUndefined(); + }); + it('should leave recurrent ad as is', () => { + const recurrentAd: CreateAdRequest = { + userUuid: '', + frequency: Frequency.RECURRENT, + schedule: { + mon: '08:30', + tue: '08:30', + wed: '09:00', + fri: '09:00', + }, + addresses: [], + }; + expect(recurrentNormaliser.fromDateResolver(recurrentAd)).toBe( + recurrentAd.departure, + ); + expect(recurrentNormaliser.toDateResolver(recurrentAd)).toBe( + recurrentAd.departure, + ); + expect(recurrentNormaliser.scheduleMonResolver(recurrentAd)).toBe( + recurrentAd.schedule.mon, + ); + expect(recurrentNormaliser.scheduleTueResolver(recurrentAd)).toBe( + recurrentAd.schedule.tue, + ); + expect(recurrentNormaliser.scheduleWedResolver(recurrentAd)).toBe( + recurrentAd.schedule.wed, + ); + expect(recurrentNormaliser.scheduleThuResolver(recurrentAd)).toBe( + recurrentAd.schedule.thu, + ); + expect(recurrentNormaliser.scheduleFriResolver(recurrentAd)).toBe( + recurrentAd.schedule.fri, + ); + expect(recurrentNormaliser.scheduleSatResolver(recurrentAd)).toBe( + recurrentAd.schedule.sat, + ); + expect(recurrentNormaliser.scheduleSunResolver(recurrentAd)).toBe( + recurrentAd.schedule.sun, + ); + }); + it('should pass for each day of the week of a deprarture ', () => { + const punctualAd: CreateAdRequest = { + userUuid: '', + frequency: Frequency.PUNCTUAL, + departure: undefined, + schedule: {} as ScheduleDTO, + addresses: [], + }; + punctualAd.departure = new Date('05-01-2023 '); + expect(recurrentNormaliser.scheduleMonResolver(punctualAd)).toBe('00:00'); + punctualAd.departure = new Date('05-02-2023 06:32:45'); + expect(recurrentNormaliser.scheduleTueResolver(punctualAd)).toBe('06:32'); + punctualAd.departure = new Date('05-03-2023 10:21'); + expect(recurrentNormaliser.scheduleWedResolver(punctualAd)).toBe('10:21'); + punctualAd.departure = new Date('05-04-2023 11:06:00'); + expect(recurrentNormaliser.scheduleThuResolver(punctualAd)).toBe('11:06'); + punctualAd.departure = new Date('05-05-2023 05:20'); + expect(recurrentNormaliser.scheduleFriResolver(punctualAd)).toBe('05:20'); + punctualAd.departure = new Date('05-06-2023'); + expect(recurrentNormaliser.scheduleSatResolver(punctualAd)).toBe('00:00'); + punctualAd.departure = new Date('05-07-2023'); + expect(recurrentNormaliser.scheduleSunResolver(punctualAd)).toBe('00:00'); + }); +});