1 var express = require('express');
  2 var router = express.Router();
  3 var verify = require('./verify');
  4 
  5 /* GET user information. */
  6 
  7 /**
  8 	* Function to handle GET requests for user profile page.
  9 	* Render the profile page, or return a 404 error if the userID is not in the MongoDB database,
 10 	* or redirect to the login page with a 401 status code if the JSON web token does not verify.
 11 	* @deprecated
 12 	* @param {Object} req The express routing HTTP client request object,
 13 	* whose parameters include the userID.
 14 	* @param {Object} res The express routing HTTP client response object.
 15 	* @param {callback} next The express routing callback function to invoke next middleware in the stack.
 16 	* @return {Object} A JSON object that holds req, res, and next.
 17 */
 18 var profileGet = function(req, res, next) {
 19 	var db = req.app.locals.db;
 20 	var userId = parseInt(req.params.userid);
 21 
 22 	db.collection('Users')
 23 		.find({'userId': userId})
 24 		.toArray(function(err, results) {
 25 			if (results.length == 0) {
 26 				res.status(404).send("404: userId not found");
 27 			} else {
 28 				let user = results[0]; // should only be one match
 29 
 30 				let isUser = false;
 31 				if (verify.checkLogin(req.cookies.jwt, user.email)) {
 32 					isUser = true;
 33 				}
 34 
 35 				res.render('profile', {
 36 					userId: userId,
 37 					email: user.email,
 38 					name: user.name,
 39 					rating: calculateRating(user.rating),
 40 					numRatings: user.rating.numRatings,
 41 					activities: user.activities,
 42 					availability: user.availability,
 43 					isUser: isUser
 44 				});
 45 			}
 46 		});
 47 }
 48 
 49 router.get('/:userid', profileGet);
 50 
 51 /**
 52 	Calculates a rating for a user.
 53 	@param {Object} userRatingContainer The container that holds the user ratings.
 54 	@return {number}
 55 */
 56 function calculateRating(userRatingContainer) {
 57 	if (userRatingContainer.numRatings === 0) {
 58 		return 0;
 59 	}
 60 	return userRatingContainer.scoreSum / userRatingContainer.numRatings;
 61 }
 62 
 63 module.exports = router;
 64