diff --git a/src/modules/ad/core/application/queries/match/selector/passenger-oriented.selector.ts b/src/modules/ad/core/application/queries/match/selector/passenger-oriented.selector.ts index e1caae6..611e053 100644 --- a/src/modules/ad/core/application/queries/match/selector/passenger-oriented.selector.ts +++ b/src/modules/ad/core/application/queries/match/selector/passenger-oriented.selector.ts @@ -1,6 +1,7 @@ import { AdEntity } from '@modules/ad/core/domain/ad.entity'; import { Frequency, Role } from '@modules/ad/core/domain/ad.types'; import { CandidateEntity } from '@modules/ad/core/domain/candidate.entity'; +import { DateInterval } from '../../../../domain/candidate.types'; import { Point } from '../../../types/point.type'; import { Waypoint } from '../../../types/waypoint.type'; import { Selector } from '../algorithm.abstract'; @@ -46,7 +47,7 @@ export class PassengerOrientedSelector extends Selector { id: adEntity.id, role: adsRole.role == Role.DRIVER ? Role.PASSENGER : Role.DRIVER, frequency: adEntity.getProps().frequency, - dateInterval: { + dateInterval: this._fixDateInterval({ lowerDate: this._maxDateString( this.query.fromDate, adEntity.getProps().fromDate, @@ -55,7 +56,7 @@ export class PassengerOrientedSelector extends Selector { this.query.toDate, adEntity.getProps().toDate, ), - }, + }), driverWaypoints: adsRole.role == Role.PASSENGER ? adEntity.getProps().waypoints @@ -297,11 +298,26 @@ export class PassengerOrientedSelector extends Selector { azimuth + margin > 360 ? azimuth + margin - 360 : azimuth + margin, }); + //TODO If the dates are always formatted with '%Y-%m-%d', no conversion to Date is needed private _maxDateString = (date1: string, date2: string): string => new Date(date1) > new Date(date2) ? date1 : date2; private _minDateString = (date1: string, date2: string): string => new Date(date1) < new Date(date2) ? date1 : date2; + + /** + * When a punctual ad matches a punctual query, it may be on a different date than the query + * (for routes by night), and the range produced by _minDateString and _maxDateString is not correct. + * This function fixes that by inverting the dates if necessary. + */ + private _fixDateInterval(interval: DateInterval): DateInterval { + if (interval.lowerDate > interval.higherDate) { + const tmp = interval.lowerDate; + interval.lowerDate = interval.higherDate; + interval.higherDate = tmp; + } + return interval; + } } export type QueryStringRole = { diff --git a/src/modules/ad/core/domain/candidate.entity.ts b/src/modules/ad/core/domain/candidate.entity.ts index 052ba61..943a540 100644 --- a/src/modules/ad/core/domain/candidate.entity.ts +++ b/src/modules/ad/core/domain/candidate.entity.ts @@ -323,7 +323,7 @@ export class CandidateEntity extends AggregateRoot { } //TODO Use this class as part of the CandidateEntity aggregate -class Schedule extends ValueObject<{ +export class Schedule extends ValueObject<{ items: ScheduleItemProps[]; dateInterval: DateInterval; }> {