diff --git a/server/src/mail/removed-cosupervisor.js b/server/src/mail/removed-cosupervisor.js new file mode 100644 index 0000000..47f6b45 --- /dev/null +++ b/server/src/mail/removed-cosupervisor.js @@ -0,0 +1,31 @@ +const removedCosupervisorHtmlTemplate = (variables) => { + return ` + + +

Dear ${variables.name},

+

You have been removed from the list of co-supervisors for the following thesis proposal:

+

Title: ${variables.proposal.title}

+

Description: ${variables.proposal.description.substring(0, 100) + (variables.proposal.description.length > 100 ? '...' : '')}

+

Supervisor: ${variables.proposal.supervisor}

+

Co-supervisors: ${variables.proposal.co_supervisors}

+

Keywords: ${variables.proposal.keywords}

+

Groups: ${variables.proposal.groups}

+

Types: ${variables.proposal.types}

+

Level: ${variables.proposal.level}

+

CDS: ${variables.proposal.cds}

+

Expiration date: ${variables.proposal.expiration_date}

+

Best regards,

+

the Thesis Managment system

+ + + `; +}; + +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) +}); \ No newline at end of file diff --git a/server/src/routes.js b/server/src/routes.js index 516d421..f116602 100644 --- a/server/src/routes.js +++ b/server/src/routes.js @@ -49,6 +49,7 @@ const { getRequestsForStudent, isAccepted, getAcceptedProposal, + notifyRemovedCosupervisors, } = require("./theses-dao"); const { getUser } = require("./user-dao"); @@ -798,7 +799,7 @@ router.put( }); } validateProposal(res, req.body, user); - updateProposal({ + const newProposal = { proposal_id: proposal_id, title: title, supervisor: user.id, @@ -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" }); diff --git a/server/src/theses-dao.js b/server/src/theses-dao.js index 9a29740..d7dab69 100644 --- a/server/src/theses-dao.js +++ b/server/src/theses-dao.js @@ -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 @@ -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,