diff --git a/backend/src/controllers/cron/email.ts b/backend/src/controllers/cron/email.ts index d0e3d324..0dc65f4a 100644 --- a/backend/src/controllers/cron/email.ts +++ b/backend/src/controllers/cron/email.ts @@ -4,7 +4,7 @@ import { verifyIfUserAdmin } from '../../services/users' import Logger from '../../utils/logger' import { db } from '../../internal/firebase' import nodeConfig from 'config' -import { validateEmailFields, validateUpdateEmailBody } from '../../services/cron/email' +import { convertToUTC, validateEmailFields, validateUpdateEmailBody } from '../../services/cron/email' import { EmailClient, getEmailCron, initializeEmailCron, isEmailCronEnabled, setEmailCron } from '../../cron/email' import cron from 'node-cron'; @@ -226,13 +226,16 @@ export const updateEmailCronExpression = async (req: Request, res: Response) => return res.status(400).json({ error: 'bad_request' }) } - let cronExpression = `0 ${minutes} ${hours} * * ` - let setDays = [] + + let setDays : Array = [] for (let day of daysArr) { if (days[day]) { - setDays.push(day.substring(0, 3).toUpperCase()) + const tuple = convertToUTC(minutes, hours, day.substring(0, 3).toUpperCase()) + setDays.push(tuple[2]) } } + const tuple = convertToUTC(minutes, hours, "MON") + let cronExpression = `0 ${tuple[0]} ${tuple[1]} * * ` cronExpression += setDays.join(",") await db.collection(nodeConfig.get('collections.cron')).doc('email').update({ diff --git a/backend/src/services/cron/email.ts b/backend/src/services/cron/email.ts index 32115c30..0b1b3e88 100644 --- a/backend/src/services/cron/email.ts +++ b/backend/src/services/cron/email.ts @@ -15,4 +15,28 @@ export const validateUpdateEmailBody = (body: any, fields: string[]): boolean => } } return true +} + +export const convertToUTC = (minute: number, hour: number, day: string) : [number, number, string] => { + const days : {[key : string]: string}= { + "MON": "TUE", + "TUE": "WED", + "WED": "THU", + "THE": "FRI", + "FRI": "SAT", + "SAT": "SUN", + "SUN": "MON", + } + const date = new Date() + const month = date.getMonth() + let increment = 6 + // mst/mdt + if (month <= 2 || month >= 11) { + increment = 6 + } + + const newHour = (hour+increment)%24 + if (newHour < hour) day = days[day] + + return [minute, newHour, day] } \ No newline at end of file