From ada022d73f3dd5f51eff223d5817d041e9db8880 Mon Sep 17 00:00:00 2001 From: bobmw Date: Sun, 28 Apr 2024 16:36:06 -0300 Subject: [PATCH] feat: create institutions fetch --- src/app/api/fetchInstutions.js | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/app/api/fetchInstutions.js diff --git a/src/app/api/fetchInstutions.js b/src/app/api/fetchInstutions.js new file mode 100644 index 0000000..3f356d1 --- /dev/null +++ b/src/app/api/fetchInstutions.js @@ -0,0 +1,43 @@ +import { create } from "apisauce"; + +const fetchInstitutions = create({ + baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/authtest/organizations/all", +}); + +function transformData(institutions) { + const transformedData = {}; + + institutions.forEach((institution) => { + let { uf, type, name } = institution; + + if (!uf) { + uf = "DF"; + } + + if (!transformedData[uf]) { + transformedData[uf] = {}; + } + + if (!transformedData[uf][type]) { + transformedData[uf][type] = []; + } + + transformedData[uf][type].push({ value: name, label: name }); + }); + + return transformedData; +} + +export function getInstitutions() { + return fetchInstitutions.get() + .then((response) => { + if (response.ok) { + return transformData(response.data); + } else { + throw new Error("Falha ao tentar buscar instituicoes no banco de dados"); + } + }) + .catch((error) => { + throw error; + }); +}