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.

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