Skip to content

Commit

Permalink
Merge 56c7752 into 171ef21
Browse files Browse the repository at this point in the history
  • Loading branch information
Daramola98 committed Jun 27, 2018
2 parents 171ef21 + 56c7752 commit c1a348b
Show file tree
Hide file tree
Showing 33 changed files with 4,831 additions and 1,474 deletions.
204 changes: 199 additions & 5 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ exports.signUp = (req, res) => {
sendgridMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: newUser.email,
from: 'noreply@asgardcfh.com',
from: 'noreply@asgard_cfh.com',
subject: 'CFH EMAIL VERIFICATION',
text: `Hello ${newUser.name} Welcome to Card for Humanity, please kindly click ${emailVerificationURL}/activate/${temporaryToken}>here to complete your registration process`,
html: `Hello <strong>${newUser.name}</strong><br><br> Welcome to Card for Humanity, please kindly click the link to complete your activation:<br><br><a href=${emailVerificationURL}/activate/${temporaryToken}>emailVerificationURL/activate/</a>`,
Expand All @@ -180,13 +180,14 @@ exports.signUp = (req, res) => {
if (err) return err;
return res.status(201).send({
message: 'Signed up successfully, please check email for activation link',
token: temporaryToken
token: temporaryToken,
email
});
});
});
} else {
return res.status(409).send({
message: 'this email is in use already',
message: 'This email is in use already',
success: false
});
}
Expand Down Expand Up @@ -312,7 +313,7 @@ exports.avatars = (req, res) => {
return res.redirect('/#!/app');
};

