From dff1ec99a63c37c1eabe459382f9d8c3b45d2f57 Mon Sep 17 00:00:00 2001 From: Stefanos Hadjipetrou Date: Tue, 21 Mar 2023 13:11:49 +0200 Subject: [PATCH] feat: investments table views search enhancement --- src/config/filtering/disbursements.json | 15 ++ src/controllers/disbursements.controller.ts | 2 +- .../disbursements/getFilterString.ts | 130 ++++++++++++++++++ 3 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 src/config/filtering/disbursements.json create mode 100644 src/utils/filtering/disbursements/getFilterString.ts diff --git a/src/config/filtering/disbursements.json b/src/config/filtering/disbursements.json new file mode 100644 index 0000000..f86a853 --- /dev/null +++ b/src/config/filtering/disbursements.json @@ -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 AND signatureDate lt ", + "committedBarPeriod": "commitmentYear", + "search": "contains(geographicAreaCode_ISO3,) OR contains(multiCountryName,) OR contains(componentName,)" +} diff --git a/src/controllers/disbursements.controller.ts b/src/controllers/disbursements.controller.ts index 4ff9e0f..9021127 100644 --- a/src/controllers/disbursements.controller.ts +++ b/src/controllers/disbursements.controller.ts @@ -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 = { diff --git a/src/utils/filtering/disbursements/getFilterString.ts b/src/utils/filtering/disbursements/getFilterString.ts new file mode 100644 index 0000000..65e9649 --- /dev/null +++ b/src/utils/filtering/disbursements/getFilterString.ts @@ -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('', `${signedBarPeriod}-01-01`) + .replace('', `${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(//g, `'${search}'`)}`; + } + + if (str.length > 0) { + str = `${filtering.filter_operator}${filtering.param_assign_operator}${str}&`; + if (aggregationString) { + str = aggregationString.replace( + '', + `${str + .replace( + `${filtering.filter_operator}${filtering.param_assign_operator}`, + 'filter(', + ) + .replace('&', ')/')}`, + ); + } + } else if (aggregationString) { + str = aggregationString.replace('', ''); + } + + return str; +}