diff --git a/app/services/pricing.py b/app/services/pricing.py index 97793a7..025e4f5 100644 --- a/app/services/pricing.py +++ b/app/services/pricing.py @@ -103,16 +103,19 @@ class PricingService: self.tcgplayer_service.load_tcgplayer_cards(file_content) self.load_pricing_csv_content_to_db(file_content) - def get_all_prices_for_products(self, product_ids: List[str]) -> Dict[str, Dict[str, float]]: - all_prices = self.db.query(Price).filter( + def get_latest_prices_for_products(self, product_ids: List[str]) -> Dict[str, Dict[str, float]]: + latest_prices = self.db.query(Price).filter( Price.product_id.in_(product_ids) + ).distinct(Price.product_id, Price.type).order_by( + Price.product_id, + Price.type, + Price.date_created.desc() ).all() - + price_lookup = {} - for price in all_prices: - if price.product_id not in price_lookup: - price_lookup[price.product_id] = {} - price_lookup[price.product_id][price.type] = price.price + for price in latest_prices: + price_lookup.setdefault(price.product_id, {})[price.type] = price.price + return price_lookup def apply_price_to_df_columns(self, row: pd.Series, price_lookup: Dict[str, Dict[str, float]]) -> pd.Series: @@ -156,7 +159,7 @@ class PricingService: tcg_low = Decimal(str(row.get('tcg_low_price'))) if not pd.isna(row.get('tcg_low_price')) else None tcg_low_shipping = Decimal(str(row.get('tcg_low_price_with_shipping'))) if not pd.isna(row.get('tcg_low_price_with_shipping')) else None tcg_market_price = Decimal(str(row.get('tcg_market_price'))) if not pd.isna(row.get('tcg_market_price')) else None - current_price = Decimal(str(row.get('tcg_marketplace_price'))) if not pd.isna(row.get('tcg_marketplace_price')) else None + #current_price = Decimal(str(row.get('tcg_marketplace_price'))) if not pd.isna(row.get('tcg_marketplace_price')) else None total_quantity = str(row.get('total_quantity')) if not pd.isna(row.get('total_quantity')) else "0" added_quantity = str(row.get('add_to_quantity')) if not pd.isna(row.get('add_to_quantity')) else "0" quantity = int(total_quantity) + int(added_quantity) @@ -219,9 +222,9 @@ class PricingService: new_price = Decimal('0.25') # Avoid huge price drops - if current_price is not None and Decimal(str(((current_price - new_price) / current_price))) > Decimal('0.5'): - logger.warning(f"Price drop too large for row: {row}") - new_price = current_price + #if current_price is not None and Decimal(str(((current_price - new_price) / current_price))) > Decimal('0.5'): + #logger.warning(f"Price drop too large for row: {row}") + #new_price = current_price # Round to 2 decimal places new_price = new_price.quantize(TWO_PLACES, rounding=ROUND_HALF_UP) @@ -236,7 +239,6 @@ class PricingService: TCG Low: {tcg_low} TCG Low Shipping: {tcg_low_shipping} TCG Market Price: {tcg_market_price} - Current Price: {current_price} Total Quantity: {total_quantity} Added Quantity: {added_quantity} Quantity: {quantity} @@ -349,7 +351,7 @@ class PricingService: if null_product_ids: logger.warning(f"The following tcgplayer_ids could not be mapped to a product_id: {null_product_ids}") - price_lookup = self.get_all_prices_for_products(df['product_id'].unique()) + price_lookup = self.get_latest_prices_for_products(df['product_id'].unique()) # Apply price columns df = df.apply(lambda row: self.apply_price_to_df_columns(row, price_lookup), axis=1)