Skip to content

Commit

Permalink
Merge pull request #126 from lennygir/feat/198-notify-added-cosup
Browse files Browse the repository at this point in the history
feat: notify added cosupervisors
  • Loading branch information
valerianoCarlos authored Jan 17, 2024
2 parents 41dd1ab + a032e60 commit 3b4bc2f
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 28 deletions.
23 changes: 23 additions & 0 deletions server/src/mail/added-cosupervisor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const addedCosupervisorHtmlTemplate = (variables) => {
return `
<html>
<body>
<p>Dear ${variables.name},</p>
<p>You have been added to the list of co-supervisors for the following thesis proposal:</p>
<p>Title: ${variables.proposal.title}</p>
<p>Consult the list of proposals to have more details.</p>
<p>Best regards,</p>
<p>the Thesis Managment system</p>
</body>
</html>
`;
};

const addedCosupervisorTextTemplate = (variables) => {
return `Dear ${variables.name}, You have been added to the list of co-supervisors for the following thesis proposal:\nTitle: ${variables.proposal.title}\nConsult the list of proposals to have more details.\nBest regards,\nthe Thesis Managment system\n`;
};

exports.addedCosupervisorTemplate = (variables) => ({
html: addedCosupervisorHtmlTemplate(variables),
text: addedCosupervisorTextTemplate(variables)
});
2 changes: 2 additions & 0 deletions server/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const {
getStartedThesisRequest,
cancelPendingApplicationsOfStudent,
notifyRemovedCosupervisors,
notifyAddedCosupervisors,
} = require("./theses-dao");
const { getUser } = require("./user-dao");
const { runCronjob, cronjobNames } = require("./cronjobs");
Expand Down Expand Up @@ -841,6 +842,7 @@ router.put(
cds: cds,
};
await notifyRemovedCosupervisors(proposal, newProposal);
await notifyAddedCosupervisors(proposal, newProposal);
updateProposal(newProposal);
return res.status(200).json({ message: "Proposal updated successfully" });
} catch (e) {
Expand Down
99 changes: 72 additions & 27 deletions server/src/theses-dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { supervisorStartRequestTemplate } = require("./mail/supervisor-start-requ
const { cosupervisorApplicationDecisionTemplate } = require("./mail/cosupervisor-application-decision");
const { cosupervisorStartRequestTemplate } = require("./mail/cosupervisor-start-request");
const { removedCosupervisorTemplate } = require("./mail/removed-cosupervisor");
const { addedCosupervisorTemplate } = require("./mail/added-cosupervisor");

exports.insertApplication = (proposal, student, state) => {
const result = db
Expand Down Expand Up @@ -481,7 +482,7 @@ exports.notifyProposalExpiration = async (proposal) => {

// Save email in DB
db.prepare(
"INSERT INTO NOTIFICATIONS(teacher_id, object, content) VALUES(?,?,?)",
"INSERT INTO NOTIFICATIONS(teacher_id, object, content, date) VALUES(?,?,?, DATETIME(DATETIME('now'), '+' || (select delta from VIRTUAL_CLOCK where id = 1) || ' days'))",
).run(proposal.supervisor, "Your proposal expires in 7 days", mailBody.text);
};

Expand Down Expand Up @@ -650,38 +651,82 @@ exports.isAccepted = (proposal_id, student_id) => {
return accepted_proposal !== undefined;
};

function getCosupervisorsFromProposal(proposal) {
return proposal.co_supervisors ? proposal.co_supervisors.split(", ") : [];
}

function getArrayDifference(arrayIn, arrayNotIn) {
return arrayIn.filter((el) => !arrayNotIn.includes(el));
}

exports.notifyRemovedCosupervisors = async (oldProposal, newProposal) => {
const oldCosupervisors = (oldProposal.co_supervisors || "").split(", ");
const newCosupervisors = (newProposal.co_supervisors || []);
const oldCosupervisors = getCosupervisorsFromProposal(oldProposal);
const newCosupervisors = getCosupervisorsFromProposal(newProposal);
if(oldCosupervisors && newCosupervisors) {
const removedCosupervisors = oldCosupervisors.filter((cosupervisor) => {
return !newCosupervisors.includes(cosupervisor);
});
const removedCosupervisors = getArrayDifference(oldCosupervisors, newCosupervisors);
for(let cosupervisorEmail of removedCosupervisors) {
const teacher = this.getTeacherByEmail(cosupervisorEmail);
// -- Email
const mailBody = removedCosupervisorTemplate({
name: teacher.surname + " " + teacher.name,
proposal: newProposal
});
try {
await nodemailer.sendMail({
to: cosupervisorEmail,
subject: "You have been removed from a thesis proposal",
text: mailBody.text,
html: mailBody.html,
if(teacher) {
// -- Email
const mailBody = removedCosupervisorTemplate({
name: teacher.surname + " " + teacher.name,
proposal: newProposal
});
} catch (e) {
console.log("[mail service]", e);
try {
await nodemailer.sendMail({
to: cosupervisorEmail,
subject: "You have been removed from a thesis proposal",
text: mailBody.text,
html: mailBody.html,
});
} catch (e) {
console.log("[mail service]", e);
}
// -- Website notification
db.prepare(
"INSERT INTO NOTIFICATIONS(teacher_id, object, content) VALUES(?,?,?)",
).run(
teacher.id,
"You have been removed from a thesis proposal",
mailBody.text,
);
}
}
}
};

exports.notifyAddedCosupervisors = async (oldProposal, newProposal) => {
const oldCosupervisors = getCosupervisorsFromProposal(oldProposal);
const newCosupervisors = getCosupervisorsFromProposal(newProposal);
if(oldCosupervisors && newCosupervisors) {
const addedCosupervisors = getArrayDifference(newCosupervisors, oldCosupervisors);
for(let cosupervisorEmail of addedCosupervisors) {
const teacher = this.getTeacherByEmail(cosupervisorEmail);
if(teacher) {
// -- Email
const mailBody = addedCosupervisorTemplate({
name: teacher.surname + " " + teacher.name,
proposal: newProposal
});
try {
await nodemailer.sendMail({
to: cosupervisorEmail,
subject: "You have been added to a thesis proposal",
text: mailBody.text,
html: mailBody.html,
});
} catch (e) {
console.log("[mail service]", e);
}
// -- Website notification
db.prepare(
"INSERT INTO NOTIFICATIONS(teacher_id, object, content) VALUES(?,?,?)",
).run(
teacher.id,
"You have been added to a thesis proposal",
mailBody.text,
);
}
// -- Website notification
db.prepare(
"INSERT INTO NOTIFICATIONS(teacher_id, object, content) VALUES(?,?,?)",
).run(
teacher.id,
"You have been removed from a thesis proposal",
mailBody.text,
);
}
}
};
Expand Down
6 changes: 5 additions & 1 deletion server/tests/proposal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const {
getRequestsForClerk,
getProposalsThatExpireInXDays,
notifyRemovedCosupervisors,
notifyAddedCosupervisors,
} = require("../src/theses-dao");

const dayjs = require("dayjs");
Expand Down Expand Up @@ -969,6 +970,7 @@ describe("PUT /api/proposals/:id", () => {

const originalModule = jest.requireActual("../src/theses-dao");
notifyRemovedCosupervisors.mockImplementation(originalModule.notifyRemovedCosupervisors);
notifyAddedCosupervisors.mockImplementation(originalModule.notifyAddedCosupervisors);

isLoggedIn.mockImplementation((req, res, next) => {
req.user = {
Expand Down Expand Up @@ -999,7 +1001,9 @@ describe("PUT /api/proposals/:id", () => {
id: 1,
title: "test",
description: "desc test",
co_supervisors: [],
co_supervisors: [
"[email protected]"
],
keywords: ["keyword1", "keyword2"],
groups: [],
types: ["EXPERIMENTAL"],
Expand Down

0 comments on commit 3b4bc2f

Please sign in to comment.