@@ -194,7 +194,9 @@ async function _ensurePaidWorkPeriodsNotDeleted (resourceBookingId, oldValue, ne
194194 return
195195 }
196196 // gather workPeriod dates from provided dates
197- const newWorkPeriods = helper . extractWorkPeriods ( newValue . startDate || oldValue . startDate , newValue . endDate || oldValue . endDate )
197+ const newWorkPeriods = helper . extractWorkPeriods (
198+ _ . isUndefined ( newValue . startDate ) ? oldValue . startDate : newValue . startDate ,
199+ _ . isUndefined ( newValue . endDate ) ? oldValue . endDate : newValue . endDate )
198200 // find which workPeriods should be removed
199201 const workPeriodsToRemove = _ . differenceBy ( workPeriods , newWorkPeriods , 'startDate' )
200202 // we can't delete workperiods with paymentStatus 'partially-completed' or 'completed'.
@@ -477,14 +479,7 @@ async function searchResourceBookings (currentUser, criteria, options = { return
477479 body : {
478480 query : {
479481 bool : {
480- must : [
481- {
482- nested : {
483- path : 'workPeriods' ,
484- query : { bool : { must : [ ] } }
485- }
486- }
487- ]
482+ must : [ ]
488483 }
489484 } ,
490485 from : ( page - 1 ) * perPage ,
@@ -525,9 +520,18 @@ async function searchResourceBookings (currentUser, criteria, options = { return
525520 }
526521 } ]
527522 }
523+ const workPeriodFilters = [ 'workPeriods.paymentStatus' , 'workPeriods.startDate' , 'workPeriods.endDate' , 'workPeriods.userHandle' ]
524+ if ( _ . includes ( criteria , workPeriodFilters ) ) {
525+ esQuery . body . query . bool . must . push ( {
526+ nested : {
527+ path : 'workPeriods' ,
528+ query : { bool : { must : [ ] } }
529+ }
530+ } )
531+ }
528532 // Apply WorkPeriod filters
529- _ . each ( _ . pick ( criteria , [ 'workPeriods.paymentStatus' , 'workPeriods.startDate' , 'workPeriods.endDate' , 'workPeriods.userHandle' ] ) , ( value , key ) => {
530- esQuery . body . query . bool . must [ 0 ] . nested . query . bool . must . push ( {
533+ _ . each ( _ . pick ( criteria , workPeriodFilters ) , ( value , key ) => {
534+ esQuery . body . query . bool . must [ esQuery . body . query . bool . must . length - 1 ] . nested . query . bool . must . push ( {
531535 term : {
532536 [ key ] : {
533537 value
@@ -541,7 +545,7 @@ async function searchResourceBookings (currentUser, criteria, options = { return
541545 let resourceBookings = _ . map ( body . hits . hits , '_source' )
542546 // ESClient will return ResourceBookings with it's all nested WorkPeriods
543547 // We re-apply WorkPeriod filters
544- _ . each ( _ . pick ( criteria , [ 'workPeriods.startDate' , 'workPeriods.endDate' , 'workPeriods.userHandle' , 'workPeriods.paymentStatus' ] ) , ( value , key ) => {
548+ _ . each ( _ . pick ( criteria , workPeriodFilters ) , ( value , key ) => {
545549 key = key . split ( '.' ) [ 1 ]
546550 _ . each ( resourceBookings , r => {
547551 r . workPeriods = _ . filter ( r . workPeriods , { [ key ] : value } )
0 commit comments