Skip to content

Commit

Permalink
Merge pull request #127 from lennygir/fix/203-improve-fix-tests
Browse files Browse the repository at this point in the history
fix/203-improve-fix-tests
  • Loading branch information
s317743 authored Jan 17, 2024
2 parents 78b4d32 + 62a669f commit 889b96f
Show file tree
Hide file tree
Showing 17 changed files with 1,254 additions and 1,027 deletions.
48 changes: 28 additions & 20 deletions server/src/cronjobs.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
const { CronJob } = require('cron');
const { getProposalsThatExpireInXDays, notifyProposalExpiration } = require('./theses-dao');
const { CronJob } = require("cron");
const { getProposalsThatExpireInXDays } = require("./dao/proposals");
const { notifyProposalExpiration } = require("./dao/misc");

const cronjobNames = {
THESIS_EXPIRED: 'THESIS_EXPIRED'
THESIS_EXPIRED: "THESIS_EXPIRED",
};

const cronjobs = {};

const initCronjobs = () => {
cronjobs[cronjobNames.THESIS_EXPIRED] = new CronJob("0 0 * * *" , () => {
const proposals = getProposalsThatExpireInXDays(7);
if(proposals) {
proposals.forEach((proposal) => {
notifyProposalExpiration(proposal);
});
}
}, undefined, true, "Europe/Rome", undefined, true);
cronjobs[cronjobNames.THESIS_EXPIRED] = new CronJob(
"0 0 * * *",
() => {
const proposals = getProposalsThatExpireInXDays(7);
if (proposals) {
proposals.forEach((proposal) => {
notifyProposalExpiration(proposal);
});
}
},
undefined,
true,
"Europe/Rome",
undefined,
true,
);
};

const runCronjob = (cronjobName) => {
console.log('[Cron] Running job ' + cronjobName);
if(cronjobs[cronjobName]) {
cronjobs[cronjobName].fireOnTick();
}
console.log("[Cron] Running job " + cronjobName);
if (cronjobs[cronjobName]) {
cronjobs[cronjobName].fireOnTick();
}
};

module.exports = {
cronjobNames,
runCronjob,
initCronjobs
};
cronjobNames,
runCronjob,
initCronjobs,
};
154 changes: 154 additions & 0 deletions server/src/dao/applications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
"use strict";

const { db } = require("../db");

exports.insertApplication = (proposal, student, state) => {
const result = db
.prepare(
"insert into APPLICATIONS(proposal_id, student_id, state) values (?,?,?)",
)
.run(proposal, student, state);
const applicationId = result.lastInsertRowid;
return {
application_id: applicationId,
proposal_id: proposal,
student_id: student,
state: state,
};
};

exports.insertPDFInApplication = (file, applicationId) => {
return db
.prepare(
"update APPLICATIONS set attached_file = ? where main.APPLICATIONS.id = ?",
)
.run(file, applicationId);
};

exports.getApplicationById = (id) => {
return db.prepare("select * from APPLICATIONS where id = ?").get(id);
};

exports.cancelPendingApplications = (of_proposal) => {
db.prepare(
"update APPLICATIONS set state = 'canceled' where proposal_id = ? AND state = 'pending'",
).run(of_proposal);
};

exports.cancelPendingApplicationsOfStudent = (student_id) => {
db.prepare(
"update APPLICATIONS set state = 'canceled' where main.APPLICATIONS.student_id = ? and state = 'pending'",
).run(student_id);
};

exports.updateApplication = (id, state) => {
db.prepare("update APPLICATIONS set state = ? where id = ?").run(state, id);
};

exports.getAcceptedApplicationsOfStudent = (student_id) => {
return db
.prepare(
`select * from APPLICATIONS where student_id = ? and state = 'accepted'`,
)
.all(student_id);
};

exports.getPendingApplicationsOfStudent = (student_id) => {
return db
.prepare(
`select * from APPLICATIONS where student_id = ? and state = 'pending'`,
)
.all(student_id);
};

exports.findRejectedApplication = (proposal_id, student_id) => {
return db
.prepare(
`select * from APPLICATIONS where proposal_id = ? and student_id = ? and state = 'rejected'`,
)
.get(proposal_id, student_id);
};

/**
* @param teacher_id
* @returns {[
* {
* application_id,
* proposal_id,
* teacher_id,
* state,
* student_name,
* student_surname,
* teacher_name,
* teacher_surname
* title
* }
* ]}
*/
exports.getApplicationsOfTeacher = (teacher_id) => {
return db
.prepare(
`select APPLICATIONS.id,
APPLICATIONS.proposal_id,
APPLICATIONS.student_id,
APPLICATIONS.state,
APPLICATIONS.attached_file,
STUDENT.name as student_name,
STUDENT.surname as student_surname,
TEACHER.name as teacher_name,
TEACHER.surname as teacher_surname,
PROPOSALS.title as title
from APPLICATIONS,
PROPOSALS,
STUDENT,
TEACHER
where APPLICATIONS.proposal_id = PROPOSALS.id
and PROPOSALS.supervisor = TEACHER.id
and APPLICATIONS.student_id = STUDENT.id
and PROPOSALS.supervisor = ?`,
)
.all(teacher_id);
};

exports.getApplicationsOfStudent = (student_id) => {
return db
.prepare(
`select APPLICATIONS.id,
APPLICATIONS.proposal_id,
APPLICATIONS.student_id,
APPLICATIONS.state,
APPLICATIONS.attached_file,
STUDENT.name as student_name,
STUDENT.surname as student_surname,
TEACHER.name as teacher_name,
TEACHER.surname as teacher_surname,
PROPOSALS.title as title
from APPLICATIONS,
PROPOSALS,
STUDENT,
TEACHER
where APPLICATIONS.proposal_id = PROPOSALS.id
and PROPOSALS.supervisor = TEACHER.id
and APPLICATIONS.student_id = STUDENT.id
and APPLICATIONS.student_id = ?`,
)
.all(student_id);
};

/**
* Is the given application accepted?
* @param proposal_id the proposal of the application
* @param student_id the student who applied
* @returns {boolean} accepted or not
*/
exports.isAccepted = (proposal_id, student_id) => {
const accepted_proposal = db
.prepare(
`select * from main.APPLICATIONS
where APPLICATIONS.proposal_id = ?
and APPLICATIONS.student_id = ?
and APPLICATIONS.state = 'accepted'`,
)
.get(proposal_id, student_id);
return accepted_proposal !== undefined;
};
13 changes: 13 additions & 0 deletions server/src/dao/dao_utils/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";
function getCosupervisorsFromProposal(proposal) {
return proposal.co_supervisors ? proposal.co_supervisors.split(", ") : [];
}

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

module.exports = {
getCosupervisorsFromProposal,
getArrayDifference,
};
Loading

0 comments on commit 889b96f

Please sign in to comment.