diff --git a/src/modules/ad/core/domain/candidate.entity.ts b/src/modules/ad/core/domain/candidate.entity.ts index b9e99c1..e503a0e 100644 --- a/src/modules/ad/core/domain/candidate.entity.ts +++ b/src/modules/ad/core/domain/candidate.entity.ts @@ -60,7 +60,7 @@ export class CandidateEntity extends AggregateRoot { * This is a tedious process : additional information can be found in deeper methods ! */ createJourneys = (): CandidateEntity => { - // driver and passenger schedules are mandatory + // driver and passenger schedules are eventually mandatory if (!this.props.driverSchedule) this._createDriverSchedule(); if (!this.props.passengerSchedule) this._createPassengerSchedule(); try { @@ -96,50 +96,47 @@ export class CandidateEntity extends AggregateRoot { * Create the driver schedule based on the passenger schedule */ private _createDriverSchedule = (): void => { - if (this.props.passengerSchedule) { - let driverSchedule: ScheduleItemProps[] = - this.props.passengerSchedule.map( - (scheduleItemProps: ScheduleItemProps) => ({ - day: scheduleItemProps.day, - time: scheduleItemProps.time, - margin: scheduleItemProps.margin, - }), + let driverSchedule: ScheduleItemProps[] = this.props.passengerSchedule!.map( + (scheduleItemProps: ScheduleItemProps) => ({ + day: scheduleItemProps.day, + time: scheduleItemProps.time, + margin: scheduleItemProps.margin, + }), + ); + // adjust the driver theoretical schedule : + // we guess the ideal driver departure time based on the duration to + // reach the passenger starting point from the driver starting point + driverSchedule = driverSchedule.map( + (scheduleItemProps: ScheduleItemProps) => { + const driverDate: Date = CalendarTools.firstDate( + scheduleItemProps.day, + this.props.dateInterval, ); - // adjust the driver theoretical schedule : - // we guess the ideal driver departure time based on the duration to - // reach the passenger starting point from the driver starting point - driverSchedule = driverSchedule.map( - (scheduleItemProps: ScheduleItemProps) => { - const driverDate: Date = CalendarTools.firstDate( - scheduleItemProps.day, - this.props.dateInterval, - ); - const driverStartDatetime: Date = CalendarTools.datetimeWithSeconds( - driverDate, - scheduleItemProps.time, - -this._passengerStartDuration(), - ); - return { - day: driverDate.getUTCDay(), - margin: scheduleItemProps.margin, - time: `${driverStartDatetime - .getUTCHours() - .toString() - .padStart(2, '0')}:${driverStartDatetime - .getUTCMinutes() - .toString() - .padStart(2, '0')}`, - }; - }, - ); - this.props.driverSchedule = driverSchedule.map( - (scheduleItemProps: ScheduleItemProps) => ({ - day: scheduleItemProps.day, - time: scheduleItemProps.time, + const driverStartDatetime: Date = CalendarTools.datetimeWithSeconds( + driverDate, + scheduleItemProps.time, + -this._passengerStartDuration(), + ); + return { + day: driverDate.getUTCDay(), margin: scheduleItemProps.margin, - }), - ); - } + time: `${driverStartDatetime + .getUTCHours() + .toString() + .padStart(2, '0')}:${driverStartDatetime + .getUTCMinutes() + .toString() + .padStart(2, '0')}`, + }; + }, + ); + this.props.driverSchedule = driverSchedule.map( + (scheduleItemProps: ScheduleItemProps) => ({ + day: scheduleItemProps.day, + time: scheduleItemProps.time, + margin: scheduleItemProps.margin, + }), + ); }; /** @@ -162,50 +159,47 @@ export class CandidateEntity extends AggregateRoot { * Create the passenger schedule based on the driver schedule */ private _createPassengerSchedule = (): void => { - if (this.props.driverSchedule) { - let passengerSchedule: ScheduleItemProps[] = - this.props.driverSchedule.map( - (scheduleItemProps: ScheduleItemProps) => ({ - day: scheduleItemProps.day, - time: scheduleItemProps.time, - margin: scheduleItemProps.margin, - }), + let passengerSchedule: ScheduleItemProps[] = this.props.driverSchedule!.map( + (scheduleItemProps: ScheduleItemProps) => ({ + day: scheduleItemProps.day, + time: scheduleItemProps.time, + margin: scheduleItemProps.margin, + }), + ); + // adjust the passenger theoretical schedule : + // we guess the ideal passenger departure time based on the duration to + // reach the passenger starting point from the driver starting point + passengerSchedule = passengerSchedule.map( + (scheduleItemProps: ScheduleItemProps) => { + const passengerDate: Date = CalendarTools.firstDate( + scheduleItemProps.day, + this.props.dateInterval, ); - // adjust the passenger theoretical schedule : - // we guess the ideal passenger departure time based on the duration to - // reach the passenger starting point from the driver starting point - passengerSchedule = passengerSchedule.map( - (scheduleItemProps: ScheduleItemProps) => { - const passengerDate: Date = CalendarTools.firstDate( - scheduleItemProps.day, - this.props.dateInterval, - ); - const passengeStartDatetime: Date = CalendarTools.datetimeWithSeconds( - passengerDate, - scheduleItemProps.time, - this._passengerStartDuration(), - ); - return { - day: passengerDate.getUTCDay(), - margin: scheduleItemProps.margin, - time: `${passengeStartDatetime - .getUTCHours() - .toString() - .padStart(2, '0')}:${passengeStartDatetime - .getUTCMinutes() - .toString() - .padStart(2, '0')}`, - }; - }, - ); - this.props.passengerSchedule = passengerSchedule.map( - (scheduleItemProps: ScheduleItemProps) => ({ - day: scheduleItemProps.day, - time: scheduleItemProps.time, + const passengeStartDatetime: Date = CalendarTools.datetimeWithSeconds( + passengerDate, + scheduleItemProps.time, + this._passengerStartDuration(), + ); + return { + day: passengerDate.getUTCDay(), margin: scheduleItemProps.margin, - }), - ); - } + time: `${passengeStartDatetime + .getUTCHours() + .toString() + .padStart(2, '0')}:${passengeStartDatetime + .getUTCMinutes() + .toString() + .padStart(2, '0')}`, + }; + }, + ); + this.props.passengerSchedule = passengerSchedule.map( + (scheduleItemProps: ScheduleItemProps) => ({ + day: scheduleItemProps.day, + time: scheduleItemProps.time, + margin: scheduleItemProps.margin, + }), + ); }; private _createJourney = (driverScheduleItem: ScheduleItem): Journey =>