-
Notifications
You must be signed in to change notification settings - Fork 1
/
massSync.js
44 lines (36 loc) · 1.67 KB
/
massSync.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
const refreshToken = require('./lib/functions/lightspeed/refreshToken.js');
const getAccountID = require('./lib/functions/lightspeed/getAccountID.js');
const getFullInventory = require('./lib/functions/lightspeed/getFullInventory.js');
const buildInventoryFeed = require('./lib/functions/amazon/buildInventoryFeed.js');
const submitInventoryFeed = require('./lib/functions/amazon/submitInventoryFeed.js');
const massSync = async () => {
return new Promise(async (resolve, reject) => {
// refresh the token
const accessToken = await refreshToken();
if (typeof accessToken == 'string') {
// creating the authentication header for all future API calls
const authHeader = {
Authorization: `Bearer ${accessToken}`
};
// getting the account ID
const accountID = await getAccountID(authHeader);
// getting the full inventory of all items with Custom SKUs
let inventory = await getFullInventory(authHeader, accountID);
// pruning the full inventory to only their SKUs and quantities
inventory = inventory.map(item => {
return {
SKU: item.customSku,
qty: parseInt(item.ItemShops.ItemShop[0].sellable)
};
});
// builds an XML file for submitting to Amazon as an inventory update
// the second function parameter is for your "fulfillment latency" on Amazon; the time it
// takes you to ship an item.. unfortunately this is a required field on Amazon's end.
const inventoryFeed = await buildInventoryFeed(inventory, 4);
// submits the inventory feed to Amazon
await submitInventoryFeed(inventoryFeed);
resolve();
}
});
};
module.exports = massSync;