improve creation of schedules

This commit is contained in:
Sylvain Briat 2024-04-02 11:09:59 +02:00 committed by sbriat
parent 100fb3487d
commit 0446d267ef
1 changed files with 82 additions and 62 deletions

View File

@ -96,40 +96,50 @@ export class CandidateEntity extends AggregateRoot<CandidateProps> {
* Create the driver schedule based on the passenger schedule * Create the driver schedule based on the passenger schedule
*/ */
private _createDriverSchedule = (): void => { private _createDriverSchedule = (): void => {
let driverSchedule: ScheduleItemProps[] = this.props.passengerSchedule!.map( let driverSchedule: Array<ScheduleItemProps | undefined> =
(scheduleItemProps: ScheduleItemProps) => ({ this.props.passengerSchedule!.map(
day: scheduleItemProps.day, (scheduleItemProps: ScheduleItemProps) => ({
time: scheduleItemProps.time, day: scheduleItemProps.day,
margin: scheduleItemProps.margin, time: scheduleItemProps.time,
}), margin: scheduleItemProps.margin,
); }),
);
// adjust the driver theoretical schedule : // adjust the driver theoretical schedule :
// we guess the ideal driver departure time based on the duration to // we guess the ideal driver departure time based on the duration to
// reach the passenger starting point from the driver starting point // reach the passenger starting point from the driver starting point
driverSchedule = driverSchedule.map( driverSchedule = driverSchedule
(scheduleItemProps: ScheduleItemProps) => { .map((scheduleItemProps: ScheduleItemProps) => {
const driverDate: Date = CalendarTools.firstDate( try {
scheduleItemProps.day, const driverDate: Date = CalendarTools.firstDate(
this.props.dateInterval, scheduleItemProps.day,
); this.props.dateInterval,
const driverStartDatetime: Date = CalendarTools.datetimeWithSeconds( );
driverDate, const driverStartDatetime: Date = CalendarTools.datetimeWithSeconds(
scheduleItemProps.time, driverDate,
-this._passengerStartDuration(), scheduleItemProps.time,
); -this._passengerStartDuration(),
return { );
day: driverDate.getUTCDay(), return <ScheduleItemProps>{
margin: scheduleItemProps.margin, day: driverDate.getUTCDay(),
time: `${driverStartDatetime margin: scheduleItemProps.margin,
.getUTCHours() time: `${driverStartDatetime
.toString() .getUTCHours()
.padStart(2, '0')}:${driverStartDatetime .toString()
.getUTCMinutes() .padStart(2, '0')}:${driverStartDatetime
.toString() .getUTCMinutes()
.padStart(2, '0')}`, .toString()
}; .padStart(2, '0')}`,
}, };
); } catch (e) {
// no possible driver date or time
// TODO : find a test case !
return undefined;
}
})
.filter(
(scheduleItemProps: ScheduleItemProps | undefined) =>
scheduleItemProps !== undefined,
);
this.props.driverSchedule = driverSchedule.map( this.props.driverSchedule = driverSchedule.map(
(scheduleItemProps: ScheduleItemProps) => ({ (scheduleItemProps: ScheduleItemProps) => ({
day: scheduleItemProps.day, day: scheduleItemProps.day,
@ -159,40 +169,50 @@ export class CandidateEntity extends AggregateRoot<CandidateProps> {
* Create the passenger schedule based on the driver schedule * Create the passenger schedule based on the driver schedule
*/ */
private _createPassengerSchedule = (): void => { private _createPassengerSchedule = (): void => {
let passengerSchedule: ScheduleItemProps[] = this.props.driverSchedule!.map( let passengerSchedule: Array<ScheduleItemProps | undefined> =
(scheduleItemProps: ScheduleItemProps) => ({ this.props.driverSchedule!.map(
day: scheduleItemProps.day, (scheduleItemProps: ScheduleItemProps) => ({
time: scheduleItemProps.time, day: scheduleItemProps.day,
margin: scheduleItemProps.margin, time: scheduleItemProps.time,
}), margin: scheduleItemProps.margin,
); }),
);
// adjust the passenger theoretical schedule : // adjust the passenger theoretical schedule :
// we guess the ideal passenger departure time based on the duration to // we guess the ideal passenger departure time based on the duration to
// reach the passenger starting point from the driver starting point // reach the passenger starting point from the driver starting point
passengerSchedule = passengerSchedule.map( passengerSchedule = passengerSchedule
(scheduleItemProps: ScheduleItemProps) => { .map((scheduleItemProps: ScheduleItemProps) => {
const passengerDate: Date = CalendarTools.firstDate( try {
scheduleItemProps.day, const passengerDate: Date = CalendarTools.firstDate(
this.props.dateInterval, scheduleItemProps.day,
); this.props.dateInterval,
const passengeStartDatetime: Date = CalendarTools.datetimeWithSeconds( );
passengerDate, const passengeStartDatetime: Date = CalendarTools.datetimeWithSeconds(
scheduleItemProps.time, passengerDate,
this._passengerStartDuration(), scheduleItemProps.time,
); this._passengerStartDuration(),
return { );
day: passengerDate.getUTCDay(), return {
margin: scheduleItemProps.margin, day: passengerDate.getUTCDay(),
time: `${passengeStartDatetime margin: scheduleItemProps.margin,
.getUTCHours() time: `${passengeStartDatetime
.toString() .getUTCHours()
.padStart(2, '0')}:${passengeStartDatetime .toString()
.getUTCMinutes() .padStart(2, '0')}:${passengeStartDatetime
.toString() .getUTCMinutes()
.padStart(2, '0')}`, .toString()
}; .padStart(2, '0')}`,
}, };
); } catch (e) {
// no possible passenger date or time
// TODO : find a test case !
return undefined;
}
})
.filter(
(scheduleItemProps: ScheduleItemProps | undefined) =>
scheduleItemProps !== undefined,
);
this.props.passengerSchedule = passengerSchedule.map( this.props.passengerSchedule = passengerSchedule.map(
(scheduleItemProps: ScheduleItemProps) => ({ (scheduleItemProps: ScheduleItemProps) => ({
day: scheduleItemProps.day, day: scheduleItemProps.day,