@@ -194,7 +194,9 @@ async function _ensurePaidWorkPeriodsNotDeleted (resourceBookingId, oldValue, ne
194
194
return
195
195
}
196
196
// 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 )
198
200
// find which workPeriods should be removed
199
201
const workPeriodsToRemove = _ . differenceBy ( workPeriods , newWorkPeriods , 'startDate' )
200
202
// we can't delete workperiods with paymentStatus 'partially-completed' or 'completed'.
@@ -470,21 +472,15 @@ async function searchResourceBookings (currentUser, criteria, options = { return
470
472
criteria . sortOrder = 'desc'
471
473
}
472
474
try {
475
+ throw new Error ( 'fallback to DB' )
473
476
const esQuery = {
474
477
index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
475
478
_source_includes : queryOpt . include ,
476
479
_source_excludes : [ 'workPeriods.payments' , ...queryOpt . excludeRB , ...queryOpt . excludeWP ] ,
477
480
body : {
478
481
query : {
479
482
bool : {
480
- must : [
481
- {
482
- nested : {
483
- path : 'workPeriods' ,
484
- query : { bool : { must : [ ] } }
485
- }
486
- }
487
- ]
483
+ must : [ ]
488
484
}
489
485
} ,
490
486
from : ( page - 1 ) * perPage ,
@@ -526,22 +522,33 @@ async function searchResourceBookings (currentUser, criteria, options = { return
526
522
} ]
527
523
}
528
524
// 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
+ }
534
534
}
535
+ } )
536
+ } )
537
+
538
+ esQuery . body . query . bool . must . push ( {
539
+ nested : {
540
+ path : 'workPeriods' ,
541
+ query : { bool : { must : workPeriodsMust } }
535
542
}
536
543
} )
537
- } )
544
+ }
538
545
logger . debug ( { component : 'ResourceBookingService' , context : 'searchResourceBookings' , message : `Query: ${ JSON . stringify ( esQuery ) } ` } )
539
546
540
547
const { body } = await esClient . search ( esQuery )
541
548
let resourceBookings = _ . map ( body . hits . hits , '_source' )
542
549
// ESClient will return ResourceBookings with it's all nested WorkPeriods
543
550
// 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 ) => {
545
552
key = key . split ( '.' ) [ 1 ]
546
553
_ . each ( resourceBookings , r => {
547
554
r . workPeriods = _ . filter ( r . workPeriods , { [ key ] : value } )
@@ -612,9 +619,10 @@ async function searchResourceBookings (currentUser, criteria, options = { return
612
619
queryCriteria . order = [ [ { model : WorkPeriod , as : 'workPeriods' } , _ . split ( criteria . sortBy , '.' ) [ 1 ] , criteria . sortOrder ] ]
613
620
}
614
621
const resourceBookings = await ResourceBooking . findAll ( queryCriteria )
622
+ const total = await ResourceBooking . count ( _ . omit ( queryCriteria , [ 'limit' , 'offset' , 'attributes' , 'order' ] ) )
615
623
return {
616
624
fromDb : true ,
617
- total : resourceBookings . length ,
625
+ total,
618
626
page,
619
627
perPage,
620
628
result : resourceBookings
0 commit comments