exports.addDonation = (req, res) => {
exports.addDonations = (req, res) => {
if (req.body && req.user && req.user._id) {
// Verify that the object contains crowdrise data
if (req.body.amount && req.body.crowdrise_donation_id && req.body.donor_name) {
Expand All @@ -338,6 +339,41 @@ exports.addDonation = (req, res) => {
res.send();
};

exports.addDonation = (req, res) => {
if (req.body && req.user && req.user._id) {
// Verify that the object contains crowdrise data
if (req.body.amount && req.body.crowdrise_donation_id && req.body.donor_name) {
User.findOne({
_id: req.user._id
})
.exec((err, user) => {
// Confirm that this object hasn't already been entered
let duplicate = false;
for (let i = 0; i < user.donations.length; i++) {
if (user.donations[i].crowdrise_donation_id === req.body.crowdrise_donation_id) {
duplicate = true;
}
}
if (!duplicate) {
let donations = 0;
user.donations.push(req.body);
user.donations.map((donation) => {
donations += donation.amount;
});

if (donations < 50) {
user.premium = 1;
} else {
user.premium = 2;
}
user.save();
}
});
}
}
res.send();
};

/**
* Show profile
* @param {object} req
Expand Down Expand Up @@ -394,7 +430,7 @@ exports.invite = (req, res) => {
const { recieverEmail, gameURL } = req.body;
const { name } = req;
const msg = {
from: 'cfh@andela.com',
from: 'noreply@asgard_cfh.com',
to: recieverEmail,
subject: `${name} is inviting you to join a game`,
html: `<h1>Cards For Humanity Asgard</h1><p>${name} is inviting you to join this game ${gameURL}</p>`
Expand All @@ -414,6 +450,17 @@ exports.invite = (req, res) => {
});
};

/**
* @description - Generate Donations info for users
*
* @param {object} req - request
*
* @param {object} res - response
*
* @return {Object} - Success message
*
* ROUTE: POST: /api/search
*/
exports.searchUser = (req, res) => {
const { term } = req.body;
const escapeRegex = term.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
Expand Down Expand Up @@ -448,6 +495,139 @@ exports.searchUser = (req, res) => {
});
};

/**
* @description - Generate Donations info for users
*
* @param {object} req - request
*
* @param {object} res - response
*
* @return {Object} - Success message
*
* ROUTE: POST: /invite-friend
*/
exports.friendInvite = ((req, res) => {
const { email, name } = req.body;
const senderName = req.name; // the name coming from the token
const senderEmail = req.email;

User.findOneAndUpdate(
{ email: senderEmail },
{ $push: { outgoingInvitation: { email, name } } }
).then(() => {
}).catch(() => res.status(400).json({
message: 'could not send request'
}));

User.findOneAndUpdate(
{ email },
{ $push: { incomingInvitation: { senderEmail, senderName } } }
).then(() => {
}).catch(error => res.status(400).json({
message: 'could not send request'
}));

return res.status(200).json({
message: 'Friend Invite sent successfully'
});
});

/**
* @description - Generate Donations info for users
*
* @param {object} req - request
*
* @param {object} res - response
*
* @return {Object} - Success message
*
* ROUTE: POST: /accept-friend-invite
*/
exports.acceptFriend = ((req, res) => {
const { acceptEmail, acceptName } = req.body;
const { email, name } = req;

// check if user email exists in the incoming array.
// remove user from the incoming array
// add user to the friends array from both sides.
User.findOneAndUpdate(
{ email },
{
$pull: { incomingInvitation: { senderEmail: acceptEmail, senderName: acceptName } },
$push: { friends: { acceptEmail, acceptName } }
},

).then(() => {
}).catch(() => res.status(400).json({
message: 'could not send friend invite'
}));

// remove from the ooutgoing array too in the other users array
// add user to the friends array from both sides.
User.findOneAndUpdate(
{ email: acceptEmail },
{
$pull: { outgoingInvitation: { email, name } },
$push: { friends: { email, name } }
},
).then(() => {
}).catch(() => res.status(400).json({
message: 'could not send friend invite'
}));

return res.status(200).json({
message: `${acceptEmail} has been added to your friends list. `
});
});


/**
* @description - Generate Donations info for users
*
* @param {object} req - request
*
* @param {object} res - response
*
* @return {Object} - Success message
*
* ROUTE: POST: /accept-friend-invite
*/
exports.rejectFriend = ((req, res) => {
const { rejectEmail, rejectName } = req.body;
const { email, name } = req;

// check if user email exists in the incoming array.
// remove user from the incoming array
// add user to the friends array from both sides.
User.findOneAndUpdate(
{ email },
{
$pull: { incomingInvitation: { senderEmail: rejectEmail, senderName: rejectName } },
},

).then(() => {
}).catch(() => res.status(400).json({
message: 'could not send friend invite'
}));

// remove from the ooutgoing array too in the other users array
// add user to the friends array from both sides.
User.findOneAndUpdate(
{ email: rejectEmail },
{
$pull: { outgoingInvitation: { email, name } },
},
).then(() => {
}).catch(() => res.status(400).json({
message: 'could not send friend invite'
}));

return res.status(200).json({
message: `${rejectEmail}'s friend request has been rejected. `
});
});


/**
* @description - Generate Donations info for users
*
Expand All @@ -472,6 +652,17 @@ exports.getDonations = (req, res) => {
});
};

/**
* @description - Generate Donations info for users
*
* @param {object} req - request
*
* @param {object} res - response
*
* @return {Object} - Success message
*
* ROUTE: POST: /api/profile/:id
*/
exports.profile = (req, res) => {
const { id } = req.params;
const details = {};
Expand All @@ -487,6 +678,9 @@ exports.profile = (req, res) => {
details.email = user.email;
details.name = user.name;
details.username = user.username;
details.incomingInvitation = user.incomingInvitation;
details.outgoingInvitation = user.outgoingInvitation;
details.friends = user.friends;
details.image = user.profileImage;
details.gamesWon = gamesWon.length;
Game.find().exec((err, games) => {
Expand Down
5 changes: 4 additions & 1 deletion app/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const UserSchema = new Schema({
github: {},
google: {},
active: { type: Boolean, required: true, default: false },
outgoingInvitation: [],
incomingInvitation: [],
friends: []
});

/**
Expand Down Expand Up @@ -97,7 +100,7 @@ UserSchema.methods = {
if (!plainText || !this.hashed_password) {
return false;
}
return bcrypt.compareSync(plainText,this.hashed_password);
return bcrypt.compareSync(plainText, this.hashed_password);
},

/**
Expand Down
1 change: 1 addition & 0 deletions config/middlewares/isLoggedIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ exports.isLoggedIn = (req, res, next) => {
});
}
req.name = decoded.name;
req.email = decoded.email;
return next();
});
};
9 changes: 9 additions & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,13 @@ module.exports = function (app, passport) { //eslint-disable-line

// Search
app.post('/api/search', users.searchUser);

// send friends invitation
app.post('/api/invite-friend', middleWare.isLoggedIn, users.friendInvite);

// accept friends invitation
app.post('/api/accept-friend-invite', middleWare.isLoggedIn, users.acceptFriend);

// reject friends invitation
app.post('/api/reject-friend-invite', middleWare.isLoggedIn, users.rejectFriend);
};
2 changes: 1 addition & 1 deletion config/socket/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,4 @@ module.exports = function(io) {
socket.leave(socket.gameID);
};

};
};
79 changes: 79 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Karma configuration
// Generated on Wed Jun 20 2018 13:22:00 GMT+0100 (WAT)

module.exports = (config) => {
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],


// list of files / patterns to load in the browser
files: [
'node_modules/angular/angular.js',
'node_modules/angular-ui-router/release/angular-ui-router.js',
'node_modules/angular-mocks/angular-mocks.js',
'public/lib/angular-bootstrap/ui-bootstrap.js',
'node_modules/socket.io-client/dist/socket.io.js',
'public/js/app.js',
'public/js/***/*.js',
'public/js/controllers/*.js',
'public/js/services/*.js',
'public/test/***/*.js',
],


// list of files / patterns to exclude
exclude: [
],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR
// || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,

// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
});
};
Loading

0 comments on commit c1a348b

Please sign in to comment.