-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
items.js
43 lines (37 loc) · 1.43 KB
/
items.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
const fetch = require("node-fetch");
const CONFIG = require("./config.js");
const authentication = require("./auth.js");
const isValidData = data => {
return data.success === true;
};
const extractItems = (cobaltId, campaignId) => {
return new Promise((resolve, reject) => {
console.log(`Retrieving items for ${cobaltId}`);
console.log("ITEMS API CACHE_ITEMS MISS!");
const url = CONFIG.urls.itemsAPI(campaignId);
const headers = (authentication.CACHE_AUTH.exists(cobaltId).data !== null) ? {headers: {"Authorization": `Bearer ${authentication.CACHE_AUTH.exists(cobaltId).data}`}} : {};
fetch(url, headers)
.then(res => res.json())
.then(json => {
// console.log(json.data.map(sp => sp.definition.name).join(", "));
if (isValidData(json)) {
const filteredItems = json.data.filter(item =>
item.sources && (item.sources.length === 0 || item.sources.some((source) => source.sourceId != 39))
);
console.log(
`Adding ${filteredItems.length} items available to cache for ${cobaltId}...`
);
resolve(filteredItems);
} else {
console.log("Received no valid item data, instead:" + json.message);
reject(json.message);
}
})
.catch(error => {
console.log("Error retrieving items");
console.log(error);
reject(error);
});
});
};
exports.extractItems = extractItems;