X-Git-Url: https://us-delaporte.pagekite.me/gitweb/matrix-shop-bot.git/blobdiff_plain/aa3ac50778f5cfcd643274d07e702b01bb8de7cf..050ee98fbbfea0a1cde53a5853e1da905f8cbb7b:/matrix_shop_bot/db.py diff --git a/matrix_shop_bot/db.py b/matrix_shop_bot/db.py index 007e758..a225f3c 100644 --- a/matrix_shop_bot/db.py +++ b/matrix_shop_bot/db.py @@ -1,6 +1,7 @@ import logging import os from datetime import datetime as dt +from datetime import timedelta 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: - 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' @@ -190,10 +191,15 @@ class Data(): return ['Success'] def list_purchases(self, store_name): + '''List unique purchases at store in the past 90 days''' 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() - return records + bought = set() + for item in records: + bought.add(item.name) + return list(bought) def purchase_items(self, item_ids): results = []