You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

400 lines
12 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. //File: controllers/travelController.js
  2. var mongoose = require('mongoose');
  3. var userModel = mongoose.model('userModel');
  4. var notificationModel = mongoose.model('notificationModel');
  5. var travelModel = mongoose.model('travelModel');
  6. var commentModel = mongoose.model('commentModel');
  7. //GET
  8. exports.getAllTravels = function(req, res) {
  9. //get travels with futures dates ($gte - greater than and equal than)
  10. travelModel.find({date: {$gte: new Date()}})
  11. .limit(Number(req.query.pageSize))
  12. .skip(Number(req.query.pageSize) * Number(req.query.page))
  13. .exec(function (err, travels) {
  14. if (err) return res.send(500, err.message);
  15. res.status(200).jsonp(travels);
  16. });
  17. };
  18. exports.getTravelById = function (req, res) {
  19. travelModel.findOne({_id: req.params.travelid})
  20. .lean()
  21. .populate('user', 'username avatar telegram phone')
  22. .populate('joins', 'username avatar')
  23. .populate('joinPetitions', 'username avatar')
  24. .populate('comments', 'comment user')
  25. .exec(function (err, travel) {
  26. if (err) return res.send(500, err.message);
  27. if (!travel) {
  28. res.json({success: false, message: 'travel not found.'});
  29. } else if (travel) {
  30. res.status(200).jsonp(travel);
  31. }
  32. });
  33. };
  34. exports.addTravel = function(req, res) {
  35. userModel.findOne({'token': req.headers['x-access-token']})
  36. .exec(function(err, user){
  37. if (err) return res.send(500, err.message);
  38. if (!user) {
  39. console.log("user not found");
  40. res.json({success: false, message: 'User not found.'});
  41. } else if (user) {
  42. var travel = new travelModel({
  43. title: req.body.title,
  44. description: req.body.description,
  45. user: user._id,
  46. from: req.body.from,
  47. to: req.body.to,
  48. date: req.body.date,
  49. periodic: req.body.periodic,
  50. generateddate: Date(),
  51. seats: req.body.seats,
  52. package: req.body.package,
  53. collectivized: req.body.collectivized,
  54. type: req.body.type
  55. });
  56. travel.save(function(err, travel) {
  57. if(err) return res.send(500, err.message);
  58. user.travels.push(travel._id);
  59. user.save(function (err, user) {
  60. if (err) return res.send(500, err.message);
  61. exports.getAllTravels(req, res);
  62. });
  63. });//end of travel.save
  64. }
  65. });//end of usermodel.find
  66. };
  67. exports.updateTravel = function(req, res) {
  68. userModel.findOne({'token': req.headers['x-access-token']})
  69. .exec(function(err, user){
  70. if (err) return res.send(500, err.message);
  71. console.log(travel);
  72. travelModel.findOne({_id: travel._id})
  73. .lean()
  74. .populate('travels', 'title from to date')
  75. .exec(function (err, travel) {
  76. if (err) return res.send(500, err.message);
  77. if (!travel) {
  78. res.json({success: false, message: 'travel not found.'});
  79. } else if (travel) {
  80. res.status(200).jsonp(travel);
  81. }
  82. });
  83. });
  84. };
  85. //DELETE
  86. exports.deleteTravel = function(req, res) {
  87. userModel.findOne({'token': req.headers['x-access-token']})
  88. .exec(function(err, user){
  89. if (err) return res.send(500, err.message);
  90. travelModel.findById(req.params.travelid, function(err, travel) {
  91. if (err) return res.send(500, err.message);
  92. if(travel.user.equals(user._id))
  93. {
  94. travel.remove(function(err) {
  95. if(err) return res.send(500, err.message);
  96. console.log("deleted");
  97. exports.getAllTravels(req, res);
  98. });
  99. }
  100. });
  101. });
  102. };
  103. /* join */
  104. exports.addJoinPetition = function(req, res) {
  105. userModel.findOne({'token': req.headers['x-access-token']})
  106. .exec(function(err, userJoining){
  107. if (err) return res.send(500, err.message);
  108. if (!userJoining) {
  109. res.json({success: false, message: 'User not found.'});
  110. } else if (userJoining) {
  111. travelModel.findOne({
  112. _id: req.params.travelid,
  113. user: {'$ne': userJoining._id},
  114. joins: {'$ne': userJoining._id},
  115. joinPetitions: {'$ne': userJoining._id}
  116. })
  117. .exec(function(err, travel){
  118. if (err) return res.send(500, err.message);
  119. if (!travel) {
  120. res.json({success: false, message: 'travel not found. You can not join a travel if you have created it, or if you have already joined'});
  121. } else if (travel) {
  122. travel.joinPetitions.push(userJoining._id);
  123. travel.save(function(err, travel) {
  124. if(err) return res.send(500, err.message);
  125. //start saving notification, get user owner of travel
  126. userModel.findOne({_id: travel.user})
  127. .exec(function(err, user){
  128. if (err) return res.send(500, err.message);
  129. if (!user) {
  130. res.json({success: false, message: 'User not found.'});
  131. } else if (user) {
  132. //notification
  133. var notification = new notificationModel({
  134. concept: "join",
  135. message: "user "+userJoining.username+" joins your travel "+travel.title,
  136. date: new Date(),
  137. icon: 'join.png',
  138. link: "travels/" + travel._id
  139. });
  140. notification.save(function(err, notification) {
  141. if (err) return res.send(500, err.message);
  142. user.notifications.push(notification._id);
  143. user.save(function(err, user) {
  144. if (err) return res.send(500, err.message);
  145. console.log("notification saved");
  146. exports.getTravelById(req, res);
  147. });
  148. });
  149. }
  150. });//end saving notification
  151. });
  152. }//end of else if travel
  153. });
  154. }//end of else if user
  155. });
  156. };
  157. exports.unJoin = function(req, res) {
  158. userModel.findOne({'token': req.headers['x-access-token']})
  159. .exec(function(err, userJoining){
  160. if (!userJoining) {
  161. res.json({success: false, message: 'userJoining not found.'});
  162. } else if (userJoining) {
  163. travelModel.findOne({
  164. _id: req.params.travelid,
  165. joinPetitions: userJoining._id
  166. })
  167. .exec(function(err, travel){
  168. if (err) return res.send(500, err.message);
  169. if (!travel) {
  170. res.json({success: false, message: 'can not unjoin this travel'});
  171. } else if (travel) {
  172. for(var i=0; i<travel.joinPetitions.length; i++)
  173. {
  174. if(travel.joinPetitions[i].equals(userJoining._id))
  175. {
  176. travel.joinPetitions.splice(i, 1);
  177. }
  178. }
  179. travel.save(function(err, travel) {
  180. if(err) return res.send(500, err.message);
  181. //start saving notification, get user owner of travel
  182. userModel.findOne({_id: travel.user})
  183. .exec(function(err, user){
  184. if (err) return res.send(500, err.message);
  185. if (!user) {
  186. res.json({success: false, message: 'User not found.'});
  187. } else if (user) {
  188. //notification
  189. var notification = new notificationModel({
  190. concept: "unjoin",
  191. message: "user "+userJoining.username+" unjoins your travel "+travel.title,
  192. date: new Date(),
  193. icon: 'unjoin.png',
  194. link: "travels/" + travel._id
  195. });
  196. notification.save(function(err, notification) {
  197. if (err) return res.send(500, err.message);
  198. user.notifications.push(notification._id);
  199. user.save(function(err, user) {
  200. if (err) return res.send(500, err.message);
  201. console.log("notification saved");
  202. exports.getTravelById(req, res);
  203. });
  204. });
  205. }
  206. });//end saving notification
  207. });
  208. }
  209. });
  210. }
  211. });
  212. };
  213. exports.acceptJoin = function(req, res) {
  214. userModel.findOne({'token': req.headers['x-access-token']})
  215. .exec(function(err, userOwner){
  216. if (err) return res.send(500, err.message);
  217. if (!userOwner) {
  218. res.json({success: false, message: 'User not found.'});
  219. } else if (userOwner) {
  220. travelModel.findOne({
  221. _id: req.params.travelid,
  222. user: userOwner._id,
  223. joinPetitions: req.body.userid
  224. })
  225. .exec(function(err, travel){
  226. if (err) return res.send(500, err.message);
  227. if (!travel) {
  228. res.json({success: false, message: 'travel not found. You can not join a travel if you have created it, or if you have already joined'});
  229. } else if (travel) {
  230. var indexPetition=-1;
  231. for(var i=0; i<travel.joinPetitions.length; i++)
  232. {
  233. if(travel.joinPetitions[i].equals(req.body.userid))
  234. {
  235. indexPetition=JSON.parse(JSON.stringify(i));
  236. }
  237. }
  238. if(indexPetition>-1)
  239. {
  240. travel.joins.push(JSON.parse(JSON.stringify(travel.joinPetitions[indexPetition])));
  241. travel.joinPetitions.splice(indexPetition, 1);
  242. console.log(travel);
  243. }
  244. travel.save(function(err, travel) {
  245. if(err) return res.send(500, err.message);
  246. //start saving notification, get user owner of travel
  247. userModel.findOne({_id: req.body.userid})
  248. .exec(function(err, user){
  249. if (err) return res.send(500, err.message);
  250. if (!user) {
  251. res.json({success: false, message: 'User not found.'});
  252. } else if (user) {
  253. //notification
  254. var notification = new notificationModel({
  255. concept: "travel",
  256. message: "user "+userOwner.username+" accepts your petition for "+travel.title,
  257. date: new Date(),
  258. icon: 'travel.png',
  259. link: "travels/" + travel._id
  260. });
  261. notification.save(function(err, notification) {
  262. if (err) return res.send(500, err.message);
  263. user.notifications.push(notification._id);
  264. user.save(function(err, user) {
  265. if (err) return res.send(500, err.message);
  266. console.log("notification saved");
  267. exports.getTravelById(req, res);
  268. });
  269. });
  270. }
  271. });//end saving notification
  272. });//end of travel save
  273. }//end of else if travel
  274. });
  275. }//end of else if user
  276. });
  277. };
  278. exports.getTravelsByUserId = function(req, res) {
  279. travelModel.find({
  280. user: req.params.userid
  281. })
  282. .lean()
  283. .populate('joins', 'username avatar')
  284. .populate('comments', 'comment user')
  285. .exec(function (err, travels) {
  286. if (err) return res.send(500, err.message);
  287. if (!travels) {
  288. res.json({success: false, message: 'travel not found.'});
  289. } else if (travels) {
  290. res.status(200).jsonp(travels);
  291. }
  292. });
  293. };
  294. /* comment */
  295. exports.addComment = function(req, res) {
  296. /*var comment = new commentModel({
  297. travelId: req.params.travelId,
  298. commentUserId: req.body.commentUserId,
  299. commentUsername: req.body.commentUsername,
  300. comment: req.body.comment,
  301. commentAvatar: req.body.commentAvatar
  302. });
  303. comment.save(function(err, comment) {
  304. if(err) return res.send(500, err.message);
  305. res.status(200).jsonp(comment);
  306. });*/
  307. userModel.find({
  308. token: req.headers['x-access-token']
  309. }, function(err, users){
  310. var user=users[0];
  311. travelModel.findById(req.params.travelId, function(err, travel){
  312. console.log(travel.title);
  313. var comment = {
  314. commentUserId: user._id,
  315. commentUsername: user.username,
  316. comment: req.body.comment,
  317. commentAvatar: user.avatar
  318. };
  319. travel.comments.push(comment);
  320. travel.save(function(err, travel) {
  321. if(err) return res.send(500, err.message);
  322. //res.status(200).jsonp(travel);
  323. travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
  324. if(err) res.send(500, err.message);
  325. res.status(200).jsonp(travels);
  326. });
  327. });
  328. //start saving notification, get user owner of travel
  329. userModel.find({
  330. username: travel.owner
  331. }, function(err, userowners) {
  332. var userowner=userowners[0];
  333. //notification
  334. var notification = {
  335. concept: "comment",
  336. otherusername: user.username,
  337. description: "user "+user.username+" comments your travel "+travel.title,
  338. date: new Date(),
  339. link: ""
  340. };
  341. userowner.notifications.push(notification);
  342. userowner.save(function(err, userowner) {
  343. console.log("notification saved");
  344. });
  345. });//end saving notification
  346. });
  347. });//end of userModel.find
  348. };
  349. exports.getCommentsByTravelId = function(req, res) {
  350. commentModel.find({
  351. travelId: req.params.travelId
  352. }, function(err, comments) {
  353. if (err) throw err;
  354. if (!comments) {
  355. res.json({ success: false, message: 'no comments for travelId' });
  356. } else if (comments) {
  357. // return the information including token as JSON
  358. res.jsonp(comments);
  359. }
  360. });
  361. };