Combine journey creation and filtering to avoid missing results

This commit is contained in:
Romain Thouvenin 2024-04-04 17:24:23 +02:00
parent 945ce80840
commit 0dc01da2b0
1 changed files with 25 additions and 20 deletions

View File

@ -1,28 +1,28 @@
import {
AggregateRoot,
AggregateID,
AggregateRoot,
ArgumentInvalidException,
} from '@mobicoop/ddd-library';
import { Role } from './ad.types';
import { CalendarTools } from './calendar-tools.service';
import {
CandidateProps,
CreateCandidateProps,
Target,
} from './candidate.types';
import { ActorTime } from './value-objects/actor-time.value-object';
import { Actor } from './value-objects/actor.value-object';
import {
CarpoolPathItem,
CarpoolPathItemProps,
} from './value-objects/carpool-path-item.value-object';
import { Step, StepProps } from './value-objects/step.value-object';
import { JourneyItem } from './value-objects/journey-item.value-object';
import { Journey, JourneyProps } from './value-objects/journey.value-object';
import {
ScheduleItem,
ScheduleItemProps,
} from './value-objects/schedule-item.value-object';
import { Journey } from './value-objects/journey.value-object';
import { CalendarTools } from './calendar-tools.service';
import { JourneyItem } from './value-objects/journey-item.value-object';
import { Actor } from './value-objects/actor.value-object';
import { ActorTime } from './value-objects/actor-time.value-object';
import { Role } from './ad.types';
import { Step, StepProps } from './value-objects/step.value-object';
export class CandidateEntity extends AggregateRoot<CandidateProps> {
protected readonly _id: AggregateID;
@ -63,18 +63,23 @@ export class CandidateEntity extends AggregateRoot<CandidateProps> {
// driver and passenger schedules are eventually mandatory
if (!this.props.driverSchedule) this._createDriverSchedule();
if (!this.props.passengerSchedule) this._createPassengerSchedule();
try {
this.props.journeys = (this.props.driverSchedule as ScheduleItemProps[])
// first we create the journeys
.map((driverScheduleItem: ScheduleItem) =>
this._createJourney(driverScheduleItem),
)
// then we filter the ones with invalid pickups
.filter((journey: Journey) => journey.hasValidPickUp());
} catch (e) {
// irrelevant journeys fall here
// eg. no available day for the given date range
}
this.props.journeys = this.props.driverSchedule!.reduce(
(accJourneys: JourneyProps[], driverScheduleItem: ScheduleItem) => {
try {
// first we create the journeys
const journey = this._createJourney(driverScheduleItem);
// then we filter the ones with invalid pickups
if (journey.hasValidPickUp()) {
accJourneys.push(journey);
}
} catch (e) {
// irrelevant journeys fall here
// eg. no available day for the given date range
}
return accJourneys;
},
new Array<JourneyProps>(),
);
return this;
};