From b0cb6afe1f9692d23fe3e5dc300f48a0533a723a Mon Sep 17 00:00:00 2001
From: Arno Wunderlich <arno.a.wunderlich@student.jyu.fi>
Date: Mon, 20 Feb 2023 08:48:49 +0000
Subject: [PATCH] use reference currency in notifications

---
 src/tjts5901/views.py | 39 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 20 deletions(-)

diff --git a/src/tjts5901/views.py b/src/tjts5901/views.py
index 7b1c506e..b85448e8 100644
--- a/src/tjts5901/views.py
+++ b/src/tjts5901/views.py
@@ -84,20 +84,14 @@ def get_item_price(item: Item) -> int:
 def check_auction_ends():
     """
     Check if all previously not processed auctions have ended now
-    Get all closed items, check if they belong to the current user and inform about 
-    the ending of the auction.
-    Then process all bids on these items and check if they won
+    Get all closed items, process all bids on these items and check if they won
+    Then check if current_user needs to be informed
     """
-    items = Item.objects.filter(Q(closes_at=datetime.utcnow()) & Q(closed_processed=False)) \
+    items = Item.objects.filter(Q(closes_at__lte=datetime.utcnow()) & Q(closed_processed=False)) \
         .order_by('-closes_at') 
     for item in items:
         item.update(closed_processed = True)
-        message = None
-
-        if item.seller == current_user:
-            message = "The auction of your item \'{}\' has ended. ".format(item.title)
-            item.update(seller_informed_about_result = True)
-
+       
         # Get (potential) bids for the item and process them
         bids = Bid.objects(item=item).order_by('-amount')
 
@@ -111,19 +105,24 @@ def check_auction_ends():
                 bid.update(winning_bid = False)
                 bid.update(auction_end_processed = True)
 
-            # Inform seller if this is current user
-            if item.seller == current_user:
-                message += "Congrats! It was sold for {}.".format(bids[0].amount)
-
-            # Inform current user if no one bid on the item
-        else:
-            if item.seller == current_user:
-                message += "No one bid on your item. Try again!"
-
+    # Inform current user about results of his auctions
+    items_closed = Item.objects.filter(Q(closed_processed=True) & Q(seller_informed_about_result=False) \
+                   & Q(seller=current_user))
+    message = None
+    for item in items_closed:
+        # Get (potential) bids for the item and process them
+        message = "The auction of your item \'{}\' has ended. ".format(item.title)
+        bids = Bid.objects(item=item).order_by('-amount')
+        if len(bids) >= 1:
+             message += "Congrats! It was sold for {}.".format(bids[0].amount)
+        else: 
+            message += "No one bid on your item. Try again!"
+        item.update(seller_informed_about_result = True)
+    
         #Display message if not None
         if message is not None:
             flash((message))
-
+    
 
 def check_bids_ended():
     """
-- 
GitLab