Last active
July 11, 2018 21:30
-
-
Save gje4/7d78c7cff7928ba9023bdef6d37c3016 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// moltin SDK setup | |
const moltin = require('@moltin/sdk'); | |
const MoltinSite = moltin.gateway({ | |
client_id: process.env.CLIENT_ID_SITE, | |
client_secret: process.env.CLIENT_SECRET_SITE | |
}); | |
exports.formatOrders = function(orders, items) { | |
return new Promise(function(resolve, reject) { | |
let formattedOrders = []; | |
orders.data.forEach(function(order) { | |
exports.itemsLookup(order, orders.included.items) | |
.then((order) => { | |
formattedOrders.push(order); | |
if(formattedOrders.length === orders.data.length) { | |
console.log('all orders are finished formatting'); | |
resolve(formattedOrders); | |
}; | |
}); | |
}); | |
}); | |
} | |
exports.itemsLookup = function(order, items) { | |
return new Promise(function(resolve, reject) { | |
// initialise our counter | |
var itemsProcessed = 0; | |
let itemsArray = []; | |
//for each of the orders related items | |
order.relationships.items.data.forEach(function(item) { | |
// ID for the orders item | |
let id = item.id; | |
// increment the counter | |
itemsProcessed++; | |
// look up each item | |
items.forEach(function(item) { | |
if(item.id === id) { | |
console.log('matching order item object found'); | |
itemsArray.push(item); | |
console.log(itemsArray); | |
} | |
}); | |
// if there are not order items left to process | |
if(itemsProcessed === order.relationships.items.data.length) { | |
console.log('finished processing items'); | |
order.relationships.items = itemsArray; | |
resolve(order); | |
}; | |
}); | |
}); | |
}; | |
Moltin.Orders | |
.With('items') | |
.All() | |
.then((orders) => { | |
exports.formatOrders(orders, orders.included.items) | |
.then((formattedOrders) => { | |
console.log(formattedOrders); | |
}) | |
}) | |
.catch((e) => { | |
console.log(e); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment