Skip to content

Commit

Permalink
now it will look for the closest data to the day that is consulted
Browse files Browse the repository at this point in the history
  • Loading branch information
camipozas committed Sep 25, 2022
1 parent 3599b63 commit 1644a81
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 30 deletions.
39 changes: 26 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,41 @@ const getUTM = require("./indicators/getUTM");
const getIPC = require("./indicators/getIPC");

/**
* It fetches data from the Central Bank of Chile's API, merges it into a single object, and returns it
* It fetches data from the Central Bank of Chile's API, and returns a single object with all the data
* @returns An object with the following structure:
* {
* UF: {
* date: "2020-04-01",
* value: 28.8
* },
* USD: {
* date: "2020-04-01",
* value: 755.5
* },
* EUR: {
* date: "2020-04-01",
*/
const main = async () => {
const today = dayjs().format("YYYY/MM/[dias]/DD");
const thisMonth = dayjs().format("YYYY/MM");
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(today),
getUSD(today),
getEUR(today),
getUTM(thisMonth),
getIPC(lastMonth),
getUF(sevenDaysAgo),
getUSD(sevenDaysAgo),
getEUR(sevenDaysAgo),
getUTM(lastMonth),
getIPC(twoMonthBefore),
]);

const mergedData = {
...ufData,
...usdData,
...eurData,
...utmData,
...ipcData,
UF: ufData,
USD: usdData,
EUR: eurData,
UTM: utmData,
IPC: ipcData,
};

return mergedData;
};

Expand Down
12 changes: 8 additions & 4 deletions src/indicators/getEUR.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
require("dotenv").config();
const axios = require("axios");
const dayjs = require("dayjs");

const memoryCache = require("../cache");

/**
* It makes a request to the CMF API and returns the data
* It gets the latest EUR exchange rate from the Chilean Central Bank API
* @param date - The date you want to get the EUR for.
* @returns The data is being returned.
* @returns The latest EUR value
*/
const getEURWithoutCache = async (date) => {
const { data } = await axios.get(
`https://api.cmfchile.cl/api-sbifv3/recursos_api/euro/${date}?apikey=${process.env.CMF_API_KEY}&formato=json`
`https://api.cmfchile.cl/api-sbifv3/recursos_api/euro/posteriores/${date}?apikey=${process.env.CMF_API_KEY}&formato=json`
);
return data;
const [latest] = data.Euros.sort(
(fecha) => -1 * dayjs().diff(dayjs(fecha["Fecha"]))
);
return latest;
};

/**
Expand Down
8 changes: 6 additions & 2 deletions src/indicators/getIPC.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require("dotenv").config();
const axios = require("axios");
const dayjs = require("dayjs");

const memoryCache = require("../cache");

Expand All @@ -10,9 +11,12 @@ const memoryCache = require("../cache");
*/
const getIPCWithoutCache = async (date) => {
const { data } = await axios.get(
`https://api.cmfchile.cl/api-sbifv3/recursos_api/ipc/${date}?apikey=${process.env.CMF_API_KEY}&formato=json`
`https://api.cmfchile.cl/api-sbifv3/recursos_api/ipc/posteriores/${date}?apikey=${process.env.CMF_API_KEY}&formato=json`
);
return data;
const [latest] = data.IPCs.sort(
(fecha) => -1 * dayjs().diff(dayjs(fecha["Fecha"]))
);
return latest;
};

/**
Expand Down
14 changes: 9 additions & 5 deletions src/indicators/getUF.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
require("dotenv").config();
const axios = require("axios");
const dayjs = require("dayjs");

const memoryCache = require("../cache");

/**
* It gets the UF value for a given date from the CMF API
* @param date - The date you want to get the UF value for.
* @returns The data is being returned.
* It gets the latest UF value from the CMF API
* @param date - The date you want to get the UF for.
* @returns The latest UF value
*/
const getUFWithoutCache = async (date) => {
const { data } = await axios.get(
`https://api.cmfchile.cl/api-sbifv3/recursos_api/uf/${date}?apikey=${process.env.CMF_API_KEY}&formato=json`
`https://api.cmfchile.cl/api-sbifv3/recursos_api/uf/posteriores/${date}?apikey=${process.env.CMF_API_KEY}&formato=json`
);
return data;
const [latest] = data.UFs.sort(
(fecha) => -1 * dayjs().diff(dayjs(fecha["Fecha"]))
);
return latest;
};

/**
Expand Down
8 changes: 6 additions & 2 deletions src/indicators/getUSD.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require("dotenv").config();
const axios = require("axios");
const dayjs = require("dayjs");

const memoryCache = require("../cache");

Expand All @@ -10,9 +11,12 @@ const memoryCache = require("../cache");
*/
const getUSDWithoutCache = async (date) => {
const { data } = await axios.get(
`https://api.cmfchile.cl/api-sbifv3/recursos_api/dolar/${date}?apikey=${process.env.CMF_API_KEY}&formato=json`
`https://api.cmfchile.cl/api-sbifv3/recursos_api/dolar/posteriores/${date}?apikey=${process.env.CMF_API_KEY}&formato=json`
);
return data;
const [latest] = data.Dolares.sort(
(fecha) => -1 * dayjs().diff(dayjs(fecha["Fecha"]))
);
return latest;
};

/**
Expand Down
12 changes: 8 additions & 4 deletions src/indicators/getUTM.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
require("dotenv").config();
const axios = require("axios");
const dayjs = require("dayjs");

const memoryCache = require("../cache");

/**
* It gets the UTM value for a given date from the Central Bank of Chile's API
* It gets the latest UTM value from the Chilean Central Bank's API
* @param date - The date you want to get the UTM for.
* @returns The UTM value for the given date.
* @returns The latest UTM value
*/
const getUTMWithoutCache = async (date) => {
const { data } = await axios.get(
`https://api.cmfchile.cl/api-sbifv3/recursos_api/utm/${date}?apikey=${process.env.CMF_API_KEY}&formato=json`
`https://api.cmfchile.cl/api-sbifv3/recursos_api/utm/posteriores/${date}?apikey=${process.env.CMF_API_KEY}&formato=json`
);
return data;
const [latest] = data.UTMs.sort(
(fecha) => -1 * dayjs().diff(dayjs(fecha["Fecha"]))
);
return latest;
};

/**
Expand Down

1 comment on commit 1644a81

@vercel
Copy link

@vercel vercel bot commented on 1644a81 Sep 25, 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.