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=2;
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 });
156 notification.save(function(err, notification) {
157 if (err) return res.send(500, err.message);
158 user.notifications.push(notification._id);
159 user.save(function(err, user) {
160 if (err) return res.send(500, err.message);
161
162 console.log("notification saved");
163 exports.getTravelById(req, res);
164 });
165 });
166 }
167 });//end saving notification
168 });
169 }//end of else if travel
170 });
171 }//end of else if user
172 });
173};
174
175exports.unJoin = function(req, res) {
176 userModel.findOne({'token': req.headers['x-access-token']})
177 .exec(function(err, userJoining){
178 if (!userJoining) {
179 res.json({success: false, message: 'userJoining not found.'});
180 } else if (userJoining) {
181 travelModel.findOne({
182 _id: req.params.travelid,
183 joinPetitions: userJoining._id
184 })
185 .exec(function(err, travel){
186 if (err) return res.send(500, err.message);
187 if (!travel) {
188 res.json({success: false, message: 'can not unjoin this travel'});
189 } else if (travel) {
190 for(var i=0; i<travel.joinPetitions.length; i++)
191 {
192 if(travel.joinPetitions[i].equals(userJoining._id))
193 {
194 travel.joinPetitions.splice(i, 1);
195 }
196 }
197 travel.save(function(err, travel) {
198 if(err) return res.send(500, err.message);
199 //start saving notification, get user owner of travel
200 userModel.findOne({_id: travel.user})
201 .exec(function(err, user){
202 if (err) return res.send(500, err.message);
203 if (!user) {
204 res.json({success: false, message: 'User not found.'});
205 } else if (user) {
206 //notification
207 var notification = new notificationModel({
208 concept: "unjoin",
209 message: "user "+userJoining.username+" unjoins your travel "+travel.title,
210 date: new Date(),
211 icon: 'unjoin.png',
212 link: "travels/" + travel._id
213 });
214 notification.save(function(err, notification) {
215 if (err) return res.send(500, err.message);
216 user.notifications.push(notification._id);
217 user.save(function(err, user) {
218 if (err) return res.send(500, err.message);
219
220 console.log("notification saved");
221 exports.getTravelById(req, res);
222 });
223 });
224 }
225 });//end saving notification
226 });
227 }
228 });
229 }
230
231 });
232};
233
234
235exports.declineJoin = function(req, res) {
236 userModel.findOne({'token': req.headers['x-access-token']})
237 .exec(function(err, userOwner){
238 if (err) return res.send(500, err.message);
239 if (!userOwner) {
240 res.json({success: false, message: 'User not found.'});
241 } else if (userOwner) {
242 travelModel.findOne({
243 _id: req.params.travelid,
244 user: userOwner._id,
245 joinPetitions: req.body.userid
246 })
247 .exec(function(err, travel){
248 if (err) return res.send(500, err.message);
249 if (!travel) {
250 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'});
251 } else if (travel) {
252 var indexPetition=-1;
253 for(var i=0; i<travel.joinPetitions.length; i++)
254 {
255 if(travel.joinPetitions[i].equals(req.body.userid))
256 {
257 indexPetition=JSON.parse(JSON.stringify(i));
258 }
259 }
260 if(indexPetition>-1)
261 {
262 travel.joinPetitions.splice(indexPetition, 1);
263 }
264 travel.save(function(err, travel) {
265 if(err) return res.send(500, err.message);
266
267 //start saving notification, get user owner of travel
268 userModel.findOne({_id: req.body.userid})
269 .exec(function(err, user){
270 if (err) return res.send(500, err.message);
271 if (!user) {
272 res.json({success: false, message: 'User not found.'});
273 } else if (user) {
274 //notification
275 var notification = new notificationModel({
276 concept: "travel",
277 message: "user "+userOwner.username+" declines your petition for "+travel.title,
278 date: new Date(),
279 icon: 'travel.png',
280 link: "travels/" + travel._id
281 });
282 notification.save(function(err, notification) {
283 if (err) return res.send(500, err.message);
284 user.notifications.push(notification._id);
285 user.save(function(err, user) {
286 if (err) return res.send(500, err.message);
287
288 console.log("notification saved");
289 exports.getTravelById(req, res);
290 });
291 });
292 }
293 });//end saving notification
294 });//end of travel save
295 }//end of else if travel
296 });
297 }//end of else if user
298 });
299};
300
301exports.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
335 //start saving notification, get user owner of travel
336 userModel.findOne({_id: req.body.userid})
337 .exec(function(err, user){
338 if (err) return res.send(500, err.message);
339 if (!user) {
340 res.json({success: false, message: 'User not found.'});
341 } else if (user) {
342 //notification
343 var notification = new notificationModel({
344 concept: "travel",
345 message: "user "+userOwner.username+" accepts your petition for "+travel.title,
346 date: new Date(),
347 icon: 'travel.png',
348 link: "travels/" + travel._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
356 console.log("notification saved");
357 exports.getTravelById(req, res);
358 });
359 });
360 }
361 });//end saving notification
362 });//end of travel save
363 }//end of else if travel
364 });
365 }//end of else if user
366 });
367};
368
369exports.leave = function(req, res) {
370 userModel.findOne({'token': req.headers['x-access-token']})
371 .exec(function(err, userLeaving){
372 if (!userLeaving) {
373 res.json({success: false, message: 'userLeaving not found.'});
374 } else if (userLeaving) {
375 travelModel.findOne({
376 _id: req.params.travelid,
377 joins: userLeaving._id
378 })
379 .exec(function(err, travel){
380 if (err) return res.send(500, err.message);
381 if (!travel) {
382 res.json({success: false, message: 'can not unjoin this travel'});
383 } else if (travel) {
384 for(var i=0; i<travel.joins.length; i++)
385 {
386 if(travel.joins[i].equals(userLeaving._id))
387 {
388 travel.joins.splice(i, 1);
389 }
390 }
391 travel.save(function(err, travel) {
392 if(err) return res.send(500, err.message);
393 //start saving notification, get user owner of travel
394 userModel.findOne({_id: travel.user})
395 .exec(function(err, user){
396 if (err) return res.send(500, err.message);
397 if (!user) {
398 res.json({success: false, message: 'User not found.'});
399 } else if (user) {
400 //notification
401 var notification = new notificationModel({
402 concept: "leave",
403 message: "user "+userLeaving.username+" leaves your travel "+travel.title,
404 date: new Date(),
405 icon: 'leave.png',
406 link: "travels/" + travel._id
407 });
408 notification.save(function(err, notification) {
409 if (err) return res.send(500, err.message);
410 user.notifications.push(notification._id);
411 user.save(function(err, user) {
412 if (err) return res.send(500, err.message);
413
414 console.log("notification saved");
415 exports.getTravelById(req, res);
416 });
417 });
418 }
419 });//end saving notification
420 });
421 }
422 });
423 }
424
425 });
426};
427
428
429
430
431
432
433/* comment */
434exports.addComment = function(req, res) {
435 /*var comment = new commentModel({
436 travelId: req.params.travelId,
437 commentUserId: req.body.commentUserId,
438 commentUsername: req.body.commentUsername,
439 comment: req.body.comment,
440 commentAvatar: req.body.commentAvatar
441 });
442
443 comment.save(function(err, comment) {
444 if(err) return res.send(500, err.message);
445 res.status(200).jsonp(comment);
446 });*/
447 userModel.find({
448 token: req.headers['x-access-token']
449 }, function(err, users){
450 var user=users[0];
451
452 travelModel.findById(req.params.travelId, function(err, travel){
453 console.log(travel.title);
454 var comment = {
455 commentUserId: user._id,
456 commentUsername: user.username,
457 comment: req.body.comment,
458 commentAvatar: user.avatar
459 };
460 travel.comments.push(comment);
461
462 travel.save(function(err, travel) {
463 if(err) return res.send(500, err.message);
464 //res.status(200).jsonp(travel);
465 travelModel.find({date: {$gte: new Date()}}, function(err, travels) {
466 if(err) res.send(500, err.message);
467 res.status(200).jsonp(travels);
468 });
469 });
470
471 //start saving notification, get user owner of travel
472 userModel.find({
473 username: travel.owner
474 }, function(err, userowners) {
475 var userowner=userowners[0];
476 //notification
477 var notification = {
478 concept: "comment",
479 otherusername: user.username,
480 description: "user "+user.username+" comments your travel "+travel.title,
481 date: new Date(),
482 link: ""
483 };
484 userowner.notifications.push(notification);
485 userowner.save(function(err, userowner) {
486 console.log("notification saved");
487 });
488 });//end saving notification
489
490 });
491 });//end of userModel.find
492};
493
494exports.getCommentsByTravelId = function(req, res) {
495 commentModel.find({
496 travelId: req.params.travelId
497 }, function(err, comments) {
498
499 if (err) throw err;
500
501 if (!comments) {
502 res.json({ success: false, message: 'no comments for travelId' });
503 } else if (comments) {
504 // return the information including token as JSON
505 res.jsonp(comments);
506
507 }
508
509 });
510};