mirror of
https://github.com/frappe/books.git
synced 2025-01-11 10:38:14 +00:00
3e3235b814
- Add base fields in SalesInvoice and SalesInvoiceItem - Get exchange rate from exchangeratesapi - Fix infinite update loop due to actions - Filter submitted invoices in UnpaidInvoices - Revert action - disableCreation in Link - Ledger Entries action - Set default filters in Report
31 lines
955 B
JavaScript
31 lines
955 B
JavaScript
let { DateTime } = require('luxon');
|
|
|
|
export async function getExchangeRate({ fromCurrency, toCurrency, date }) {
|
|
if (!date) {
|
|
date = DateTime.local().toISODate();
|
|
}
|
|
if (!fromCurrency || !toCurrency) {
|
|
throw new Error(
|
|
'Please provide `fromCurrency` and `toCurrency` to get exchange rate.'
|
|
);
|
|
}
|
|
let cacheKey = `currencyExchangeRate:${date}:${fromCurrency}:${toCurrency}`;
|
|
let exchangeRate = parseFloat(localStorage.getItem(cacheKey));
|
|
if (!exchangeRate) {
|
|
try {
|
|
let res = await fetch(
|
|
`https://api.exchangeratesapi.io/${date}?base=${fromCurrency}&symbols=${toCurrency}`
|
|
);
|
|
let data = await res.json();
|
|
exchangeRate = data.rates[toCurrency];
|
|
localStorage.setItem(cacheKey, exchangeRate);
|
|
} catch (error) {
|
|
console.error(error);
|
|
throw new Error(
|
|
`Could not fetch exchange rate for ${fromCurrency} -> ${toCurrency}`
|
|
);
|
|
}
|
|
}
|
|
return exchangeRate;
|
|
}
|