-
Notifications
You must be signed in to change notification settings - Fork 0
/
dealer.js
78 lines (63 loc) · 2.61 KB
/
dealer.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
var http = require('https');
var getDealsByCentre = function (centreId, callback) {
requestDeals(process.env["WF_DEALS_API_ENDPOINT"] + "?api_key=" + process.env["WF_API_KEY"], "¢re_id=" + centreId + "&fields=title%2Csubtitle%2Cdescription%2Cstores%2C_links&per_page=3&state=live", function(err, data) {
if (err) {
return callback(err)
}
return callback(err, data)
})
}
var getRetailerDealsByCentre = function (centreId, callback) {
requestDeals(process.env["WF_DEALS_API_ENDPOINT"] + "?api_key=" + process.env["WF_API_KEY"], "¢re_id=" + centreId + "&fields=_links&per_page=10&state=live", function(err, data) {
if (err) {
return callback(err)
}
return callback(err, data)
})
}
var getDealsByCentreByStore = function (centreId, storeId, callback) {
requestDeals(process.env["WF_DEALS_API_ENDPOINT"] + "?api_key=" + process.env["WF_API_KEY"], "¢re_id=" + centreId + "&store_id=" + storeId + "&fields=title%2Csubtitle%2Cdescription%2Cstores%2C_links&per_page=3&state=live", function(err, data) {
if (err) {
return callback(err)
}
return callback(err, data)
})
}
var getDealsByCentreByRetailer = function (centreId, retailerID, callback) {
requestDeals(process.env["WF_DEALS_API_ENDPOINT"] + "?api_key=" + process.env["WF_API_KEY"], "¢re_id=" + centreId + "&retailer_id=" + retailerId + "&fields=title%2Csubtitle%2Cdescription%2Cstores%2C_links&per_page=3&state=live", function(err, data) {
if (err) {
return callback(err)
}
return callback(err, data)
})
}
var requestDeals = function (endpoint, queryString, callback) {
http.get(endpoint + queryString, function (res) {
var responseString = '';
console.log('Status Code: ' + res.statusCode);
if (res.statusCode != 200) {
callback(new Error("Non 200 Response"));
}
res.on('data', function (data) {
responseString += data;
});
res.on('end', function () {
var responseObject = JSON.parse(responseString);
if (responseObject.error) {
console.log("NOAA error: " + responseObject.error.message);
callback(new Error(responseObject.error.message));
} else {
callback(null, responseObject);
}
});
}).on('error', function (e) {
console.log("Communications error: " + e.message);
callback(new Error(e.message));
});
}
module.exports = {
getDealsByCentre : getDealsByCentre,
getDealsByCentreByStore : getDealsByCentreByStore,
getDealsByCentreByRetailer : getDealsByCentreByRetailer,
getRetailerDealsByCentre: getRetailerDealsByCentre,
}