This commit is contained in:
Andrew Pimenov 2026-10-07 06:59:24 +03:00 • committed by GitHub
commit 3685ebd167
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -219,13 +219,42 @@ export async function fetchHistoricalRatesBatch(dates: string[]): Promise<Record
if (isTauri()) { if (isTauri()) {
return await invoke<Record<string, Record<string, number>>>("fetch_historical_rates_batch", { dates }); return await invoke<Record<string, Record<string, number>>>("fetch_historical_rates_batch", { dates });
} }
const sortedDates = [...dates].sort();
const minDate = sortedDates[0];
const maxDate = sortedDates[sortedDates.length - 1];
const results: Record<string, Record<string, number>> = {}; const results: Record<string, Record<string, number>> = {};
await Promise.all( const res = await fetch(`https://api.frankfurter.dev/v1/${minDate}..${maxDate}?from=USD`);
dates.map(async (date) => { const json = await res.json();
const rates = await fetchHistoricalRates(date);
if (rates) results[date] = rates; if (json?.rates) {
}) const availableDates = Object.keys(json.rates).sort();
);
for (const date of dates) {
let rawRates = json.rates[date];
// Fallback to closest previous available date if exact match is missing (e.g., weekends)
if (!rawRates) {
let targetDate = date;
for (let i = availableDates.length - 1; i >= 0; i--) {
if (availableDates[i] <= date) {
targetDate = availableDates[i];
break;
}
}
rawRates = json.rates[targetDate];
}
if (rawRates) {
const out: Record<string, number> = { USD: 1 };
for (const [k, v] of Object.entries(rawRates as Record<string, number>)) {
if (v > 0) out[k.toUpperCase()] = v;
}
results[date] = out;
}
}
}
return results; return results;
} catch (e) { } catch (e) {
logger.warn("fetchHistoricalRatesBatch failed", e); logger.warn("fetchHistoricalRatesBatch failed", e);