Check which team user is in

This commit is contained in:
Alicia Sykes
2018-01-27 14:47:01 +00:00
parent 141c4e257a
commit 71ad74d00c
2 changed files with 49 additions and 13 deletions

View File

@@ -5,7 +5,6 @@ export default class SaveResponseController {
static saveResponse(req, res) {
let _response = req.body;
console.log("===============>", _response);
TeamRecordModel
.insertUserResponse(_response)
.then(data => res.status(201).json(data))

View File

@@ -15,18 +15,25 @@ class ResponseSaver {
errorMessage = "Malformed input. Must be valid JSON.";
return reject(new TypeError(errorMessage));
}
// TODO check if userhash is part of a valid team
TeamMembersModel.find({ }, function(err, teams) {
teams.forEach((team)=>{
let teamName = team.teamName;
team.members.forEach((member)=>{
console.log(EmailAddressHasher.checkEmailAgainstHash(member.email));
if(EmailAddressHasher.checkEmailAgainstHash(member.email)){
console.log('user found, in team '+teamName);
}
})
})
});
/* Put input into valid format */
// userResponse = this.putInputIntoValidFormat(userResponse);
/* Check if user is part of a valid team*/
ResponseSaver.checkIfUserFoundInTeam( userResponse.emailHash,
(teamName)=> {
if(!teamName){ // User was not found in any team :(
errorMessage = "User cold not be found.";
return reject(new TypeError(errorMessage));
}
/* Check that the user has not yet responded already today */
ResponseSaver.checkIfUserAlreadySubittedToday(userResponse.emailHash,
()=>{
console.log("Ready for the next stage....")
})
});
// TODO check user has not already responded today
@@ -70,6 +77,36 @@ class ResponseSaver {
}
}
/**
* If input was passed as a string, quickly convert it to an object
* @param input
* @returns {*}
*/
static putInputIntoValidFormat (input) {
if (typeof input === 'string' || input instanceof String) {
return JSON.parse(input);
}
return input;
}
static checkIfUserFoundInTeam(userHash, cb) {
TeamMembersModel.find({}, (err, teams) => {
teams.forEach((team) => {
let teamName = team.teamName;
team.members.forEach((member) => {
if (EmailAddressHasher.checkEmailAgainstHash(member.email, userHash)) {
cb(teamName);
}
})
});
cb(null);
});
}
static checkIfUserAlreadySubittedToday(userHash, cb){
cb()
}
}
module.exports = ResponseSaver;