Skip to content

Commit 167ce16

Browse files
committed
fix: don't fallback to DB if search filter is defined
1 parent 44925aa commit 167ce16

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/routes/projects/list.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const PROJECT_PHASE_PRODUCTS_ATTRIBUTES = _.without(
4747
'deletedAt',
4848
);
4949

50+
const SUPPORTED_FILTERS = ['id', 'status', 'memberOnly', 'keyword', 'type', 'name', 'code', 'customer', 'manager'];
5051

5152
const escapeEsKeyword = keyword => keyword.replace(/[+-=><!|(){}[&\]^"~*?:\\/]/g, '\\\\$&');
5253

@@ -567,8 +568,7 @@ module.exports = [
567568
'name', 'name asc', 'name desc',
568569
'type', 'type asc', 'type desc',
569570
];
570-
if (!util.isValidFilter(filters,
571-
['id', 'status', 'memberOnly', 'keyword', 'type', 'name', 'code', 'customer', 'manager']) ||
571+
if (!util.isValidFilter(filters, SUPPORTED_FILTERS) ||
572572
(sort && _.indexOf(sortableProps, sort) < 0)) {
573573
return util.handleError('Invalid filters or sort', null, req, next);
574574
}
@@ -584,6 +584,7 @@ module.exports = [
584584
page: req.query.page || 1,
585585
};
586586
req.log.info(criteria);
587+
// TODO refactor (DRY) code below so we don't repeat the same logic for admins and non-admin users
587588
if (!memberOnly
588589
&& (util.hasAdminRole(req)
589590
|| util.hasRoles(req, MANAGER_ROLES))) {
@@ -592,6 +593,15 @@ module.exports = [
592593
.then((result) => {
593594
if (result.rows.length === 0) {
594595
req.log.debug('No projects found in ES');
596+
597+
// if we have some filters and didn't get any data from ES
598+
// we don't fallback to DB, because DB doesn't support all of the filters
599+
// so we don't want DB to return unrelated data, ref issue #450
600+
if (_.intersection(_.keys(filters), SUPPORTED_FILTERS).length > 0) {
601+
req.log.debug('Don\'t fallback to DB because some filters are defined.');
602+
return util.setPaginationHeaders(req, res, util.maskInviteEmails('$[*].invites[?(@.email)]', result, req));
603+
}
604+
595605
return retrieveProjectsFromDB(req, criteria, sort, req.query.fields)
596606
.then(r => util.setPaginationHeaders(req, res, util.maskInviteEmails('$[*].invites[?(@.email)]', r, req)));
597607
}
@@ -609,6 +619,15 @@ module.exports = [
609619
.then((result) => {
610620
if (result.rows.length === 0) {
611621
req.log.debug('No projects found in ES');
622+
623+
// if we have some filters and didn't get any data from ES
624+
// we don't fallback to DB, because DB doesn't support all of the filters
625+
// so we don't want DB to return unrelated data, ref issue #450
626+
if (_.intersection(_.keys(filters), SUPPORTED_FILTERS).length > 0) {
627+
req.log.debug('Don\'t fallback to DB because some filters are defined.');
628+
return util.setPaginationHeaders(req, res, util.maskInviteEmails('$[*].invites[?(@.email)]', result, req));
629+
}
630+
612631
return retrieveProjectsFromDB(req, criteria, sort, req.query.fields)
613632
.then(r => util.setPaginationHeaders(req, res, util.maskInviteEmails('$[*].invites[?(@.email)]', r, req)));
614633
}

0 commit comments

Comments
 (0)