Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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
You can’t perform that action at this time.