Browse Source

alert system implemented

master
arnaucode 7 years ago
parent
commit
509f061016
4 changed files with 54 additions and 30 deletions
  1. +48
    -1
      controllers/eventController.js
  2. +0
    -28
      models/alertModel.js
  3. +2
    -1
      models/eventModel.js
  4. +4
    -0
      server.js

+ 48
- 1
controllers/eventController.js

@ -10,7 +10,24 @@ var eventModel = mongoose.model('eventModel');
var pageSize=config.pageSize;
exports.getAllEvents = function(req, res) {
eventModel.find({date: {$gte: new Date()}})
eventModel.find({
date: {$gte: new Date()}//cal filtrar per type d'event, de si es alert o no
})
.lean()
.populate('user', 'username img shortDescription')
.sort('date')
.limit(pageSize)
.skip(pageSize * Number(req.query.page))
.exec(function (err, events) {
if (err) return res.send(500, err.message);
res.status(200).jsonp(events);
});
};
exports.getAllAlerts = function(req, res) {
eventModel.find({
date: {$gte: new Date()},
type: "alert"
})
.lean()
.populate('user', 'username img shortDescription')
.sort('date')
@ -21,6 +38,7 @@ exports.getAllEvents = function(req, res) {
res.status(200).jsonp(events);
});
};
exports.getEventById = function (req, res) {
eventModel.findOne({_id: req.params.eventid})
.lean()
@ -67,6 +85,35 @@ exports.addEvent = function(req, res) {
}
});//end of usermodel.find
};
exports.addAlert = function(req, res) {
userModel.findOne({'tokens.token': req.headers['x-access-token']})
.exec(function(err, user){
if (err) return res.send(500, err.message);
if (!user) {
console.log("user not found");
res.json({success: false, message: 'User not found.'});
} else if (user) {
var event = new eventModel({
title: req.body.title,
description: req.body.description,
date: req.body.date,
generateddate: Date(),
user: user._id,
type: "alert"
});
event.save(function(err, event) {
if(err) return res.send(500, err.message);
user.events.push(event._id);
user.save(function (err, user) {
if (err) return res.send(500, err.message);
exports.getAllEvents(req, res);
});
});//end of event.save
}
});//end of usermodel.find
};
exports.deleteEvent = function(req, res) {
userModel.findOne({'tokens.token': req.headers['x-access-token']})
.exec(function(err, user){

+ 0
- 28
models/alertModel.js

@ -1,28 +0,0 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var mongooseUniqueValidator = require('mongoose-unique-validator');
var alertSchema = new Schema({
title: { type: String },
description: { type: String },
img: { type: String },
date: { type: Date },
location:{
direction: { type: String },
city: { type: String },
district: { type: String },
geo: {
lat: {type: Number},
long: {type: Number},
name: { type: String }
}
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'userModel'
}
});
alertSchema.plugin(mongooseUniqueValidator);
module.exports = mongoose.model('alertModel', alertSchema);

+ 2
- 1
models/eventModel.js

@ -25,7 +25,8 @@ var eventSchema = new Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'userModel'
}
},
type: {type: String}
});
eventSchema.plugin(mongooseUniqueValidator);

+ 4
- 0
server.js

@ -59,6 +59,8 @@ apiRoutes.route('/users/id/:userid')
apiRoutes.route('/events')
.get(eventCtrl.getAllEvents);
apiRoutes.route('/alerts')
.get(eventCtrl.getAllAlerts);
apiRoutes.route('/events/id/:eventid')
.get(eventCtrl.getEventById);
apiRoutes.route('/events/following')
@ -100,6 +102,8 @@ apiRoutes.use(function(req, res, next) {
apiRoutes.route('/events')
.post(eventCtrl.addEvent);
apiRoutes.route('/alerts')
.post(eventCtrl.addAlert);
apiRoutes.route('/events/id/:eventid')
.delete(eventCtrl.deleteEvent);

Loading…
Cancel
Save