Skip to content

Commit

Permalink
Merge pull request #277 from UofA-Blueprint/fix/mst-to-utc
Browse files Browse the repository at this point in the history
utc time
  • Loading branch information
royayush1 authored Dec 23, 2023
2 parents 3d55b88 + d9117ae commit fdad387
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
11 changes: 7 additions & 4 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 { 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';

Expand Down Expand Up @@ -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<string> = []
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({
Expand Down
24 changes: 24 additions & 0 deletions backend/src/services/cron/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

0 comments on commit fdad387

Please sign in to comment.