@@ -47,6 +47,7 @@ const PROJECT_PHASE_PRODUCTS_ATTRIBUTES = _.without(
47
47
'deletedAt' ,
48
48
) ;
49
49
50
+ const SUPPORTED_FILTERS = [ 'id' , 'status' , 'memberOnly' , 'keyword' , 'type' , 'name' , 'code' , 'customer' , 'manager' ] ;
50
51
51
52
const escapeEsKeyword = keyword => keyword . replace ( / [ + - = > < ! | ( ) { } [ & \] ^ " ~ * ? : \\ / ] / g, '\\\\$&' ) ;
52
53
@@ -567,8 +568,7 @@ module.exports = [
567
568
'name' , 'name asc' , 'name desc' ,
568
569
'type' , 'type asc' , 'type desc' ,
569
570
] ;
570
- if ( ! util . isValidFilter ( filters ,
571
- [ 'id' , 'status' , 'memberOnly' , 'keyword' , 'type' , 'name' , 'code' , 'customer' , 'manager' ] ) ||
571
+ if ( ! util . isValidFilter ( filters , SUPPORTED_FILTERS ) ||
572
572
( sort && _ . indexOf ( sortableProps , sort ) < 0 ) ) {
573
573
return util . handleError ( 'Invalid filters or sort' , null , req , next ) ;
574
574
}
@@ -584,6 +584,7 @@ module.exports = [
584
584
page : req . query . page || 1 ,
585
585
} ;
586
586
req . log . info ( criteria ) ;
587
+ // TODO refactor (DRY) code below so we don't repeat the same logic for admins and non-admin users
587
588
if ( ! memberOnly
588
589
&& ( util . hasAdminRole ( req )
589
590
|| util . hasRoles ( req , MANAGER_ROLES ) ) ) {
@@ -592,6 +593,15 @@ module.exports = [
592
593
. then ( ( result ) => {
593
594
if ( result . rows . length === 0 ) {
594
595
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
+
595
605
return retrieveProjectsFromDB ( req , criteria , sort , req . query . fields )
596
606
. then ( r => util . setPaginationHeaders ( req , res , util . maskInviteEmails ( '$[*].invites[?(@.email)]' , r , req ) ) ) ;
597
607
}
@@ -609,6 +619,15 @@ module.exports = [
609
619
. then ( ( result ) => {
610
620
if ( result . rows . length === 0 ) {
611
621
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
+
612
631
return retrieveProjectsFromDB ( req , criteria , sort , req . query . fields )
613
632
. then ( r => util . setPaginationHeaders ( req , res , util . maskInviteEmails ( '$[*].invites[?(@.email)]' , r , req ) ) ) ;
614
633
}
0 commit comments