diff --git a/apps/backend/src/modules/quotes/handlers/getMonthlyLast.ts b/apps/backend/src/modules/quotes/handlers/getMonthlyLast.ts index be1f663..458b518 100644 --- a/apps/backend/src/modules/quotes/handlers/getMonthlyLast.ts +++ b/apps/backend/src/modules/quotes/handlers/getMonthlyLast.ts @@ -113,14 +113,21 @@ export async function getMonthlyLast(c: Context) { const externalData = new Map>(); - for (const [type, casa] of Object.entries(CASA_MAP)) { - if (!missingByType.has(type)) continue; + const fetches = Object.entries(CASA_MAP).map(async ([type, casa]) => { + if (!missingByType.has(type)) return; try { + const controller = new AbortController(); + const timeout = setTimeout(() => controller.abort(), 30_000); const res = await fetch( `https://api.argentinadatos.com/v1/cotizaciones/dolares/${casa}`, + { signal: controller.signal }, ); - if (!res.ok) continue; + clearTimeout(timeout); + if (!res.ok) { + console.error(`[monthlyLast] ${casa} responded ${res.status}`); + return; + } const quotes: ExternalQuote[] = await res.json(); const monthLast = new Map(); @@ -143,10 +150,15 @@ export async function getMonthlyLast(c: Context) { typeMap.set(month, data.compra); } externalData.set(type, typeMap); - } catch { - // If external API fails, continue without those values + console.error( + `[monthlyLast] ${casa}: fetched ${quotes.length} quotes, ${monthLast.size} months`, + ); + } catch (e) { + console.error(`[monthlyLast] ${casa} fetch failed:`, e); } - } + }); + + await Promise.all(fetches); const result: MonthlyLastRow[] = []; for (const month of expectedMonths) { @@ -164,6 +176,10 @@ export async function getMonthlyLast(c: Context) { } } + console.error( + `[monthlyLast] result: ${result.length} rows, months: ${[...new Set(result.map((r) => r.month))].sort().join(", ")}`, + ); + cache.set(cacheKey, result, 300); return c.json(result); }