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