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.

519 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
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. var config = require('../config');
  8. var pageSize=config.pageSize;
  9. //GET
  10. exports.getAllTravels = function(req, res) {
  11. //get travels with futures dates ($gte - greater than and equal than)
  12. travelModel.find({date: {$gte: new Date()}})
  13. .sort('date')
  14. .limit(pageSize)
  15. .skip(pageSize * Number(req.query.page))
  16. .lean()
  17. .populate('user', 'username avatar')
  18. .exec(function (err, travels) {
  19. if (err) return res.send(500, err.message);
  20. res.status(200).jsonp(travels);
  21. });
  22. };
  23. exports.getTravelById = function (req, res) {
  24. travelModel.findOne({_id: req.params.travelid})
  25. .lean()
  26. .populate('user', 'username avatar telegram phone')
  27. .populate('joins', 'username avatar')
  28. .populate('joinPetitions', 'username avatar')
  29. .populate('comments', 'comment user')
  30. .exec(function (err, travel) {
  31. if (err) return res.send(500, err.message);
  32. if (!travel) {
  33. res.json({success: false, message: 'travel not found.'});
  34. } else if (travel) {
  35. res.status(200).jsonp(travel);
  36. }
  37. });
  38. };
  39. exports.addTravel = function(req, res) {
  40. userModel.findOne({'token': req.headers['x-access-token']})
  41. .exec(function(err, user){
  42. if (err) return res.send(500, err.message);
  43. if (!user) {
  44. console.log("user not found");
  45. res.json({success: false, message: 'User not found.'});
  46. } else if (user) {
  47. var travel = new travelModel({
  48. title: req.body.title,
  49. description: req.body.description,
  50. user: user._id,
  51. from: req.body.from,
  52. to: req.body.to,
  53. date: req.body.date,
  54. periodic: req.body.periodic,
  55. generateddate: Date(),
  56. seats: req.body.seats,
  57. package: req.body.package,
  58. collectivized: req.body.collectivized,
  59. type: req.body.type
  60. });
  61. travel.save(function(err, travel) {
  62. if(err) return res.send(500, err.message);
  63. user.travels.push(travel._id);
  64. user.save(function (err, user) {
  65. if (err) return res.send(500, err.message);
  66. exports.getAllTravels(req, res);
  67. });
  68. });//end of travel.save
  69. }
  70. });//end of usermodel.find
  71. };
  72. exports.updateTravel = function(req, res) {
  73. userModel.findOne({'token': req.headers['x-access-token']})
  74. .exec(function(err, user){
  75. if (err) return res.send(500, err.message);
  76. console.log(travel);
  77. travelModel.findOne({_id: travel._id})
  78. .lean()
  79. .populate('travels', 'title from to date')
  80. .exec(function (err, travel) {
  81. if (err) return res.send(500, err.message);
  82. if (!travel) {
  83. res.json({success: false, message: 'travel not found.'});
  84. } else if (travel) {
  85. res.status(200).jsonp(travel);
  86. }
  87. });
  88. });
  89. };
  90. //DELETE
  91. exports.deleteTravel = function(req, res) {
  92. userModel.findOne({'token': req.headers['x-access-token']})
  93. .exec(function(err, user){
  94. if (err) return res.send(500, err.message);
  95. travelModel.findById(req.params.travelid, function(err, travel) {
  96. if (err) return res.send(500, err.message);
  97. if(travel.user.equals(user._id))
  98. {
  99. travel.remove(function(err) {
  100. if(err) return res.send(500, err.message);
  101. console.log("deleted");
  102. exports.getAllTravels(req, res);
  103. });
  104. }
  105. });
  106. });
  107. };
  108. /* join */
  109. exports.addJoinPetition = function(req, res) {
  110. userModel.findOne({'token': req.headers['x-access-token']})
  111. .exec(function(err, userJoining){
  112. if (err) return res.send(500, err.message);
  113. if (!userJoining) {
  114. res.json({success: false, message: 'User not found.'});
  115. } else if (userJoining) {
  116. travelModel.findOne({
  117. _id: req.params.travelid,
  118. user: {'$ne': userJoining._id},
  119. joins: {'$ne': userJoining._id},
  120. joinPetitions: {'$ne': userJoining._id}
  121. })
  122. .exec(function(err, travel){
  123. if (err) return res.send(500, err.message);
  124. if (!travel) {
  125. 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'});
  126. } else if (travel) {
  127. travel.joinPetitions.push(userJoining._id);
  128. travel.save(function(err, travel) {
  129. if(err) return res.send(500, err.message);
  130. //start saving notification, get user owner of travel
  131. userModel.findOne({_id: travel.user})
  132. .exec(function(err, user){
  133. if (err) return res.send(500, err.message);
  134. if (!user) {
  135. res.json({success: false, message: 'User not found.'});
  136. } else if (user) {
  137. //notification
  138. var notification = new notificationModel({
  139. concept: "join",
  140. message: "user "+userJoining.username+" joins your travel "+travel.title,
  141. date: new Date(),
  142. icon: 'join.png',
  143. link: "travels/" + travel._id,
  144. user: user._id
  145. });
  146. notification.save(function(err, notification) {
  147. if (err) return res.send(500, err.message);
  148. user.notifications.push(notification._id);
  149. user.save(function(err, user) {
  150. if (err) return res.send(500, err.message);
  151. console.log("notification saved");
  152. exports.getTravelById(req, res);
  153. });
  154. });
  155. }
  156. });//end saving notification
  157. });
  158. }//end of else if travel
  159. });
  160. }//end of else if user
  161. });
  162. };
  163. exports.unJoin = function(req, res) {
  164. userModel.findOne({'token': req.headers['x-access-token']})
  165. .exec(function(err, userJoining){
  166. if (!userJoining) {
  167. res.json({success: false, message: 'userJoining not found.'});
  168. } else if (userJoining) {
  169. travelModel.findOne({
  170. _id: req.params.travelid,
  171. joinPetitions: userJoining._id
  172. })
  173. .exec(function(err, travel){
  174. if (err) return res.send(500, err.message);
  175. if (!travel) {
  176. res.json({success: false, message: 'can not unjoin this travel'});
  177. } else if (travel) {
  178. for(var i=0; i<travel.joinPetitions.length; i++)
  179. {
  180. if(travel.joinPetitions[i].equals(userJoining._id))
  181. {
  182. travel.joinPetitions.splice(i, 1);
  183. }
  184. }
  185. travel.save(function(err, travel) {
  186. if(err) return res.send(500, err.message);
  187. //start saving notification, get user owner of travel
  188. userModel.findOne({_id: travel.user})
  189. .exec(function(err, user){
  190. if (err) return res.send(500, err.message);
  191. if (!user) {
  192. res.json({success: false, message: 'User not found.'});
  193. } else if (user) {
  194. //notification
  195. var notification = new notificationModel({
  196. concept: "unjoin",
  197. message: "user "+userJoining.username+" unjoins your travel "+travel.title,
  198. date: new Date(),
  199. icon: 'unjoin.png',
  200. link: "travels/" + travel._id,
  201. user: user._id
  202. });
  203. notification.save(function(err, notification) {
  204. if (err) return res.send(500, err.message);
  205. user.notifications.push(notification._id);
  206. user.save(function(err, user) {
  207. if (err) return res.send(500, err.message);
  208. console.log("notification saved");
  209. exports.getTravelById(req, res);
  210. });
  211. });
  212. }
  213. });//end saving notification
  214. });
  215. }
  216. });
  217. }
  218. });
  219. };
  220. exports.declineJoin = function(req, res) {
  221. userModel.findOne({'token': req.headers['x-access-token']})
  222. .exec(function(err, userOwner){
  223. if (err) return res.send(500, err.message);
  224. if (!userOwner) {
  225. res.json({success: false, message: 'User not found.'});
  226. } else if (userOwner) {
  227. travelModel.findOne({
  228. _id: req.params.travelid,
  229. user: userOwner._id,
  230. joinPetitions: req.body.userid
  231. })
  232. .exec(function(err, travel){
  233. if (err) return res.send(500, err.message);
  234. if (!travel) {
  235. 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'});
  236. } else if (travel) {
  237. var indexPetition=-1;
  238. for(var i=0; i<travel.joinPetitions.length; i++)
  239. {
  240. if(travel.joinPetitions[i].equals(req.body.userid))
  241. {
  242. indexPetition=JSON.parse(JSON.stringify(i));
  243. }
  244. }
  245. if(indexPetition>-1)
  246. {
  247. travel.joinPetitions.splice(indexPetition, 1);
  248. }
  249. travel.save(function(err, travel) {
  250. if(err) return res.send(500, err.message);
  251. //start saving notification, get user owner of travel
  252. userModel.findOne({_id: req.body.userid})
  253. .exec(function(err, user){
  254. if (err) return res.send(500, err.message);
  255. if (!user) {
  256. res.json({success: false, message: 'User not found.'});
  257. } else if (user) {
  258. //notification
  259. var notification = new notificationModel({
  260. concept: "travel",
  261. message: "user "+userOwner.username+" declines your petition for "+travel.title,
  262. date: new Date(),
  263. icon: 'travel.png',
  264. link: "travels/" + travel._id,
  265. user: user._id
  266. });
  267. notification.save(function(err, notification) {
  268. if (err) return res.send(500, err.message);
  269. user.notifications.push(notification._id);
  270. user.save(function(err, user) {
  271. if (err) return res.send(500, err.message);
  272. console.log("notification saved");
  273. exports.getTravelById(req, res);
  274. });
  275. });
  276. }
  277. });//end saving notification
  278. });//end of travel save
  279. }//end of else if travel
  280. });
  281. }//end of else if user
  282. });
  283. };
  284. exports.acceptJoin = function(req, res) {
  285. userModel.findOne({'token': req.headers['x-access-token']})
  286. .exec(function(err, userOwner){
  287. if (err) return res.send(500, err.message);
  288. if (!userOwner) {
  289. res.json({success: false, message: 'User not found.'});
  290. } else if (userOwner) {
  291. travelModel.findOne({
  292. _id: req.params.travelid,
  293. user: userOwner._id,
  294. joinPetitions: req.body.userid
  295. })
  296. .exec(function(err, travel){
  297. if (err) return res.send(500, err.message);
  298. if (!travel) {
  299. 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'});
  300. } else if (travel) {
  301. var indexPetition=-1;
  302. for(var i=0; i<travel.joinPetitions.length; i++)
  303. {
  304. if(travel.joinPetitions[i].equals(req.body.userid))
  305. {
  306. indexPetition=JSON.parse(JSON.stringify(i));
  307. }
  308. }
  309. if(indexPetition>-1)
  310. {
  311. travel.joins.push(JSON.parse(JSON.stringify(travel.joinPetitions[indexPetition])));
  312. travel.joinPetitions.splice(indexPetition, 1);
  313. console.log(travel);
  314. }
  315. travel.save(function(err, travel) {
  316. if(err) return res.send(500, err.message);
  317. //start saving notification, get user owner of travel
  318. userModel.findOne({_id: req.body.userid})
  319. .exec(function(err, user){
  320. if (err) return res.send(500, err.message);
  321. if (!user) {
  322. res.json({success: false, message: 'User not found.'});
  323. } else if (user) {
  324. //notification
  325. var notification = new notificationModel({
  326. concept: "travel",
  327. message: "user "+userOwner.username+" accepts your petition for "+travel.title,
  328. date: new Date(),
  329. icon: 'travel.png',
  330. link: "travels/" + travel._id,
  331. user: user._id
  332. });
  333. notification.save(function(err, notification) {
  334. if (err) return res.send(500, err.message);
  335. user.notifications.push(notification._id);
  336. user.save(function(err, user) {
  337. if (err) return res.send(500, err.message);
  338. console.log("notification saved");
  339. exports.getTravelById(req, res);
  340. });
  341. });
  342. }
  343. });//end saving notification
  344. });//end of travel save
  345. }//end of else if travel
  346. });
  347. }//end of else if user
  348. });
  349. };
  350. exports.leave = function(req, res) {
  351. userModel.findOne({'token': req.headers['x-access-token']})
  352. .exec(function(err, userLeaving){
  353. if (!userLeaving) {
  354. res.json({success: false, message: 'userLeaving not found.'});
  355. } else if (userLeaving) {
  356. travelModel.findOne({
  357. _id: req.params.travelid,
  358. joins: userLeaving._id
  359. })
  360. .exec(function(err, travel){
  361. if (err) return res.send(500, err.message);
  362. if (!travel) {
  363. res.json({success: false, message: 'can not unjoin this travel'});
  364. } else if (travel) {
  365. for(var i=0; i<travel.joins.length; i++)
  366. {
  367. if(travel.joins[i].equals(userLeaving._id))
  368. {
  369. travel.joins.splice(i, 1);
  370. }
  371. }
  372. travel.save(function(err, travel) {
  373. if(err) return res.send(500, err.message);
  374. //start saving notification, get user owner of travel
  375. userModel.findOne({_id: travel.user})
  376. .exec(function(err, user){
  377. if (err) return res.send(500, err.message);
  378. if (!user) {
  379. res.json({success: false, message: 'User not found.'});
  380. } else if (user) {
  381. //notification
  382. var notification = new notificationModel({
  383. concept: "leave",
  384. message: "user "+userLeaving.username+" leaves your travel "+travel.title,
  385. date: new Date(),
  386. icon: 'leave.png',
  387. link: "travels/" + travel._id,
  388. user: user._id
  389. });
  390. notification.save(function(err, notification) {
  391. if (err) return res.send(500, err.message);
  392. user.notifications.push(notification._id);
  393. user.save(function(err, user) {
  394. if (err) return res.send(500, err.message);
  395. console.log("notification saved");
  396. exports.getTravelById(req, res);
  397. });
  398. });
  399. }
  400. });//end saving notification
  401. });
  402. }
  403. });
  404. }
  405. });
  406. };
  407. /* comment */
  408. exports.addComment = function(req, res) {
  409. /*var comment = new commentModel({
  410. travelId: req.params.travelId,
  411. commentUserId: req.body.commentUserId,
  412. commentUsername: req.body.commentUsername,
  413. comment: req.body.comment,
  414. commentAvatar: req.body.commentAvatar
  415. });
  416. comment.save(function(err, comment) {
  417. if(err) return res.send(500, err.message);
  418. res.status(200).jsonp(comment);
  419. });*/
  420. userModel.find({
  421. token: req.headers['x-access-token']
  422. }, function(err, users){
  423. var user=users[0];
  424. travelModel.findById(req.params.travelId, function(err, travel){
  425. console.log(travel.title);
  426. var comment = {
  427. commentUserId: user._id,
  428. commentUsername: user.username,
  429. comment: req.body.comment,
  430. commentAvatar: user.avatar
  431. };
  432. travel.comments.push(comment);
  433. travel.save(function(err, travel) {
  434. if(err) return res.send(500, err.message);
  435. //res.status(200).jsonp(travel);
  436. travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
  437. if(err) res.send(500, err.message);
  438. res.status(200).jsonp(travels);
  439. });
  440. });
  441. //start saving notification, get user owner of travel
  442. userModel.find({
  443. username: travel.owner
  444. }, function(err, userowners) {
  445. var userowner=userowners[0];
  446. //notification
  447. var notification = {
  448. concept: "comment",
  449. otherusername: user.username,
  450. description: "user "+user.username+" comments your travel "+travel.title,
  451. date: new Date(),
  452. link: "",
  453. user: userowners[0]._id
  454. };
  455. userowner.notifications.push(notification);
  456. userowner.save(function(err, userowner) {
  457. console.log("notification saved");
  458. });
  459. });//end saving notification
  460. });
  461. });//end of userModel.find
  462. };
  463. exports.getCommentsByTravelId = function(req, res) {
  464. commentModel.find({
  465. travelId: req.params.travelId
  466. }, function(err, comments) {
  467. if (err) throw err;
  468. if (!comments) {
  469. res.json({ success: false, message: 'no comments for travelId' });
  470. } else if (comments) {
  471. // return the information including token as JSON
  472. res.jsonp(comments);
  473. }
  474. });
  475. };