-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-operations.js
90 lines (85 loc) · 2.1 KB
/
db-operations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function fetchNearestCops(db, coordinates, callback) {
db.collection('policeData').createIndex({
"location": "2dsphere"
}, function () {
db.collection("policeData").find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: coordinates
},
$maxDistance: 2000
}
}
}).toArray(function (err, results) {
if (err) {
console.log(err)
} else {
callback(results);
}
});
});
}
function fetchCopDetails(db, userId, callback) {
db.collection('policeData').findOne({
userId: userId,
}, function (err, results) {
if (err) return console.log(err);
callback({
copId: results.userId,
displayName: results.displayName,
phone: results.phone,
location: results.location,
});
})
}
function saveRequest(db, issueId, requestTime, location, citizenId, status, callback) {
db.collection('requestsData').insert({
"_id": issueId,
"requestTime": requestTime,
"location": location,
"citizenId": citizenId,
"status": status
}, function (err, results) {
if (err) {
console.log(err);
} else {
callback(results);
}
});
}
function updateRequest(db, requestId, copId, status, callback) {
db.collection('requestsData').update({
"_id": requestId //Perform update for the given requestId
}, {
$set: {
"status": status, //Update status to 'engaged'
"copId": copId //save cop's userId
}
}, function (err, results) {
if (err) {
console.log(err);
} else {
callback("Issue updated")
}
});
}
function fetchRequests(db, callback) {
db.collection('requestsData').find({}, {
requestTime: true,
status: true,
location: true
}).toArray(function (err, results) {
if (err) {
console.log(err)
} else {
callback(results);
}
});
}
exports.fetchRequests = fetchRequests;
exports.updateRequest = updateRequest;
exports.saveRequest = saveRequest;
exports.fetchNearestCops = fetchNearestCops;
exports.fetchCopDetails = fetchCopDetails;