@@ -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'.
@@ -470,21 +472,15 @@ async function searchResourceBookings (currentUser, criteria, options = { return
470472 criteria . sortOrder = 'desc'
471473 }
472474 try {
475+ throw new Error ( 'fallback to DB' )
473476 const esQuery = {
474477 index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
475478 _source_includes : queryOpt . include ,
476479 _source_excludes : [ 'workPeriods.payments' , ...queryOpt . excludeRB , ...queryOpt . excludeWP ] ,
477480 body : {
478481 query : {
479482 bool : {
480- must : [
481- {
482- nested : {
483- path : 'workPeriods' ,
484- query : { bool : { must : [ ] } }
485- }
486- }
487- ]
483+ must : [ ]
488484 }
489485 } ,
490486 from : ( page - 1 ) * perPage ,
@@ -526,22 +522,33 @@ async function searchResourceBookings (currentUser, criteria, options = { return
526522 } ]
527523 }
528524 // 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 ( {
531- term : {
532- [ key ] : {
533- value
525+ const workPeriodFilters = [ 'workPeriods.paymentStatus' , 'workPeriods.startDate' , 'workPeriods.endDate' , 'workPeriods.userHandle' ]
526+ if ( _ . intersection ( criteria , workPeriodFilters ) . length > 0 ) {
527+ const workPeriodsMust = [ ]
528+ _ . each ( _ . pick ( criteria , workPeriodFilters ) , ( value , key ) => {
529+ workPeriodsMust . push ( {
530+ term : {
531+ [ key ] : {
532+ value
533+ }
534534 }
535+ } )
536+ } )
537+
538+ esQuery . body . query . bool . must . push ( {
539+ nested : {
540+ path : 'workPeriods' ,
541+ query : { bool : { must : workPeriodsMust } }
535542 }
536543 } )
537- } )
544+ }
538545 logger . debug ( { component : 'ResourceBookingService' , context : 'searchResourceBookings' , message : `Query: ${ JSON . stringify ( esQuery ) } ` } )
539546
540547 const { body } = await esClient . search ( esQuery )
541548 let resourceBookings = _ . map ( body . hits . hits , '_source' )
542549 // ESClient will return ResourceBookings with it's all nested WorkPeriods
543550 // We re-apply WorkPeriod filters
544- _ . each ( _ . pick ( criteria , [ 'workPeriods.startDate' , 'workPeriods.endDate' , 'workPeriods.userHandle' , 'workPeriods.paymentStatus' ] ) , ( value , key ) => {
551+ _ . each ( _ . pick ( criteria , workPeriodFilters ) , ( value , key ) => {
545552 key = key . split ( '.' ) [ 1 ]
546553 _ . each ( resourceBookings , r => {
547554 r . workPeriods = _ . filter ( r . workPeriods , { [ key ] : value } )
@@ -612,9 +619,10 @@ async function searchResourceBookings (currentUser, criteria, options = { return
612619 queryCriteria . order = [ [ { model : WorkPeriod , as : 'workPeriods' } , _ . split ( criteria . sortBy , '.' ) [ 1 ] , criteria . sortOrder ] ]
613620 }
614621 const resourceBookings = await ResourceBooking . findAll ( queryCriteria )
622+ const total = await ResourceBooking . count ( _ . omit ( queryCriteria , [ 'limit' , 'offset' , 'attributes' , 'order' ] ) )
615623 return {
616624 fromDb : true ,
617- total : resourceBookings . length ,
625+ total,
618626 page,
619627 perPage,
620628 result : resourceBookings
0 commit comments