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.
75 lines
2.3 KiB
JavaScript
75 lines
2.3 KiB
JavaScript
|
|
module.exports = class {
|
|
constructor(listing) {
|
|
this.listingId = listing.idInv;
|
|
this.description = listing.strDesc;
|
|
this.condition = listing.codeNew === 'U' ? 'USED' : 'NEW';
|
|
this.complete = listing.codeComplete;
|
|
this.quantity = listing.n4Qty;
|
|
this.colorDesc = listing.strColor;
|
|
this.colorId = listing.idColor;
|
|
this.minBuy = parseMinBuy(listing.mMinBuy);
|
|
this.sellerUsername = listing.strSellerUsername;
|
|
this.sellerStoreName = listing.strStorename;
|
|
this.instantCheckout = listing.instantCheckout;
|
|
this.legoPartNumber = null;
|
|
this.blPartNumber = null;
|
|
this.calculatedPrice = 0
|
|
this.prices = [
|
|
{
|
|
quantity: 1,
|
|
value: parseDollars(listing.mDisplaySalePrice)
|
|
}
|
|
]
|
|
if(listing.nTier1Qty) {
|
|
//add bulk pricing tier
|
|
this.prices.push({
|
|
quantity: listing.nTier1Qty,
|
|
value: parseDollars(listing.nTier1DisplayPrice)
|
|
})
|
|
}
|
|
if(listing.nTier2Qty) {
|
|
//add bulk pricing tier
|
|
this.prices.push({
|
|
quantity: listing.nTier2Qty,
|
|
value: parseDollars(listing.nTier2DisplayPrice)
|
|
})
|
|
}
|
|
if(listing.nTier3Qty) {
|
|
//add bulk pricing tier
|
|
this.prices.push({
|
|
quantity: listing.nTier3Qty,
|
|
value: parseDollars(listing.nTier3DisplayPrice)
|
|
})
|
|
}
|
|
}
|
|
|
|
getPrice(quantity) {
|
|
for(var i = this.prices.length - 1;i >= 0;i--){
|
|
const price = this.prices[i];
|
|
|
|
if(quantity >= price.quantity){
|
|
return price.value * quantity
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
toString(){
|
|
return `${this.legoPartNumber} (${this.quantity}) -${this.colorId}- @ $${this.getPrice(this.quantity)}`
|
|
}
|
|
|
|
}
|
|
const priceRegex = /US \$(?<price>\d+\.\d+)/;
|
|
function parseMinBuy(value){
|
|
if(value === 'None') return 0;
|
|
return parseDollars(value);
|
|
}
|
|
function parseDollars(val) {
|
|
const regexed = priceRegex.exec(val);
|
|
const stringPrice = regexed?.groups?.price;
|
|
if(!stringPrice){
|
|
return 999999999;
|
|
} else {
|
|
return parseFloat(stringPrice) === 0 ? 0.01 : parseFloat(stringPrice);
|
|
}
|
|
} |