From 0dc01da2b0564a04584cca993f0c05ddbccc765a Mon Sep 17 00:00:00 2001 From: Romain Thouvenin Date: Thu, 4 Apr 2024 17:24:23 +0200 Subject: [PATCH] Combine journey creation and filtering to avoid missing results --- .../ad/core/domain/candidate.entity.ts | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/modules/ad/core/domain/candidate.entity.ts b/src/modules/ad/core/domain/candidate.entity.ts index 5ab8100..3da8964 100644 --- a/src/modules/ad/core/domain/candidate.entity.ts +++ b/src/modules/ad/core/domain/candidate.entity.ts @@ -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 { protected readonly _id: AggregateID; @@ -63,18 +63,23 @@ export class CandidateEntity extends AggregateRoot { // 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(), + ); return this; };