adding ad default parameters to .env

This commit is contained in:
Grégoire Chevalier 2023-05-23 15:38:20 +02:00
parent 10f4d1a0dc
commit f5ce6ca005
9 changed files with 85 additions and 53 deletions

View File

@ -15,3 +15,16 @@ RMQ_EXCHANGE=mobicoop
REDIS_HOST=v3-redis REDIS_HOST=v3-redis
REDIS_PASSWORD=redis REDIS_PASSWORD=redis
REDIS_PORT=6379 REDIS_PORT=6379
# DEFAULT CARPOOL DEPARTURE MARGIN (in seconds)
DEPARTURE_MARGIN=900
# DEFAULT ROLE
ROLE=passenger
# SEATS PROVIDED AS DRIVER / REQUESTED AS PASSENGER
SEATS_PROVIDED=3
SEATS_REQUESTED=1
# ACCEPT ONLY SAME FREQUENCY REQUESTS
STRICT_FREQUENCY=false

View File

@ -6,20 +6,19 @@ import { IProvideParams } from '../../domain/interfaces/param-provider.interface
@Injectable() @Injectable()
export class DefaultParamsProvider implements IProvideParams { export class DefaultParamsProvider implements IProvideParams {
constructor(private readonly configService: ConfigService) {} constructor(private readonly configService: ConfigService) {}
//TODO adding config service
getParams = (): DefaultParams => { getParams = (): DefaultParams => {
return { return {
MON_MARGIN: 900, MON_MARGIN: this.configService.get('DEPARTURE_MARGIN'),
TUE_MARGIN: 900, TUE_MARGIN: this.configService.get('DEPARTURE_MARGIN'),
WED_MARGIN: 900, WED_MARGIN: this.configService.get('DEPARTURE_MARGIN'),
THU_MARGIN: 900, THU_MARGIN: this.configService.get('DEPARTURE_MARGIN'),
FRI_MARGIN: 900, FRI_MARGIN: this.configService.get('DEPARTURE_MARGIN'),
SAT_MARGIN: 900, SAT_MARGIN: this.configService.get('DEPARTURE_MARGIN'),
SUN_MARGIN: 900, SUN_MARGIN: this.configService.get('DEPARTURE_MARGIN'),
DRIVER: false, DRIVER: this.configService.get('ROLE') == 'driver' ? true : false,
DRIVER_SEAT: 0, SEATS_PROVIDED: this.configService.get('SEATS_PROVIDED'),
PASSENGER: true, PASSENGER: this.configService.get('ROLE') == 'passenger' ? true : false,
PASSENGER_SEATS: 1, SEATS_REQUESTED: this.configService.get('SEATS_REQUESTED'),
STRICT: false, STRICT: false,
}; };
}; };

View File

@ -50,7 +50,6 @@ export class CreateAdRequest {
@AutoMap() @AutoMap()
frequency: Frequency; frequency: Frequency;
// TODO create a proper validator
@ValidateIf((ad) => ad.frequency === 'PUNCTUAL') @ValidateIf((ad) => ad.frequency === 'PUNCTUAL')
@Type(() => Date) @Type(() => Date)
@IsDate() @IsDate()

View File

@ -4,8 +4,7 @@ import {
ValidationOptions, ValidationOptions,
buildMessage, buildMessage,
} from 'class-validator'; } from 'class-validator';
// TODO refactor ??
// TODO propely set driver max limit
export function hasProperDriverSeats(value: any, args: ValidationArguments) { export function hasProperDriverSeats(value: any, args: ValidationArguments) {
if (value === true && typeof args.object['seatsDriver'] === 'number') if (value === true && typeof args.object['seatsDriver'] === 'number')
return args.object['seatsDriver'] > 0; return args.object['seatsDriver'] > 0;

View File

@ -4,8 +4,7 @@ import {
ValidationOptions, ValidationOptions,
buildMessage, buildMessage,
} from 'class-validator'; } from 'class-validator';
// TODO refactor ??
// TODO propely set passenger max limit
export function hasProperPassengerSeats(value: any, args: ValidationArguments) { export function hasProperPassengerSeats(value: any, args: ValidationArguments) {
if (value === true && typeof args.object['seatsPassenger'] === 'number') if (value === true && typeof args.object['seatsPassenger'] === 'number')
return args.object['seatsPassenger'] > 0; return args.object['seatsPassenger'] > 0;

View File

@ -3,8 +3,6 @@ import { CreateAdRequest } from '../dtos/create-ad.request';
import { Frequency } from '../types/frequency.enum'; import { Frequency } from '../types/frequency.enum';
export class ReccurentNormaliser { export class ReccurentNormaliser {
constructor() {}
fromDateResolver(createAdRequest: CreateAdRequest): Date { fromDateResolver(createAdRequest: CreateAdRequest): Date {
if (createAdRequest.frequency === Frequency.PUNCTUAL) if (createAdRequest.frequency === Frequency.PUNCTUAL)
return createAdRequest.departure; return createAdRequest.departure;

View File

@ -7,8 +7,8 @@ export type DefaultParams = {
SAT_MARGIN: number; SAT_MARGIN: number;
SUN_MARGIN: number; SUN_MARGIN: number;
DRIVER: boolean; DRIVER: boolean;
DRIVER_SEAT: number; SEATS_PROVIDED: number;
PASSENGER: boolean; PASSENGER: boolean;
PASSENGER_SEATS: number; SEATS_REQUESTED: number;
STRICT: boolean; STRICT: boolean;
}; };

View File

@ -11,7 +11,6 @@ import { IProvideParams } from '../interfaces/param-provider.interface';
import { DefaultParams } from '../types/default-params.type'; import { DefaultParams } from '../types/default-params.type';
import { AdCreation } from '../dtos/ad.creation'; import { AdCreation } from '../dtos/ad.creation';
import { Ad } from '../entities/ad'; import { Ad } from '../entities/ad';
import { Frequency } from '../types/frequency.enum';
@CommandHandler(CreateAdCommand) @CommandHandler(CreateAdCommand)
export class CreateAdUseCase { export class CreateAdUseCase {
@ -84,9 +83,9 @@ export class CreateAdUseCase {
setDefaultDriverAndPassengerParameters(): void { setDefaultDriverAndPassengerParameters(): void {
if (!this.ad.driver && !this.ad.passenger) { if (!this.ad.driver && !this.ad.passenger) {
this.ad.driver = this.defaultParams.DRIVER; this.ad.driver = this.defaultParams.DRIVER;
this.ad.seatsDriver = this.defaultParams.DRIVER_SEAT; this.ad.seatsDriver = this.defaultParams.SEATS_PROVIDED;
this.ad.passenger = this.defaultParams.PASSENGER; this.ad.passenger = this.defaultParams.PASSENGER;
this.ad.seatsPassenger = this.defaultParams.PASSENGER_SEATS; this.ad.seatsPassenger = this.defaultParams.SEATS_REQUESTED;
} else { } else {
if (!this.ad.driver) { if (!this.ad.driver) {
this.ad.driver = false; this.ad.driver = false;
@ -105,8 +104,4 @@ export class CreateAdUseCase {
} }
} }
} }
TransformPunctualToReccurent(): void {
if (this.ad.frequency === Frequency.PUNCTUAL) {
}
}
} }

View File

@ -79,25 +79,6 @@ const newAdRequest: CreateAdRequest = {
seatsDriver: 2, seatsDriver: 2,
addresses: [mockAddress1, mockAddress2], addresses: [mockAddress1, mockAddress2],
}; };
const newPunctualPassengerAdRequest: CreateAdRequest = {
userUuid: '113e0000-0000-4000-a000-000000000000',
passenger: true,
frequency: Frequency.PUNCTUAL,
departure: new Date('05-22-2023'),
marginDurations: {
mon: undefined,
tue: undefined,
wed: undefined,
thu: undefined,
fri: undefined,
sat: undefined,
sun: undefined,
},
seatsPassenger: 1,
addresses: [mockAddressWithoutPos1, mockAddressWithoutPos2],
schedule: {},
};
const mockMessager = { const mockMessager = {
publish: jest.fn().mockImplementation(), publish: jest.fn().mockImplementation(),
@ -113,14 +94,14 @@ const mockDefaultParamsProvider = {
SAT_MARGIN: 900, SAT_MARGIN: 900,
SUN_MARGIN: 900, SUN_MARGIN: 900,
DRIVER: false, DRIVER: false,
DRIVER_SEAT: 0, SEATS_PROVIDED: 0,
PASSENGER: true, PASSENGER: true,
PASSENGER_SEATS: 1, SEATS_REQUESTED: 1,
STRICT: false, STRICT: false,
}; };
}, },
}; };
let mockAdRepository = { const mockAdRepository = {
create: jest create: jest
.fn() .fn()
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
@ -134,7 +115,7 @@ let mockAdRepository = {
.mockImplementationOnce(() => { .mockImplementationOnce(() => {
throw new Error('Already exists'); throw new Error('Already exists');
}) })
.mockImplementation((command?: CreateAdCommand) => {}), .mockImplementation(),
}; };
describe('CreateAdUseCase', () => { describe('CreateAdUseCase', () => {
let createAdUseCase: CreateAdUseCase; let createAdUseCase: CreateAdUseCase;
@ -206,9 +187,9 @@ describe('CreateAdUseCase', () => {
satMargin: mockDefaultParamsProvider.getParams().SAT_MARGIN, satMargin: mockDefaultParamsProvider.getParams().SAT_MARGIN,
sunMargin: mockDefaultParamsProvider.getParams().SUN_MARGIN, sunMargin: mockDefaultParamsProvider.getParams().SUN_MARGIN,
driver: mockDefaultParamsProvider.getParams().DRIVER, driver: mockDefaultParamsProvider.getParams().DRIVER,
seatsDriver: mockDefaultParamsProvider.getParams().DRIVER_SEAT, seatsDriver: mockDefaultParamsProvider.getParams().SEATS_PROVIDED,
passenger: mockDefaultParamsProvider.getParams().PASSENGER, passenger: mockDefaultParamsProvider.getParams().PASSENGER,
seatsPassenger: mockDefaultParamsProvider.getParams().PASSENGER_SEATS, seatsPassenger: mockDefaultParamsProvider.getParams().SEATS_REQUESTED,
strict: mockDefaultParamsProvider.getParams().STRICT, strict: mockDefaultParamsProvider.getParams().STRICT,
addresses: { addresses: {
create: minimalReccurentAdREquest.addresses as AddressCreation[], create: minimalReccurentAdREquest.addresses as AddressCreation[],
@ -219,8 +200,57 @@ describe('CreateAdUseCase', () => {
expect(mockAdRepository.create).toBeCalledWith(expectedAdCreation); expect(mockAdRepository.create).toBeCalledWith(expectedAdCreation);
}); });
it('should create an passengerAd with addresses without position ', async () => { it('should create an passengerAd with addresses without position ', async () => {
const newPunctualPassengerAdRequest: CreateAdRequest = {
userUuid: '113e0000-0000-4000-a000-000000000000',
passenger: true,
frequency: Frequency.PUNCTUAL,
departure: new Date('05-22-2023 09:36'),
marginDurations: {
mon: undefined,
tue: undefined,
wed: undefined,
thu: undefined,
fri: undefined,
sat: undefined,
sun: undefined,
},
seatsPassenger: 1,
addresses: [mockAddressWithoutPos1, mockAddressWithoutPos2],
schedule: {},
};
const newAdCommand = new CreateAdCommand(newPunctualPassengerAdRequest); const newAdCommand = new CreateAdCommand(newPunctualPassengerAdRequest);
await createAdUseCase.execute(newAdCommand); await createAdUseCase.execute(newAdCommand);
const expectedAdCreation = {
userUuid: newPunctualPassengerAdRequest.userUuid,
frequency: newPunctualPassengerAdRequest.frequency,
fromDate: newPunctualPassengerAdRequest.departure,
toDate: newPunctualPassengerAdRequest.departure,
monTime: '09:36',
tueTime: undefined,
wedTime: undefined,
thuTime: undefined,
friTime: undefined,
satTime: undefined,
sunTime: undefined,
monMargin: mockDefaultParamsProvider.getParams().MON_MARGIN,
tueMargin: mockDefaultParamsProvider.getParams().TUE_MARGIN,
wedMargin: mockDefaultParamsProvider.getParams().WED_MARGIN,
thuMargin: mockDefaultParamsProvider.getParams().THU_MARGIN,
friMargin: mockDefaultParamsProvider.getParams().FRI_MARGIN,
satMargin: mockDefaultParamsProvider.getParams().SAT_MARGIN,
sunMargin: mockDefaultParamsProvider.getParams().SUN_MARGIN,
driver: false,
seatsDriver: 0,
passenger: newPunctualPassengerAdRequest.passenger,
seatsPassenger: newPunctualPassengerAdRequest.seatsPassenger,
strict: mockDefaultParamsProvider.getParams().STRICT,
addresses: {
create: newPunctualPassengerAdRequest.addresses as AddressCreation[],
},
createdAt: undefined,
} as AdCreation;
expect(mockAdRepository.create).toBeCalledWith(expectedAdCreation);
}); });
}); });
}); });