Skip to content
This repository has been archived by the owner on Sep 8, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:ultra-hyper-storm-ohtuprojekti/gr…
Browse files Browse the repository at this point in the history
…appa-backend
  • Loading branch information
Mikael Wide committed May 6, 2016
2 parents acf3ced + 6db61dd commit 39bf840
Show file tree
Hide file tree
Showing 21 changed files with 278 additions and 257 deletions.
2 changes: 1 addition & 1 deletion controllers/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const Thesis = require("../models/thesis");
*@param {Object} req - The build of req.body being {thesis[], "professor/student"}
*/
module.exports.sendReminder = (req, res) => {
if (req.body.type === "professor"){
if (req.body.type === "professor") {
Reminder.sendProfessorReminder(req.body.thesis)
.then(result => {
res.status(200).send({
Expand Down
2 changes: 1 addition & 1 deletion controllers/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ router.get("/auth", auth.authenticate, authTest);
router.get("/thesis", auth.authenticate, thesisCtrl.findAllByUserRole);
// router.get("/thesis", thesisCtrl.findAll);
router.put("/thesis/:id", thesisCtrl.updateOne);
router.post("/thesis", thesisCtrl.saveOne);
router.post("/thesis", auth.authenticate, thesisCtrl.saveOne);
router.get("/thesis/:id", thesisCtrl.findOne);
router.delete("/thesis/:id", thesisCtrl.deleteOne);
router.post("/thesis/ethesis", thesisCtrl.updateOneWithEthesis);
Expand Down
91 changes: 48 additions & 43 deletions controllers/thesis.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,20 @@ module.exports.createAllPdfs = (req, res) => {
}
else {
let docStream = pdfCreator.generateThesesDocs(req.body.thesesToPrint);
console.log(req.body);
Thesis
.findAll()
.then(theses => {
let docStream = pdfCreator.generateThesesDocs(theses, req.body.thesesToPrint);

docStream.on('data', data => {
res.write(data);
});
docStream.on("data", data => {
res.write(data);
});

docStream.on('end', () => {
res.end();
});
docStream.on("end", () => {
res.end();
});
})
}
};

Expand All @@ -79,11 +85,11 @@ module.exports.createPdf = (req, res) => {
.then(thesis => {
let docStream = pdfCreator.generateOneThesisDoc(thesis);

docStream.on('data', data => {
docStream.on("data", data => {
res.write(data);
});

docStream.on('end', () => {
docStream.on("end", () => {
res.end();
});
})
Expand All @@ -100,20 +106,20 @@ module.exports.createPdf = (req, res) => {
* request is in form { token: "ABC123", thesis: { ethesis: "link.com" } }
*/
module.exports.updateOneWithEthesis = (req, res) => {
Thesis
.update(req.body.thesis, { id: tokenGen.decodeEthesisToken(req.body.token).thesisId })
.then(thesis => {
Thesis
.update(req.body.thesis, { id: tokenGen.decodeEthesisToken(req.body.token).thesisId })
.then(thesis => {
res.status(200).send(thesis);
})
.catch(err => {
.catch(err => {
res.status(500).send({
message: "Thesis update produced an error",
error: err,
});
});
};
};

module.exports.updateOne = (req, res) => {
module.exports.updateOne = (req, res) => {
Thesis
.update(req.body, { id: req.params.id })
.then(thesis => {
Expand Down Expand Up @@ -153,25 +159,24 @@ module.exports.deleteOne = (req, res) => {
* author, email, deadline, graders?, and stuff?
*/
module.exports.saveOne = (req, res) => {
let savedthesis;
let foundCouncilMeeting;
const originalDate = new Date(req.body.deadline);

CouncilMeeting
.findOne({ date: originalDate })
.then(cm => {
let savedthesis;
let foundCouncilMeeting;
console.log(req.body);
CouncilMeeting
.findOne({ id: req.body.CouncilMeetingId })
.then(cm => {
if (cm === null) {
throw new TypeError("ValidationError: unvalid deadline, no such CouncilMeeting found");
throw new TypeError("ValidationError: Unvalid CouncilMeetingId, no such CouncilMeeting found");
} else {
foundCouncilMeeting = cm;
if (typeof req.body.graders === "undefined") {
if (req.body.graders === undefined) {
return;
}
return Promise.all(req.body.graders.map(grader => Grader.findOrCreate(grader)));
}
})
.then(() => Thesis.saveOne(req.body))
.then(thesis => {
.then(() => Thesis.saveOne(req.body, foundCouncilMeeting))
.then(thesis => {
savedthesis = thesis;
const token = tokenGen.generateEthesisToken(thesis.author, thesis.id);
return Promise.all([
Expand All @@ -182,27 +187,27 @@ module.exports.deleteOne = (req, res) => {
}),
Reminder.sendStudentReminder(thesis.email, token, thesis.id),
ThesisProgress.saveFromNewThesis(thesis),
CouncilMeeting.linkThesisToCouncilMeeting(thesis, originalDate),
CouncilMeeting.linkThesisToCouncilMeeting(thesis, req.body.CouncilMeetingId),
Grader.linkThesisToGraders(thesis, req.body.graders),
Thesis.linkStudyField(thesis, req.body.field),
Thesis.addUser(thesis, req),
Thesis.linkStudyField(thesis, req.body.StudyFieldName),
Thesis.addUser(thesis, req.user),
]);
})
.then(() => ThesisProgress.evaluateGraders(savedthesis.id, req.body.graders))
.then(() => {
.then(() => ThesisProgress.evaluateGraders(savedthesis.id, req.body.graders))
.then(() => {
res.status(200).send(savedthesis);
})
.catch(err => {
if (err.message.indexOf("ValidationError") !== -1) {
res.status(400).send({
message: "Thesis saveOne failed validation",
error: err.message,
});
} else {
res.status(500).send({
message: "Thesis saveOne produced an error",
error: err.message,
});
}
});
// .catch(err => {
// if (err.message.indexOf("ValidationError") !== -1) {
// res.status(400).send({
// message: "Thesis saveOne failed validation",
// error: err.message,
// });
// } else {
// res.status(500).send({
// message: "Thesis saveOne produced an error",
// error: err.message,
// });
// }
// });
};
2 changes: 1 addition & 1 deletion controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ module.exports.loginUser = (req, res) => {
message: "Logging in failed authentication",
error: "",
});
} else if (!user.isActive){
} else if (!user.isActive) {
res.status(401).send({
message: "Your account is inactive, please contact admin for activation",
error: "",
Expand Down
1 change: 0 additions & 1 deletion db/db_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const Sequelize = require("sequelize");

const dbUrl = process.env.DATABASE_URL || "postgres://keuoblxbkbspkf:7DOjZx2eHir2SYgemNZo_3EbxI@ec2-54-225-151-64.compute-1.amazonaws.com:5432/d2ts0v8erk5vak";
// console.log("env " + process.env.NODE_ENV);
let seq;
if (process.env.NODE_ENV === "production") {
seq = new Sequelize(dbUrl, {
Expand Down
2 changes: 1 addition & 1 deletion db/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ module.exports.addTestData = () => Promise.all([
deadline: new Date("1 1 2017"),
wasError: true,
})
])
])
// add connections here
.then((createdTables) => {
const graders = createdTables.filter(table => {
Expand Down
27 changes: 5 additions & 22 deletions middleware/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const jwt = require("jwt-simple");
// const secret = process.env.TOKEN_SECRET;
const secret = require("../config/authentication").secret;

/*
/**
* Authentication middleware that is called before any requests
*
* Checks the request for the correct Headers and then decodes
Expand Down Expand Up @@ -61,30 +61,13 @@ module.exports.authenticate = (req, res, next) => {
return res.status(401).send({
message: "User authentication failed",
});
} else if (decoded.created > decoded.expires) {
return res.status(401).send({
message: "Token has expired",
});
} else {
console.log("autentikoitu!");
req.user = decoded.user;
next();
}
};

module.exports.authenticate2 = (req, res, next) => {
if (!req.headers.authorization) {
return res.status(401).send({ message: "Please make sure your request has an Authorization header" });
}
var token = req.headers.authorization.split(" ")[1];

var payload = null;
try {
payload = jwt.decode(token, config.TOKEN_SECRET);
}
catch (err) {
return res.status(401).send({ message: err.message });
}

if (payload.exp <= moment().unix()) {
return res.status(401).send({ message: "Token has expired" });
}
req.user = payload.sub;
next();
};
6 changes: 3 additions & 3 deletions models/base_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BaseModel {
delete(params) {
return this.Models[this.modelname].destroy({ where: params });
}
/*
/**
* Returns all the rows from a table.
*
* Basically SELECT * FROM @this.modelname
Expand All @@ -32,7 +32,7 @@ class BaseModel {
}
return this.Models[this.modelname].findAll();
}
/*
/**
* Creates new instance of table with validated(!) @params.
*
* Kinda like INSERT (@params) INTO @modelname RETURNING ID
Expand All @@ -44,7 +44,7 @@ class BaseModel {
saveOne(params) {
return this.Models[this.modelname].create(params);
}
/*
/**
* Updates a field
*/
update(values, params) {
Expand Down
6 changes: 2 additions & 4 deletions models/councilmeeting.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ class CouncilMeeting extends BaseModel {
constructor() {
super("CouncilMeeting");
}
linkThesisToCouncilMeeting(thesis, date) {
linkThesisToCouncilMeeting(thesis, id) {
return this.getModel()
.findOne({ where: { date: new Date(date) } })
.findOne({ where: { id, } })
.then((CM) => {
// console.log("HEI CM");
// console.log(CM);
return CM.addTheses(thesis);
})
.then(() => {
Expand Down
14 changes: 7 additions & 7 deletions models/grader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class Grader extends BaseModel {

findOrCreate(grader) {
return this.getModel()
.findOne({ where: { name: grader.name, title: grader.title } })
.then((newgrader) => {
if (newgrader === null) {
return this.getModel().create({ name: grader.name, title: grader.title });
}
return newgrader;
});
.findOne({ where: { name: grader.name, title: grader.title } })
.then((newgrader) => {
if (newgrader === null) {
return this.getModel().create({ name: grader.name, title: grader.title });
}
return newgrader;
});
}

linkThesisToGraders(thesis, graders) {
Expand Down
22 changes: 11 additions & 11 deletions models/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ const Thesis = seq.define("Thesis", {
validate: {
notEmpty: true,
isIn: [[
"Approbatur",
"Lubenter Approbatur",
"Non Sine Laude Approbatur",
"Cum Laude Approbatur",
"Magna Cum Laude Approbatur",
"Eximia Cum Laude Approbatur",
"Laudatur",
"Approbatur",
"Lubenter Approbatur",
"Non Sine Laude Approbatur",
"Cum Laude Approbatur",
"Magna Cum Laude Approbatur",
"Eximia Cum Laude Approbatur",
"Laudatur",
]],
},
},
Expand Down Expand Up @@ -139,10 +139,10 @@ const StudyField = seq.define("StudyField", {
validate: {
notEmpty: true,
isIn: [[
"Algorithmic Bioinformatics",
"Algorithms, Data Analytics and Machine Learning",
"Networking and Services",
"Software Systems",
"Algorithmic Bioinformatics",
"Algorithms, Data Analytics and Machine Learning",
"Networking and Services",
"Software Systems",
]],
},
},
Expand Down

0 comments on commit 39bf840

Please sign in to comment.