added updating status on upsert
This commit is contained in:
1 parent
d6f48afeed
commit
9496d0dfde
1 file changed
+5
-9
@@ -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))
|
||||
|
||||
Reference in new issue
Block a user