diff --git a/src/monitor_utils/database.py b/src/monitor_utils/database.py index 66f07d6..5199667 100644 --- a/src/monitor_utils/database.py +++ b/src/monitor_utils/database.py @@ -96,20 +96,16 @@ async def add_products_to_database( document["_id"] = doc_id old_price = existing_prices.get(doc_id) new_price = document.get("currentPrice") - - price_dropped = old_price is None or ( - new_price is not None and new_price < old_price - ) - + price_changed = new_price is not None and new_price != old_price set_on_insert = {"discoveredDate": now} - if price_dropped: + if old_price is None: + set_on_insert["status"] = ProductStatus.PENDING + elif price_changed: document["status"] = ProductStatus.PENDING document["priceBeforeDrop"] = old_price - else: - set_on_insert["status"] = ProductStatus.PENDING update = {"$set": document, "$setOnInsert": set_on_insert} - if new_price is not None and new_price != old_price: + if price_changed: update["$set"][f"pastPrices.{price_key}"] = new_price ops.append(UpdateOne({"_id": doc_id}, update, upsert=True))