feat: modify to handle minutes

This commit is contained in:
karishmas6
2024-10-22 18:26:28 +05:30
parent a368ea2cd0
commit 0c45a62d6c

View File

@@ -250,15 +250,16 @@ router.put('/schedule/:id/', requireSignIn, async (req, res) => {
runEvery, runEvery,
runEveryUnit, runEveryUnit,
startFrom, startFrom,
atTime, atTimeStart,
atTimeEnd,
timezone timezone
} = req.body; } = req.body;
if (!id || !runEvery || !runEveryUnit || !startFrom || !atTime || !timezone) { if (!id || !runEvery || !runEveryUnit || !startFrom || !timezone || (runEveryUnit === 'HOURS' || runEveryUnit === 'MINUTES') && (!atTimeStart || !atTimeEnd)) {
return res.status(400).json({ error: 'Missing required parameters' }); return res.status(400).json({ error: 'Missing required parameters' });
} }
if (!['HOURS', 'DAYS', 'WEEKS', 'MONTHS'].includes(runEveryUnit)) { if (!['HOURS', 'DAYS', 'WEEKS', 'MONTHS', 'MINUTES'].includes(runEveryUnit)) {
return res.status(400).json({ error: 'Invalid runEvery unit' }); return res.status(400).json({ error: 'Invalid runEvery unit' });
} }
@@ -266,8 +267,13 @@ router.put('/schedule/:id/', requireSignIn, async (req, res) => {
return res.status(400).json({ error: 'Invalid timezone' }); return res.status(400).json({ error: 'Invalid timezone' });
} }
const [hours, minutes] = atTime.split(':').map(Number); const [startHours, startMinutes] = atTimeStart.split(':').map(Number);
if (isNaN(hours) || isNaN(minutes) || hours < 0 || hours > 23 || minutes < 0 || minutes > 59) { const [endHours, endMinutes] = atTimeEnd.split(':').map(Number);
// Validate the time format for In Between
if (isNaN(startHours) || isNaN(startMinutes) || isNaN(endHours) || isNaN(endMinutes) ||
startHours < 0 || startHours > 23 || startMinutes < 0 || startMinutes > 59 ||
endHours < 0 || endHours > 23 || endMinutes < 0 || endMinutes > 59) {
return res.status(400).json({ error: 'Invalid time format' }); return res.status(400).json({ error: 'Invalid time format' });
} }
@@ -278,18 +284,21 @@ router.put('/schedule/:id/', requireSignIn, async (req, res) => {
let cronExpression; let cronExpression;
switch (runEveryUnit) { switch (runEveryUnit) {
case 'MINUTES':
cronExpression = `${startMinutes}-${endMinutes} */${runEvery} * * *`;
break;
case 'HOURS': case 'HOURS':
cronExpression = `${minutes} */${runEvery} * * *`; cronExpression = `${startMinutes}-${endMinutes} */${runEvery} * * *`;
break; break;
case 'DAYS': case 'DAYS':
cronExpression = `${minutes} ${hours} */${runEvery} * *`; cronExpression = `${startMinutes} ${startHours} */${runEvery} * *`;
break; break;
case 'WEEKS': case 'WEEKS':
const dayIndex = days.indexOf(startFrom); const dayIndex = days.indexOf(startFrom);
cronExpression = `${minutes} ${hours} * * ${dayIndex}/${7 * runEvery}`; cronExpression = `${startMinutes} ${startHours} * * ${dayIndex}/${7 * runEvery}`;
break; break;
case 'MONTHS': case 'MONTHS':
cronExpression = `${minutes} ${hours} 1-7 */${runEvery} *`; cronExpression = `${startMinutes} ${startHours} 1-7 */${runEvery} *`;
if (startFrom !== 'SUNDAY') { if (startFrom !== 'SUNDAY') {
const dayIndex = days.indexOf(startFrom); const dayIndex = days.indexOf(startFrom);
cronExpression += ` ${dayIndex}`; cronExpression += ` ${dayIndex}`;
@@ -318,8 +327,6 @@ router.put('/schedule/:id/', requireSignIn, async (req, res) => {
res.status(200).json({ res.status(200).json({
message: 'success', message: 'success',
runId, runId,
// cronExpression,
// nextRunTime: getNextRunTime(cronExpression, timezone)
}); });
} catch (error) { } catch (error) {
@@ -328,6 +335,7 @@ router.put('/schedule/:id/', requireSignIn, async (req, res) => {
} }
}); });
// function getNextRunTime(cronExpression, timezone) { // function getNextRunTime(cronExpression, timezone) {
// const schedule = cron.schedule(cronExpression, () => {}, { timezone }); // const schedule = cron.schedule(cronExpression, () => {}, { timezone });
// const nextDate = schedule.nextDate(); // const nextDate = schedule.nextDate();