import httpx import logging from datetime import datetime from typing import List, Dict, Any, Optional from app.config.rider_preferences import RIDER_PREFERRED_KITCHENS logger = logging.getLogger(__name__) async def fetch_active_riders() -> List[Dict[str, Any]]: """ Fetch active rider logs from the external API for the current date. Returns a list of rider log dictionaries. """ try: today_str = datetime.now().strftime("%Y-%m-%d") url = "https://jupiter.nearle.app/live/api/v2/partners/getriderlogs/" params = { "applocationid": 1, "partnerid": 44, "fromdate": today_str, "todate": today_str, "keyword": "" } async with httpx.AsyncClient(timeout=30.0) as client: response = await client.get(url, params=params) response.raise_for_status() data = response.json() if data and data.get("code") == 200 and data.get("details"): # Filter riders who are in our preferences list and are 'active' or 'idle' (assuming we want online riders) # The user's example showed "onduty": 1. We might want to filter by that. # For now, returning all logs, filtering can happen in assignment logic or here. # Let's return the raw list as requested, filtering logic will be applied during assignment. return data.get("details", []) logger.warning(f"Fetch active riders returned no details: {data}") return [] except Exception as e: logger.error(f"Error fetching active riders: {e}", exc_info=True) return [] async def fetch_created_orders() -> List[Dict[str, Any]]: """ Fetch all orders in 'created' state for the current date. """ try: today_str = datetime.now().strftime("%Y-%m-%d") url = "https://jupiter.nearle.app/live/api/v1/orders/tenant/getorders/" # Removed pagesize as per user request to fetch all params = { "applocationid": 0, "tenantid": 0, "locationid": 0, "status": "created", "fromdate": today_str, "todate": today_str, "keyword": "", "pageno": 1 # "pagesize" intentionally omitted to fetch all } async with httpx.AsyncClient(timeout=60.0) as client: response = await client.get(url, params=params) response.raise_for_status() data = response.json() if data and data.get("code") == 200 and data.get("details"): return data.get("details", []) logger.warning(f"Fetch created orders returned no details: {data}") return [] except Exception as e: logger.error(f"Error fetching created orders: {e}", exc_info=True) return [] async def fetch_rider_pricing() -> List[Dict[str, Any]]: """ Fetch rider pricing configuration from external API. """ try: url = "https://jupiter.nearle.app/live/api/v1/partners/getriderpricing" async with httpx.AsyncClient(timeout=30.0) as client: response = await client.get(url) response.raise_for_status() data = response.json() if data and data.get("code") == 200: return data.get("details", []) logger.warning(f"Fetch rider pricing returned no details: {data}") return [] except Exception as e: logger.error(f"Error fetching rider pricing: {e}", exc_info=True) return []