Last active
October 26, 2018 11:17
-
-
Save Bigomby/f2ce609228abd924c5d17e415aefdb2a 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
const orderItemListPromises = orderItemList.map(orderItems => { | |
return Promise.all( | |
orderItems.data.map(async orderItem => { | |
const filter = { where: { orderId: orderItem.order_id } }; | |
const dateString = orderItem.date + ""; | |
const stadistics = { | |
shopId: orderItem.shopId, | |
date: parseInt(dateString.slice(0, -3)), | |
itemName: orderItem.item_name, | |
amount: orderItem.total, | |
quantity: orderItem.quantity, | |
finished: 0 | |
}; | |
try { | |
const receipt = await axios.get( | |
url + | |
"/" + | |
orderItem.shopId + | |
"/receipts?filter=" + | |
JSON.stringify(filter), | |
{ headers } | |
); | |
if (receipt.data.length > 1) { | |
console.log( | |
"¡¡¡¡¡ALERTA!!!!! orderId duplicado x" + receipt.data.length | |
); | |
} | |
if (!receipt.data[0].workerName) { | |
stadistics.worker = "Desconocido"; | |
} else { | |
stadistics.worker = receipt.data[0].workerName; | |
} | |
console.log("SAVE", stadistics); | |
listStatsToSave.push(stadistics); | |
console.log("listStatsToSave ", listStatsToSave); | |
} catch (error) { | |
return; | |
} | |
}) | |
); | |
}); | |
await Promise.all(orderItemListPromises); | |
console.log("XX", listStatsToSave); | |
console.log("XX", listStatsToSave.length); |
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
const orderItemListPromises = orderItemList.map(orderItems => { | |
return Promise.all( | |
orderItems.data.map(async orderItem => { | |
const filter = { where: { orderId: orderItem.order_id } }; | |
const dateString = orderItem.date + ""; | |
const stadistics = { | |
shopId: orderItem.shopId, | |
date: parseInt(dateString.slice(0, -3)), | |
itemName: orderItem.item_name, | |
amount: orderItem.total, | |
quantity: orderItem.quantity, | |
finished: 0 | |
}; | |
try { | |
const receipt = await axios.get( | |
url + | |
"/" + | |
orderItem.shopId + | |
"/receipts?filter=" + | |
JSON.stringify(filter), | |
{ headers } | |
); | |
if (receipt.data.length > 1) { | |
console.log( | |
"¡¡¡¡¡ALERTA!!!!! orderId duplicado x" + receipt.data.length | |
); | |
} | |
if (!receipt.data[0].workerName) { | |
stadistics.worker = "Desconocido"; | |
} else { | |
stadistics.worker = receipt.data[0].workerName; | |
} | |
return stadistics; | |
} catch (error) { | |
return; | |
} | |
}) | |
); | |
}); | |
const result = await Promise.all(orderItemListPromises); | |
result.reduce((acc, current) => { | |
// Aquí haces tus cosas | |
}); |
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
// const data = require("./data"); | |
const data = [ | |
/** El enorme array **/ | |
]; | |
function groupBy(group) { | |
return function(acc, entry) { | |
const result = { | |
shopId: entry.shopId, | |
date: entry.date, | |
worker: entry.worker, | |
itemName: entry.itemName, | |
finished: entry.finished | |
}; | |
delete result[group]; | |
if (!acc.has(entry[group])) { | |
result.amount = entry.amount; | |
} else { | |
const { amount } = acc.get(entry[group]); | |
result.amount = amount + entry.amount; | |
} | |
acc.set(entry[group], result); | |
return acc; | |
}; | |
} | |
const groupedByWorker = data.reduce(groupBy("worker"), new Map()); | |
const groupedByItem = data.reduce(groupBy("itemName"), new Map()); | |
const groupedByWorkerArray = Array.from(groupedByWorker).map( | |
([worker, entry]) => ({ worker, ...entry }) | |
); | |
const groupedByItemArray = Array.from(groupedByItem).map( | |
([itemName, entry]) => ({ itemName, ...entry }) | |
); | |
console.log("BY WORKER", groupedByWorkerArray); | |
console.log("BY ITEM", groupedByItemArray); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment