Skip to content

Commit

Permalink
Merge pull request #3 from camipozas/fix/error-get-values
Browse files Browse the repository at this point in the history
Fix get values
  • Loading branch information
camipozas authored Oct 4, 2022
2 parents 1644a81 + cfe09a2 commit 7803ecf
Showing 1 changed file with 39 additions and 25 deletions.
64 changes: 39 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const axios = require("axios");
const dayjs = require("dayjs");

const getUF = require("./indicators/getUF");
Expand All @@ -8,40 +7,55 @@ const getUTM = require("./indicators/getUTM");
const getIPC = require("./indicators/getIPC");

/**
* It fetches data from the Central Bank of Chile's API, and returns a single object with all the data
* It fetches data from the Central Bank of Chile's API and returns it in a single object
* @returns An object with the following structure:
* {
* UF: {
* date: "2020-04-01",
* value: 28.8
* date: "2020-09-01",
* value: 0.0027
* },
* USD: {
* date: "2020-04-01",
* value: 755.5
* date: "2020-09-01",
* value: 0.0013
* },
* EUR: {
* date: "2020-04-01",
* date: "2020-09-01",
*/
const main = async () => {
const sevenDaysAgo = dayjs().subtract(7, "day").format("YYYY/MM/[dias]/DD");
const lastMonth = dayjs().subtract(1, "month").format("YYYY/MM");
const twoMonthBefore = dayjs().subtract(2, "month").format("YYYY/MM");

const [ufData, usdData, eurData, utmData, ipcData] = await Promise.all([
getUF(sevenDaysAgo),
getUSD(sevenDaysAgo),
getEUR(sevenDaysAgo),
getUTM(lastMonth),
getIPC(twoMonthBefore),
]);

const mergedData = {
UF: ufData,
USD: usdData,
EUR: eurData,
UTM: utmData,
IPC: ipcData,
};
const twoMonthAgo = dayjs().subtract(2, "month").format("YYYY/MM");
const threeMonthAgo = dayjs().subtract(3, "month").format("YYYY/MM");

const [ufData, usdData, eurData, utmData, ipcData] =
await Promise.allSettled([
getUF(sevenDaysAgo),
getUSD(sevenDaysAgo),
getEUR(sevenDaysAgo),
getUTM(twoMonthAgo),
getIPC(threeMonthAgo),
]);

const mergedData = {};

if (ufData.status === "fulfilled") {
mergedData.UF = ufData.value;
}

if (usdData.status === "fulfilled") {
mergedData.USD = usdData.value;
}

if (eurData.status === "fulfilled") {
mergedData.EUR = eurData.value;
}

if (utmData.status === "fulfilled") {
mergedData.UTM = utmData.value;
}

if (ipcData.status === "fulfilled") {
mergedData.IPC = ipcData.value;
}

return mergedData;
};
Expand Down

1 comment on commit 7803ecf

@vercel
Copy link

@vercel vercel bot commented on 7803ecf Oct 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.