refactor validators

This commit is contained in:
Grégoire Chevalier 2023-05-25 13:39:28 +02:00
parent c0c486f34a
commit e2e3bd4407
18 changed files with 278 additions and 173 deletions

View File

@ -13,13 +13,13 @@ import {
} from 'class-validator'; } from 'class-validator';
import { Frequency } from '../types/frequency.enum'; import { Frequency } from '../types/frequency.enum';
import { Transform, Type } from 'class-transformer'; 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 { MarginDTO } from './create.margin.dto';
import { ScheduleDTO } from './create.schedule.dto'; import { ScheduleDTO } from './create.schedule.dto';
import { AddressRequestDTO } from './create.address.request'; import { AddressRequestDTO } from './create.address.request';
import { HasProperPassengerSeats } from './utils/has-passenger-seats.validator'; import { HasProperPassengerSeats } from './validators/has-passenger-seats.validator';
import { HasProperDriverSeats } from './utils/has-driver-seats.validator'; import { HasProperDriverSeats } from './validators/has-driver-seats.validator';
import { HasProperPositionIndexes } from './utils/address-position.validator'; import { HasProperPositionIndexes } from './validators/address-position.validator';
export class CreateAdRequest { export class CreateAdRequest {
@IsOptional() @IsOptional()
@ -32,13 +32,11 @@ export class CreateAdRequest {
userUuid: string; userUuid: string;
@IsOptional() @IsOptional()
@HasProperDriverSeats()
@IsBoolean() @IsBoolean()
@AutoMap() @AutoMap()
driver?: boolean; driver?: boolean;
@IsOptional() @IsOptional()
@HasProperPassengerSeats()
@IsBoolean() @IsBoolean()
@AutoMap() @AutoMap()
passenger?: boolean; passenger?: boolean;
@ -81,12 +79,14 @@ export class CreateAdRequest {
@AutoMap() @AutoMap()
marginDurations?: MarginDTO; marginDurations?: MarginDTO;
@ValidateIf((ad) => ad.driver) @IsOptional()
@HasProperDriverSeats()
@IsInt() @IsInt()
@AutoMap() @AutoMap()
seatsDriver?: number; seatsDriver?: number;
@IsOptional() @IsOptional()
@HasProperPassengerSeats()
@IsInt() @IsInt()
@AutoMap() @AutoMap()
seatsPassenger?: number; seatsPassenger?: number;

View File

@ -5,11 +5,16 @@ import {
buildMessage, buildMessage,
} from 'class-validator'; } from 'class-validator';
export function hasProperDriverSeats(value: any, args: ValidationArguments) { export function hasProperDriverSeats(args: ValidationArguments) {
if (value === true && typeof args.object['seatsDriver'] === 'number') if (
args.object['driver'] === true &&
typeof args.object['seatsDriver'] === 'number'
)
return args.object['seatsDriver'] > 0; return args.object['seatsDriver'] > 0;
else if ( 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'] === 0 ||
args.object['seatsDriver'] === null || args.object['seatsDriver'] === null ||
args.object['seatsDriver'] === undefined) args.object['seatsDriver'] === undefined)
@ -26,8 +31,8 @@ export function HasProperDriverSeats(
name: '', name: '',
constraints: [], constraints: [],
validator: { validator: {
validate: (value, args: ValidationArguments): boolean => validate: (value: any, args: ValidationArguments): boolean =>
hasProperDriverSeats(value, args), hasProperDriverSeats(args),
defaultMessage: buildMessage( defaultMessage: buildMessage(
() => `driver and driver seats are not correct`, () => `driver and driver seats are not correct`,
validationOptions, validationOptions,

View File

@ -5,11 +5,16 @@ import {
buildMessage, buildMessage,
} from 'class-validator'; } from 'class-validator';
export function hasProperPassengerSeats(value: any, args: ValidationArguments) { export function hasProperPassengerSeats(args: ValidationArguments) {
if (value === true && typeof args.object['seatsPassenger'] === 'number') if (
args.object['passenger'] === true &&
typeof args.object['seatsPassenger'] === 'number'
)
return args.object['seatsPassenger'] > 0; return args.object['seatsPassenger'] > 0;
else if ( 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'] === 0 ||
args.object['seatsPassenger'] === null || args.object['seatsPassenger'] === null ||
args.object['seatsPassenger'] === undefined) args.object['seatsPassenger'] === undefined)
@ -27,7 +32,7 @@ export function HasProperPassengerSeats(
constraints: [], constraints: [],
validator: { validator: {
validate: (value, args: ValidationArguments): boolean => validate: (value, args: ValidationArguments): boolean =>
hasProperPassengerSeats(value, args), hasProperPassengerSeats(args),
defaultMessage: buildMessage( defaultMessage: buildMessage(
() => `passenger and passenger seats are not correct`, () => `passenger and passenger seats are not correct`,
validationOptions, validationOptions,

View File

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

View File

@ -2,8 +2,12 @@ import { CreateAdRequest } from '../dtos/create-ad.request';
import { Frequency } from '../types/frequency.enum'; import { Frequency } from '../types/frequency.enum';
export class ReccurentNormaliser { export class RecurrentNormaliser {
constructor() {
console.log('resolver call');
}
fromDateResolver(createAdRequest: CreateAdRequest): Date { fromDateResolver(createAdRequest: CreateAdRequest): Date {
console.log('resolver call');
if (createAdRequest.frequency === Frequency.PUNCTUAL) if (createAdRequest.frequency === Frequency.PUNCTUAL)
return createAdRequest.departure; return createAdRequest.departure;
return createAdRequest.fromDate; return createAdRequest.fromDate;

View File

@ -28,6 +28,7 @@ export class CreateAdUseCase {
async execute(command: CreateAdCommand): Promise<Ad> { async execute(command: CreateAdCommand): Promise<Ad> {
console.log('usecase'); console.log('usecase');
console.log(command.createAdRequest);
this.ad = this._mapper.map( this.ad = this._mapper.map(
command.createAdRequest, command.createAdRequest,
CreateAdRequest, CreateAdRequest,

View File

@ -5,11 +5,11 @@ 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 { AdCreation } from '../domain/dtos/ad.creation'; import { AdCreation } from '../domain/dtos/ad.creation';
import { ReccurentNormaliser } from '../domain/entities/reccurent-normaliser'; import { RecurrentNormaliser } from '../domain/entities/recurrent-normaliser';
@Injectable() @Injectable()
export class AdProfile extends AutomapperProfile { export class AdProfile extends AutomapperProfile {
reccurentNormaliser = new ReccurentNormaliser(); recurrentNormaliser = new RecurrentNormaliser();
constructor(@InjectMapper() mapper: Mapper) { constructor(@InjectMapper() mapper: Mapper) {
super(mapper); super(mapper);
} }
@ -84,53 +84,53 @@ export class AdProfile extends AutomapperProfile {
forMember( forMember(
(destination) => destination.fromDate, (destination) => destination.fromDate,
mapFrom((source) => mapFrom((source) =>
this.reccurentNormaliser.fromDateResolver(source), this.recurrentNormaliser.fromDateResolver(source),
), ),
), ),
forMember( forMember(
(destination) => destination.toDate, (destination) => destination.toDate,
mapFrom((source) => this.reccurentNormaliser.toDateResolver(source)), mapFrom((source) => this.recurrentNormaliser.toDateResolver(source)),
), ),
forMember( forMember(
(destination) => destination.monTime, (destination) => destination.monTime,
mapFrom((source) => mapFrom((source) =>
this.reccurentNormaliser.scheduleMonResolver(source), this.recurrentNormaliser.scheduleMonResolver(source),
), ),
), ),
forMember( forMember(
(destination) => destination.tueTime, (destination) => destination.tueTime,
mapFrom((source) => mapFrom((source) =>
this.reccurentNormaliser.scheduleTueResolver(source), this.recurrentNormaliser.scheduleTueResolver(source),
), ),
), ),
forMember( forMember(
(destination) => destination.wedTime, (destination) => destination.wedTime,
mapFrom((source) => mapFrom((source) =>
this.reccurentNormaliser.scheduleWedResolver(source), this.recurrentNormaliser.scheduleWedResolver(source),
), ),
), ),
forMember( forMember(
(destination) => destination.thuTime, (destination) => destination.thuTime,
mapFrom((source) => mapFrom((source) =>
this.reccurentNormaliser.scheduleThuResolver(source), this.recurrentNormaliser.scheduleThuResolver(source),
), ),
), ),
forMember( forMember(
(destination) => destination.friTime, (destination) => destination.friTime,
mapFrom((source) => mapFrom((source) =>
this.reccurentNormaliser.scheduleFriResolver(source), this.recurrentNormaliser.scheduleFriResolver(source),
), ),
), ),
forMember( forMember(
(destination) => destination.satTime, (destination) => destination.satTime,
mapFrom((source) => mapFrom((source) =>
this.reccurentNormaliser.scheduleSatResolver(source), this.recurrentNormaliser.scheduleSatResolver(source),
), ),
), ),
forMember( forMember(
(destination) => destination.sunTime, (destination) => destination.sunTime,
mapFrom((source) => mapFrom((source) =>
this.reccurentNormaliser.scheduleSunResolver(source), this.recurrentNormaliser.scheduleSunResolver(source),
), ),
), ),
); );

View File

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

View File

@ -402,7 +402,7 @@ describe('Ad Repository', () => {
it('should Create an ad ', async () => {}); it('should Create an ad ', async () => {});
}); });
describe('create', () => { describe('create', () => {
it('should create an ad', async () => { it('should create an punctual ad', async () => {
const beforeCount = await prismaService.ad.count(); const beforeCount = await prismaService.ad.count();
const adToCreate: AdCreation = new AdCreation(); const adToCreate: AdCreation = new AdCreation();
@ -434,5 +434,39 @@ describe('Ad Repository', () => {
expect(afterCount - beforeCount).toBe(1); expect(afterCount - beforeCount).toBe(1);
expect(ad.uuid).toBe('be459a29-7a41-4c0b-b371-abe90bfb6f00'); 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');
});
}); });
}); });

View File

@ -45,7 +45,7 @@ const mockAddressWithoutPos2: AddressRequestDTO = {
postalCode: '06000', postalCode: '06000',
country: 'France', country: 'France',
}; };
const minimalReccurentAdREquest: CreateAdRequest = { const minimalRecurrentAdREquest: CreateAdRequest = {
userUuid: '224e0000-0000-4000-a000-000000000000', userUuid: '224e0000-0000-4000-a000-000000000000',
frequency: Frequency.RECURRENT, frequency: Frequency.RECURRENT,
fromDate: new Date('01-05-2023'), fromDate: new Date('01-05-2023'),
@ -165,14 +165,14 @@ describe('CreateAdUseCase', () => {
}); });
it('should define mimimal ad as 1 passager add', async () => { it('should define mimimal ad as 1 passager add', async () => {
const newAdCommand = new CreateAdCommand(minimalReccurentAdREquest); const newAdCommand = new CreateAdCommand(minimalRecurrentAdREquest);
await createAdUseCase.execute(newAdCommand); await createAdUseCase.execute(newAdCommand);
const expectedAdCreation = { const expectedAdCreation = {
userUuid: minimalReccurentAdREquest.userUuid, userUuid: minimalRecurrentAdREquest.userUuid,
frequency: minimalReccurentAdREquest.frequency, frequency: minimalRecurrentAdREquest.frequency,
fromDate: minimalReccurentAdREquest.fromDate, fromDate: minimalRecurrentAdREquest.fromDate,
toDate: minimalReccurentAdREquest.toDate, toDate: minimalRecurrentAdREquest.toDate,
monTime: minimalReccurentAdREquest.schedule.mon, monTime: minimalRecurrentAdREquest.schedule.mon,
tueTime: undefined, tueTime: undefined,
wedTime: undefined, wedTime: undefined,
thuTime: undefined, thuTime: undefined,
@ -192,7 +192,7 @@ describe('CreateAdUseCase', () => {
seatsPassenger: mockDefaultParamsProvider.getParams().SEATS_REQUESTED, seatsPassenger: mockDefaultParamsProvider.getParams().SEATS_REQUESTED,
strict: mockDefaultParamsProvider.getParams().STRICT, strict: mockDefaultParamsProvider.getParams().STRICT,
addresses: { addresses: {
create: minimalReccurentAdREquest.addresses as Address[], create: minimalRecurrentAdREquest.addresses as Address[],
}, },
createdAt: undefined, createdAt: undefined,
} as AdCreation; } as AdCreation;

View File

@ -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'; import { Frequency } from '../../../domain/types/frequency.enum';
describe('frequency mapping function ', () => { describe('frequency mapping function ', () => {

View File

@ -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', () => { describe('driver and/or driver seats validator', () => {
it('should validate if driver and drivers seats is not provided ', () => { it('should validate if driver and drivers seats is not provided ', () => {
expect( expect(
hasProperDriverSeats(undefined, { hasProperDriverSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: {}, object: { driver: undefined },
property: '', property: '',
}), }),
).toBe(true); ).toBe(true);
expect( expect(
hasProperDriverSeats(false, { hasProperDriverSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: {}, object: { driver: false },
property: '', property: '',
}), }),
).toBe(true); ).toBe(true);
expect( expect(
hasProperDriverSeats(null, { hasProperDriverSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: {}, object: { driver: null },
property: '', property: '',
}), }),
).toBe(true); ).toBe(true);
}); });
it('should not validate if driver is set to true but not the related seats is not provided or 0', () => { it('should not validate if driver is set to true but not the related seats is not provided or 0', () => {
expect( expect(
hasProperDriverSeats(true, { hasProperDriverSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: {}, object: { driver: true },
property: '', property: '',
}), }),
).toBe(false); ).toBe(false);
expect( expect(
hasProperDriverSeats(true, { hasProperDriverSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: { seatsDriver: 0 }, object: { driver: true, seatsDriver: 0 },
property: '', property: '',
}), }),
).toBe(false); ).toBe(false);
expect( expect(
hasProperDriverSeats(true, { hasProperDriverSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: { seatsDriver: undefined }, object: { driver: true, seatsDriver: undefined },
property: '', property: '',
}), }),
).toBe(false); ).toBe(false);
expect( expect(
hasProperDriverSeats(true, { hasProperDriverSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: { seatsDriver: null }, object: { driver: true, seatsDriver: null },
property: '', property: '',
}), }),
).toBe(false); ).toBe(false);
}); });
it('should not validate if driver seats are provided but driver is not set or set to false ', () => { it('should not validate if driver seats are provided but driver is not set or set to false ', () => {
expect( expect(
hasProperDriverSeats(false, { hasProperDriverSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: { seatsDriver: 1 }, object: { driver: false, seatsDriver: 1 },
property: '', property: '',
}), }),
).toBe(false); ).toBe(false);
expect( expect(
hasProperDriverSeats(undefined, { hasProperDriverSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: { seatsDriver: 1 }, object: { driver: undefined, seatsDriver: 1 },
property: '', property: '',
}), }),
).toBe(false); ).toBe(false);
expect( expect(
hasProperDriverSeats(null, { hasProperDriverSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: { seatsDriver: 1 }, object: { driver: null, seatsDriver: 1 },
property: '', property: '',
}), }),
).toBe(false); ).toBe(false);

View File

@ -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', () => { describe('driver and/or passenger seats validator', () => {
it('should validate if passenger and passengers seats is not provided ', () => { it('should validate if passenger and passengers seats is not provided ', () => {
expect( expect(
hasProperPassengerSeats(undefined, { hasProperPassengerSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: {}, object: { passenger: undefined },
property: '', property: '',
}), }),
).toBe(true); ).toBe(true);
expect( expect(
hasProperPassengerSeats(false, { hasProperPassengerSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: {}, object: { passenger: false },
property: '', property: '',
}), }),
).toBe(true); ).toBe(true);
expect( expect(
hasProperPassengerSeats(null, { hasProperPassengerSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: {}, object: { passenger: null },
property: '', property: '',
}), }),
).toBe(true); ).toBe(true);
}); });
it('should not validate if passenger is set to true but not the related seats is not provided or 0', () => { it('should not validate if passenger is set to true but not the related seats is not provided or 0', () => {
expect( expect(
hasProperPassengerSeats(true, { hasProperPassengerSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: {}, object: { passenger: true },
property: '', property: '',
}), }),
).toBe(false); ).toBe(false);
expect( expect(
hasProperPassengerSeats(true, { hasProperPassengerSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: { seatsPassenger: 0 }, object: { passenger: true, seatsPassenger: 0 },
property: '', property: '',
}), }),
).toBe(false); ).toBe(false);
expect( expect(
hasProperPassengerSeats(true, { hasProperPassengerSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: { seatsPassenger: undefined }, object: { passenger: true, seatsPassenger: undefined },
property: '', property: '',
}), }),
).toBe(false); ).toBe(false);
expect( expect(
hasProperPassengerSeats(true, { hasProperPassengerSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: { seatsPassenger: null }, object: { passenger: true, seatsPassenger: null },
property: '', property: '',
}), }),
).toBe(false); ).toBe(false);
}); });
it('should not validate if passenger seats are provided but passenger is not set or set to false ', () => { it('should not validate if passenger seats are provided but passenger is not set or set to false ', () => {
expect( expect(
hasProperPassengerSeats(false, { hasProperPassengerSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: { seatsPassenger: 1 }, object: { passenger: false, seatsPassenger: 1 },
property: '', property: '',
}), }),
).toBe(false); ).toBe(false);
expect( expect(
hasProperPassengerSeats(undefined, { hasProperPassengerSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: { seatsPassenger: 1 }, object: { passenger: undefined, seatsPassenger: 1 },
property: '', property: '',
}), }),
).toBe(false); ).toBe(false);
expect( expect(
hasProperPassengerSeats(null, { hasProperPassengerSeats({
value: undefined, value: undefined,
constraints: [], constraints: [],
targetName: '', targetName: '',
object: { seatsPassenger: 1 }, object: { passenger: null, seatsPassenger: 1 },
property: '', property: '',
}), }),
).toBe(false); ).toBe(false);

View File

@ -1,5 +1,5 @@
import { AddressRequestDTO } from '../../../domain/dtos/create.address.request'; 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', () => { describe('addresses position validators', () => {
const mockAddress1: AddressRequestDTO = { const mockAddress1: AddressRequestDTO = {
lon: 48.68944505415954, lon: 48.68944505415954,

View File

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

View File

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