Skip to content

Commit

Permalink
feat: investments table views search enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanoshadjipetrou committed Mar 21, 2023
1 parent 5e70cf1 commit dff1ec9
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/config/filtering/disbursements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"country": "geographicAreaCode_ISO3",
"multicountry": "multiCountryName",
"component": "componentName",
"status": "grantAgreementStatusTypeName",
"partner": "principalRecipientId",
"partner_sub_type": "principalRecipientSubClassificationId",
"partner_type": "principalRecipientClassificationId",
"grantId": "grantAgreementNumber",
"IPnumber": "implementationPeriodNumber",
"barPeriod": "disbursementYear",
"signedBarPeriod": "signatureDate gt <date1> AND signatureDate lt <date2>",
"committedBarPeriod": "commitmentYear",
"search": "contains(geographicAreaCode_ISO3,<value>) OR contains(multiCountryName,<value>) OR contains(componentName,<value>)"
}
2 changes: 1 addition & 1 deletion src/controllers/disbursements.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import {BudgetsTreemapDataItem} from '../interfaces/budgetsTreemap';
import {DisbursementsTreemapDataItem} from '../interfaces/disbursementsTreemap';
import staticCountries from '../static-assets/countries.json';
import {handleDataApiError} from '../utils/dataApiError';
import {getFilterString} from '../utils/filtering/disbursements/getFilterString';
import {
grantDetailGetFilterString,
grantDetailTreemapGetFilterString,
} from '../utils/filtering/disbursements/grantDetailGetFilterString';
import {getGeoMultiCountriesFilterString} from '../utils/filtering/disbursements/multicountries/getFilterString';
import {getFilterString} from '../utils/filtering/grants/getFilterString';
import {formatFinancialValue} from '../utils/formatFinancialValue';

const DISBURSEMENTS_TIME_CYCLE_RESPONSE: ResponseObject = {
Expand Down
130 changes: 130 additions & 0 deletions src/utils/filtering/disbursements/getFilterString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import _ from 'lodash';
import filteringDisbursements from '../../../config/filtering/disbursements.json';
import filtering from '../../../config/filtering/index.json';

export function getFilterString(params: any, aggregationString?: string) {
let str = '';

const locations = _.filter(
_.get(params, 'locations', '').split(','),
(loc: string) => loc.length > 0,
).map((loc: string) => `'${loc}'`);
if (locations.length > 0) {
str += `(${filteringDisbursements.country}${filtering.in}(${locations.join(
filtering.multi_param_separator,
)}) OR ${filteringDisbursements.multicountry}${
filtering.in
}(${locations.join(filtering.multi_param_separator)}))`;
}

const components = _.filter(
_.get(params, 'components', '').split(','),
(comp: string) => comp.length > 0,
).map((comp: string) => `'${comp}'`);
if (components.length > 0) {
str += `${str.length > 0 ? ' AND ' : ''}${
filteringDisbursements.component
}${filtering.in}(${components.join(filtering.multi_param_separator)})`;
}

const statuses = _.filter(
_.get(params, 'status', '').split(','),
(stat: string) => stat.length > 0,
).map((stat: string) => `'${stat}'`);
if (statuses.length > 0) {
str += `${str.length > 0 ? ' AND ' : ''}${filteringDisbursements.status}${
filtering.in
}(${statuses.join(filtering.multi_param_separator)})`;
}

const partners = _.filter(
_.get(params, 'partners', '').split(','),
(partner: string) => partner.length > 0,
).map((partner: string) => `'${partner}'`);
if (partners.length > 0) {
str += `${str.length > 0 ? ' AND ' : ''}${filteringDisbursements.partner}${
filtering.in
}(${partners.join(filtering.multi_param_separator)})`;
}

const partnerSubTypes = _.filter(
_.get(params, 'partnerSubTypes', '').split(','),
(type: string) => type.length > 0,
).map((type: string) => `'${type}'`);
if (partnerSubTypes.length > 0) {
str += `${str.length > 0 ? ' AND ' : ''}${
filteringDisbursements.partner_sub_type
}${filtering.in}(${partnerSubTypes.join(filtering.multi_param_separator)})`;
}

const partnerTypes = _.filter(
_.get(params, 'partnerTypes', '').split(','),
(type: string) => type.length > 0,
).map((type: string) => `'${type}'`);
if (partnerTypes.length > 0) {
str += `${str.length > 0 ? ' AND ' : ''}${
filteringDisbursements.partner_type
}${filtering.in}(${partnerTypes.join(filtering.multi_param_separator)})`;
}

const grantId = _.get(params, 'grantId', null);
if (grantId) {
str += `${str.length > 0 ? ' AND ' : ''}${filteringDisbursements.grantId}${
filtering.eq
}${grantId}`;
}

const IPnumber = _.get(params, 'IPnumber', null);
if (IPnumber) {
str += `${str.length > 0 ? ' AND ' : ''}${filteringDisbursements.IPnumber}${
filtering.eq
}${IPnumber}`;
}

const barPeriod = _.get(params, 'barPeriod', null);
if (barPeriod) {
str += `${str.length > 0 ? ' AND ' : ''}${
filteringDisbursements.barPeriod
}${filtering.eq}${barPeriod}`;
}
const signedBarPeriod = _.get(params, 'signedBarPeriod', null);
if (signedBarPeriod) {
str += `${
str.length > 0 ? ' AND ' : ''
}${filteringDisbursements.signedBarPeriod
.replace('<date1>', `${signedBarPeriod}-01-01`)
.replace('<date2>', `${parseInt(signedBarPeriod, 10) + 1}-01-01`)}`;
}
const committedBarPeriod = _.get(params, 'committedBarPeriod', null);
if (committedBarPeriod) {
str += `${str.length > 0 ? ' AND ' : ''}${
filteringDisbursements.committedBarPeriod
}${filtering.eq}${committedBarPeriod}`;
}

const search = _.get(params, 'q', '');
if (search.length > 0) {
str += `${
str.length > 0 ? ' AND ' : ''
}${filteringDisbursements.search.replace(/<value>/g, `'${search}'`)}`;
}

if (str.length > 0) {
str = `${filtering.filter_operator}${filtering.param_assign_operator}${str}&`;
if (aggregationString) {
str = aggregationString.replace(
'<filterString>',
`${str
.replace(
`${filtering.filter_operator}${filtering.param_assign_operator}`,
'filter(',
)
.replace('&', ')/')}`,
);
}
} else if (aggregationString) {
str = aggregationString.replace('<filterString>', '');
}

return str;
}

0 comments on commit dff1ec9

Please sign in to comment.