Skip to content

Commit

Permalink
feat: notify removed cosupervisors
Browse files Browse the repository at this point in the history
  • Loading branch information
lennygir committed Jan 15, 2024
1 parent 1ba683f commit 2e59d3a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
31 changes: 31 additions & 0 deletions server/src/mail/removed-cosupervisor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const removedCosupervisorHtmlTemplate = (variables) => {
return `
<html>
<body>
<p>Dear ${variables.name},</p>
<p>You have been removed from the list of co-supervisors for the following thesis proposal:</p>
<p>Title: ${variables.proposal.title}</p>
<p>Description: ${variables.proposal.description.substring(0, 100) + (variables.proposal.description.length > 100 ? '...' : '')}</p>
<p>Supervisor: ${variables.proposal.supervisor}</p>
<p>Co-supervisors: ${variables.proposal.co_supervisors}</p>
<p>Keywords: ${variables.proposal.keywords}</p>
<p>Groups: ${variables.proposal.groups}</p>
<p>Types: ${variables.proposal.types}</p>
<p>Level: ${variables.proposal.level}</p>
<p>CDS: ${variables.proposal.cds}</p>
<p>Expiration date: ${variables.proposal.expiration_date}</p>
<p>Best regards,</p>
<p>the Thesis Managment system</p>
</body>
</html>
`;
};

const removedCosupervisorTextTemplate = (variables) => {
return `Dear ${variables.name}, You have been removed from the list of co-supervisors for the following thesis proposal:\nTitle: ${variables.proposal.title}\nDescription: ${variables.proposal.description.substring(0, 100) + (variables.proposal.description.length > 100 ? '...' : '')}\nSupervisor: ${variables.proposal.supervisor}\nCo-supervisors: ${variables.proposal.co_supervisors}\nKeywords: ${variables.proposal.keywords}\nGroups: ${variables.proposal.groups}\nTypes: ${variables.proposal.types}\nLevel: ${variables.proposal.level}\nCDS: ${variables.proposal.cds}\nExpiration date: ${variables.proposal.expiration_date}\nBest regards,\nthe Thesis Managment system\n`;
};

exports.removedCosupervisorTemplate = (variables) => ({
html: removedCosupervisorHtmlTemplate(variables),
text: removedCosupervisorTextTemplate(variables)
});
7 changes: 5 additions & 2 deletions server/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const {
getRequestsForStudent,
isAccepted,
getAcceptedProposal,
notifyRemovedCosupervisors,
} = require("./theses-dao");
const { getUser } = require("./user-dao");

Expand Down Expand Up @@ -798,7 +799,7 @@ router.put(
});
}
validateProposal(res, req.body, user);
updateProposal({
const newProposal = {
proposal_id: proposal_id,
title: title,
supervisor: user.id,
Expand All @@ -812,7 +813,9 @@ router.put(
expiration_date: dayjs(expiration_date).format("YYYY-MM-DD"),
level: level,
cds: cds,
});
};
notifyRemovedCosupervisors(proposal, newProposal);
updateProposal(newProposal);
return res.status(200).json({ message: "Proposal updated successfully" });
} catch (e) {
return res.status(500).json({ message: "Internal server error" });
Expand Down
35 changes: 35 additions & 0 deletions server/src/theses-dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { newApplicationTemplate } = require("./mail/new-application");
const { supervisorStartRequestTemplate } = require("./mail/supervisor-start-request");
const { cosupervisorApplicationDecisionTemplate } = require("./mail/cosupervisor-application-decision");
const { cosupervisorStartRequestTemplate } = require("./mail/cosupervisor-start-request");
const { removedCosupervisorTemplate } = require("./mail/removed-cosupervisor");

exports.insertApplication = (proposal, student, state) => {
const result = db
Expand Down Expand Up @@ -575,6 +576,40 @@ exports.isAccepted = (proposal_id, student_id) => {
return accepted_proposal !== undefined;
};

exports.notifyRemovedCosupervisors = async (oldProposal, newProposal) => {
const oldCosupervisors = oldProposal.co_supervisors.split(", ");
const newCosupervisors = newProposal.co_supervisors.split(", ");
const removedCosupervisors = oldCosupervisors.filter((cosupervisor) => {
return !newCosupervisors.includes(cosupervisor);
});
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,
});
} 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.updateProposal = (proposal) => {
const {
proposal_id,
Expand Down

0 comments on commit 2e59d3a

Please sign in to comment.