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.

516 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. .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. user: user._id
  142. });
  143. notification.save(function(err, notification) {
  144. if (err) return res.send(500, err.message);
  145. user.notifications.push(notification._id);
  146. user.save(function(err, user) {
  147. if (err) return res.send(500, err.message);
  148. console.log("notification saved");
  149. exports.getTravelById(req, res);
  150. });
  151. });
  152. }
  153. });//end saving notification
  154. });
  155. }//end of else if travel
  156. });
  157. }//end of else if user
  158. });
  159. };
  160. exports.unJoin = function(req, res) {
  161. userModel.findOne({'token': req.headers['x-access-token']})
  162. .exec(function(err, userJoining){
  163. if (!userJoining) {
  164. res.json({success: false, message: 'userJoining not found.'});
  165. } else if (userJoining) {
  166. travelModel.findOne({
  167. _id: req.params.travelid,
  168. joinPetitions: userJoining._id
  169. })
  170. .exec(function(err, travel){
  171. if (err) return res.send(500, err.message);
  172. if (!travel) {
  173. res.json({success: false, message: 'can not unjoin this travel'});
  174. } else if (travel) {
  175. for(var i=0; i<travel.joinPetitions.length; i++)
  176. {
  177. if(travel.joinPetitions[i].equals(userJoining._id))
  178. {
  179. travel.joinPetitions.splice(i, 1);
  180. }
  181. }
  182. travel.save(function(err, travel) {
  183. if(err) return res.send(500, err.message);
  184. //start saving notification, get user owner of travel
  185. userModel.findOne({_id: travel.user})
  186. .exec(function(err, user){
  187. if (err) return res.send(500, err.message);
  188. if (!user) {
  189. res.json({success: false, message: 'User not found.'});
  190. } else if (user) {
  191. //notification
  192. var notification = new notificationModel({
  193. concept: "unjoin",
  194. message: "user "+userJoining.username+" unjoins your travel "+travel.title,
  195. date: new Date(),
  196. icon: 'unjoin.png',
  197. link: "travels/" + travel._id,
  198. user: user._id
  199. });
  200. notification.save(function(err, notification) {
  201. if (err) return res.send(500, err.message);
  202. user.notifications.push(notification._id);
  203. user.save(function(err, user) {
  204. if (err) return res.send(500, err.message);
  205. console.log("notification saved");
  206. exports.getTravelById(req, res);
  207. });
  208. });
  209. }
  210. });//end saving notification
  211. });
  212. }
  213. });
  214. }
  215. });
  216. };
  217. exports.declineJoin = function(req, res) {
  218. userModel.findOne({'token': req.headers['x-access-token']})
  219. .exec(function(err, userOwner){
  220. if (err) return res.send(500, err.message);
  221. if (!userOwner) {
  222. res.json({success: false, message: 'User not found.'});
  223. } else if (userOwner) {
  224. travelModel.findOne({
  225. _id: req.params.travelid,
  226. user: userOwner._id,
  227. joinPetitions: req.body.userid
  228. })
  229. .exec(function(err, travel){
  230. if (err) return res.send(500, err.message);
  231. if (!travel) {
  232. 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'});
  233. } else if (travel) {
  234. var indexPetition=-1;
  235. for(var i=0; i<travel.joinPetitions.length; i++)
  236. {
  237. if(travel.joinPetitions[i].equals(req.body.userid))
  238. {
  239. indexPetition=JSON.parse(JSON.stringify(i));
  240. }
  241. }
  242. if(indexPetition>-1)
  243. {
  244. travel.joinPetitions.splice(indexPetition, 1);
  245. }
  246. travel.save(function(err, travel) {
  247. if(err) return res.send(500, err.message);
  248. //start saving notification, get user owner of travel
  249. userModel.findOne({_id: req.body.userid})
  250. .exec(function(err, user){
  251. if (err) return res.send(500, err.message);
  252. if (!user) {
  253. res.json({success: false, message: 'User not found.'});
  254. } else if (user) {
  255. //notification
  256. var notification = new notificationModel({
  257. concept: "travel",
  258. message: "user "+userOwner.username+" declines your petition for "+travel.title,
  259. date: new Date(),
  260. icon: 'travel.png',
  261. link: "travels/" + travel._id,
  262. user: user._id
  263. });
  264. notification.save(function(err, notification) {
  265. if (err) return res.send(500, err.message);
  266. user.notifications.push(notification._id);
  267. user.save(function(err, user) {
  268. if (err) return res.send(500, err.message);
  269. console.log("notification saved");
  270. exports.getTravelById(req, res);
  271. });
  272. });
  273. }
  274. });//end saving notification
  275. });//end of travel save
  276. }//end of else if travel
  277. });
  278. }//end of else if user
  279. });
  280. };
  281. exports.acceptJoin = function(req, res) {
  282. userModel.findOne({'token': req.headers['x-access-token']})
  283. .exec(function(err, userOwner){
  284. if (err) return res.send(500, err.message);
  285. if (!userOwner) {
  286. res.json({success: false, message: 'User not found.'});
  287. } else if (userOwner) {
  288. travelModel.findOne({
  289. _id: req.params.travelid,
  290. user: userOwner._id,
  291. joinPetitions: req.body.userid
  292. })
  293. .exec(function(err, travel){
  294. if (err) return res.send(500, err.message);
  295. if (!travel) {
  296. 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'});
  297. } else if (travel) {
  298. var indexPetition=-1;
  299. for(var i=0; i<travel.joinPetitions.length; i++)
  300. {
  301. if(travel.joinPetitions[i].equals(req.body.userid))
  302. {
  303. indexPetition=JSON.parse(JSON.stringify(i));
  304. }
  305. }
  306. if(indexPetition>-1)
  307. {
  308. travel.joins.push(JSON.parse(JSON.stringify(travel.joinPetitions[indexPetition])));
  309. travel.joinPetitions.splice(indexPetition, 1);
  310. console.log(travel);
  311. }
  312. travel.save(function(err, travel) {
  313. if(err) return res.send(500, err.message);
  314. //start saving notification, get user owner of travel
  315. userModel.findOne({_id: req.body.userid})
  316. .exec(function(err, user){
  317. if (err) return res.send(500, err.message);
  318. if (!user) {
  319. res.json({success: false, message: 'User not found.'});
  320. } else if (user) {
  321. //notification
  322. var notification = new notificationModel({
  323. concept: "travel",
  324. message: "user "+userOwner.username+" accepts your petition for "+travel.title,
  325. date: new Date(),
  326. icon: 'travel.png',
  327. link: "travels/" + travel._id,
  328. user: user._id
  329. });
  330. notification.save(function(err, notification) {
  331. if (err) return res.send(500, err.message);
  332. user.notifications.push(notification._id);
  333. user.save(function(err, user) {
  334. if (err) return res.send(500, err.message);
  335. console.log("notification saved");
  336. exports.getTravelById(req, res);
  337. });
  338. });
  339. }
  340. });//end saving notification
  341. });//end of travel save
  342. }//end of else if travel
  343. });
  344. }//end of else if user
  345. });
  346. };
  347. exports.leave = function(req, res) {
  348. userModel.findOne({'token': req.headers['x-access-token']})
  349. .exec(function(err, userLeaving){
  350. if (!userLeaving) {
  351. res.json({success: false, message: 'userLeaving not found.'});
  352. } else if (userLeaving) {
  353. travelModel.findOne({
  354. _id: req.params.travelid,
  355. joins: userLeaving._id
  356. })
  357. .exec(function(err, travel){
  358. if (err) return res.send(500, err.message);
  359. if (!travel) {
  360. res.json({success: false, message: 'can not unjoin this travel'});
  361. } else if (travel) {
  362. for(var i=0; i<travel.joins.length; i++)
  363. {
  364. if(travel.joins[i].equals(userLeaving._id))
  365. {
  366. travel.joins.splice(i, 1);
  367. }
  368. }
  369. travel.save(function(err, travel) {
  370. if(err) return res.send(500, err.message);
  371. //start saving notification, get user owner of travel
  372. userModel.findOne({_id: travel.user})
  373. .exec(function(err, user){
  374. if (err) return res.send(500, err.message);
  375. if (!user) {
  376. res.json({success: false, message: 'User not found.'});
  377. } else if (user) {
  378. //notification
  379. var notification = new notificationModel({
  380. concept: "leave",
  381. message: "user "+userLeaving.username+" leaves your travel "+travel.title,
  382. date: new Date(),
  383. icon: 'leave.png',
  384. link: "travels/" + travel._id,
  385. user: user._id
  386. });
  387. notification.save(function(err, notification) {
  388. if (err) return res.send(500, err.message);
  389. user.notifications.push(notification._id);
  390. user.save(function(err, user) {
  391. if (err) return res.send(500, err.message);
  392. console.log("notification saved");
  393. exports.getTravelById(req, res);
  394. });
  395. });
  396. }
  397. });//end saving notification
  398. });
  399. }
  400. });
  401. }
  402. });
  403. };
  404. /* comment */
  405. exports.addComment = function(req, res) {
  406. /*var comment = new commentModel({
  407. travelId: req.params.travelId,
  408. commentUserId: req.body.commentUserId,
  409. commentUsername: req.body.commentUsername,
  410. comment: req.body.comment,
  411. commentAvatar: req.body.commentAvatar
  412. });
  413. comment.save(function(err, comment) {
  414. if(err) return res.send(500, err.message);
  415. res.status(200).jsonp(comment);
  416. });*/
  417. userModel.find({
  418. token: req.headers['x-access-token']
  419. }, function(err, users){
  420. var user=users[0];
  421. travelModel.findById(req.params.travelId, function(err, travel){
  422. console.log(travel.title);
  423. var comment = {
  424. commentUserId: user._id,
  425. commentUsername: user.username,
  426. comment: req.body.comment,
  427. commentAvatar: user.avatar
  428. };
  429. travel.comments.push(comment);
  430. travel.save(function(err, travel) {
  431. if(err) return res.send(500, err.message);
  432. //res.status(200).jsonp(travel);
  433. travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
  434. if(err) res.send(500, err.message);
  435. res.status(200).jsonp(travels);
  436. });
  437. });
  438. //start saving notification, get user owner of travel
  439. userModel.find({
  440. username: travel.owner
  441. }, function(err, userowners) {
  442. var userowner=userowners[0];
  443. //notification
  444. var notification = {
  445. concept: "comment",
  446. otherusername: user.username,
  447. description: "user "+user.username+" comments your travel "+travel.title,
  448. date: new Date(),
  449. link: "",
  450. user: userowners[0]._id
  451. };
  452. userowner.notifications.push(notification);
  453. userowner.save(function(err, userowner) {
  454. console.log("notification saved");
  455. });
  456. });//end saving notification
  457. });
  458. });//end of userModel.find
  459. };
  460. exports.getCommentsByTravelId = function(req, res) {
  461. commentModel.find({
  462. travelId: req.params.travelId
  463. }, function(err, comments) {
  464. if (err) throw err;
  465. if (!comments) {
  466. res.json({ success: false, message: 'no comments for travelId' });
  467. } else if (comments) {
  468. // return the information including token as JSON
  469. res.jsonp(comments);
  470. }
  471. });
  472. };