Álvaro Valenzuela Valdes commited on
Commit
df23d27
·
1 Parent(s): f9803ac

🚀 Production: 3-day deep search fix + Lifecycle UI + Data persistence

Browse files
backend/app/services/mercado_publico.py CHANGED
@@ -213,19 +213,23 @@ async def _fetch(params: Dict[str, str], retries: int = 3) -> List[Tender]:
213
  return []
214
 
215
  async def get_active_tenders() -> List[Tender]:
216
- """Fetch tenders active today in Chile (UTC-4)."""
217
- # Force Chile time (UTC-4)
218
  chile_tz = timezone(timedelta(hours=-4))
219
- today = datetime.now(chile_tz).strftime("%d%m%Y")
220
- results = await _fetch({"fecha": today})
221
 
222
- # Fallback to yesterday if today is empty (common early in the morning)
223
- if not results:
224
- yesterday = (datetime.now(chile_tz) - timedelta(days=1)).strftime("%d%m%Y")
225
- print(f"ℹ️ Today ({today}) is empty, falling back to yesterday ({yesterday})")
226
- results = await _fetch({"fecha": yesterday})
227
-
228
- return results
 
 
 
 
 
229
 
230
  async def get_tenders_by_date(date_ddmmaaaa: str) -> List[Tender]:
231
  """Fetch tenders for a specific date (ddmmaaaa)."""
 
213
  return []
214
 
215
  async def get_active_tenders() -> List[Tender]:
216
+ """Fetch tenders from the last 3 days to ensure good coverage."""
 
217
  chile_tz = timezone(timedelta(hours=-4))
218
+ all_results = []
219
+ seen_codes = set()
220
 
221
+ # Fetch today, yesterday, and day before yesterday
222
+ for i in range(3):
223
+ date_to_fetch = (datetime.now(chile_tz) - timedelta(days=i)).strftime("%d%m%Y")
224
+ print(f"[MP API] Fetching tenders for: {date_to_fetch} (Day -{i})")
225
+ day_results = await _fetch({"fecha": date_to_fetch})
226
+
227
+ for t in day_results:
228
+ if t.code not in seen_codes:
229
+ seen_codes.add(t.code)
230
+ all_results.append(t)
231
+
232
+ return all_results
233
 
234
  async def get_tenders_by_date(date_ddmmaaaa: str) -> List[Tender]:
235
  """Fetch tenders for a specific date (ddmmaaaa)."""