-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSendUpdate.js
92 lines (88 loc) · 2.73 KB
/
SendUpdate.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
91
92
/**
*
* main() will be run when you invoke this action
*
* @param Cloud Functions actions accept a single parameter, which must be a JSON object.
*
* @return The output of this action, which must be a JSON object.
*
*/
let rp = require("request-promise");
let Cloudant = require("@cloudant/cloudant");
function main(params) {
let update = params.update;
let address = params.address;
let session = params.session;
console.log(update, address, session);
let geoOptions = {
uri: "https://maps.googleapis.com/maps/api/geocode/json",
qs: {
address: address,
key: ""
},
json: true
};
let geoPromise = rp(geoOptions)
.then(data => {
return data;
})
.catch(err => {
console.log(err);
return err;
});
let cloudant = Cloudant({
username: "",
password: "",
url: ""
});
let users = cloudant.db.use("users");
let sessions = cloudant.db.use("sessions");
let sessionPromise = sessions
.find({ selector: { sessionID: session } })
.then(data => {
return data;
})
.catch(err => {
console.log(err);
return err;
});
return Promise.all([geoPromise, sessionPromise])
.then(data => {
let aggregateData = {
lng: data[0].results[0].geometry.location.lng,
lat: data[0].results[0].geometry.location.lat,
userID: data[1].docs[0]._id
};
console.log(aggregateData);
return aggregateData;
})
.then(data => {
users.list({ include_docs: true }).then(body => {
body.rows.forEach(doc => {
// console.log(doc.doc);
if (
doc.doc.location &&
doc.doc._id !== data.userID &&
Math.abs(doc.doc.location.results[0].geometry.location.lng - data.lng) < 0.5 &&
Math.abs(doc.doc.location.results[0].geometry.location.lat - data.lat) < 0.5
) {
let updateOptions = {
method: "POST",
uri: "",
form: {
userID: doc.doc._id,
message: update
},
json: true
};
rp(updateOptions);
}
});
return { msg: "success" };
});
})
.catch(err => {
console.log(err);
return err;
});
}