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.

525 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
  1. var config = require('../config');
  2. var pageSize=config.pageSize;
  3. //import data models
  4. var mongoose = require('mongoose');
  5. var userModel = mongoose.model('userModel');
  6. var notificationModel = mongoose.model('notificationModel');
  7. var travelModel = mongoose.model('travelModel');
  8. var commentModel = mongoose.model('commentModel');
  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 validated')
  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 validated 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. // search if the travel exist for that user
  76. travelModel.findOne({_id: req.body._id, user: user._id})
  77. .exec(function(err, travel){
  78. if (!travel) {
  79. res.send(500, 'travel not found.');
  80. } else if (travel) {
  81. // now update travel
  82. travelModel.update({
  83. _id: req.body._id, user: user._id
  84. }, req.body,
  85. function(err, travel) {
  86. if (err) {
  87. res.send(500, 'travel not found.');
  88. }
  89. console.log(travel);
  90. res.status(200).jsonp(travel);
  91. });
  92. }
  93. });
  94. });
  95. };
  96. exports.deleteTravel = function(req, res) {
  97. userModel.findOne({'token': req.headers['x-access-token']})
  98. .exec(function(err, user){
  99. if (err) return res.send(500, err.message);
  100. travelModel.findById(req.params.travelid, function(err, travel) {
  101. if (err) return res.send(500, err.message);
  102. if(travel.user.equals(user._id))
  103. {
  104. travel.remove(function(err) {
  105. if(err) return res.send(500, err.message);
  106. console.log("deleted");
  107. exports.getAllTravels(req, res);
  108. });
  109. }
  110. });
  111. });
  112. };
  113. exports.addJoinPetition = function(req, res) {
  114. userModel.findOne({'token': req.headers['x-access-token']})
  115. .exec(function(err, userJoining){
  116. if (err) return res.send(500, err.message);
  117. if (!userJoining) {
  118. res.json({success: false, message: 'User not found.'});
  119. } else if (userJoining) {
  120. travelModel.findOne({
  121. _id: req.params.travelid,
  122. user: {'$ne': userJoining._id},
  123. joins: {'$ne': userJoining._id},
  124. joinPetitions: {'$ne': userJoining._id}
  125. })
  126. .exec(function(err, travel){
  127. if (err) return res.send(500, err.message);
  128. if (!travel) {
  129. 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'});
  130. } else if (travel) {
  131. travel.joinPetitions.push(userJoining._id);
  132. travel.save(function(err, travel) {
  133. if(err) return res.send(500, err.message);
  134. //start saving notification, get user owner of travel
  135. userModel.findOne({_id: travel.user})
  136. .exec(function(err, user){
  137. if (err) return res.send(500, err.message);
  138. if (!user) {
  139. res.json({success: false, message: 'User not found.'});
  140. } else if (user) {
  141. //notification
  142. var notification = new notificationModel({
  143. concept: "join",
  144. message: "user "+userJoining.username+" joins your travel "+travel.title,
  145. date: new Date(),
  146. icon: 'ion-person-add',
  147. link: "travels/" + travel._id,
  148. user: user._id
  149. });
  150. notification.save(function(err, notification) {
  151. if (err) return res.send(500, err.message);
  152. user.notifications.push(notification._id);
  153. user.save(function(err, user) {
  154. if (err) return res.send(500, err.message);
  155. console.log("notification saved");
  156. exports.getTravelById(req, res);
  157. });
  158. });
  159. }
  160. });//end saving notification
  161. });
  162. }//end of else if travel
  163. });
  164. }//end of else if user
  165. });
  166. };
  167. exports.unJoin = function(req, res) {
  168. userModel.findOne({'token': req.headers['x-access-token']})
  169. .exec(function(err, userJoining){
  170. if (!userJoining) {
  171. res.json({success: false, message: 'userJoining not found.'});
  172. } else if (userJoining) {
  173. travelModel.findOne({
  174. _id: req.params.travelid,
  175. joinPetitions: userJoining._id
  176. })
  177. .exec(function(err, travel){
  178. if (err) return res.send(500, err.message);
  179. if (!travel) {
  180. res.json({success: false, message: 'can not unjoin this travel'});
  181. } else if (travel) {
  182. for(var i=0; i<travel.joinPetitions.length; i++)
  183. {
  184. if(travel.joinPetitions[i].equals(userJoining._id))
  185. {
  186. travel.joinPetitions.splice(i, 1);
  187. }
  188. }
  189. travel.save(function(err, travel) {
  190. if(err) return res.send(500, err.message);
  191. //start saving notification, get user owner of travel
  192. userModel.findOne({_id: travel.user})
  193. .exec(function(err, user){
  194. if (err) return res.send(500, err.message);
  195. if (!user) {
  196. res.json({success: false, message: 'User not found.'});
  197. } else if (user) {
  198. //notification
  199. var notification = new notificationModel({
  200. concept: "unjoin",
  201. message: "user "+userJoining.username+" unjoins your travel "+travel.title,
  202. date: new Date(),
  203. icon: 'ion-arrow-return-left',
  204. link: "travels/" + travel._id,
  205. user: user._id
  206. });
  207. notification.save(function(err, notification) {
  208. if (err) return res.send(500, err.message);
  209. user.notifications.push(notification._id);
  210. user.save(function(err, user) {
  211. if (err) return res.send(500, err.message);
  212. console.log("notification saved");
  213. exports.getTravelById(req, res);
  214. });
  215. });
  216. }
  217. });//end saving notification
  218. });
  219. }
  220. });
  221. }
  222. });
  223. };
  224. exports.declineJoin = function(req, res) {
  225. userModel.findOne({'token': req.headers['x-access-token']})
  226. .exec(function(err, userOwner){
  227. if (err) return res.send(500, err.message);
  228. if (!userOwner) {
  229. res.json({success: false, message: 'User not found.'});
  230. } else if (userOwner) {
  231. travelModel.findOne({
  232. _id: req.params.travelid,
  233. user: userOwner._id,
  234. joinPetitions: req.body.userid
  235. })
  236. .exec(function(err, travel){
  237. if (err) return res.send(500, err.message);
  238. if (!travel) {
  239. 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'});
  240. } else if (travel) {
  241. var indexPetition=-1;
  242. for(var i=0; i<travel.joinPetitions.length; i++)
  243. {
  244. if(travel.joinPetitions[i].equals(req.body.userid))
  245. {
  246. indexPetition=JSON.parse(JSON.stringify(i));
  247. }
  248. }
  249. if(indexPetition>-1)
  250. {
  251. travel.joinPetitions.splice(indexPetition, 1);
  252. }
  253. travel.save(function(err, travel) {
  254. if(err) return res.send(500, err.message);
  255. //start saving notification, get user owner of travel
  256. userModel.findOne({_id: req.body.userid})
  257. .exec(function(err, user){
  258. if (err) return res.send(500, err.message);
  259. if (!user) {
  260. res.json({success: false, message: 'User not found.'});
  261. } else if (user) {
  262. //notification
  263. var notification = new notificationModel({
  264. concept: "travel",
  265. message: "user "+userOwner.username+" declines your petition for "+travel.title,
  266. date: new Date(),
  267. icon: 'ion-close',
  268. link: "travels/" + travel._id,
  269. user: user._id
  270. });
  271. notification.save(function(err, notification) {
  272. if (err) return res.send(500, err.message);
  273. user.notifications.push(notification._id);
  274. user.save(function(err, user) {
  275. if (err) return res.send(500, err.message);
  276. console.log("notification saved");
  277. exports.getTravelById(req, res);
  278. });
  279. });
  280. }
  281. });//end saving notification
  282. });//end of travel save
  283. }//end of else if travel
  284. });
  285. }//end of else if user
  286. });
  287. };
  288. exports.acceptJoin = function(req, res) {
  289. userModel.findOne({'token': req.headers['x-access-token']})
  290. .exec(function(err, userOwner){
  291. if (err) return res.send(500, err.message);
  292. if (!userOwner) {
  293. res.json({success: false, message: 'User not found.'});
  294. } else if (userOwner) {
  295. travelModel.findOne({
  296. _id: req.params.travelid,
  297. user: userOwner._id,
  298. joinPetitions: req.body.userid
  299. })
  300. .exec(function(err, travel){
  301. if (err) return res.send(500, err.message);
  302. if (!travel) {
  303. 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'});
  304. } else if (travel) {
  305. var indexPetition=-1;
  306. for(var i=0; i<travel.joinPetitions.length; i++)
  307. {
  308. if(travel.joinPetitions[i].equals(req.body.userid))
  309. {
  310. indexPetition=JSON.parse(JSON.stringify(i));
  311. }
  312. }
  313. if(indexPetition>-1)
  314. {
  315. travel.joins.push(JSON.parse(JSON.stringify(travel.joinPetitions[indexPetition])));
  316. travel.joinPetitions.splice(indexPetition, 1);
  317. console.log(travel);
  318. }
  319. travel.save(function(err, travel) {
  320. if(err) return res.send(500, err.message);
  321. //start saving notification, get user owner of travel
  322. userModel.findOne({_id: req.body.userid})
  323. .exec(function(err, user){
  324. if (err) return res.send(500, err.message);
  325. if (!user) {
  326. res.json({success: false, message: 'User not found.'});
  327. } else if (user) {
  328. //notification
  329. var notification = new notificationModel({
  330. concept: "travel",
  331. message: "user "+userOwner.username+" accepts your petition for "+travel.title,
  332. date: new Date(),
  333. icon: 'ion-checkmark',
  334. link: "travels/" + travel._id,
  335. user: user._id
  336. });
  337. notification.save(function(err, notification) {
  338. if (err) return res.send(500, err.message);
  339. user.notifications.push(notification._id);
  340. user.save(function(err, user) {
  341. if (err) return res.send(500, err.message);
  342. console.log("notification saved");
  343. exports.getTravelById(req, res);
  344. });
  345. });
  346. }
  347. });//end saving notification
  348. });//end of travel save
  349. }//end of else if travel
  350. });
  351. }//end of else if user
  352. });
  353. };
  354. exports.leave = function(req, res) {
  355. userModel.findOne({'token': req.headers['x-access-token']})
  356. .exec(function(err, userLeaving){
  357. if (!userLeaving) {
  358. res.json({success: false, message: 'userLeaving not found.'});
  359. } else if (userLeaving) {
  360. travelModel.findOne({
  361. _id: req.params.travelid,
  362. joins: userLeaving._id
  363. })
  364. .exec(function(err, travel){
  365. if (err) return res.send(500, err.message);
  366. if (!travel) {
  367. res.json({success: false, message: 'can not unjoin this travel'});
  368. } else if (travel) {
  369. for(var i=0; i<travel.joins.length; i++)
  370. {
  371. if(travel.joins[i].equals(userLeaving._id))
  372. {
  373. travel.joins.splice(i, 1);
  374. }
  375. }
  376. travel.save(function(err, travel) {
  377. if(err) return res.send(500, err.message);
  378. //start saving notification, get user owner of travel
  379. userModel.findOne({_id: travel.user})
  380. .exec(function(err, user){
  381. if (err) return res.send(500, err.message);
  382. if (!user) {
  383. res.json({success: false, message: 'User not found.'});
  384. } else if (user) {
  385. //notification
  386. var notification = new notificationModel({
  387. concept: "leave",
  388. message: "user "+userLeaving.username+" leaves your travel "+travel.title,
  389. date: new Date(),
  390. icon: 'ion-log-out',
  391. link: "travels/" + travel._id,
  392. user: user._id
  393. });
  394. notification.save(function(err, notification) {
  395. if (err) return res.send(500, err.message);
  396. user.notifications.push(notification._id);
  397. user.save(function(err, user) {
  398. if (err) return res.send(500, err.message);
  399. console.log("notification saved");
  400. exports.getTravelById(req, res);
  401. });
  402. });
  403. }
  404. });//end saving notification
  405. });
  406. }
  407. });
  408. }
  409. });
  410. };
  411. //currently not used
  412. exports.addComment = function(req, res) {
  413. /*var comment = new commentModel({
  414. travelId: req.params.travelId,
  415. commentUserId: req.body.commentUserId,
  416. commentUsername: req.body.commentUsername,
  417. comment: req.body.comment,
  418. commentAvatar: req.body.commentAvatar
  419. });
  420. comment.save(function(err, comment) {
  421. if(err) return res.send(500, err.message);
  422. res.status(200).jsonp(comment);
  423. });*/
  424. userModel.find({
  425. token: req.headers['x-access-token']
  426. }, function(err, users){
  427. var user=users[0];
  428. travelModel.findById(req.params.travelId, function(err, travel){
  429. console.log(travel.title);
  430. var comment = {
  431. commentUserId: user._id,
  432. commentUsername: user.username,
  433. comment: req.body.comment,
  434. commentAvatar: user.avatar
  435. };
  436. travel.comments.push(comment);
  437. travel.save(function(err, travel) {
  438. if(err) return res.send(500, err.message);
  439. //res.status(200).jsonp(travel);
  440. travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
  441. if(err) res.send(500, err.message);
  442. res.status(200).jsonp(travels);
  443. });
  444. });
  445. //start saving notification, get user owner of travel
  446. userModel.find({
  447. username: travel.owner
  448. }, function(err, userowners) {
  449. var userowner=userowners[0];
  450. //notification
  451. var notification = {
  452. concept: "comment",
  453. otherusername: user.username,
  454. description: "user "+user.username+" comments your travel "+travel.title,
  455. date: new Date(),
  456. link: "",
  457. user: userowners[0]._id
  458. };
  459. userowner.notifications.push(notification);
  460. userowner.save(function(err, userowner) {
  461. console.log("notification saved");
  462. });
  463. });//end saving notification
  464. });
  465. });//end of userModel.find
  466. };
  467. //currently not used
  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. };