Skip to content

Commit 479b790

Browse files
committed
fix: filter WorkPeriods in RB endpoint
before this fix it returned error like 'cannot get "nested" of undefined'
1 parent ba0af40 commit 479b790

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/services/ResourceBookingService.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -520,25 +520,27 @@ async function searchResourceBookings (currentUser, criteria, options = { return
520520
}
521521
}]
522522
}
523+
// Apply WorkPeriod filters
523524
const workPeriodFilters = ['workPeriods.paymentStatus', 'workPeriods.startDate', 'workPeriods.endDate', 'workPeriods.userHandle']
524-
if (_.includes(criteria, workPeriodFilters)) {
525+
if (_.intersection(criteria, workPeriodFilters).length > 0) {
526+
const workPeriodsMust = []
527+
_.each(_.pick(criteria, workPeriodFilters), (value, key) => {
528+
workPeriodsMust.push({
529+
term: {
530+
[key]: {
531+
value
532+
}
533+
}
534+
})
535+
})
536+
525537
esQuery.body.query.bool.must.push({
526538
nested: {
527539
path: 'workPeriods',
528-
query: { bool: { must: [] } }
540+
query: { bool: { must: workPeriodsMust } }
529541
}
530542
})
531543
}
532-
// Apply WorkPeriod filters
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({
535-
term: {
536-
[key]: {
537-
value
538-
}
539-
}
540-
})
541-
})
542544
logger.debug({ component: 'ResourceBookingService', context: 'searchResourceBookings', message: `Query: ${JSON.stringify(esQuery)}` })
543545

544546
const { body } = await esClient.search(esQuery)

0 commit comments

Comments
 (0)