diff --git a/backend/src/controllers/cron/email.ts b/backend/src/controllers/cron/email.ts index 0dc65f4a..772aa443 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 { convertToUTC, validateEmailFields, validateUpdateEmailBody } from '../../services/cron/email' +import { convertToMT, convertToUTC, validateEmailFields, validateUpdateEmailBody } from '../../services/cron/email' import { EmailClient, getEmailCron, initializeEmailCron, isEmailCronEnabled, setEmailCron } from '../../cron/email' import cron from 'node-cron'; @@ -29,8 +29,25 @@ export const getEmail = async (req: Request, res: Response) => { moduleName, function: 'getEmail', }) - return res.status(200).json({ cron }) + + const utcExpr = cron?.expression.split(" ") + const days = utcExpr[utcExpr.length-1].split(",") + const minutes = parseInt(utcExpr[1]) + const hours = parseInt(utcExpr[2]) + + let setDays : Array = [] + for (let day of days) { + const tuple = convertToMT(minutes, hours, day) + setDays.push(tuple[2]) + } + const tuple = convertToMT(minutes, hours, "MON") + let cronExpression = `0 ${tuple[0]} ${tuple[1]} * * ` + return res.status(200).json({ cron : { + ...cron, + expression: cronExpression + } }) } catch (error: any) { + console.log(error) Logger.error({ error: error, moduleName, diff --git a/backend/src/services/cron/email.ts b/backend/src/services/cron/email.ts index 0b1b3e88..52f4cb0b 100644 --- a/backend/src/services/cron/email.ts +++ b/backend/src/services/cron/email.ts @@ -22,7 +22,7 @@ export const convertToUTC = (minute: number, hour: number, day: string) : [numbe "MON": "TUE", "TUE": "WED", "WED": "THU", - "THE": "FRI", + "THU": "FRI", "FRI": "SAT", "SAT": "SUN", "SUN": "MON", @@ -32,11 +32,36 @@ export const convertToUTC = (minute: number, hour: number, day: string) : [numbe let increment = 6 // mst/mdt if (month <= 2 || month >= 11) { - increment = 6 + increment = 7 } const newHour = (hour+increment)%24 if (newHour < hour) day = days[day] + return [minute, newHour, day] +} + + +export const convertToMT = (minute: number, hour: number, day: string) : [number, number, string] => { + const days : {[key : string]: string}= { + "MON": "SUN", + "TUE": "MON", + "WED": "TUE", + "THU": "WED", + "FRI": "THU", + "SAT": "FRI", + "SUN": "SAT", + } + const date = new Date() + const month = date.getMonth() + let increment = -6 + // mst/mdt + if (month <= 2 || month >= 11) { + increment = -7 + } + + const newHour = (hour+increment+24)%24 + if (newHour < hour) day = days[day] + return [minute, newHour, day] } \ No newline at end of file