diff --git a/package.json b/package.json index 0686b3a..168252c 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,8 @@ "@turf/center": "^7.2.0", "@turf/helpers": "^7.2.0", "@types/lodash": "^4.17.14", - "axios": "^1.15.2", + "axios": "^1.16.0", + "dotenv": "^17.3.1", "lodash": "^4.18.1", "map-transform": "^0.3.12", "moment": "^2.30.1", diff --git a/pm2.config.js b/pm2.config.js index 892f9b4..67f9295 100644 --- a/pm2.config.js +++ b/pm2.config.js @@ -19,6 +19,7 @@ module.exports = { watch: true, ignore_watch: [ '.git', + 'logs', 'public', '.circle', '.vscode', diff --git a/src/application.ts b/src/application.ts index ea61755..bffeb69 100644 --- a/src/application.ts +++ b/src/application.ts @@ -7,8 +7,11 @@ import { RestExplorerComponent, } from '@loopback/rest-explorer'; import {ServiceMixin} from '@loopback/service-proxy'; +import dotenv from 'dotenv'; import path from 'path'; +dotenv.config(); + export {ApplicationConfig}; export class ApiApplication extends BootMixin( diff --git a/src/controllers/budgets.controller.ts b/src/controllers/budgets.controller.ts index 04f69b7..808afbd 100644 --- a/src/controllers/budgets.controller.ts +++ b/src/controllers/budgets.controller.ts @@ -11,7 +11,6 @@ import BudgetsTableFieldsMapping from '../config/mapping/budgets/table.json'; import BudgetsTreemapFieldsMapping from '../config/mapping/budgets/treemap.json'; import urls from '../config/urls/index.json'; import {BudgetSankeyChartData} from '../interfaces/budgetSankey'; -import CycleMapping from '../static-assets/cycle-mapping.json'; import {handleDataApiError} from '../utils/dataApiError'; import {filterFinancialIndicators} from '../utils/filtering/financialIndicators'; @@ -453,9 +452,20 @@ export class BudgetsController { return axios .get(url) - .then((resp: AxiosResponse) => { + .then(async (resp: AxiosResponse) => { const rawData = _.get(resp.data, BudgetsCyclesMapping.dataPath, []); + const cmsResponse = await axios.get( + `${process.env.CMS_API}/pages-home?locale=en`, + { + headers: { + Authorization: `Bearer ${process.env.CMS_TOKEN}`, + }, + }, + ); + + const chartCycles = _.get(cmsResponse, 'data.data.chartCycles', []); + const data = _.orderBy( _.map( _.filter( @@ -473,7 +483,7 @@ export class BudgetsController { value = `${from} - ${to}`; } - const name = _.find(CycleMapping, {value})?.name ?? value; + const name = _.find(chartCycles, {value})?.name ?? value; return { name, @@ -1271,9 +1281,20 @@ export class BudgetsController { return axios .get(url) - .then((resp: AxiosResponse) => { + .then(async (resp: AxiosResponse) => { const rawData = _.get(resp.data, BudgetsCyclesMapping.dataPath, []); + const cmsResponse = await axios.get( + `${process.env.CMS_API}/pages-home?locale=en`, + { + headers: { + Authorization: `Bearer ${process.env.CMS_TOKEN}`, + }, + }, + ); + + const chartCycles = _.get(cmsResponse, 'data.data.chartCycles', []); + const data = _.orderBy( _.map( _.filter( @@ -1291,7 +1312,7 @@ export class BudgetsController { value = `${from} - ${to}`; } - const name = _.find(CycleMapping, {value})?.name ?? value; + const name = _.find(chartCycles, {value})?.name ?? value; return { name, diff --git a/src/controllers/disbursements.controller.ts b/src/controllers/disbursements.controller.ts index f238e69..d9af20c 100644 --- a/src/controllers/disbursements.controller.ts +++ b/src/controllers/disbursements.controller.ts @@ -10,7 +10,6 @@ import TableFieldsMapping from '../config/mapping/disbursements/table.json'; import FinancialInsightsStatsMapping from '../config/mapping/financialInsightsStats.json'; import urls from '../config/urls/index.json'; import {BudgetSankeyChartData} from '../interfaces/budgetSankey'; -import CycleMapping from '../static-assets/cycle-mapping.json'; import {handleDataApiError} from '../utils/dataApiError'; import {filterFinancialIndicators} from '../utils/filtering/financialIndicators'; @@ -730,13 +729,24 @@ export class DisbursementsController { return axios .get(url) - .then((resp: AxiosResponse) => { + .then(async (resp: AxiosResponse) => { const rawData = _.get( resp.data, DisbursementsCyclesMapping.dataPath, [], ); + const cmsResponse = await axios.get( + `${process.env.CMS_API}/pages-home?locale=en`, + { + headers: { + Authorization: `Bearer ${process.env.CMS_TOKEN}`, + }, + }, + ); + + const chartCycles = _.get(cmsResponse, 'data.data.chartCycles', []); + const data = _.map( _.filter( rawData, @@ -753,7 +763,7 @@ export class DisbursementsController { value = `${from} - ${to}`; } - const name = _.find(CycleMapping, {value})?.name ?? value; + const name = _.find(chartCycles, {value})?.name ?? value; return { name, diff --git a/src/controllers/expenditures.controller.ts b/src/controllers/expenditures.controller.ts index fb8232f..a019b01 100644 --- a/src/controllers/expenditures.controller.ts +++ b/src/controllers/expenditures.controller.ts @@ -8,7 +8,6 @@ import ExpendituresCyclesMapping from '../config/mapping/expenditures/cycles.jso import ExpendituresHeatmapMapping from '../config/mapping/expenditures/heatmap.json'; import ExpendituresTableMapping from '../config/mapping/expenditures/table.json'; import urls from '../config/urls/index.json'; -import CycleMapping from '../static-assets/cycle-mapping.json'; import {handleDataApiError} from '../utils/dataApiError'; import {filterFinancialIndicators} from '../utils/filtering/financialIndicators'; @@ -433,13 +432,24 @@ export class ExpendituresController { return axios .get(url) - .then((resp: AxiosResponse) => { + .then(async (resp: AxiosResponse) => { const rawData = _.get( resp.data, ExpendituresCyclesMapping.dataPath, [], ); + const cmsResponse = await axios.get( + `${process.env.CMS_API}/pages-home?locale=en`, + { + headers: { + Authorization: `Bearer ${process.env.CMS_TOKEN}`, + }, + }, + ); + + const chartCycles = _.get(cmsResponse, 'data.data.chartCycles', []); + const data = _.map(rawData, item => { const from = _.get(item, ExpendituresCyclesMapping.cycleFrom, ''); const to = _.get(item, ExpendituresCyclesMapping.cycleTo, ''); @@ -450,7 +460,7 @@ export class ExpendituresController { value = `${from} - ${to}`; } - const name = _.find(CycleMapping, {value})?.name ?? value; + const name = _.find(chartCycles, {value})?.name ?? value; return { name, diff --git a/src/controllers/grants.controller.ts b/src/controllers/grants.controller.ts index 3360c99..d74a5eb 100644 --- a/src/controllers/grants.controller.ts +++ b/src/controllers/grants.controller.ts @@ -510,7 +510,7 @@ export class GrantsController { node => node.name === cat3 && node.level === 3, ); if (existingCategoryIndex === -1) { - const name1 = `${cat3}${sameLevel1Or2Category ? ' (1)' : ''}`; + const name1 = `${cat3}${sameLevel1Or2Category ? ' (2)' : ''}`; data.nodes.push({ name: name1, level: 3, diff --git a/src/utils/filtering/financialIndicators.ts b/src/utils/filtering/financialIndicators.ts index 8d204b6..b8dd810 100644 --- a/src/utils/filtering/financialIndicators.ts +++ b/src/utils/filtering/financialIndicators.ts @@ -1,6 +1,6 @@ +import axios from 'axios'; import _ from 'lodash'; import filtering from '../../config/filtering/index.json'; -import CycleMapping from '../../static-assets/cycle-mapping.json'; import {GeographyFiltering} from './geographies'; const MAPPING = { @@ -53,10 +53,20 @@ export async function filterFinancialIndicators( let str = ''; if (_.get(params, 'cycleNames', '')) { + const cmsResponse = await axios.get( + `${process.env.CMS_API}/pages-home?locale=en`, + { + headers: { + Authorization: `Bearer ${process.env.CMS_TOKEN}`, + }, + }, + ); + const chartCycles = _.get(cmsResponse, 'data.data.chartCycles', []); + const cycles = _.get(params, 'cycleNames', '').split(','); const cycleValues = _.filter( cycles.map( - (cycle: string) => _.find(CycleMapping, {name: cycle})?.value ?? '', + (cycle: string) => _.find(chartCycles, {name: cycle})?.value ?? '', ), (c: string) => c.length > 0, ); diff --git a/yarn.lock b/yarn.lock index 893a99e..19def17 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1040,6 +1040,13 @@ acorn@^8.14.0, acorn@^8.9.0: resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.14.0.tgz#063e2c70cac5fb4f6467f0b11152e04c682795b0" integrity sha512-cl669nCJTZBsL97OF4kUQm5g5hC2uihk0NxY3WENAC0TYdILVkAyHymAntgxGkl7K+t0cXIrH5siy5S4XkFycA== +agent-base@6: + version "6.0.2" + resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77" + integrity sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ== + dependencies: + debug "4" + aggregate-error@^3.0.0: version "3.1.0" resolved "https://registry.yarnpkg.com/aggregate-error/-/aggregate-error-3.1.0.tgz#92670ff50f5359bdb7a3e0d40d0ec30c5737687a" @@ -1176,13 +1183,14 @@ asynckit@^0.4.0: resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q== -axios@^1.15.2: - version "1.15.2" - resolved "https://registry.yarnpkg.com/axios/-/axios-1.15.2.tgz#eb8fb6d30349abace6ade5b4cb4d9e8a0dc23e5b" - integrity sha512-wLrXxPtcrPTsNlJmKjkPnNPK2Ihe0hn0wGSaTEiHRPxwjvJwT3hKmXF4dpqxmPO9SoNb2FsYXj/xEo0gHN+D5A== +axios@^1.16.0: + version "1.18.0" + resolved "https://registry.yarnpkg.com/axios/-/axios-1.18.0.tgz#8a7f8854af280fcaae063272df2ed9f3837d2398" + integrity sha512-E32NzpYKp++W7XRe52rHiXV2ehxmh3wbdgO7MHeFM+vqxLBYHzt0ElkiImtOBxtOmyp0yoC8C6uESVV84Y2/hw== dependencies: - follow-redirects "^1.15.11" + follow-redirects "^1.16.0" form-data "^4.0.5" + https-proxy-agent "^5.0.1" proxy-from-env "^2.1.0" balanced-match@^1.0.0: @@ -1556,6 +1564,13 @@ debug@2.6.9: dependencies: ms "2.0.0" +debug@4: + version "4.4.3" + resolved "https://registry.yarnpkg.com/debug/-/debug-4.4.3.tgz#c6ae432d9bd9662582fce08709b038c58e9e3d6a" + integrity sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA== + dependencies: + ms "^2.1.3" + debug@^4, debug@^4.1.0, debug@^4.1.1, debug@^4.2.0, debug@^4.3.1, debug@^4.3.2, debug@^4.3.4, debug@^4.3.5, debug@^4.4.0: version "4.4.0" resolved "https://registry.yarnpkg.com/debug/-/debug-4.4.0.tgz#2b3f2aea2ffeb776477460267377dc8710faba8a" @@ -1640,6 +1655,11 @@ dot-case@^3.0.4: no-case "^3.0.4" tslib "^2.0.3" +dotenv@^17.3.1: + version "17.3.1" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-17.3.1.tgz#2706f5b0165e45a1503348187b8468f87fe6aae2" + integrity sha512-IO8C/dzEb6O3F9/twg6ZLXz164a2fhTnEWb95H23Dm4OuN+92NmEAlTrupP9VW6Jm3sO26tQlqyvyi4CsnY9GA== + dunder-proto@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/dunder-proto/-/dunder-proto-1.0.1.tgz#d7ae667e1dc83482f8b70fd0f6eefc50da30f58a" @@ -2129,10 +2149,10 @@ flatted@^3.2.9: resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.3.2.tgz#adba1448a9841bec72b42c532ea23dbbedef1a27" integrity sha512-AiwGJM8YcNOaobumgtng+6NHuOqC3A7MixFeDafM3X9cIUM+xUXoS5Vfgf+OihAYe20fxqNM9yPBXJzRtZ/4eA== -follow-redirects@^1.15.11: - version "1.15.11" - resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.11.tgz#777d73d72a92f8ec4d2e410eb47352a56b8e8340" - integrity sha512-deG2P0JfjrTxl50XGCDyfI97ZGVCxIpfKYmfyrQ54n5FO/0gfIES8C/Psl6kWVDolizcaaxZJnTS0QSMxvnsBQ== +follow-redirects@^1.16.0: + version "1.16.0" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.16.0.tgz#28474a159d3b9d11ef62050a14ed60e4df6d61bc" + integrity sha512-y5rN/uOsadFT/JfYwhxRS5R7Qce+g3zG97+JrtFZlC9klX/W5hD7iiLzScI4nZqUS7DNUdhPgw4xI8W2LuXlUw== foreground-child@^2.0.0: version "2.0.0" @@ -2459,6 +2479,14 @@ http2-client@^1.2.5: resolved "https://registry.yarnpkg.com/http2-client/-/http2-client-1.3.5.tgz#20c9dc909e3cc98284dd20af2432c524086df181" integrity sha512-EC2utToWl4RKfs5zd36Mxq7nzHHBuomZboI0yYL6Y0RmBgT7Sgkq4rQ0ezFTYoIsSs7Tm9SJe+o2FcAg6GBhGA== +https-proxy-agent@^5.0.1: + version "5.0.1" + resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-5.0.1.tgz#c59ef224a04fe8b754f3db0063a25ea30d0005d6" + integrity sha512-dFcAjpTQFgoLMzC2VwU+C/CbS7uRL0lWmxDITmqm7C+7F0Odmj6s9l6alZc6AELXhrnggM2CeWSXHGOdX2YtwA== + dependencies: + agent-base "6" + debug "4" + human-signals@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-1.1.1.tgz#c5b1cd14f50aeae09ab6c59fe63ba3395fe4dfa3"