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