@trusthab/composable-resources
Version:
migrating https://github.com/knetikmedia/hab-api/tree/integration/app/resources/composable
92 lines (83 loc) • 2.42 kB
JavaScript
const _ = require('lodash');
function buildRangeClause(property, range) {
// start (inclusive) end (exclusive)
const rangeArray = range.split(',');
const start = parseInt(rangeArray[0], 10);
const end = parseInt(rangeArray[1], 10);
const rangeClause = { range: { [property]: {} } };
if (_.isFinite(start)) {
rangeClause.range[property].gte = start;
}
if (_.isFinite(end)) {
rangeClause.range[property].lt = end;
}
return Promise.resolve([rangeClause]);
}
module.exports = App => ({
locations_second_tier_children: {
insertion_path: 'query.bool.must', // to be used when areas are added to search
callback: id => App.Areas.send('get', [`/areas/?filter_parent=${id}`])
.then(result => result.content.map(item => item.id))
.then(ids => App.Areas.send('get', [`/areas/?filter_parent=${ids.join(',')}`]))
.then(
areas => (!areas.content.length
? [
{
bool: {
should: [
{
term: {
'additional_properties.location_id.value.keyword': id
}
}
]
}
}
]
: [
{
bool: {
should: areas.content.map(item => ({
term: {
'additional_properties.location_id.value.keyword':
item.id
}
}))
}
}
])
)
},
location_id_and_children: {
insertion_path: 'query.bool.must',
callback: id => Promise.resolve([{
bool: {
should: [
{
term: {
'additional_properties.location_id.value.keyword': id
}
},
{
term: {
'additional_properties.parent_location_id.value.keyword': id
}
},
{
term: {
'additional_properties.root_location_id.value.keyword': id
}
}
]
}
}])
},
battery_level_between: {
insertion_path: 'query.bool.filter',
callback: range => buildRangeClause('additional_properties.battery_level.value', range)
},
signal_between: {
insertion_path: 'query.bool.filter',
callback: range => buildRangeClause('additional_properties.signal.value', range)
}
});