-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from lennygir/feat/169-thesis-expiration
feat: notify when a proposal expires in 7 days
- Loading branch information
Showing
8 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const { CronJob } = require('cron'); | ||
const { getProposalsThatExpireInXDays, notifyProposalExpiration } = require('./theses-dao'); | ||
|
||
const cronjobNames = { | ||
THESIS_EXPIRED: 'THESIS_EXPIRED' | ||
}; | ||
|
||
const cronjobs = {}; | ||
|
||
const initCronjobs = () => { | ||
cronjobs[cronjobNames.THESIS_EXPIRED] = new CronJob("0 0 * * *" , () => { | ||
const proposals = getProposalsThatExpireInXDays(7); | ||
if(proposals) { | ||
proposals.forEach((proposal) => { | ||
notifyProposalExpiration(proposal); | ||
}); | ||
} | ||
}, undefined, true, "Europe/Rome", undefined, true); | ||
}; | ||
|
||
const runCronjob = (cronjobName) => { | ||
console.log('[Cron] Running job ' + cronjobName); | ||
if(cronjobs[cronjobName]) { | ||
cronjobs[cronjobName].fireOnTick(); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
cronjobNames, | ||
runCronjob, | ||
initCronjobs | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const proposalExpirationHtmlTemplate = (variables) => { | ||
return ` | ||
<html> | ||
<body> | ||
<p>Dear ${variables.name},</p> | ||
<p>your thesis proposal "${variables.thesis}" expires in ${variables.nbOfDays} days (${variables.date}).</p> | ||
<p>Best regards,</p> | ||
<p>the Thesis Managment system</p> | ||
</body> | ||
</html> | ||
`; | ||
}; | ||
|
||
const proposalExpirationTextTemplate = (variables) => { | ||
return `Dear ${variables.name},\nyour thesis proposal "${variables.thesis}" expires in ${variables.nbOfDays} days (${variables.date}).\nBest regards,\nthe Thesis Managment system`; | ||
}; | ||
|
||
exports.proposalExpirationTemplate = (variables) => ({ | ||
html: proposalExpirationHtmlTemplate(variables), | ||
text: proposalExpirationTextTemplate(variables) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,10 +27,12 @@ const { | |
getPendingApplicationsOfStudent, | ||
getAcceptedApplicationsOfStudent, | ||
getRequestsForClerk, | ||
getProposalsThatExpireInXDays, | ||
} = require("../src/theses-dao"); | ||
|
||
const dayjs = require("dayjs"); | ||
const isLoggedIn = require("../src/protect-routes"); | ||
const { runCronjob, cronjobNames } = require("../src/cronjobs"); | ||
|
||
jest.mock("../src/theses-dao"); | ||
jest.mock("../src/protect-routes"); | ||
|
@@ -942,3 +944,21 @@ describe("GET /api/start-requests", () => { | |
.expect(200); | ||
}); | ||
}); | ||
|
||
describe("Cronjobs", () => { | ||
test("Notify on thesis expiration (7 days before)", async () => { | ||
const expectedDate = dayjs().add(7 + 2, "day"); | ||
getDelta.mockReturnValue({ delta: 2 }); | ||
getProposalsThatExpireInXDays.mockReturnValue([ | ||
{ | ||
supervisor: "s234567", | ||
teacher_surname: "Torchiano", | ||
teacher_email: "[email protected]", | ||
teacher_name: "Marco", | ||
expiration_date: expectedDate.format("YYYY-MM-DD"), | ||
title: "Test proposal" | ||
} | ||
]); | ||
await runCronjob(cronjobNames.THESIS_EXPIRED); | ||
}); | ||
}); |