diff options
Diffstat (limited to 'services/pantry_service.py')
| -rw-r--r-- | services/pantry_service.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/services/pantry_service.py b/services/pantry_service.py new file mode 100644 index 0000000..19271e5 --- /dev/null +++ b/services/pantry_service.py @@ -0,0 +1,51 @@ +from sqlalchemy.orm import Session +from datetime import datetime, timedelta +from models import Ingredient, MealLog +from collections import Counter + + +def build_pantry_context(db: Session) -> dict: + """Return a dict with pantry state for use as AI context.""" + today = datetime.utcnow().date() + soon = today + timedelta(days=3) + + # All ingredients + all_ingredients = db.query(Ingredient).all() + available = [ + { + "name": i.name, + "quantity": i.quantity, + "unit": i.unit, + "category": i.category, + "expiry": i.expiry_date.isoformat() if i.expiry_date else None, + } + for i in all_ingredients + ] + + # Expiring within 3 days + expiring = [ + i for i in available + if i["expiry"] and i["expiry"] <= soon.isoformat() + ] + + # Recent meals (last 14 days) + cutoff = datetime.utcnow() - timedelta(days=14) + recent_meal_logs = db.query(MealLog).filter(MealLog.eaten_at >= cutoff).order_by(MealLog.eaten_at.desc()).all() + recent_meals = [ + { + "date": m.eaten_at.date().isoformat(), + "meal_type": m.meal_type, + "meal_name": m.meal_name, + } + for m in recent_meal_logs + ] + + # Meal frequency (count by name) + meal_frequency = dict(Counter(m["meal_name"] for m in recent_meals)) + + return { + "available_ingredients": available, + "expiring_soon": expiring, + "recent_meals": recent_meals, + "meal_frequency": meal_frequency, + } |
