From 441789513b9a7f1f0491df0f0d4a1d6bc5c0ed66 Mon Sep 17 00:00:00 2001 From: Stefanos Hadjipetrou Date: Fri, 4 Oct 2024 12:19:55 +0300 Subject: [PATCH] feat: datasets latest update date --- src/config/mapping/latestupdate/index.json | 26 +++++++++++----------- src/config/urls/index.json | 3 ++- src/controllers/latestupdate.controller.ts | 26 +++++++++------------- 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/config/mapping/latestupdate/index.json b/src/config/mapping/latestupdate/index.json index 8febbb2..8ce4db3 100644 --- a/src/config/mapping/latestupdate/index.json +++ b/src/config/mapping/latestupdate/index.json @@ -1,47 +1,47 @@ { - "dataPath": "value[0]", - "updated": "dateTimeUpdated", - "created": "dateTimeCreated", + "dataPath": "value", + "updated": "lastUpdated", + "dataset": "dataset", "datasets": [ { "name": "allocations", - "url": "https://fetch.theglobalfund.org/v4/odata/allFinancialIndicators?$filter=indicatorName eq 'Communicated Allocation - Reference Rate' AND financialDataSet eq 'CommunicatedAllocation_ReferenceRate'&$top=1&$orderby=dateTimeUpdated desc,dateTimeCreated desc&$select=dateTimeUpdated,dateTimeCreated" + "datasetLabel": "CommunicatedAllocation_ReferenceRate" }, { "name": "budgets", - "url": "https://fetch.theglobalfund.org/v4/odata/allFinancialIndicators?$filter=contains(indicatorName, 'reference') AND financialDataSet eq 'GrantBudget_ReferenceRate'&$top=1&$orderby=dateTimeUpdated desc,dateTimeCreated desc&$select=dateTimeUpdated,dateTimeCreated" + "datasetLabel": "GrantBudget_ReferenceRate" }, { "name": "disbursements", - "url": "https://fetch.theglobalfund.org/v4/odata/allFinancialIndicators?$filter=contains(indicatorName, 'reference') AND financialDataSet eq 'Disbursement_ReferenceRate'&$top=1&$orderby=dateTimeUpdated desc,dateTimeCreated desc&$select=dateTimeUpdated,dateTimeCreated" + "datasetLabel": "Disbursement_ReferenceRate" }, { "name": "eligibility", - "url": "https://fetch.theglobalfund.org/v4/odata/Eligibility?$top=1&$orderby=dateTimeUpdated desc,dateTimeCreated desc&$select=dateTimeUpdated,dateTimeCreated" + "datasetLabel": "Eligibility" }, { "name": "expenditures", - "url": "https://fetch.theglobalfund.org/v4/odata/allFinancialIndicators?$filter=financialDataSet eq 'Expenditure_Intervention_ReferenceRate' AND isLatestReported eq true&$top=1&$orderby=dateTimeUpdated desc,dateTimeCreated desc&$select=dateTimeUpdated,dateTimeCreated" + "datasetLabel": "Expenditure_InvestmentLandscape_ReferenceRate" }, { "name": "funding_requests", - "url": "https://fetch.theglobalfund.org/v4/odata/FundingRequests?$top=1&$orderby=dateTimeUpdated desc,dateTimeCreated desc&$select=dateTimeUpdated,dateTimeCreated" + "datasetLabel": "Funding_Requests" }, { "name": "geographies", - "url": "https://fetch.theglobalfund.org/v4/odata/Geographies?$top=1&$orderby=dateTimeUpdated desc,dateTimeCreated desc&$select=dateTimeUpdated,dateTimeCreated" + "datasetLabel": "Geography" }, { "name": "grants", - "url": "https://fetch.theglobalfund.org/v4/odata/Grants?$top=1&$orderby=dateTimeUpdated desc,dateTimeCreated desc&$select=dateTimeUpdated,dateTimeCreated" + "datasetLabel": "Grants" }, { "name": "pledges-contributions", - "url": "https://fetch.theglobalfund.org/v4/odata/allFinancialIndicators?$filter=financialDataSet eq 'Pledges_Contributions' AND indicatorName eq 'Pledge - Reference Rate'&$top=1&$orderby=dateTimeUpdated desc,dateTimeCreated desc&$select=dateTimeUpdated,dateTimeCreated" + "datasetLabel": "Pledges_Contributions" }, { "name": "results", - "url": "https://fetch.theglobalfund.org/v4/odata/allProgrammaticIndicators?$filter=programmaticDataset eq 'Annual_Results'&$top=1&$orderby=dateTimeUpdated desc,dateTimeCreated desc&$select=dateTimeUpdated,dateTimeCreated" + "datasetLabel": "Annual_Results" } ] } diff --git a/src/config/urls/index.json b/src/config/urls/index.json index 57aa426..291647b 100644 --- a/src/config/urls/index.json +++ b/src/config/urls/index.json @@ -18,5 +18,6 @@ "GEOGRAPHIES": "https://fetch.theglobalfund.org/v4/odata/Geographies", "FUNDING_REQUESTS": "https://fetch.theglobalfund.org/v4/odata/FundingRequests", "DOCUMENTS": "https://fetch.theglobalfund.org/v4/odata/Documents", - "ELIGIBILITY": "https://fetch.theglobalfund.org/v4/odata/Eligibility" + "ELIGIBILITY": "https://fetch.theglobalfund.org/v4/odata/Eligibility", + "DATASET_INFORMATION": "https://fetch.theglobalfund.org/v4.1/odata/DatasetInformation?a=b" } diff --git a/src/controllers/latestupdate.controller.ts b/src/controllers/latestupdate.controller.ts index 2c7d05d..645c190 100644 --- a/src/controllers/latestupdate.controller.ts +++ b/src/controllers/latestupdate.controller.ts @@ -4,6 +4,7 @@ import axios from 'axios'; import _ from 'lodash'; import moment from 'moment'; import LatestUpdateMapping from '../config/mapping/latestupdate/index.json'; +import urls from '../config/urls/index.json'; export class LatestUpdateController { constructor(@inject(RestBindings.Http.REQUEST) private req: Request) {} @@ -11,24 +12,17 @@ export class LatestUpdateController { @get('/latest-update') @response(200) async latestUpdate() { - const responses = await Promise.all( - LatestUpdateMapping.datasets.map(dataset => axios.get(dataset.url)), - ); + const response = await axios.get(urls.DATASET_INFORMATION); + const rawData = _.get(response.data, LatestUpdateMapping.dataPath, []); - const data = responses.map((response, index) => { - const updated = _.get( - response, - `data.${LatestUpdateMapping.dataPath}[${LatestUpdateMapping.updated}]`, - '', - ); - const created = _.get( - response, - `data.${LatestUpdateMapping.dataPath}[${LatestUpdateMapping.created}]`, - '', - ); + const data = LatestUpdateMapping.datasets.map(dataset => { + const fDataset = _.find(rawData, { + [LatestUpdateMapping.dataset]: dataset.datasetLabel, + }); + const updated = _.get(fDataset, `[${LatestUpdateMapping.updated}]`, ''); return { - name: _.get(LatestUpdateMapping.datasets, `[${index}].name`, ''), - date: moment(updated ?? created).format('DD MMMM YYYY'), + name: dataset.name, + date: moment(updated).format('DD MMMM YYYY'), }; });