Skip to content

Commit

Permalink
Fix route
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Aug 3, 2024
1 parent 6b9d363 commit e93b7da
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion apps/engine/src/routes/rates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ app.openapi(indexRoute, async (c) => {
return c.json(
{
error: "Internal server error",
requestId: c.get("requestId"),
message: "Internal server error",
requestId: c.get("requestId"),
code: "400",
},
400,
Expand Down
33 changes: 23 additions & 10 deletions apps/engine/src/utils/rates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ async function getCurrency(currency: string) {
return response.json();
}

function transformKeysToUppercase(obj) {
// Convert the object into an array of [key, value] pairs
function transformKeysToUppercase(obj: Record<string, number>) {
const entries = Object.entries(obj);

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

// Convert the transformed entries back into an object
const transformedObject = Object.fromEntries(upperCaseEntries);
Expand All @@ -30,15 +31,27 @@ export async function getRates() {
);

return rates
.filter((rate) => rate.status === "fulfilled")
.filter(
(rate): rate is PromiseFulfilledResult<Record<string, unknown>> =>
rate.status === "fulfilled",
)
.map((rate) => rate.value)
.map((value) => {
const currency = Object.keys(value).at(1);

if (!currency) {
return null;
}

const currencyData = value[currency];
if (typeof currencyData !== "object" || currencyData === null) {
return null;
}

return {
source: currency?.toUpperCase(),
rates: transformKeysToUppercase(value[currency]),
// TODO: Filter currencies that are not in the list of unique currencies
source: currency.toUpperCase(),
rates: transformKeysToUppercase(currencyData as Record<string, number>),
};
});
})
.filter((item) => item !== null);
}

0 comments on commit e93b7da

Please sign in to comment.