]> us-delaporte.pagekite.me Git - matrix-shop-bot.git/blobdiff - matrix_shop_bot/db.py
Use contains check for zones
[matrix-shop-bot.git] / matrix_shop_bot / db.py
index 007e75852ee82d2819a351d46e8d1a2a1f97a8e3..a225f3c109731d4550e2e0482b8ce659ea9b1230 100644 (file)
@@ -1,6 +1,7 @@
 import logging
 import os
 from datetime import datetime as dt
 import logging
 import os
 from datetime import datetime as dt
+from datetime import timedelta
 from decimal import Decimal
 from typing import List
 
 from decimal import Decimal
 from typing import List
 
@@ -50,7 +51,7 @@ class PreferStore(SQLModel, table=True):
     store_id: int | None = Field(default=None, foreign_key="store.id", primary_key=True)
 
 def _get_item_zone(item_name, session) -> str:
     store_id: int | None = Field(default=None, foreign_key="store.id", primary_key=True)
 
 def _get_item_zone(item_name, session) -> str:
-    zone = session.exec(select(ItemZone).where(func.lower(ItemZone.name) == func.lower(item_name))).first()
+    zone = session.exec(select(ItemZone).where(func.lower(item_name).contains(func.lower(ItemZone.name)))).first()
     if zone:
         return zone.zone
     return 'No Zone Assigned'
     if zone:
         return zone.zone
     return 'No Zone Assigned'
@@ -190,10 +191,15 @@ class Data():
         return ['Success']
 
     def list_purchases(self, store_name):
         return ['Success']
 
     def list_purchases(self, store_name):
+        '''List unique purchases at store in the past 90 days'''
         with Session(self.engine) as session:
         with Session(self.engine) as session:
-            cmd = select(PurchaseRecord).where(func.lower(PurchaseRecord.store) == func.lower(store_name))
+            recent = dt.now() - timedelta(days=90)
+            cmd = select(PurchaseRecord).where(func.lower(PurchaseRecord.store) == func.lower(store_name)).where(PurchaseRecord.purchase_date > recent)
             records = session.exec(cmd).all()
             records = session.exec(cmd).all()
-            return records
+            bought = set()
+            for item in records:
+                 bought.add(item.name)
+            return list(bought)
 
     def purchase_items(self, item_ids):
         results = []
 
     def purchase_items(self, item_ids):
         results = []