Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Aug 3, 2024
1 parent 81576cb commit 6b9d363
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions apps/engine/src/utils/rates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,40 @@ const ENDPOINT =

async function getCurrency(currency: string) {
const response = await fetch(`${ENDPOINT}/currencies/${currency}.json`);

return response.json();
}

export async function getRates() {
console.log(uniqueCurrencies);
function transformKeysToUppercase(obj) {
// Convert the object into an array of [key, value] pairs
const entries = Object.entries(obj);

// Transform each entry's key to uppercase
const upperCaseEntries = entries.map(([key, value]) => {
return [key.toUpperCase(), value];
});

// Convert the transformed entries back into an object
const transformedObject = Object.fromEntries(upperCaseEntries);

return transformedObject;
}

export async function getRates() {
const rates = await Promise.allSettled(
uniqueCurrencies.map((currency) => getCurrency(currency.toLowerCase())),
);

return rates;
return rates
.filter((rate) => rate.status === "fulfilled")
.map((rate) => rate.value)
.map((value) => {
const currency = Object.keys(value).at(1);

return {
source: currency?.toUpperCase(),
rates: transformKeysToUppercase(value[currency]),
// TODO: Filter currencies that are not in the list of unique currencies
};
});
}

0 comments on commit 6b9d363

Please sign in to comment.