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';
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;

View File

@ -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,

View File

@ -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,

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';
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;

View File

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

View File

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

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 () => {});
});
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');
});
});
});

View File

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

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';
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', () => {
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);

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', () => {
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);

View File

@ -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,

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