Skip to content

Commit

Permalink
added getsos & sos collection
Browse files Browse the repository at this point in the history
  • Loading branch information
nik-gautam committed Jan 29, 2020
1 parent 531734a commit b878bbb
Show file tree
Hide file tree
Showing 7 changed files with 508 additions and 20 deletions.
4 changes: 0 additions & 4 deletions controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ exports.postSignUp = (req, res, next) => {
email: user.email,
phone: parseInt(user.phone, 10),
address: user.address,
sos: {
isTrue: false,
previous_sos: []
},
password: user.password,
emergency_contact: []
});
Expand Down
97 changes: 97 additions & 0 deletions controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
const Pusher = require('pusher')

const User = require('../models/user');
const Sos = require('../models/sos');
const Location = require('../models/location');

let pusher = new Pusher({
appId: '939281',
key: '8d9b4faca051e85c76ba',
secret: '84ebe0a6905a55e6f3ec',
cluster: 'ap2',
encrypted: true
});

exports.listUsers = (req, res, next) => {

Expand Down Expand Up @@ -75,4 +87,89 @@ exports.postEmergencyList = (req, res, next) => {
});
}
});
}

exports.getSos = (req, res, next) => {
let id = req.query.uid;
Location.find({
uid: id
}, (err, loc) => {
if (!err) {
if (loc) {

Sos.findOne({
uid: id
}, (err, result) => {
if (!err) {
if (result) {
result.previous_sos.push({
date: new Date(),
geometry: loc.geometry
});

result.save().then(resultt => {
pusher.trigger('sos-channel', req.query.uid, {
"sos": true
});
res.json({
success: true,
msg: "Previos SOS List successfully updated",
uid: result.uid
});
}).catch(err => {
res.status(404).json({
success: false,
err
});
})


} else {
let newSos = new Sos({
uid: req.query.uid,
previous_sos: [{
date: new Date(),
geometry: loc.geometry
}]
})

newSos.save().then((result) => {
pusher.trigger('sos-channel', req.query.uid, {
"sos": true
});

res.json({
success: true,
uid: result.uid,
msg: "Sos sent"
});
}).catch(err => {
res.status(404).json({
success: false,
msg: "Failed to add user",
err
});
});

}
} else {
res.status(404).json({
success: false,
err
});
}
})
} else {
res.status(404).json({
success: false,
msg: "User not found!"
});
}
} else {
res.status(404).json({
success: false,
err
});
}
});
}
29 changes: 29 additions & 0 deletions models/sos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const geoSchema = new Schema({
type: {
type: String,
default: "Point"
},
coordinates: {
type: [Number],
index: "2dsphere"
}
});

const sosSchema = new Schema({
uid: {
type: String,
required: true
},
previous_sos: [{
date: {
type: String,
},
geometry: geoSchema
}]
});

module.exports = mongoose.model('Sos', sosSchema);
14 changes: 0 additions & 14 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,6 @@ const userSchema = new Schema({
type: String,
required: true
},
sos: {
isTrue: {
type: Boolean,
required: true
},
previous_sos: [{
date: {
type: String,
},
time: {
type: Number,
}
}]
},
password: {
type: String,
required: true
Expand Down
Loading

0 comments on commit b878bbb

Please sign in to comment.