added updating status on upsert

This commit is contained in:
TPV committed 2026-07-06 18:40:43 -07:00
1 parent d6f48afeed
commit 9496d0dfde
1 file changed
+5 -9
+5 -9
View File
@@ -96,20 +96,16 @@ async def add_products_to_database(
document["_id"] = doc_id document["_id"] = doc_id
old_price = existing_prices.get(doc_id) old_price = existing_prices.get(doc_id)
new_price = document.get("currentPrice") new_price = document.get("currentPrice")
price_changed = new_price is not None and new_price != old_price
price_dropped = old_price is None or (
new_price is not None and new_price < old_price
)
set_on_insert = {"discoveredDate": now} 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["status"] = ProductStatus.PENDING
document["priceBeforeDrop"] = old_price document["priceBeforeDrop"] = old_price
else:
set_on_insert["status"] = ProductStatus.PENDING
update = {"$set": document, "$setOnInsert": set_on_insert} 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 update["$set"][f"pastPrices.{price_key}"] = new_price
ops.append(UpdateOne({"_id": doc_id}, update, upsert=True)) ops.append(UpdateOne({"_id": doc_id}, update, upsert=True))