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.

526 lines
16 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.declineJoin = 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.joinPetitions.splice(indexPetition, 1);
  241. }
  242. travel.save(function(err, travel) {
  243. if(err) return res.send(500, err.message);
  244. //start saving notification, get user owner of travel
  245. userModel.findOne({_id: req.body.userid})
  246. .exec(function(err, user){
  247. if (err) return res.send(500, err.message);
  248. if (!user) {
  249. res.json({success: false, message: 'User not found.'});
  250. } else if (user) {
  251. //notification
  252. var notification = new notificationModel({
  253. concept: "travel",
  254. message: "user "+userOwner.username+" declines your petition for "+travel.title,
  255. date: new Date(),
  256. icon: 'travel.png',
  257. link: "travels/" + travel._id
  258. });
  259. notification.save(function(err, notification) {
  260. if (err) return res.send(500, err.message);
  261. user.notifications.push(notification._id);
  262. user.save(function(err, user) {
  263. if (err) return res.send(500, err.message);
  264. console.log("notification saved");
  265. exports.getTravelById(req, res);
  266. });
  267. });
  268. }
  269. });//end saving notification
  270. });//end of travel save
  271. }//end of else if travel
  272. });
  273. }//end of else if user
  274. });
  275. };
  276. exports.acceptJoin = function(req, res) {
  277. userModel.findOne({'token': req.headers['x-access-token']})
  278. .exec(function(err, userOwner){
  279. if (err) return res.send(500, err.message);
  280. if (!userOwner) {
  281. res.json({success: false, message: 'User not found.'});
  282. } else if (userOwner) {
  283. travelModel.findOne({
  284. _id: req.params.travelid,
  285. user: userOwner._id,
  286. joinPetitions: req.body.userid
  287. })
  288. .exec(function(err, travel){
  289. if (err) return res.send(500, err.message);
  290. if (!travel) {
  291. 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'});
  292. } else if (travel) {
  293. var indexPetition=-1;
  294. for(var i=0; i<travel.joinPetitions.length; i++)
  295. {
  296. if(travel.joinPetitions[i].equals(req.body.userid))
  297. {
  298. indexPetition=JSON.parse(JSON.stringify(i));
  299. }
  300. }
  301. if(indexPetition>-1)
  302. {
  303. travel.joins.push(JSON.parse(JSON.stringify(travel.joinPetitions[indexPetition])));
  304. travel.joinPetitions.splice(indexPetition, 1);
  305. console.log(travel);
  306. }
  307. travel.save(function(err, travel) {
  308. if(err) return res.send(500, err.message);
  309. //start saving notification, get user owner of travel
  310. userModel.findOne({_id: req.body.userid})
  311. .exec(function(err, user){
  312. if (err) return res.send(500, err.message);
  313. if (!user) {
  314. res.json({success: false, message: 'User not found.'});
  315. } else if (user) {
  316. //notification
  317. var notification = new notificationModel({
  318. concept: "travel",
  319. message: "user "+userOwner.username+" accepts your petition for "+travel.title,
  320. date: new Date(),
  321. icon: 'travel.png',
  322. link: "travels/" + travel._id
  323. });
  324. notification.save(function(err, notification) {
  325. if (err) return res.send(500, err.message);
  326. user.notifications.push(notification._id);
  327. user.save(function(err, user) {
  328. if (err) return res.send(500, err.message);
  329. console.log("notification saved");
  330. exports.getTravelById(req, res);
  331. });
  332. });
  333. }
  334. });//end saving notification
  335. });//end of travel save
  336. }//end of else if travel
  337. });
  338. }//end of else if user
  339. });
  340. };
  341. exports.leave = function(req, res) {
  342. userModel.findOne({'token': req.headers['x-access-token']})
  343. .exec(function(err, userLeaving){
  344. if (!userLeaving) {
  345. res.json({success: false, message: 'userLeaving not found.'});
  346. } else if (userLeaving) {
  347. travelModel.findOne({
  348. _id: req.params.travelid,
  349. joins: userLeaving._id
  350. })
  351. .exec(function(err, travel){
  352. if (err) return res.send(500, err.message);
  353. if (!travel) {
  354. res.json({success: false, message: 'can not unjoin this travel'});
  355. } else if (travel) {
  356. for(var i=0; i<travel.joins.length; i++)
  357. {
  358. if(travel.joins[i].equals(userLeaving._id))
  359. {
  360. travel.joins.splice(i, 1);
  361. }
  362. }
  363. travel.save(function(err, travel) {
  364. if(err) return res.send(500, err.message);
  365. //start saving notification, get user owner of travel
  366. userModel.findOne({_id: travel.user})
  367. .exec(function(err, user){
  368. if (err) return res.send(500, err.message);
  369. if (!user) {
  370. res.json({success: false, message: 'User not found.'});
  371. } else if (user) {
  372. //notification
  373. var notification = new notificationModel({
  374. concept: "leave",
  375. message: "user "+userLeaving.username+" leaves your travel "+travel.title,
  376. date: new Date(),
  377. icon: 'leave.png',
  378. link: "travels/" + travel._id
  379. });
  380. notification.save(function(err, notification) {
  381. if (err) return res.send(500, err.message);
  382. user.notifications.push(notification._id);
  383. user.save(function(err, user) {
  384. if (err) return res.send(500, err.message);
  385. console.log("notification saved");
  386. exports.getTravelById(req, res);
  387. });
  388. });
  389. }
  390. });//end saving notification
  391. });
  392. }
  393. });
  394. }
  395. });
  396. };
  397. exports.getTravelsByUserId = function(req, res) {
  398. travelModel.find({
  399. user: req.params.userid
  400. })
  401. .lean()
  402. .populate('joins', 'username avatar')
  403. .populate('comments', 'comment user')
  404. .exec(function (err, travels) {
  405. if (err) return res.send(500, err.message);
  406. if (!travels) {
  407. res.json({success: false, message: 'travel not found.'});
  408. } else if (travels) {
  409. res.status(200).jsonp(travels);
  410. }
  411. });
  412. };
  413. /* comment */
  414. exports.addComment = function(req, res) {
  415. /*var comment = new commentModel({
  416. travelId: req.params.travelId,
  417. commentUserId: req.body.commentUserId,
  418. commentUsername: req.body.commentUsername,
  419. comment: req.body.comment,
  420. commentAvatar: req.body.commentAvatar
  421. });
  422. comment.save(function(err, comment) {
  423. if(err) return res.send(500, err.message);
  424. res.status(200).jsonp(comment);
  425. });*/
  426. userModel.find({
  427. token: req.headers['x-access-token']
  428. }, function(err, users){
  429. var user=users[0];
  430. travelModel.findById(req.params.travelId, function(err, travel){
  431. console.log(travel.title);
  432. var comment = {
  433. commentUserId: user._id,
  434. commentUsername: user.username,
  435. comment: req.body.comment,
  436. commentAvatar: user.avatar
  437. };
  438. travel.comments.push(comment);
  439. travel.save(function(err, travel) {
  440. if(err) return res.send(500, err.message);
  441. //res.status(200).jsonp(travel);
  442. travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
  443. if(err) res.send(500, err.message);
  444. res.status(200).jsonp(travels);
  445. });
  446. });
  447. //start saving notification, get user owner of travel
  448. userModel.find({
  449. username: travel.owner
  450. }, function(err, userowners) {
  451. var userowner=userowners[0];
  452. //notification
  453. var notification = {
  454. concept: "comment",
  455. otherusername: user.username,
  456. description: "user "+user.username+" comments your travel "+travel.title,
  457. date: new Date(),
  458. link: ""
  459. };
  460. userowner.notifications.push(notification);
  461. userowner.save(function(err, userowner) {
  462. console.log("notification saved");
  463. });
  464. });//end saving notification
  465. });
  466. });//end of userModel.find
  467. };
  468. exports.getCommentsByTravelId = function(req, res) {
  469. commentModel.find({
  470. travelId: req.params.travelId
  471. }, function(err, comments) {
  472. if (err) throw err;
  473. if (!comments) {
  474. res.json({ success: false, message: 'no comments for travelId' });
  475. } else if (comments) {
  476. // return the information including token as JSON
  477. res.jsonp(comments);
  478. }
  479. });
  480. };