You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.6 KiB
JavaScript

const brickLinkAPI = require('./bricklink');
const _ = require('lodash');
const cart = [
{
partNumber: '4592c01',
quantity: 2
},
{
partNumber: '3962b',
quantity: 1
},
{
partNumber: '3022',
quantity: 1
},
{
partNumber: '4081b',
quantity: 4
},
{
partNumber: '4740',
quantity: 1
},
{
partNumber: '3031',
quantity: 1
},
{
partNumber: '30303',
quantity: 1
},
{
partNumber: '3049b',
quantity: 1
},
{
partNumber: '3069bpc1',
quantity: 1
},
{
partNumber: '3004',
quantity: 1
},
{
partNumber: '2342',
quantity: 1
},
]
Promise.each = async function(arr, fn) { // take an array and a function
const results = [];
for(const item of arr) results.push(await item);
return results;
}
Promise.each(cart.map(lot => {
return new Promise((resolve, reject) => {
brickLinkAPI.getInternalPartId(lot.partNumber).then((blId) => {
brickLinkAPI.getListings(blId).then(listings => {
console.log(listings.length + 'Listings found');
const lotsWithEnoughItems = listings.filter(l => l.quantity >= lot.quantity);
// const lotsWithEnoughItemsAndAboveMinBuy = lotsWithEnoughItems.filter(lotf => lotf.minBuy === 'None' || (lotf.getPrice(lot.quantity) > lotf.minBuy)).map(lotf => {
// lotf.calculatedPrice = lotf.getPrice(lot.quantity);
// return lotf;
// })
setTimeout(() => {
resolve(lotsWithEnoughItems);
}, 1000)
}).catch(error => {
console.error('Failed to fetch listings', error);
reject(error);
})
}).catch(error => {
console.error('Failed to fetch internal id', error);
reject(error);
})
});
})).then(parts => {
var groupedLots = [];
const possibleLots = _.flatten(parts);
possibleLots.forEach(lot => {
const group = _.findIndex(groupedLots, l => l.sellerUsername === lot.sellerUsername);
if(group === -1) {
const newGroup = {
sellerUsername: lot.sellerUsername,
minBuy: lot.minBuy,
lots: [lot]
}
groupedLots.push(newGroup);
} else {
groupedLots[group].lots.push(lot);
}
})
console.log(groupedLots.filter(gl => gl.lots.length > 1));
})