@ -1,109 +0,0 @@ |
|||
var express = require('express'); |
|||
var app = express(); |
|||
var jwt = require('jsonwebtoken'); |
|||
var config = require('../config/config'); |
|||
var crypto = require('crypto'); |
|||
|
|||
app.set('superSecret', config.secret); |
|||
|
|||
/*******MODELS*********/ |
|||
//var trainerModel = require('../models/trainerModel');
|
|||
var userModel = require('../models/userModel'); |
|||
var dietModel = require('../models/dietModel'); |
|||
var routineModel = require('../models/routineModel'); |
|||
|
|||
/** GET '/admin/users/' **/ |
|||
exports.getUsers = function (req, res) { |
|||
userModel.find() |
|||
.limit(Number(req.query.pageSize)) |
|||
.skip(Number(req.query.pageSize) * Number(req.query.page)) |
|||
.exec(function (err, users) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(users); |
|||
}); |
|||
}; |
|||
/** GET '/admin/users/:userid' **/ |
|||
exports.getUserById = function (req, res) { |
|||
userModel.findOne({_id: req.params.userid}) |
|||
.lean() |
|||
.populate('followers', 'name avatar') |
|||
.populate('following', 'name avatar') |
|||
.exec(function (err, user) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!user) { |
|||
res.json({success: false, message: 'User not found.'}); |
|||
} else if (user) { |
|||
var nodes=[]; |
|||
var edges=[]; |
|||
var node={ |
|||
title: user.name, |
|||
label: user.name, |
|||
image: user.avatar, |
|||
shape: "image", |
|||
id: user._id |
|||
}; |
|||
nodes.push(node); |
|||
for(var i=0; i<user.followers.length; i++) |
|||
{ |
|||
var node={ |
|||
title: user.followers[i].name, |
|||
label: user.followers[i].name, |
|||
image: user.followers[i].avatar, |
|||
shape: "image", |
|||
id: user.followers[i]._id |
|||
}; |
|||
nodes.push(node); |
|||
var edge={ |
|||
from: user._id, |
|||
to: user.followers[i]._id, |
|||
arrows: { |
|||
from: user._id |
|||
}, |
|||
color: { |
|||
color: "#36bc9b" |
|||
} |
|||
}; |
|||
edges.push(edge); |
|||
} |
|||
|
|||
for(var i=0; i<user.following.length; i++) |
|||
{ |
|||
var indexJ=-1 |
|||
for(var j=0; j<nodes.length; j++) |
|||
{ |
|||
if(nodes[j].id.equals(user.following[i]._id)) |
|||
{ |
|||
indexJ=JSON.parse(JSON.stringify(j)); |
|||
} |
|||
} |
|||
if(indexJ==-1) |
|||
{//el node no estava als followers, afegim el node
|
|||
var node={ |
|||
title: user.following[i].name, |
|||
label: user.following[i].name, |
|||
image: user.following[i].avatar, |
|||
shape: "image", |
|||
id: user.following[i]._id |
|||
}; |
|||
nodes.push(node); |
|||
} |
|||
var edge={ |
|||
from: user._id, |
|||
to: user.following[i]._id, |
|||
arrows: { |
|||
to: user.following[i]._id |
|||
}, |
|||
color: { |
|||
color: "#4876b4" |
|||
} |
|||
}; |
|||
edges.push(edge); |
|||
} |
|||
|
|||
res.status(200).jsonp({ |
|||
nodes: nodes, |
|||
edges: edges |
|||
}); |
|||
} |
|||
}); |
|||
}; |
@ -1,125 +0,0 @@ |
|||
var express = require('express'); |
|||
var app = express(); |
|||
var jwt = require('jsonwebtoken'); |
|||
var config = require('../config/config'); |
|||
var crypto = require('crypto'); |
|||
|
|||
app.set('superSecret', config.secret); |
|||
|
|||
/*******MODELS*********/ |
|||
//var chefModel = require('../models/chefModel');
|
|||
var userModel = require('../models/userModel'); |
|||
var dietModel = require('../models/dietModel'); |
|||
|
|||
/**GET '/chefs' **/ |
|||
exports.getChefs = function (req, res) { |
|||
userModel.find({role: 'chef'}) |
|||
.limit(Number(req.query.pageSize)) |
|||
.skip(Number(req.query.pageSize)*Number(req.query.page)) |
|||
.exec(function (err, chefs) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(chefs); |
|||
}); |
|||
}; |
|||
|
|||
/** GET '/chefs/:chefid' **/ |
|||
exports.getChefById = function (req, res) { |
|||
userModel.findOne({_id: req.params.chefid}) |
|||
.lean() |
|||
.populate('diets', 'title description') |
|||
.exec(function (err, chef) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(chef); |
|||
}); |
|||
}; |
|||
|
|||
/** PUT '/chefs/:chefid' **/ |
|||
exports.updateChefById = function (req, res) { |
|||
var id = req.params.chefid; |
|||
var chef = req.body; |
|||
userModel.update({"_id": id}, chef, |
|||
function (err) { |
|||
if (err) return console.log(err); |
|||
console.log(chef); |
|||
res.status(200).jsonp(chef); |
|||
}); |
|||
}; |
|||
|
|||
/** DELETE '/chefs/:chefid' **/ |
|||
exports.deleteChefById = function (req, res) { |
|||
userModel.findByIdAndRemove({_id: req.params.chefid}, function (err) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).send("Chef deleted"); |
|||
}); |
|||
}; |
|||
|
|||
/** POST '/chefs/register' **/ |
|||
exports.register = function (req, res) { |
|||
var chef = new userModel({ |
|||
name: req.body.name, |
|||
password: crypto.createHash('sha256').update(req.body.password).digest('base64'), |
|||
email: req.body.email, |
|||
role: req.body.role |
|||
}); |
|||
chef.save(function (err, chef) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(chef); |
|||
}); |
|||
}; |
|||
|
|||
/** POST '/chefs/login' **/ |
|||
exports.login = function (req, res) { |
|||
userModel.findOne({ |
|||
email: req.body.email |
|||
}) |
|||
.select('+password') |
|||
.exec(function (err, chef) { |
|||
if (err) throw err; |
|||
if (!chef) { |
|||
res.json({success: false, message: 'Authentication failed. chef not found.'}); |
|||
} else if (chef) { |
|||
req.body.password = crypto.createHash('sha256').update(req.body.password).digest('base64'); |
|||
if (chef.password != req.body.password) { |
|||
res.json({success: false, message: 'Authentication failed. Wrong password.'}); |
|||
} else { |
|||
var indexToken = -1; |
|||
for (var i = 0; i < chef.tokens.length; i++) { |
|||
if (chef.tokens[i].userAgent == req.body.userAgent) { |
|||
indexToken = JSON.parse(JSON.stringify(i)); |
|||
} |
|||
} |
|||
console.log(indexToken); |
|||
if (indexToken == -1) {//userAgent no exist
|
|||
|
|||
var tokenGenerated = jwt.sign({foo: 'bar'}, app.get('superSecret'), { |
|||
// expiresIn: 86400 // expires in 24 hours
|
|||
}); |
|||
var newToken = { |
|||
userAgent: req.body.userAgent, |
|||
token: tokenGenerated |
|||
}; |
|||
chef.tokens.push(newToken); |
|||
} else {//userAgent already exist
|
|||
chef.tokens[indexToken].token = ""; |
|||
|
|||
var tokenGenerated = jwt.sign({foo: 'bar'}, app.get('superSecret'), { |
|||
// expiresIn: 86400 // expires in 24 hours
|
|||
}); |
|||
chef.tokens[indexToken].token = tokenGenerated; |
|||
} |
|||
chef.save(function (err, chef) { |
|||
if (err) return res.send(500, err.message); |
|||
|
|||
// return the information including token as JSON
|
|||
chef.password = ""; |
|||
res.json({ |
|||
user: chef, |
|||
success: true, |
|||
message: 'Enjoy your token!', |
|||
token: tokenGenerated |
|||
}); |
|||
}); |
|||
} |
|||
} |
|||
}); |
|||
}; |
@ -1,42 +0,0 @@ |
|||
var express = require('express'); |
|||
var app = express(); |
|||
var config = require('../config/config'); |
|||
/*******MODELS*********/ |
|||
var contactModel = require('../models/contactModel'); |
|||
|
|||
/** GET '/contacts/' **/ |
|||
exports.getContacts = function (req, res) { |
|||
contactModel.find() |
|||
.limit(Number(req.query.pageSize)) |
|||
.skip(Number(req.query.pageSize) * Number(req.query.page)) |
|||
.exec(function (err, contacts) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(contacts); |
|||
}); |
|||
}; |
|||
|
|||
/** GET '/contacts/:contactsid' **/ |
|||
exports.getContactById = function (req, res) { |
|||
contactModel.findOne({_id: req.params.contactid}) |
|||
.exec(function (err, contact) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(contact); |
|||
}); |
|||
}; |
|||
|
|||
/**POST '/contact' **/ |
|||
exports.createContact = function (req, res) { |
|||
var contact = new contactModel({ |
|||
name: req.body.name, |
|||
subject: req.body.subject, |
|||
email: req.body.email, |
|||
description: req.body.description |
|||
}); |
|||
contact.save(function (err, contact) { |
|||
if (err) { |
|||
console.log(err.message); |
|||
return res.status(500).send(err.message); |
|||
} |
|||
res.status(200).jsonp(contact); |
|||
}); |
|||
}; |
@ -1,202 +0,0 @@ |
|||
var express = require('express'); |
|||
var app = express(); |
|||
var conversationModel = require('../models/conversationModel'); |
|||
var userModel = require('../models/userModel'); |
|||
//var trainerModel = require('../models/trainerModel');
|
|||
var crypto = require('crypto'); |
|||
|
|||
exports.getUserConversations = function (req, res) { |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token']}) |
|||
.lean() |
|||
.populate({//aixรฒ รฉs per fer deep population
|
|||
path: 'conversations', |
|||
populate: { |
|||
path: 'userA userB', |
|||
model: 'userModel', |
|||
select: 'name avatar' |
|||
} |
|||
}) |
|||
.populate({//aixรฒ รฉs per fer deep population
|
|||
path: 'conversations', |
|||
populate: { |
|||
path: 'messages.user', |
|||
model: 'userModel', |
|||
select: 'name avatar' |
|||
} |
|||
}) |
|||
.exec(function (err, user) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(user.conversations); |
|||
}); |
|||
}; |
|||
|
|||
/**POST '/conversations' **/ |
|||
exports.createConversation = function (req, res) {//req.body.userB
|
|||
userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, userA) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!userA) { |
|||
res.json({success: false, message: 'userA not found.'}); |
|||
} else if (userA) { |
|||
//aquรญ ja hem agafat el userA a partir del seu token
|
|||
userModel.findOne({_id: req.body.userB}, function (err, userB) {//busquem l'userB
|
|||
if (err) return res.send(500, err.message); |
|||
if (!userB) { |
|||
res.json({success: false, message: 'userB not found.'}); |
|||
} else if (userB) { |
|||
conversationModel.findOne({$or:[{userA: userA._id, userB: userB._id},{userA: userB._id, userB: userA._id}]}, function (err, conversation) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!conversation) { |
|||
console.log("conversation no exists, create new one"); |
|||
var conversation = new conversationModel({ |
|||
userA: userA._id, |
|||
userB: userB._id, |
|||
modifiedDate: Date() |
|||
}); |
|||
conversation.save(function (err, conversation) { |
|||
if (err) return res.send(500, err.message); |
|||
|
|||
userA.conversations.push(conversation._id); |
|||
userA.save(function (err) { |
|||
if (err) return res.send(500, err.message); |
|||
userB.conversations.push(conversation._id); |
|||
userB.save(function (err) { |
|||
if (err) return res.send(500, err.message); |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token']}) |
|||
.lean() |
|||
.populate({//aixรฒ รฉs per fer deep population
|
|||
path: 'conversations', |
|||
populate: { |
|||
path: 'userA userB', |
|||
model: 'userModel', |
|||
select: 'name avatar' |
|||
} |
|||
}) |
|||
.populate({//aixรฒ รฉs per fer deep population
|
|||
path: 'conversations', |
|||
populate: { |
|||
path: 'messages.user', |
|||
model: 'userModel', |
|||
select: 'name avatar' |
|||
} |
|||
}) |
|||
.exec(function (err, user) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(user.conversations); |
|||
}); |
|||
}); |
|||
}); |
|||
}); |
|||
} else if (conversation) { |
|||
console.log("conversation exists"); |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token']}) |
|||
.lean() |
|||
.populate({//aixรฒ รฉs per fer deep population
|
|||
path: 'conversations', |
|||
populate: { |
|||
path: 'userA userB', |
|||
model: 'userModel', |
|||
select: 'name avatar' |
|||
} |
|||
}) |
|||
.populate({//aixรฒ รฉs per fer deep population
|
|||
path: 'conversations', |
|||
populate: { |
|||
path: 'messages.user', |
|||
model: 'userModel', |
|||
select: 'name avatar' |
|||
} |
|||
}) |
|||
.exec(function (err, user) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(user.conversations); |
|||
}); |
|||
} |
|||
});//end of conversation find
|
|||
|
|||
}//end else if (userB)
|
|||
});//end of userB find
|
|||
}//end else if (userA)
|
|||
});//end of userA find
|
|||
}; |
|||
|
|||
|
|||
/**POST '/conversations/:conversationid' **/ |
|||
exports.addMessageToConversation = function (req, res) { |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, userSender) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!userSender) { |
|||
res.json({success: false, message: 'userSender not found.'}); |
|||
} else if (userSender) { |
|||
//aquรญ ja hem agafat el userSender a partir del seu token
|
|||
conversationModel.findOne({_id: req.params.conversationid}, function (err, conversation) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!conversation) { |
|||
res.json({success: false, message: 'conversation not found.'}); |
|||
} else if (conversation) { |
|||
var newmessage = { |
|||
user: userSender._id, |
|||
content: req.body.message, |
|||
date: Date() |
|||
}; |
|||
conversation.messages.push(newmessage); |
|||
conversation.modifiedDate=Date(); |
|||
|
|||
conversation.save(function (err, conversation) { |
|||
if (err) return res.send(500, err.message); |
|||
|
|||
//ara cal saber qui รฉs l'userReciever (el que no ha enviat el missatge)
|
|||
var idUserReciever; |
|||
if(userSender._id.equals(conversation.userA)==false){ |
|||
idUserReciever=conversation.userA; |
|||
}else if(userSender._id.equals(conversation.userB)==false){ |
|||
idUserReciever=conversation.userB; |
|||
} |
|||
userModel.findOne({_id: idUserReciever}, function (err, userReciever) {//busquem l'userReciever
|
|||
if (err) return res.send(500, err.message); |
|||
if (!userReciever) { |
|||
res.json({success: false, message: 'userReciever not found.'}); |
|||
} else if (userReciever) { |
|||
console.log("reciever: " + userReciever.name); |
|||
/*notification*/ |
|||
var notification = { |
|||
state: "pendent", |
|||
message: userSender.name + " sent a message to you", |
|||
link: "messages", |
|||
icon: "message.png", |
|||
date: Date() |
|||
}; |
|||
userReciever.notifications.push(notification); |
|||
/* end of notification*/ |
|||
userReciever.save(function (err) { |
|||
if (err) return res.send(500, err.message); |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token']}) |
|||
.lean() |
|||
.populate({//aixรฒ รฉs per fer deep population
|
|||
path: 'conversations', |
|||
populate: { |
|||
path: 'userA userB', |
|||
model: 'userModel', |
|||
select: 'name avatar' |
|||
} |
|||
}) |
|||
.populate({//aixรฒ รฉs per fer deep population
|
|||
path: 'conversations', |
|||
populate: { |
|||
path: 'messages.user', |
|||
model: 'userModel', |
|||
select: 'name avatar' |
|||
} |
|||
}) |
|||
.exec(function (err, user) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(user.conversations); |
|||
}); |
|||
}); |
|||
}//end else if (userReciever)
|
|||
});//end userReciever find
|
|||
}); |
|||
}//end else if (conversation)
|
|||
});//end of conversation find
|
|||
}//end else if (userSender)
|
|||
});//end of userSender find
|
|||
}; |
@ -1,251 +0,0 @@ |
|||
var express = require('express'); |
|||
var app = express(); |
|||
var config = require('../config/config'); |
|||
var crypto = require('crypto'); |
|||
|
|||
app.set('superSecret', config.secret); |
|||
var userModel = require('../models/userModel'); |
|||
var dietModel = require('../models/dietModel'); |
|||
//var chefModel = require('../models/chefModel');
|
|||
|
|||
/** GET '/diets' ***/ |
|||
exports.getDiets = function (req, res) { |
|||
dietModel.find() |
|||
.limit(Number(req.query.pageSize)) |
|||
.skip(Number(req.query.pageSize)*Number(req.query.page)) |
|||
.exec(function (err, diets) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(diets); |
|||
}); |
|||
}; |
|||
|
|||
/** GET '/diets/:dietid' **/ |
|||
exports.getDietById = function (req, res) { |
|||
dietModel.findOne({_id: req.params.dietid}) |
|||
.lean() |
|||
.populate('chef', 'name avatar') |
|||
.exec(function (err, diet) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(diet); |
|||
}); |
|||
}; |
|||
|
|||
/**DELETE '/diets/:dietid' **/ |
|||
exports.deleteDietById = function (req, res) { |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token'], role:'chef'}, function (err, chef) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!chef) { |
|||
res.json({success: false, message: 'Chef not found.'}); |
|||
} else if (chef) { |
|||
for (var i = 0; i < chef.diets.length; i++) { |
|||
if (chef.diets[i].equals(req.params.dietid)) { |
|||
chef.diets.splice(i, 1); |
|||
chef.save(function (err, chef) {//guardem el chef amb la dieta treta
|
|||
if (err) return res.send(500, err.message); |
|||
|
|||
dietModel.findByIdAndRemove({_id: req.params.dietid}, function (err) { |
|||
if (err !== null) return res.send(500, err.message); |
|||
res.status(200).jsonp('Deleted diet'); |
|||
}); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
}); |
|||
}; |
|||
|
|||
/** PUT '/diets/:dietid' **/ |
|||
exports.updateDietById = function (req, res) { |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token'], role:'chef'}, function (err, chef) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!chef) { |
|||
res.json({success: false, message: 'Chef not found.'}); |
|||
} else if (chef) { |
|||
for (var i = 0; i < chef.diets.length; i++) { |
|||
if (chef.diets[i].equals(req.params.dietid)) { |
|||
chef.diets.splice(i, 1); //<-- splice? quan s'estร fent un update? no s'hauria d'eliminar
|
|||
//tot i que no afecta, pq l'splice aquest desprรฉs no es guarda a la base de dades pq no hi ha cap chef.save
|
|||
/* Solo si esa dieta ha sido creada por el chef */ |
|||
var id = req.params.dietid; |
|||
var diet = req.body; |
|||
dietModel.update({"_id": id}, diet, |
|||
function (err) { |
|||
if (err) return console.log(err); |
|||
console.log(diet); |
|||
res.status(200).jsonp(diet); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
}); |
|||
}; |
|||
|
|||
/**POST '/diets' **/ |
|||
exports.createDiet = function (req, res) { |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token'], role:'chef'}, function (err, chef) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!chef) { |
|||
res.json({success: false, message: 'Diet creation failed. Chef not found.'}); |
|||
} else if (chef) { |
|||
var diet = new dietModel({ |
|||
title: req.body.title, |
|||
description: req.body.description, |
|||
chef: chef._id,//a partir del token, pillem la id
|
|||
client: req.params.clientid//es guarda de quin user รฉs la diet
|
|||
}); |
|||
//guardem la diet
|
|||
diet.save(function (err, diet) { |
|||
if (err) { |
|||
console.log(err.message); |
|||
return res.status(500).send(err.message); |
|||
} |
|||
//ara guardem la dietid al chef
|
|||
chef.diets.push(diet._id); |
|||
chef.save(function (err, chef) { |
|||
if (err) return res.send(500, err.message); |
|||
|
|||
}); |
|||
res.status(200).jsonp(diet); |
|||
}); |
|||
}//else
|
|||
}); |
|||
}; |
|||
|
|||
/** POST '/diets/:dietid/days' **/ |
|||
exports.addDayToDiet = function (req, res) { |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token'], role:'chef'}, function (err, chef) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!chef) { |
|||
res.json({success: false, message: 'Diet day addition failed. Trainer not found.'}); |
|||
} else if (chef) { |
|||
dietModel.findOne({_id: req.params.dietid}, function (err, diet) { |
|||
if (err) return res.send(500, err.message); |
|||
|
|||
if (chef._id.equals(diet.chef)) {// si el chef que fa el post realment รฉs el chef creator de la diet
|
|||
diet.days.push(req.body.day); |
|||
diet.save(function (err, diet) { |
|||
if (err) { |
|||
return res.status(500).send(err.message); |
|||
} |
|||
res.status(200).jsonp(diet); |
|||
}); |
|||
} |
|||
}); |
|||
}// end else if
|
|||
}); |
|||
}; |
|||
|
|||
/** POST '/diets/choose' **/ |
|||
exports.chooseDiet = function (req, res) { |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!user) { |
|||
res.json({success: false, message: 'choosing diet failed. user not found.'}); |
|||
} else if (user) { |
|||
user.diets.push(req.params.dietid); |
|||
/* gamification */ |
|||
var reward = { |
|||
concept: "choosing diet", |
|||
date: Date(), |
|||
value: +5 |
|||
}; |
|||
user.points.history.push(reward); |
|||
user.points.total = user.points.total + 5; |
|||
/* end of gamification */ |
|||
user.save(function (err) { |
|||
if (err) return res.send(500, err.message); |
|||
|
|||
res.status(200).jsonp(user); |
|||
}) |
|||
}//end else if
|
|||
}); |
|||
}; |
|||
|
|||
/** DELETE '/diets/choose' **/ |
|||
exports.unchooseDiet = function (req, res) { |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!user) { |
|||
res.json({success: false, message: 'user not found.'}); |
|||
} else if (user) { |
|||
for (var i = 0; i < user.diets.length; i++) { |
|||
if (user.diets[i].equals(req.params.dietid)) {//deletes the diets of the user with the dietid
|
|||
user.diets.splice(i, 1); |
|||
} |
|||
} |
|||
/* gamification */ |
|||
var reward = { |
|||
concept: "unchoosing diet", |
|||
date: Date(), |
|||
value: -7 |
|||
}; |
|||
user.points.history.push(reward); |
|||
user.points.total = user.points.total - 7; |
|||
/* end of gamification */ |
|||
user.save(function (err) { |
|||
if (err) return res.send(500, err.message); |
|||
|
|||
res.status(200).jsonp(user); |
|||
}); |
|||
}//end else if
|
|||
}); |
|||
}; |
|||
|
|||
/** POST '/diets/completeDay/:dietid' **/ |
|||
exports.completeDayGamificatedDiet = function (req, res) { |
|||
//1r intentamos darle los puntos al usuario por haber completado el dรญa
|
|||
userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) { |
|||
if (err) |
|||
return res.send(500, err.message); |
|||
if (!user) { |
|||
res.json({success: false, message: 'choosing diet failed. user not found.'}); |
|||
} |
|||
else if (user) { |
|||
/* gamification */ |
|||
var reward = |
|||
{ |
|||
concept: "diet day complete", |
|||
date: Date(), |
|||
value: +1 |
|||
}; |
|||
user.points.history.push(reward); |
|||
user.points.total = user.points.total + 1; |
|||
/* end of gamification */ |
|||
user.save(function (err) { |
|||
if (err) |
|||
return res.send(500, err.message); |
|||
}); |
|||
//Ahora intentamos aรฑadir done = true dentro del modelo dieta
|
|||
dietModel.findOne({'_id': req.params.dietid}, function (err, diet) { |
|||
if (err) |
|||
return res.send(500, err.message); |
|||
if (!diet) { |
|||
res.json({success: false, message: 'Diet not found'}); |
|||
} |
|||
else if (diet) { |
|||
var indexDay = -1; |
|||
for (var i = 0; i < diet.days.length; i++) //diet.days
|
|||
{ |
|||
if (diet.days[i]._id.equals(req.body.dayid)) { |
|||
//aquรญ hem trobat el dia que busquem
|
|||
indexDay = JSON.parse(JSON.stringify(i)); |
|||
} |
|||
}//End for looking for days
|
|||
if (indexDay > -1) { |
|||
/* True to day done*/ |
|||
diet.days[indexDay].done = true; |
|||
/* end of done*/ |
|||
diet.save(function (err) { |
|||
if (err) |
|||
return res.send(500, err.message); |
|||
res.status(200).jsonp(diet); |
|||
});//diet.save
|
|||
}//End if when day foung
|
|||
else { |
|||
res.json({success: false, message: 'Day not found'}); |
|||
} |
|||
}//End else if found diet
|
|||
});//En dietModel for done = true
|
|||
}//End else if (user)
|
|||
});//En UserModel findOne()
|
|||
};//End function
|
@ -1,303 +0,0 @@ |
|||
var express = require('express'); |
|||
var app = express(); |
|||
var config = require('../config/config'); |
|||
var crypto = require('crypto'); |
|||
|
|||
app.set('superSecret', config.secret); |
|||
var userModel = require('../models/userModel'); |
|||
var routineModel = require('../models/routineModel'); |
|||
//var trainerModel = require('../models/trainerModel');
|
|||
|
|||
/** GET '/routines/' **/ |
|||
exports.getRoutines = function (req, res) { |
|||
routineModel.find() |
|||
.limit(Number(req.query.pageSize)) |
|||
.skip(Number(req.query.pageSize)*Number(req.query.page)) |
|||
.exec(function (err, routines) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(routines); |
|||
}); |
|||
}; |
|||
|
|||
/** GET '/routines/:routineid' **/ |
|||
exports.getRoutineById = function (req, res) { |
|||
routineModel.findOne({_id: req.params.routineid}) |
|||
.lean() |
|||
.populate('trainer', 'name avatar') |
|||
.populate('client', 'name avatar points.total') |
|||
.exec(function (err, routine) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(routine); |
|||
}); |
|||
}; |
|||
|
|||
/** DELETE '/routines/:routineid' **/ |
|||
exports.deleteRoutineById = function (req, res) { |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token'], role:'trainer'}, function (err, trainer) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!trainer) { |
|||
res.json({success: false, message: 'Trainer not found.'}); |
|||
} else if (trainer) { |
|||
for (var i = 0; i < trainer.routines.length; i++) { |
|||
if (trainer.routines[i].equals(req.params.routineid)) {//Solo si esa routine ha sido creada por el trainer
|
|||
trainer.routines.splice(i, 1); |
|||
//tambรฉ s'hauria de treure la referรจncia al user que รฉs client d'aquesta routine
|
|||
trainer.save(function (err, trainer) {//guardem el trainer amb la rutina treta
|
|||
if (err) return res.send(500, err.message); |
|||
|
|||
routineModel.findByIdAndRemove({_id: req.params.routineid}, function (err) {//elminem la routine
|
|||
if (err !== null) return res.send(500, err.message); |
|||
res.status(200).jsonp('Deleted routine'); |
|||
}); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
}); |
|||
}; |
|||
|
|||
/** PUT '/routines/:routineid' **/ |
|||
exports.updateRoutineById = function (req, res) { |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token'], role:'trainer'}, function (err, user) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!user) { |
|||
res.json({success: false, message: 'Trainer not found.'}); |
|||
} else if (user) { |
|||
for (var i = 0; i < user.routines.length; i++) { |
|||
if (user.routines[i].equals(req.params.routineid)) { |
|||
user.routines.splice(i, 1);// <-- perquรจ es fa l'splice de user.routines si desprรฉs no es guarda el user??
|
|||
/* Solo si esa routine ha sido creada por el trainer */ |
|||
var id = req.params.routineid; |
|||
var routine = req.body; |
|||
routineModel.update({"_id": id}, routine, |
|||
function (err) { |
|||
if (err) return console.log(err); |
|||
console.log(routine); |
|||
res.status(200).jsonp(routine); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
}); |
|||
}; |
|||
|
|||
/** POST '/routines/addToClient/:clientid' **/ |
|||
exports.addRoutineToClient = function (req, res) { |
|||
userModel.findOne({ |
|||
'tokens.token': req.headers['x-access-token'], |
|||
'clients.client': req.params.clientid, |
|||
'role': 'trainer' |
|||
}, function (err, trainer) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!trainer) { |
|||
res.json({success: false, message: 'Routine creation failed. Trainer not found.'}); |
|||
} else if (trainer) { |
|||
var routine = new routineModel({ |
|||
title: req.body.title, |
|||
description: req.body.description, |
|||
trainer: trainer._id,//a partir del token, pillem la id
|
|||
client: req.params.clientid//es guarda de quin user รฉs la routine
|
|||
}); |
|||
//guardem la routine
|
|||
routine.save(function (err, routine) { |
|||
if (err) { |
|||
console.log(err.message); |
|||
return res.status(500).send(err.message); |
|||
} |
|||
//ara guardem la routineid al trainer
|
|||
trainer.routines.push(routine._id); |
|||
trainer.save(function (err, trainer) { |
|||
if (err) return res.send(500, err.message); |
|||
|
|||
}); |
|||
//res.status(200).jsonp(routine);
|
|||
//ara afegim la routine al client
|
|||
userModel.findOne({'_id': req.params.clientid}, function (err, user) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!user) { |
|||
res.json({success: false, message: 'adding routine to client failed. user not found.'}); |
|||
} else if (user) { |
|||
user.routines.push(routine._id); |
|||
/* gamification */ |
|||
var reward = { |
|||
concept: "new routine", |
|||
date: Date(), |
|||
value: +5 |
|||
}; |
|||
user.points.history.push(reward); |
|||
user.points.total = user.points.total + 5; |
|||
/* end of gamification */ |
|||
|
|||
var notification = { |
|||
state: "pendent", |
|||
message: "trainer has added a routine to you", |
|||
link: "training", |
|||
icon: "newroutine.png", |
|||
date: Date() |
|||
}; |
|||
user.notifications.push(notification); |
|||
user.save(function (err) { |
|||
if (err) return res.send(500, err.message); |
|||
|
|||
res.status(200).jsonp(routine); |
|||
}); |
|||
}//end else if
|
|||
}); |
|||
}); |
|||
}//else
|
|||
}); |
|||
}; |
|||
|
|||
/** POST '/routines/:routineid/days' **/ |
|||
exports.addDayToRoutine = function (req, res) { |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, trainer) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!trainer) { |
|||
res.json({success: false, message: 'Routine day addition failed. Trainer not found.'}); |
|||
} else if (trainer) { |
|||
routineModel.findOne({_id: req.params.routineid}, function (err, routine) { |
|||
if (err) return res.send(500, err.message); |
|||
|
|||
if (trainer._id.equals(routine.trainer)) {// si el trainer que fa el post realment รฉs el trainer creator de la routine
|
|||
routine.days.push(req.body.day); |
|||
routine.save(function (err, routine) { |
|||
if (err) { |
|||
return res.status(500).send(err.message); |
|||
} |
|||
routineModel.findOne({_id: routine._id}) |
|||
.lean() |
|||
.populate('trainer', 'name avatar') |
|||
.populate('client', 'name avatar points.total') |
|||
.exec(function (err, routine) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(routine); |
|||
}); |
|||
}); |
|||
} |
|||
}); |
|||
}// end else if
|
|||
}); |
|||
}; |
|||
|
|||
/** POST '/routines/choose' **/ |
|||
exports.chooseRoutine = function (req, res) { |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!user) { |
|||
res.json({success: false, message: 'choosing routine failed. user not found.'}); |
|||
} else if (user) { |
|||
console.log(user);//aquรญ potser caldria comprovar que la routine รฉs la que han creat per l'user
|
|||
user.routines.push(req.body.routineid); |
|||
/* gamification */ |
|||
var reward = { |
|||
concept: "choosing routine", |
|||
date: Date(), |
|||
value: +5 |
|||
}; |
|||
user.points.history.push(reward); |
|||
user.points.total = user.points.total + 5; |
|||
/* end of gamification */ |
|||
user.save(function (err) { |
|||
if (err) return res.send(500, err.message); |
|||
|
|||
res.status(200).jsonp(user); |
|||
}); |
|||
}//end else if
|
|||
}); |
|||
}; |
|||
|
|||
/** DELETE '/routines/choose' **/ |
|||
exports.unchooseRoutine = function (req, res) { |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!user) { |
|||
res.json({success: false, message: 'choosing routine failed. user not found.'}); |
|||
} else if (user) { |
|||
for (var i = 0; i < user.routines.length; i++) { |
|||
if (user.routines[i] == req.body.routineid) {//deletes the diets of the user with the dietid
|
|||
user.routines.splice(i, 1); |
|||
} |
|||
} |
|||
/* gamification */ |
|||
var reward = { |
|||
concept: "unchoosing routine", |
|||
date: Date(), |
|||
value: -7 |
|||
}; |
|||
user.points.history.push(reward); |
|||
user.points.total = user.points.total - 7; |
|||
/* end of gamification */ |
|||
user.save(function (err) { |
|||
if (err) return res.send(500, err.message); |
|||
|
|||
res.status(200).jsonp(user); |
|||
}); |
|||
}//end else if
|
|||
}); |
|||
}; |
|||
|
|||
/** POST '/routines/completeDay/:routineid' **/ |
|||
exports.completeDayGamificatedRoutine = function (req, res) { |
|||
//1r intentamos darle los puntos al usuario por haber completado el dรญa
|
|||
userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) { |
|||
if (err) |
|||
return res.send(500, err.message); |
|||
if (!user) { |
|||
res.json({success: false, message: 'choosing routine failed. user not found.'}); |
|||
} |
|||
else if (user) { |
|||
/* gamification */ |
|||
var reward = |
|||
{ |
|||
concept: "routine day complete", |
|||
date: Date(), |
|||
value: +1 |
|||
}; |
|||
user.points.history.push(reward); |
|||
user.points.total = user.points.total + 1; |
|||
/* end of gamification */ |
|||
user.save(function (err) { |
|||
if (err) |
|||
return res.send(500, err.message); |
|||
}); |
|||
//Ahora intentamos aรฑadir done = true dentro del modelo rutina
|
|||
routineModel.findOne({'_id': req.params.routineid}, function (err, routine) { |
|||
if (err) |
|||
return res.send(500, err.message); |
|||
if (!routine) { |
|||
res.json({success: false, message: 'Routine not found'}); |
|||
} |
|||
else if (routine) { |
|||
var indexDay = -1; |
|||
for (var i = 0; i < routine.days.length; i++) //routine.days
|
|||
{ |
|||
if (routine.days[i]._id.equals(req.body.dayid)) { |
|||
//aquรญ hem trobat el dia que busquem
|
|||
indexDay = JSON.parse(JSON.stringify(i)); |
|||
} |
|||
}//End for looking for days
|
|||
if (indexDay > -1) { |
|||
/* True to day done*/ |
|||
routine.days[indexDay].done = true; |
|||
/* end of done*/ |
|||
routine.save(function (err) { |
|||
if (err) |
|||
return res.send(500, err.message); |
|||
routineModel.findOne({_id: routine._id}) |
|||
.lean() |
|||
.populate('trainer', 'name avatar') |
|||
.populate('client', 'name avatar points.total') |
|||
.exec(function (err, routine) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(routine); |
|||
}); |
|||
});//Routine.save
|
|||
}//End if when day foung
|
|||
else { |
|||
res.json({success: false, message: 'Day not found'}); |
|||
} |
|||
}//End else if found routine
|
|||
});//En routineModel for done = true
|
|||
}//End else if (user)
|
|||
});//En UserModel findOne()
|
|||
};//End function
|
@ -1,107 +0,0 @@ |
|||
var express = require('express'); |
|||
var app = express(); |
|||
var runModel = require('../models/runModel'); |
|||
var publicationModel = require('../models/publicationModel'); |
|||
var userModel = require('../models/userModel'); |
|||
//var trainerModel = require('../models/trainerModel');
|
|||
var crypto = require('crypto'); |
|||
|
|||
|
|||
/**POST '/publications' **/ |
|||
exports.postRun = function (req, res) { |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!user) { |
|||
res.json({success: false, message: 'user not found.'}); |
|||
} else if (user) { |
|||
//aquรญ ja hem agafat el user a partir del seu token
|
|||
var run = new runModel(req.body.newRun); |
|||
run.user=user._id; |
|||
//fins aquรญ tenim la variable publication amb els continguts
|
|||
//ara cal 1r guardar el model publication a la base de dades
|
|||
run.save(function (err, run) { |
|||
if (err) return res.send(500, err.message); |
|||
|
|||
//i 2n, afegir la id de la publicaciรณ generada al user.publications
|
|||
user.runs.push(run._id); |
|||
/* gamification */ |
|||
var reward = { |
|||
concept: "added new run to user", |
|||
date: Date(), |
|||
value: +1 |
|||
}; |
|||
user.points.history.push(reward); |
|||
user.points.total = user.points.total + 1; |
|||
/* end of gamification */ |
|||
user.save(function (err, user) { |
|||
if (err) return res.send(500, err.message); |
|||
|
|||
//res.status(200).jsonp(user);
|
|||
//ara farem una publicaciรณ ensenyant que ha fet aquest run
|
|||
var publication = new publicationModel({ |
|||
title: "new run '" + run.title + "'!", |
|||
content: "distance of: " + run.distance + ". View my runs at my profile", |
|||
date: new Date(), |
|||
user: user._id, |
|||
photo: run.photo |
|||
}); |
|||
//fins aquรญ tenim la variable publication amb els continguts
|
|||
//ara cal 1r guardar el model publication a la base de dades
|
|||
publication.save(function (err, publication) { |
|||
if (err) return res.send(500, err.message); |
|||
|
|||
//i 2n, afegir la id de la publicaciรณ generada al user.publications
|
|||
user.publications.push(publication._id); |
|||
/* gamification */ |
|||
var reward = { |
|||
concept: "added new publication to Timeline", |
|||
date: Date(), |
|||
value: +1 |
|||
}; |
|||
user.points.history.push(reward); |
|||
user.points.total = user.points.total + 1; |
|||
/* end of gamification */ |
|||
|
|||
if(!user.totalkm) |
|||
{ |
|||
user.totalkm=0; |
|||
} |
|||
user.totalkm=user.totalkm + run.distance; |
|||
|
|||
user.save(function (err, user) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(user); |
|||
}); |
|||
}); |
|||
}); |
|||
}); |
|||
}//end else if
|
|||
}); |
|||
}; |
|||
|
|||
/**GET '/users/:userid/publications' **/ |
|||
exports.getRunsByUserId = function (req, res) { |
|||
userModel.findOne({ |
|||
_id: req.params.userid |
|||
}) |
|||
.populate('runs') |
|||
.exec(function (error, user) { |
|||
if (error !== null) res.send(500, error.message); |
|||
|
|||
res.status(200).jsonp(user); |
|||
}); |
|||
}; |
|||
/** GET '/run/getById/:publicationid' **/ |
|||
exports.getRunByRunId = function (req, res) { |
|||
runModel.findOne({_id: req.params.runid}) |
|||
.lean() |
|||
.populate('user', 'name avatar') |
|||
.exec(function (err, run) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!run) { |
|||
res.json({success: false, message: 'run not found.'}); |
|||
} else if (run) { |
|||
res.status(200).jsonp(run); |
|||
} |
|||
}); |
|||
}; |
@ -1,433 +0,0 @@ |
|||
var express = require('express'); |
|||
var app = express(); |
|||
var jwt = require('jsonwebtoken'); |
|||
var config = require('../config/config'); |
|||
var crypto = require('crypto'); |
|||
|
|||
app.set('superSecret', config.secret); |
|||
|
|||
/*******MODELS*********/ |
|||
//var trainerModel = require('../models/trainerModel');
|
|||
var userModel = require('../models/userModel'); |
|||
var dietModel = require('../models/dietModel'); |
|||
var routineModel = require('../models/routineModel'); |
|||
|
|||
/** GET '/trainers' **/ |
|||
exports.getTrainers = function (req, res) { |
|||
userModel.find({role: 'trainer'}) |
|||
.limit(Number(req.query.pageSize)) |
|||
.skip(Number(req.query.pageSize)*Number(req.query.page)) |
|||
.exec(function (err, trainers) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(trainers); |
|||
}); |
|||
}; |
|||
exports.getTrainersByDisciplinesArray = function (req, res) { |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!user) { |
|||
res.json({success: false, message: 'sending petition failed. user not found.'}); |
|||
} else if (user) { |
|||
if(user.disciplines[0]) |
|||
{ |
|||
userModel.find({ |
|||
role: 'trainer', |
|||
$and: [ |
|||
{_id: { $nin: user._id}}, |
|||
{_id: { $nin: user.trainers}} |
|||
], |
|||
'disciplines.name': user.disciplines[0].name//per ara torna els trainers que tinguin la discipline[0] del user client
|
|||
}) |
|||
.limit(Number(req.query.pageSize)) |
|||
.skip(Number(req.query.pageSize)*Number(req.query.page)) |
|||
.exec(function (err, trainers) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(trainers); |
|||
}); |
|||
}else{ |
|||
userModel.find({ |
|||
role: 'trainer', |
|||
$and: [ |
|||
{_id: { $nin: user._id}}, |
|||
{_id: { $nin: user.trainers}} |
|||
] |
|||
}) |
|||
.limit(Number(req.query.pageSize)) |
|||
.skip(Number(req.query.pageSize)*Number(req.query.page)) |
|||
.exec(function (err, trainers) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(trainers); |
|||
}); |
|||
} |
|||
|
|||
} |
|||
}); |
|||
}; |
|||
|
|||
/** GET '/trainers/:trainerid' **/ |
|||
exports.getTrainerById = function (req, res) { |
|||
userModel.findOne({_id: req.params.trainerid, role: 'trainer'}) |
|||
.lean() |
|||
.populate('routines', 'title description') |
|||
.populate('clients.client', 'name avatar points') |
|||
.populate('clientsPetitions.clientid', 'name avatar') |
|||
.exec(function (err, trainer) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(trainer); |
|||
}); |
|||
}; |
|||
|
|||
/** GET '/trainers/searchByDiscipline' **/ |
|||
exports.searchByDiscipline = function (req, res) { |
|||
userModel.find({'disciplines.name': req.params.discipline, role: 'trainer'}) |
|||
.limit(Number(req.query.pageSize)) |
|||
.skip(Number(req.query.pageSize)*Number(req.query.page)) |
|||
.exec(function (err, trainers) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(trainers); |
|||
}); |
|||
}; |
|||
|
|||
/** POST '/trainers/register' **/ |
|||
exports.register = function (req, res) { |
|||
var trainer = new userModel({ |
|||
name: req.body.name, |
|||
password: crypto.createHash('sha256').update(req.body.password).digest('base64'), |
|||
email: req.body.email, |
|||
avatar: 'img/user.png', |
|||
background: 'img/background.png', |
|||
role: req.body.role, |
|||
discipline: req.body.discipline, |
|||
points: { |
|||
total: 0 |
|||
} |
|||
}); |
|||
/* gamification */ |
|||
var reward = { |
|||
concept: "account created", |
|||
date: Date(), |
|||
value: +1 |
|||
}; |
|||
trainer.points.history.push(reward); |
|||
trainer.points.total = trainer.points.total + 1; |
|||
/* end of gamification */ |
|||
trainer.save(function (err, trainer) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(trainer); |
|||
}); |
|||
}; |
|||
|
|||
/** POST '/trainers/login' **/ |
|||
exports.login = function (req, res) { |
|||
userModel.findOne({ |
|||
email: req.body.email, role: 'trainer' |
|||
}) |
|||
.select('+password') |
|||
.exec(function (err, trainer) { |
|||
if (err) throw err; |
|||
if (!trainer) { |
|||
res.json({success: false, message: 'Authentication failed. trainer not found.'}); |
|||
} else if (trainer) { |
|||
req.body.password = crypto.createHash('sha256').update(req.body.password).digest('base64'); |
|||
if (trainer.password != req.body.password) { |
|||
res.json({success: false, message: 'Authentication failed. Wrong password.'}); |
|||
} else { |
|||
var indexToken = -1; |
|||
for (var i = 0; i < trainer.tokens.length; i++) { |
|||
if (trainer.tokens[i].userAgent == req.body.userAgent) { |
|||
indexToken = JSON.parse(JSON.stringify(i)); |
|||
} |
|||
} |
|||
console.log(indexToken); |
|||
if (indexToken == -1) {//userAgent no exist
|
|||
|
|||
var tokenGenerated = jwt.sign({foo: 'bar'}, app.get('superSecret'), { |
|||
// expiresIn: 86400 // expires in 24 hours
|
|||
}); |
|||
var newToken = { |
|||
userAgent: req.body.userAgent, |
|||
token: tokenGenerated, |
|||
os: req.body.os, |
|||
browser: req.body.browser, |
|||
device: req.body.device, |
|||
os_version: req.body.os_version, |
|||
browser_version: req.body.browser_version, |
|||
ip: req.body.ip, |
|||
lastLogin: Date() |
|||
}; |
|||
trainer.tokens.push(newToken); |
|||
} else {//userAgent already exist
|
|||
trainer.tokens[indexToken].token = ""; |
|||
|
|||
var tokenGenerated = jwt.sign({foo: 'bar'}, app.get('superSecret'), { |
|||
// expiresIn: 86400 // expires in 24 hours
|
|||
}); |
|||
trainer.tokens[indexToken].token = tokenGenerated; |
|||
trainer.tokens[indexToken].ip = req.body.ip; |
|||
trainer.tokens[indexToken].lastLogin = Date(); |
|||
} |
|||
trainer.save(function (err, trainer) { |
|||
if (err) return res.send(500, err.message); |
|||
|
|||
// return the information including token as JSON
|
|||
trainer.password = ""; |
|||
res.json({ |
|||
user: trainer, |
|||
success: true, |
|||
message: 'Enjoy your token!', |
|||
token: tokenGenerated |
|||
}); |
|||
}); |
|||
} |
|||
} |
|||
}); |
|||
}; |
|||
|
|||
/** POST '/trainers/acceptClientPetition' **/ |
|||
exports.acceptClientPetition = function (req, res) { |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token'], role: 'trainer'}, function (err, trainer) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!trainer) { |
|||
res.json({success: false, message: 'adding client to trainer failed. trainer not found.'}); |
|||
} else if (trainer) { |
|||
console.log(trainer);//aquรญ potser caldria comprovar que la routine รฉs la que han creat per l'trainer
|
|||
//busquem la petition que estem processant
|
|||
for (var i = 0; i < trainer.clientsPetitions.length; i++) //routine.days
|
|||
{ |
|||
if (trainer.clientsPetitions[i]._id.equals(req.body.petitionid)) { |
|||
var newClient = { |
|||
client: trainer.clientsPetitions[i].clientid, |
|||
petitionMessage: trainer.clientsPetitions[i].message, |
|||
date: Date() |
|||
}; |
|||
trainer.clients.push(newClient); |
|||
|
|||
//la peticiรณ la marco com a accepted
|
|||
trainer.clientsPetitions[i].state = "accepted"; |
|||
trainer.save(function (err) { |
|||
if (err) return res.send(500, err.message); |
|||
|
|||
userModel.findOne({_id: trainer._id, role: 'trainer'}) |
|||
.lean() |
|||
.populate('routines', 'title description') |
|||
.populate('clients.client', 'name avatar points') |
|||
.populate('clientsPetitions.clientid', 'name avatar') |
|||
.exec(function (err, trainer) { |
|||
if (err) return res.send(500, err.message); |
|||
res.status(200).jsonp(trainer); |
|||
}); |
|||
}); |
|||
//ara afegim el trainer al user.trainer
|
|||
userModel.findOne({'_id': trainer.clientsPetitions[i].clientid}, function (err, user) { |
|||
if (err) console.log(err.message); |
|||
if (!user) { |
|||
console.log('adding client to trainer failed. user not found.'); |
|||
} else if (user) { |
|||
user.trainers.push(trainer._id); |
|||
|
|||
/* gamification */ |
|||
var reward = { |
|||
concept: "new trainer", |
|||
date: Date(), |
|||
value: +5 |
|||
}; |
|||
user.points.history.push(reward); |
|||
user.points.total = user.points.total + 5; |
|||
/* end of gamification */ |
|||
|
|||
var notification = { |
|||
state: "pendent", |
|||
message: "trainer has accepted to train you", |
|||
link: "training", |
|||
icon: "newtrainer.png", |
|||
date: Date() |
|||
}; |
|||
user.notifications.push(notification); |
|||
user.save(function (err) { |
|||
if (err) console.log(err.message); |
|||
console.log("trainer added to user"); |
|||
}); |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
|
|||
}//end else if
|
|||
}); |
|||
}; |
|||
|
|||
/** PUT '/trainers/:trainerid' **/ |
|||
exports.updateTrainer = function (req, res) { |
|||
var trainer = req.body; |
|||
userModel.update({'tokens.token': req.headers['x-access-token']}, trainer, |
|||
function (err) { |
|||
if (err) return console.log(err); |
|||
console.log(trainer); |
|||
res.status(200).jsonp(trainer); |
|||
}); |
|||
}; |
|||
|
|||
/** POST '/trainers/valorateTrainer/:trainerid' **/ |
|||
exports.valorateTrainer = function (req, res) { |
|||
userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!user) { |
|||
res.json({success: false, message: 'sending valoration failed. user not found.'}); |
|||
} else if (user) { |
|||
//ara busquem el trainer
|
|||
userModel.findOne({_id: req.params.trainerid, role: 'trainer'}, function (err, trainer) { |
|||
if (err) return res.send(500, err.message); |
|||
if (!trainer) { |
|||
res.json({success: false, message: 'sending valoration failed. trainer not found.'}); |
|||
} else if (trainer) { |
|||
//comprovem que el client no hagi valorat ja el trainer
|
|||
var javalorat = false; |
|||
var indexValoration=-1; |
|||
for (var i = 0; i < trainer.valorations.length; i++) { |
|||
|
|||
if (trainer.valorations[i].clientid.equals(user._id)) { |
|||
javalorat = true; |
|||
indexValoration=JSON.parse(JSON.stringify(i)); |
|||
} |
|||
} |
|||
|
|||
if (javalorat == false) { |
|||
var valoration = { |
|||
clientid: user._id, |
|||
date: Date(), |
|||
message: req.body.message, |
|||
value: req.body.value |
|||
}; |
|||
if(!trainer.valoration) |
|||
{ |
|||
trainer.valoration=0; |
|||
} |
|||
var actual = (+trainer.valoration) * trainer.valorations.length; |
|||
var valor = ((+actual) + (+valoration.value)) / (trainer.valorations.length + 1); |
|||
trainer.valoration = valor; |
|||
trainer.valorations.push(valoration); |
|||
|
|||
var notification = { |
|||
state: "pendent", |
|||
message: "client has valorated you", |
|||
link: "dashboard", |
|||
icon: "newvaloration.png", |
|||
date: Date() |
|||
}; |
|||
trainer.notifications.push(notification); |
|||
|
|||