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.

517 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
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. 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. exports.deleteTravel = function(req, res) {
  90. userModel.findOne({'token': req.headers['x-access-token']})
  91. .exec(function(err, user){
  92. if (err) return res.send(500, err.message);
  93. travelModel.findById(req.params.travelid, function(err, travel) {
  94. if (err) return res.send(500, err.message);
  95. if(travel.user.equals(user._id))
  96. {
  97. travel.remove(function(err) {
  98. if(err) return res.send(500, err.message);
  99. console.log("deleted");
  100. exports.getAllTravels(req, res);
  101. });
  102. }
  103. });
  104. });
  105. };
  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: 'ion-person-add',
  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: 'ion-arrow-return-left',
  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: 'ion-close',
  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: 'ion-checkmark',
  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: 'ion-log-out',
  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. //currently not used
  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. //currently not used
  461. exports.getCommentsByTravelId = function(req, res) {
  462. commentModel.find({
  463. travelId: req.params.travelId
  464. }, function(err, comments) {
  465. if (err) throw err;
  466. if (!comments) {
  467. res.json({ success: false, message: 'no comments for travelId' });
  468. } else if (comments) {
  469. // return the information including token as JSON
  470. res.jsonp(comments);
  471. }
  472. });
  473. };