-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload-location.js
107 lines (100 loc) · 3.13 KB
/
load-location.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
var api = require("./sweetiq/location-api");
var loadSQL = require("./sweetiq/load-sql");
var config = require("./configuration.json");
// startLoader is the normal data upload
var startLoader = function(){
loadSQL.getSQLLocations()
.then( locations =>{
console.log("sending " + locations.length + " locations: ");
return api.login(config.sweetiqAPI.email,
config.sweetiqAPI.password)
.then( loginResponse =>{
return api.push(loginResponse.id, {locations: locations});
})
.then( response_data =>{
console.log(response_data);
})
})
.catch((error) =>{
console.log(error);
})
}
if(process.argv[2] === 'service'){
//startLoader is called at each set interval
if(config.intervalOfPushInMinutes != 0)
setInterval(startLoader, config.intervalOfPushInMinutes * 1000 * 60);
startLoader();
}
else if (process.argv[2] === 'test') {
//test if we can connect to the API
console.log('testing connection');
api.login(config.sweetiqAPI.email,
config.sweetiqAPI.password)
.then(response =>{
console.log('connection successful');
})
.catch( err =>{
console.log('something was wrong');
console.log(err);
})
}
else if (process.argv[2] === '--verify-update') {
// Same has startLoader but calls back the API to verify if the data
// is really updated. You'll see all the fields that are differents from
// the request and the response.
var token = null;
var locationsMapped;
loadSQL.getSQLLocations()
.then( locations =>{
locationsMapped = locations;
console.log("--verify mode--");
console.log("sending " + locationsMapped.length + " locations: ");
return api.login(config.sweetiqAPI.email,
config.sweetiqAPI.password)
.then( loginResponse =>{
token = loginResponse.id
return api.push(token, {locations: locationsMapped});
})
.then( pushResponse =>{
console.log(pushResponse);
for (var i in pushResponse.locations) {
pushResponse.locations[i]
api.search(token, {search_fields: {branches: [pushResponse.locations[i].branch]}})
.then( searchResponse =>{
searchedLocation = searchResponse[0];
for (var j in locationsMapped) {
//We search the original location Object
if (locationsMapped.hasOwnProperty(j)) {
if(locationsMapped[j].branch === searchResponse[0].branch){
//And we compare it with our current object
compareObjects(searchResponse[0], locationsMapped[j]);
}
}
}
})
}
//return api.search(token, searchRequest)
return null;
})
.catch( error =>{
console.log(error.stack);
});
})
}
else{
startLoader();
}
function compareObjects(a, b){
var keys = Object.keys(a);
for (var i in keys) {
if (keys.hasOwnProperty(i)) {
if(typeof a[keys[i]] === 'Object')
compareObjects(a[keys[i]], b[keys[i]]);
else if(typeof a[keys[i]] === 'Array'){
}
else if(a[keys[i]] !== b[keys[i]]){
console.log(keys[i]+": ", "'"+a[keys[i]]+"', ", "'"+b[keys[i]]+"'");
}
}
}
}