From ae60305e70735eb6b1b08481c10498c87b5cf7ea Mon Sep 17 00:00:00 2001 From: Michel Wohlert Date: Tue, 21 Feb 2017 23:01:23 +0100 Subject: [PATCH] started implementing #12 --- .../api/contestant/contestantApiController.js | 30 ++++++-- .../api/student/studentApiController.js | 21 +++++- src/server/helper/contestantHelper.js | 69 ++++++++----------- 3 files changed, 71 insertions(+), 49 deletions(-) diff --git a/src/server/controller/api/contestant/contestantApiController.js b/src/server/controller/api/contestant/contestantApiController.js index 8f1c5b3..77042ad 100644 --- a/src/server/controller/api/contestant/contestantApiController.js +++ b/src/server/controller/api/contestant/contestantApiController.js @@ -20,6 +20,26 @@ module.exports = class ContestantApiController { static save(request, response, next) { // TODO: check if strings are empty + + + if (request.body.firstName === undefined || request.body.lastName === undefined) { + return response.status(400).json({success: false, + error: {text: 'Es wurden nicht alle notwendingen Felder ausgefüllt'}}); + } + + StudentApiController.unique(request.body.firstName, request.body.lastName, (result) => { + if (result === false) { + return response.status(200).json({ + success: false, + error: {text: 'not_unique'} + }); + } else { + + } + }); + + + if (request.body.firstName === undefined || request.body.lastName === undefined || request.body.course === undefined || request.body.year === undefined || request.body.description === undefined || request.file === undefined) { return response.status(400).json({success: false, @@ -43,19 +63,19 @@ module.exports = class ContestantApiController { return response.status(200).json({success: false, error: {text: 'Du hast dich bereits aufgestellt.'}}); } - StudentApiController.validate(request.body, (validated) => { + StudentApiController.validate(request.body, (validated, student) => { if (validated === true) { contestantJSON.activated = false; contestantJSON.image = request.file.filename; // sanitize user inputs contestantJSON.firstName = xss(contestantJSON.firstName); contestantJSON.lastName = xss(contestantJSON.lastName); - contestantJSON.course = xss(contestantJSON.course); - contestantJSON.year = xss(contestantJSON.year); - contestantJSON.centuria = ''; + contestantJSON.course = student.course; + contestantJSON.year = student.year; + contestantJSON.centuria = student.centuria; contestantJSON.description = xss(contestantJSON.description); contestantJSON.token = ''; - ContestantHelper.sendActivationMail(contestantJSON, (result) => { + ContestantHelper.sendActivationMail(contestantJSON, student, (result) => { if (result === false) { return response.status(200).json({ success: false, diff --git a/src/server/controller/api/student/studentApiController.js b/src/server/controller/api/student/studentApiController.js index ad8b122..94fcb6e 100644 --- a/src/server/controller/api/student/studentApiController.js +++ b/src/server/controller/api/student/studentApiController.js @@ -22,11 +22,26 @@ module.exports = class StudentApiController { } if (students.length > 1) { - return next(false); + return next(false, null); } else if (students.length === 1) { - return next(true); + return next(true, students); + } + return next(false, null); + }); + } + + static unique(firstName, lastName, callback) { + Student.count({firstName: {$regex: StudentApiController.buildNameRegex(firstName), + $options: 'g'}, + lastName}).exec((error, count) => { + if (error) { + callback(false); + } + if (count === 1) { + callback(true); + } else { + callback(false); } - return next(false); }); } diff --git a/src/server/helper/contestantHelper.js b/src/server/helper/contestantHelper.js index a20f1e8..135241b 100644 --- a/src/server/helper/contestantHelper.js +++ b/src/server/helper/contestantHelper.js @@ -9,47 +9,34 @@ const pug = require('pug'); module.exports = class ContestantHelper { - static sendActivationMail(contestantJSON, callback) { - Student.findOne({firstName: {$regex: ContestantHelper.buildNameRegex(contestantJSON.firstName), - $options: 'g'}, - lastName: contestantJSON.lastName, - year: contestantJSON.year, - course: contestantJSON.course}).exec((error, student) => { - if (error) { - return callback(error); - } - if (student === null) { - return callback(false); - } - - const token = uuid(); - const data = {}; - data.to = student.email; - data.subject = config.get('mailer:contestantSubject'); - data.template = {}; - data.template.name = 'contestantConfirm'; - data.template.replace = []; - data.template.replace.push({placeholder: 'name', - value: student.firstName}); - data.template.replace.push({placeholder: 'link', - value: `${config.get('webserver:defaultProtocol')}://${config.get('webserver:url')}/api/contestants/activate?token=${token}`}); - - contestantJSON.token = token; - contestantJSON.centuria = student.centuria; - - const contestant = new Contestant(contestantJSON); - contestant.save((error2) => { - console.log(error2); - if (error2) { - return callback(false); - } - return Mailer.sendMailWithTemplate(data); - }).then((result) => { - return callback(true); - }, (err) => { - return callback(false); - }); - }); + static sendActivationMail(contestantJSON, student, callback) { + const token = uuid(); + const data = {}; + data.to = student.email; + data.subject = config.get('mailer:contestantSubject'); + data.template = {}; + data.template.name = 'contestantConfirm'; + data.template.replace = []; + data.template.replace.push({placeholder: 'name', + value: student.firstName}); + data.template.replace.push({placeholder: 'link', + value: `${config.get('webserver:defaultProtocol')}://${config.get('webserver:url')}/api/contestants/activate?token=${token}`}); + + contestantJSON.token = token; + contestantJSON.centuria = student.centuria; + + const contestant = new Contestant(contestantJSON); + contestant.save((error2) => { + console.log(error2); + if (error2) { + return callback(false); + } + return Mailer.sendMailWithTemplate(data); + }).then((result) => { + return callback(true); + }, (err) => { + return callback(false); + }); } static buildNameRegex(name) {