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.

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