diff --git a/server/src/theses-dao.js b/server/src/theses-dao.js index d7dab69..f420c96 100644 --- a/server/src/theses-dao.js +++ b/server/src/theses-dao.js @@ -577,36 +577,38 @@ exports.isAccepted = (proposal_id, student_id) => { }; 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 + const oldCosupervisors = oldProposal.co_supervisors?.split(", "); + const newCosupervisors = newProposal.co_supervisors?.split(", "); + if(oldCosupervisors && newCosupervisors) { + const removedCosupervisors = oldCosupervisors.filter((cosupervisor) => { + return !newCosupervisors.includes(cosupervisor); }); - try { - await nodemailer.sendMail({ - to: cosupervisorEmail, - subject: "You have been removed from a thesis proposal", - text: mailBody.text, - html: mailBody.html, + for(let cosupervisorEmail of removedCosupervisors) { + const teacher = this.getTeacherByEmail(cosupervisorEmail); + // -- 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, + ); } - // -- 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, - ); } }; diff --git a/server/tests/proposal.test.js b/server/tests/proposal.test.js index 52d1a07..b0f314d 100644 --- a/server/tests/proposal.test.js +++ b/server/tests/proposal.test.js @@ -942,3 +942,48 @@ describe("GET /api/start-requests", () => { .expect(200); }); }); + +describe("PATCH /api/proposals/:id", () => { + test("Remove a co supervisor", async () => { + isLoggedIn.mockImplementation((req, res, next) => { + req.user = { + email: "maurizio.morisio@teacher.it", + }; + next(); + }); + + const proposal = { + id: 1, + title: "test", + description: "desc test", + supervisor: "s234567", + co_supervisors: [ + "marco.torchiano@teacher.it" + ], + keywords: ["keyword1", "keyword2"], + groups: ["SOFTENG"], + types: ["EXPERIMENTAL"], + required_knowledge: "required knowledge", + notes: "notes", + expiration_date: "2021-01-01", + level: "MSC", + cds: "LM-32 (DM270)", + manually_archived: 0, + deleted: 0 + }; + + getProposal.mockReturnValue(proposal); + + const newProposal = { ...proposal }; + newProposal.co_supervisors = []; + + const requests = ( + await request(app) + .put("/api/proposals/1") + .set("Content-Type", "application/json") + .send(newProposal) + .expect(200) + ); + expect(requests.body).toEqual({ message: "Proposal updated successfully" }); + }); +});