Skip to content

Commit

Permalink
Merge pull request #278 from UofA-Blueprint/fix/mst-to-utc
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
royayush1 authored Dec 23, 2023
2 parents fdad387 + 12a3ced commit 8e6f41e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
21 changes: 19 additions & 2 deletions backend/src/controllers/cron/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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<string> = []
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,
Expand Down
29 changes: 27 additions & 2 deletions backend/src/services/cron/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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]
}

0 comments on commit 8e6f41e

Please sign in to comment.