From 1e3bd06b9febc72df5227a66488b917881c527bc Mon Sep 17 00:00:00 2001 From: arnaucode Date: Fri, 28 Jul 2017 19:31:59 +0200 Subject: [PATCH] added generate network map data of user connections (likes, travels) --- controllers/adminController.js | 153 ++++++++++++++++++++++++++++++++- server.js | 2 + 2 files changed, 154 insertions(+), 1 deletion(-) diff --git a/controllers/adminController.js b/controllers/adminController.js index 28e3ce8..27edeef 100644 --- a/controllers/adminController.js +++ b/controllers/adminController.js @@ -288,6 +288,7 @@ exports.network = function(req, res) { var edge = { from: users[i]._id, to: users[i].likes[j]._id, + label: "like", arrows: "to", color: { color: "#E57373" //red300 @@ -318,7 +319,10 @@ exports.network = function(req, res) { } var edge = { from: users[i]._id, - to: users[i].travels[j]._id + to: users[i].travels[j]._id, + color: { + color: "#616161" //grey + } }; edges.push(edge); @@ -358,3 +362,150 @@ exports.network = function(req, res) { } }); }; +exports.getUserNetwork = function(req, res) { + adminModel.findOne({ + 'token': req.headers['x-access-token'] + }) + .exec(function(err, admin) { + if (!admin) { + res.json({ + success: false, + message: 'Admin not found' + }); + } else if (admin) { + if (err) return res.send(500, err.message); + userModel.findOne({ + _id: req.params.userid + }) + .lean() + .populate('travels', 'title type joins') + .populate('likes', 'username avatar') + .exec(function(err, user) { + if (err) return res.send(500, err.message); + + /*res.status(200).jsonp(users);*/ + var nodes = []; + var edges = []; + //for (var i = 0; i < users.length; i++) { + var node = { + title: user.username, + label: user.username, + image: user.avatar, + shape: "image", + id: user._id, + group: user._id + }; + var lNode = isNodeInNodes(node, nodes); + if (lNode < 0) { + nodes.push(node); + var uNode = nodes.length - 1; + } + for (var j = 0; j < user.likes.length; j++) { + /*console.log(i + ", " + j); + console.log(nodes);*/ + var node = { + title: user.likes[j].username, + label: user.likes[j].username, + image: user.likes[j].avatar, + shape: "image", + id: user.likes[j]._id + }; + var lNode = isNodeInNodes(node, nodes); + if (lNode < 0) { + //node no exist + nodes.push(node); + lNode = nodes.length - 1; + } else { + //node already exist + + } + var edge = { + from: user._id, + to: user.likes[j]._id, + label: "like", + arrows: "to", + color: { + color: "#E57373" //red300 + } + }; + edges.push(edge); + } + for (var j = 0; j < user.travels.length; j++) { + /*console.log(i + ", " + j); + console.log(nodes);*/ + var node = { + title: user.travels[j].title, + label: user.travels[j].title, + image: "img/" + user.travels[j].type + ".png", + shape: "image", + id: user.travels[j]._id, + value: "0.5", + group: user._id + }; + var lNode = isNodeInNodes(node, nodes); + if (lNode < 0) { + //node no exist + nodes.push(node); + lNode = nodes.length - 1; + } else { + //node already exist + + } + var edge = { + from: user._id, + to: user.travels[j]._id, + color: { + color: "#616161" //grey + } + }; + edges.push(edge); + } + //add users that has add like to the user + userModel.find({ + likes: req.params.userid + }) + .exec(function(err, users) { + for (var i = 0; i < users.length; i++) { + /*console.log(i + ", " + j); + console.log(nodes);*/ + var node = { + title: users[i].username, + label: users[i].username, + image: users[i].avatar, + shape: "image", + id: users[i]._id + }; + var lNode = isNodeInNodes(node, nodes); + if (lNode < 0) { + //node no exist + nodes.push(node); + lNode = nodes.length - 1; + } else { + //node already exist + + } + var edge = { + from: users[i]._id, + to: req.params.userid, + label: "like", + arrows: "to", + color: { + color: "#E57373" //red300 + } + }; + edges.push(edge); + } + + var resp = { + nodes: nodes, + edges: edges + }; + res.status(200).jsonp(resp); + }); + + //} + + }); + } + }); +}; diff --git a/server.js b/server.js index e4b602f..77b9c1a 100755 --- a/server.js +++ b/server.js @@ -118,6 +118,8 @@ apiRoutes.use(function(req, res, next) { //admin apiRoutes.route('/admin/network') .get(adminCtrl.network); +apiRoutes.route('/admin/user/network/:userid') + .get(adminCtrl.getUserNetwork); apiRoutes.route('/admin/travels/id/:travelid') .delete(adminCtrl.deleteTravel); apiRoutes.route('/admin/users/id/:userid')