Skip to content

Commit

Permalink
fix: test and empty co-supervisors
Browse files Browse the repository at this point in the history
  • Loading branch information
lennygir committed Jan 15, 2024
1 parent 2e59d3a commit bd099da
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 27 deletions.
56 changes: 29 additions & 27 deletions server/src/theses-dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
};

Expand Down
45 changes: 45 additions & 0 deletions server/tests/proposal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]",
};
next();
});

const proposal = {
id: 1,
title: "test",
description: "desc test",
supervisor: "s234567",
co_supervisors: [
"[email protected]"
],
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" });
});
});

0 comments on commit bd099da

Please sign in to comment.