Skip to content

Commit

Permalink
feat: add document type filter
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanoshadjipetrou committed Mar 8, 2023
1 parent 3c0c5c4 commit cf23c73
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/config/filtering/documents.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"component": "componentName",
"grantId": "grantAgreementNumber",
"multicountry": "organizationId",
"documentType": "documentTypeDescription",
"search": "(contains(tolower(processName),tolower(<value>)) OR contains(tolower(componentName),tolower(<value>)) OR contains(tolower(processWindow),tolower(<value>)) OR contains(tolower(grantAgreementNumber),tolower(<value>)) OR contains(tolower(geographicAreaName),tolower(<value>)) OR contains(tolower(organizationName),tolower(<value>)))"
}
13 changes: 2 additions & 11 deletions src/controllers/documents.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,10 @@ export class DocumentsController {
this.req.query,
docsUtils.defaultFilter,
);
// const params = querystring.stringify(
// {
// ...getPage(filtering.page, parseInt(page, 10), parseInt(pageSize, 10)),
// [filtering.page_size]: pageSize,
// },
// '&',
// filtering.param_assign_operator,
// {
// encodeURIComponent: (str: string) => str,
// },
// );
const url = `${urls.documents}/?${docsUtils.defaultSelect}${docsUtils.defaultOrderBy}${filterString}`;

console.log(url);

return axios
.get(url)
.then((resp: AxiosResponse) => {
Expand Down
14 changes: 12 additions & 2 deletions src/utils/filtering/documents/getFilterString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import filteringDocuments from '../../../config/filtering/documents.json';
import filtering from '../../../config/filtering/index.json';

export function getFilterString(params: any, defaultFilter?: string) {
let str = defaultFilter ?? '';
let str = defaultFilter && !params.documentTypes ? defaultFilter : '';

const locations = _.filter(
_.get(params, 'locations', '').split(','),
Expand Down Expand Up @@ -42,6 +42,16 @@ export function getFilterString(params: any, defaultFilter?: string) {
}(${multicountries.join(filtering.multi_param_separator)})`;
}

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

const search = _.get(params, 'q', '');
if (search.length > 0) {
str += `${str.length > 0 ? ' AND ' : ''}${filteringDocuments.search.replace(
Expand All @@ -51,7 +61,7 @@ export function getFilterString(params: any, defaultFilter?: string) {
}

if (str.length > 0) {
str = `${filtering.filter_operator}${filtering.param_assign_operator}${str}&`;
str = `${filtering.filter_operator}${filtering.param_assign_operator}${str}`;
}

return str;
Expand Down

0 comments on commit cf23c73

Please sign in to comment.