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.

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