Skip to content

Commit

Permalink
feat: cycle custom name mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanoshadjipetrou committed Aug 5, 2024
1 parent 403bf25 commit fab58f0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 15 deletions.
27 changes: 17 additions & 10 deletions src/controllers/budgets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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';

Expand Down Expand Up @@ -483,7 +484,7 @@ export class BudgetsController {
item =>
_.get(item, BudgetsCyclesMapping.cycleFrom, null) !== null,
),
(item, index) => {
item => {
const from = _.get(item, BudgetsCyclesMapping.cycleFrom, '');
const to = _.get(item, BudgetsCyclesMapping.cycleTo, '');

Expand All @@ -493,9 +494,11 @@ export class BudgetsController {
value = `${from} - ${to}`;
}

const name = _.find(CycleMapping, {value})?.name ?? value;

return {
name: `Cycle ${index + 1}`,
value,
name,
value: name,
};
},
),
Expand Down Expand Up @@ -530,13 +533,14 @@ export class BudgetsController {
'implementationPeriod/grant/geography_BoardConstituencyView/code',
];
}
const years = cycle.split('-');
// const years = cycle.split('-');

const filterString1 = filterFinancialIndicators(
{
...this.req.query,
years: years[0],
yearsTo: years[1],
cycleNames: cycle,
// years: years[0],
// yearsTo: years[1],
},
BudgetsBreakdownFieldsMapping.urlParams1.replace(
'<componentField>',
Expand All @@ -562,8 +566,9 @@ export class BudgetsController {
filterString2 = filterFinancialIndicators(
{
...this.req.query,
years: years[0],
yearsTo: years[1],
cycleNames: cycle,
// years: years[0],
// yearsTo: years[1],
},
BudgetsBreakdownFieldsMapping.urlParams2.replace(
'<componentField>',
Expand Down Expand Up @@ -1324,9 +1329,11 @@ export class BudgetsController {
value = `${from} - ${to}`;
}

const name = _.find(CycleMapping, {value})?.name ?? value;

return {
name: `Cycle ${index + 1}`,
value,
name,
value: name,
};
},
),
Expand Down
9 changes: 6 additions & 3 deletions src/controllers/disbursements.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import LineChartFieldsMapping from '../config/mapping/disbursements/lineChart.js
import TableFieldsMapping from '../config/mapping/disbursements/table.json';
import FinancialInsightsStatsMapping from '../config/mapping/financialInsightsStats.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';

Expand Down Expand Up @@ -566,7 +567,7 @@ export class DisbursementsController {
item =>
_.get(item, DisbursementsCyclesMapping.cycleFrom, null) !== null,
),
(item, index) => {
item => {
const from = _.get(item, DisbursementsCyclesMapping.cycleFrom, '');
const to = _.get(item, DisbursementsCyclesMapping.cycleTo, '');

Expand All @@ -576,9 +577,11 @@ export class DisbursementsController {
value = `${from} - ${to}`;
}

const name = _.find(CycleMapping, {value})?.name ?? value;

return {
name: `Cycle ${index + 1}`,
value,
name,
value: name,
};
},
);
Expand Down
7 changes: 5 additions & 2 deletions src/controllers/expenditures.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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';

Expand Down Expand Up @@ -472,9 +473,11 @@ export class ExpendituresController {
value = `${from} - ${to}`;
}

const name = _.find(CycleMapping, {value})?.name ?? value;

return {
name: `Cycle ${index + 1}`,
value,
name,
value: name,
};
});

Expand Down
22 changes: 22 additions & 0 deletions src/static-assets/cycle-mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"name": "Previous Cycles",
"value": "2002 - 2013"
},
{
"name": "Grant Cycle 4",
"value": "2014 - 2016"
},
{
"name": "Grant Cycle 5",
"value": "2017 - 2019"
},
{
"name": "Grant Cycle 6",
"value": "2020 - 2022"
},
{
"name": "Grant Cycle 7",
"value": "2023 - 2025"
}
]
19 changes: 19 additions & 0 deletions src/utils/filtering/financialIndicators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'lodash';
import filtering from '../../config/filtering/index.json';
import CycleMapping from '../../static-assets/cycle-mapping.json';
import {getGeographyValues} from './geographies';

const MAPPING = {
Expand Down Expand Up @@ -39,6 +40,24 @@ export function filterFinancialIndicators(
): string {
let str = '';

if (_.get(params, 'cycleNames', '')) {
const cycles = _.get(params, 'cycleNames', '').split(',');
const cycleValues = _.filter(
cycles.map(
(cycle: string) => _.find(CycleMapping, {name: cycle})?.value ?? '',
),
(c: string) => c.length > 0,
);
cycleValues.forEach((cycle: string) => {
const splits = cycle.split(' - ');
params = {
...params,
years: splits[0],
yearsTo: splits[1],
};
});
}

const geos = _.filter(
_.get(params, 'geographies', '').split(','),
(o: string) => o.length > 0,
Expand Down

0 comments on commit fab58f0

Please sign in to comment.