-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathradar.js
More file actions
74 lines (70 loc) · 2.09 KB
/
radar.js
File metadata and controls
74 lines (70 loc) · 2.09 KB
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
var Radar = require('radar-sdk-js');
var axios = require('axios');
var properties = require('./properties');
// Create a Radar.io geofence only if it doesn't exist for the user yet
function createGeofence(id, description, coordinates) {
axios.get(
`https://api.radar.io/v1/geofences/users/${id}`,
{headers: {Authorization: `${properties.radarSecret}`}}
).
then(res => {
console.log("\n\n\ngeofence exists\n********");
}).
catch(err => {
console.log("\n\n\ngeofence doesn't exist, creating geofence\n\n\n");
axios.post(
'https://api.radar.io/v1/geofences',
{
tag: 'users',
externalId: id,
type: 'circle',
radius: 100,
description: description,
coordinates: coordinates
},
{headers: {Authorization: `${properties.radarSecret}`}}
).
catch(err => {
console.log(err);
});
});
}
// Get all geofences
async function getGeofences() {
let res;
try {
res = await axios.get(
'https://api.radar.io/v1/geofences',
{headers: {Authorization: `${properties.radarSecret}`}}
);
console.log('\n\n\n*******\ngetting geofences');
}
catch(err) {
console.log(err);
}
console.log(res);
return res.data.geofences;
}
// Get coordinates from geofence of user
async function getGeofenceCoords(id, httpres) {
axios.get(
`https://api.radar.io/v1/geofences/users/${id}`,
{headers: {Authorization: `${properties.radarSecret}`}}
).
then(res => {
console.log(res.data.geofence.geometryCenter.coordinates);
httpres.send({
lo: res.data.geofence.geometryCenter.coordinates[0],
la: res.data.geofence.geometryCenter.coordinates[1],
});
}).
catch(err => {
console.log(err);
httpres.send({message: error});
});
}
module.exports = {
createGeofence: createGeofence,
getGeofences: getGeofences,
getGeofenceCoords: getGeofenceCoords
};