-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOptToUpdate.js
51 lines (48 loc) · 1.51 KB
/
OptToUpdate.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
/**
*
* 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 userAddress = params.address;
let session = params.session;
console.log("userAddress,", userAddress, "session,", session);
var response;
let options = {
uri: "https://maps.googleapis.com/maps/api/geocode/json",
qs: {
address: userAddress,
key: ""
},
json: true // Automatically parses the JSON string in the response
};
const geoPromise = rp(options);
let cloudant = Cloudant({
username: "",
password: "",
url: ""
});
let users = cloudant.db.use("users");
let sessions = cloudant.db.use("sessions");
const sessionPromise = sessions.find({ selector: { sessionID: session } });
return Promise.all([geoPromise, sessionPromise])
.then(values => {
return { address: userAddress, location: values[0], userID: values[1].docs[0]._id };
})
.then(values => {
response = values;
return users.get(values.userID);
})
.then(user => {
return users.insert({ ...user, address: response.address, location: response.location });
})
.catch(err => {
return { err };
});
}