Trying to generate slots in future based on availableTimes, slots are nothing but they have start time and end time based on slotSize and startTime
Example data:
availableTimes:
{
TUESDAY: [{ dayOfTheWeek: "TUESDAY", start: 8, end: 10 }],
},
daysInFuture: 1,
timeZone: "Australia/Melbourne",
slotSizeInMinutes: 60,
timeStamp: 1640041590364
Based on this availableTime Data and considering current date is
2021-12-21T08:00:00.000+11:00
it should return
[
{
ref: "2021-12-21T08:00:00.000+11:00",
startTime: "2021-12-21T08:00:00.000+11:00",
endTime: "2021-12-21T09:00:00.000+11:00",
available: true,
},
{
ref: "2021-12-21T09:00:00.000+11:00",
startTime: "2021-12-21T09:00:00.000+11:00",
endTime: "2021-12-21T10:00:00.000+11:00",
available: true,
},
]
which is working as expected, I just want to decrease the complexity of the function.
function generateTimeSlots(
availableTimes: {
[x: string]: MenuAvailability[];
},
daysInFuture: number,
timeZone: string,
slotSizeInMinutes: number,
timeStamp: number
): orders.TimeSlot[] {
const timeSlots = [];
for (let j = 0; j <= daysInFuture; j++) {
let currentDate = DateTime.fromMillis(timeStamp)
.setZone(timeZone)
.plus({ days: j });
const currentDay = currentDate.weekdayLong.toUpperCase() as DayOfTheWeek;
const slotsForTheDay = availableTimes[currentDay] ?? [];
for (let i = 0; i < slotsForTheDay.length; i++) {
const startHour = slotsForTheDay[i].start;
const endHour = slotsForTheDay[i].end;
const numOfSlots = ((endHour - startHour) * 60) / slotSizeInMinutes;
const addMinutes = (date: DateTime, minutes: number) => {
return DateTime.fromISO(date.toISO()).plus({ minutes: minutes });
};
currentDate = currentDate.set({
hour: startHour,
minute: 0,
second: 0,
millisecond: 0,
});
for (let i = 0; i < numOfSlots; i++) {
timeSlots.push({
ref: currentDate.toISO(),
startTime: currentDate.toISO(),
endTime: addMinutes(currentDate, slotSizeInMinutes).toISO(),
available: true,
});
currentDate = addMinutes(currentDate, slotSizeInMinutes);
}
}
}
return timeSlots;
}