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