-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
159 lines (125 loc) · 4.62 KB
/
Copy pathsetup.js
File metadata and controls
159 lines (125 loc) · 4.62 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const fetch = require('node-fetch');
const { modifyAvailability } = require('./helpers');
const syncFlag = process.argv[2] === '--syncOnSetup';
async function getData(shopUrl) {
const idToSku = {};
const api_url = `${shopUrl}/products.json`;
const response = await fetch(api_url);
const productsData = await response.json();
const allVariants = {};
const inventoryItemIds = [];
for (let product of productsData.products) {
for (let variant of product.variants) {
const {
sku,
inventory_item_id,
} = variant;
inventoryItemIds.push(inventory_item_id);
idToSku[inventory_item_id] = sku;
}
}
let inventoryLevels = [];
// Retrieve all the inventory items' data. (max 50 at a time)
while(inventoryItemIds.length > 0) {
const inventoryLevelResponse = await fetch(`${shopUrl}/inventory_levels.json?inventory_item_ids=${inventoryItemIds.splice(0, 50)}`);
const inventoryLevelData = await inventoryLevelResponse.json();
inventoryLevels = [...inventoryLevels, ...inventoryLevelData.inventory_levels];
}
for (let item of inventoryLevels) {
const {
inventory_item_id,
location_id,
available,
} = item;
const sku = idToSku[inventory_item_id];
allVariants[sku] = {
available,
itemData: {
['inventory_item_id']: inventory_item_id,
['location_id']: location_id,
}
};
}
return [allVariants, idToSku];
}
async function setup(shops, apiEndpoint, appAddress) {
console.log('Setting up app...');
const registerWebhookBody = {
webhook: {
topic: 'inventory_levels/update',
address: `${appAddress}/inventory_levels/update`,
format: 'json',
}
};
let idToSku = {};
const shopsData = {};
const inventoryData = {};
for (let shop of shops) {
const shopUrl = `https://${shop.apiCredentials}@${shop.domain}${apiEndpoint}`;
const [shopData, shopIdToSkuData] = await getData(shopUrl);
shopsData[shop.name] = shopData;
idToSku = {
...idToSku,
...shopIdToSkuData,
};
const webhooksList = await fetch(`${shopUrl}/webhooks.json`);
const { webhooks } = await webhooksList.json();
const existingWebhook = webhooks.filter((webhook) => {
return webhook.address === `${appAddress}/inventory_levels/update`
});
if (existingWebhook.length === 0) {
await fetch(`${shopUrl}/webhooks.json`, {
method: 'POST',
body: JSON.stringify(registerWebhookBody),
headers: {
'Content-Type': 'application/json',
},
});
console.log(`webhook registered for shop ${shop.name}.`);
} else {
console.log(`webhook for shop ${shop.name} already registered.`);
}
}
const referenceShopName = shops[0].name;
for (let sku of Object.keys(shopsData[referenceShopName])) {
inventoryData[sku] = {
available: shopsData[referenceShopName][sku].available,
[referenceShopName]: shopsData[referenceShopName][sku].itemData,
};
for (let shop of shops) {
const { name } = shop;
if (name !== referenceShopName) {
inventoryData[sku][name] = shopsData[name][sku].itemData;
}
}
}
if (syncFlag) {
const { API_ENDPOINT } = process.env;
for (let shop of shops) {
const {
name,
apiCredentials,
domain,
} = shop;
if (name !== referenceShopName) {
console.log(`Syncing inventory from shop ${referenceShopName} to shop ${name}`);
for (let sku of Object.keys(inventoryData)) {
const { available } = inventoryData[sku];
const {
location_id,
inventory_item_id,
} = inventoryData[sku][name];
const dataToSend = {
location_id,
inventory_item_id,
available,
};
await modifyAvailability(apiCredentials, domain, API_ENDPOINT, dataToSend);
console.log(`synced item ${sku}`);
}
}
}
}
return [inventoryData, idToSku];
}
module.exports = setup;