Skip to content

Commit

Permalink
started implementing #12
Browse files Browse the repository at this point in the history
  • Loading branch information
Michel Wohlert committed Feb 21, 2017
1 parent b067e08 commit ae60305
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 49 deletions.
30 changes: 25 additions & 5 deletions src/server/controller/api/contestant/contestantApiController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
21 changes: 18 additions & 3 deletions src/server/controller/api/student/studentApiController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down
69 changes: 28 additions & 41 deletions src/server/helper/contestantHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ae60305

Please sign in to comment.