Skip to content

Instantly share code, notes, and snippets.

@IsraaMoqbel
Created July 26, 2022 21:06
Show Gist options
  • Select an option

  • Save IsraaMoqbel/0e51d359db58b6f3ad9d16e064829f71 to your computer and use it in GitHub Desktop.

Select an option

Save IsraaMoqbel/0e51d359db58b6f3ad9d16e064829f71 to your computer and use it in GitHub Desktop.
Create schedule for number of sessions giving right dates and days for the new sessions schedule distributed on number of sessions per week with specific weekdays starting from specific date
//Old task in node.js aimed for scheduling sessions for trainees in a gym subscription system
// TODO create schedule for number of sessions
// giving right dates and days for the new sessions schedule
// distributed on number of sessions per week with specific weekdays
// starting from specific date
const moment = require('moment');
// TODO use moment weekday method
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
// saturday day number is 6
// sunday day number is 0
const unsortedSchedules = [
// Data provided
{
day: 'Friday',
time: '10:04:00',
},
{ day: 'Monday', time: '10:03:00' },
{ day: 'Wednesday', time: '11:03:00' },
]
const schedules = unsortedSchedules.sort((e, a) => getWeekDayNumber(e.day) - getWeekDayNumber(a.day))
function getWeekDayNumber(day) {
for (let index = 0; index < weekday.length; index++) {
if (day.toUpperCase() == weekday[index].toUpperCase()) {
return index;
}
}
}
// Data provided
const sessionsNumber = 7;
const sessionsPerWeek = 4;
const DBSubscriptionStartDate = new Date('2020-09-03T00:00:00.000Z');
let subscriptionStartDate = DBSubscriptionStartDate
subscriptionStartDate = moment(subscriptionStartDate)
const iterationEveryWeek = Math.floor(sessionsNumber / sessionsPerWeek);
const arrayOfSessions = [];
let previousFirstWeekdays = 0;
const timeZone = 'Europe/London';
let sessionsNumberForLoop = sessionsNumber;
let count = 0;
let firstRound = true;
// Actual loop to create sessions
while (arrayOfSessions.length < sessionsNumberForLoop) {
// handle start from the same week
const isComingDayes = moment(subscriptionStartDate).day(schedules[count].day).isSameOrAfter(moment(DBSubscriptionStartDate));
if(firstRound) {
if(!isComingDayes) {
// Don't create sessions for past days in first week
} else {
// book session for the next days in first week
const sessionObj = bookSession(subscriptionStartDate.day(schedules[count].day), schedules[count], timeZone, arrayOfSessions);
arrayOfSessions.push(sessionObj);
}
} else {
const sessionObj =bookSession(subscriptionStartDate.day(schedules[count].day), schedules[count], timeZone, arrayOfSessions);
arrayOfSessions.push(sessionObj);
}
count++;
if(count === schedules.length) {
count=0;
firstRound = false;
// start next week
subscriptionStartDate.add(1, 'week')
}
}
function bookSession(sessionStartingAt, scheduleInfo, timeZone, arrayOfSessions) {
console.log(sessionStartingAt, 'sessionStartingAt')
var sessionObj = {};
var hours = parseInt(scheduleInfo.time.slice(0, 2));
var minutes = parseInt(scheduleInfo.time.slice(3, 5));
// const day= sessionStartingAt
sessionObj.date = moment(sessionStartingAt).utc().hour(hours).minute(minutes).format('YYYY-MM-DD HH:mm:ss');
return sessionObj
}
console.log(arrayOfSessions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment