From e93b7da394a466a5c8d69be3e198d4be23d7002f Mon Sep 17 00:00:00 2001 From: Pontus Abrahamsson Date: Sat, 3 Aug 2024 10:55:28 +0200 Subject: [PATCH] Fix route --- apps/engine/src/routes/rates/index.ts | 2 +- apps/engine/src/utils/rates.ts | 33 +++++++++++++++++++-------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/apps/engine/src/routes/rates/index.ts b/apps/engine/src/routes/rates/index.ts index 3bbad3beb3..8fc7b123ca 100644 --- a/apps/engine/src/routes/rates/index.ts +++ b/apps/engine/src/routes/rates/index.ts @@ -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, diff --git a/apps/engine/src/utils/rates.ts b/apps/engine/src/utils/rates.ts index 062b7a1921..3655cfcbf7 100644 --- a/apps/engine/src/utils/rates.ts +++ b/apps/engine/src/utils/rates.ts @@ -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) { 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); @@ -30,15 +31,27 @@ export async function getRates() { ); return rates - .filter((rate) => rate.status === "fulfilled") + .filter( + (rate): rate is PromiseFulfilledResult> => + 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), }; - }); + }) + .filter((item) => item !== null); }