Álvaro Valenzuela Valdes commited on
Commit ·
beebad7
1
Parent(s): b143c59
fix: force Chile timezone for API calls and fix status mapping to resolve 500 errors
Browse files
backend/app/services/mercado_publico.py
CHANGED
|
@@ -3,7 +3,7 @@ import httpx
|
|
| 3 |
from typing import List, Optional, Dict, Any
|
| 4 |
from app.config import settings
|
| 5 |
from app.schemas.tender import Tender, TenderItem
|
| 6 |
-
from datetime import datetime
|
| 7 |
|
| 8 |
# Global semaphore to avoid "peticiones simultáneas" error from MP API
|
| 9 |
mp_api_semaphore = asyncio.Semaphore(1)
|
|
@@ -171,9 +171,19 @@ async def _fetch(params: Dict[str, str], retries: int = 3) -> List[Tender]:
|
|
| 171 |
return []
|
| 172 |
|
| 173 |
async def get_active_tenders() -> List[Tender]:
|
| 174 |
-
"""Fetch tenders active today."""
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
async def get_tenders_by_date(date_ddmmaaaa: str) -> List[Tender]:
|
| 179 |
"""Fetch tenders for a specific date (ddmmaaaa)."""
|
|
@@ -201,7 +211,12 @@ async def get_tenders_by_filters(
|
|
| 201 |
params["fecha"] = datetime.now().strftime("%d%m%Y")
|
| 202 |
|
| 203 |
if status:
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
if org_code:
|
| 206 |
params["CodigoOrganismo"] = org_code
|
| 207 |
if provider_code:
|
|
|
|
| 3 |
from typing import List, Optional, Dict, Any
|
| 4 |
from app.config import settings
|
| 5 |
from app.schemas.tender import Tender, TenderItem
|
| 6 |
+
from datetime import datetime, timedelta, timezone
|
| 7 |
|
| 8 |
# Global semaphore to avoid "peticiones simultáneas" error from MP API
|
| 9 |
mp_api_semaphore = asyncio.Semaphore(1)
|
|
|
|
| 171 |
return []
|
| 172 |
|
| 173 |
async def get_active_tenders() -> List[Tender]:
|
| 174 |
+
"""Fetch tenders active today in Chile (UTC-4)."""
|
| 175 |
+
# Force Chile time (UTC-4)
|
| 176 |
+
chile_tz = timezone(timedelta(hours=-4))
|
| 177 |
+
today = datetime.now(chile_tz).strftime("%d%m%Y")
|
| 178 |
+
results = await _fetch({"fecha": today})
|
| 179 |
+
|
| 180 |
+
# Fallback to yesterday if today is empty (common early in the morning)
|
| 181 |
+
if not results:
|
| 182 |
+
yesterday = (datetime.now(chile_tz) - timedelta(days=1)).strftime("%d%m%Y")
|
| 183 |
+
print(f"ℹ️ Today ({today}) is empty, falling back to yesterday ({yesterday})")
|
| 184 |
+
results = await _fetch({"fecha": yesterday})
|
| 185 |
+
|
| 186 |
+
return results
|
| 187 |
|
| 188 |
async def get_tenders_by_date(date_ddmmaaaa: str) -> List[Tender]:
|
| 189 |
"""Fetch tenders for a specific date (ddmmaaaa)."""
|
|
|
|
| 211 |
params["fecha"] = datetime.now().strftime("%d%m%Y")
|
| 212 |
|
| 213 |
if status:
|
| 214 |
+
# Map friendly status to MP codes
|
| 215 |
+
# 'activas' is usually handled by not specifying a closed status or by specific date
|
| 216 |
+
if status == "activas":
|
| 217 |
+
pass # Default behavior for date-based fetch is often active/recent ones
|
| 218 |
+
else:
|
| 219 |
+
params["estado"] = status
|
| 220 |
if org_code:
|
| 221 |
params["CodigoOrganismo"] = org_code
|
| 222 |
if provider_code:
|
backend/app/services/mercado_publico_oc.py
CHANGED
|
@@ -3,7 +3,7 @@ import httpx
|
|
| 3 |
from typing import List, Optional, Dict, Any
|
| 4 |
from app.config import settings
|
| 5 |
from app.schemas.oc import PurchaseOrder, OCItem
|
| 6 |
-
from datetime import datetime
|
| 7 |
|
| 8 |
# Global semaphore to avoid "peticiones simultáneas" error from MP API
|
| 9 |
mp_api_semaphore = asyncio.Semaphore(1)
|
|
@@ -125,7 +125,9 @@ async def get_oc_by_code(code: str) -> Optional[PurchaseOrder]:
|
|
| 125 |
|
| 126 |
async def get_ocs_by_date(date: str, status: str = "todos") -> List[PurchaseOrder]:
|
| 127 |
params = {"estado": status}
|
| 128 |
-
|
|
|
|
|
|
|
| 129 |
if date is None or (date == today_str and status == "todos"):
|
| 130 |
return await _fetch_oc({"fecha": today_str})
|
| 131 |
|
|
|
|
| 3 |
from typing import List, Optional, Dict, Any
|
| 4 |
from app.config import settings
|
| 5 |
from app.schemas.oc import PurchaseOrder, OCItem
|
| 6 |
+
from datetime import datetime, timedelta, timezone
|
| 7 |
|
| 8 |
# Global semaphore to avoid "peticiones simultáneas" error from MP API
|
| 9 |
mp_api_semaphore = asyncio.Semaphore(1)
|
|
|
|
| 125 |
|
| 126 |
async def get_ocs_by_date(date: str, status: str = "todos") -> List[PurchaseOrder]:
|
| 127 |
params = {"estado": status}
|
| 128 |
+
chile_tz = timezone(timedelta(hours=-4))
|
| 129 |
+
today_str = datetime.now(chile_tz).strftime("%d%m%Y")
|
| 130 |
+
|
| 131 |
if date is None or (date == today_str and status == "todos"):
|
| 132 |
return await _fetch_oc({"fecha": today_str})
|
| 133 |
|