@trusthab/composable-resources
Version:
migrating https://github.com/knetikmedia/hab-api/tree/integration/app/resources/composable
124 lines (112 loc) • 3.58 kB
JavaScript
const _ = require('lodash');
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) => {
let terms = [];
if (areas.content.length) {
terms = areas.content.map(item => ({
term: { 'data.location_id.keyword': item.id }
}));
} else {
terms = [{ term: { 'data.location_id.keyword': id } }];
}
return [{ bool: { should: terms } }];
},
)
},
location_id_and_children: {
insertion_path: 'query.bool.must',
callback: id => Promise.resolve([{
bool: {
should: [
{
term: {
'data.location_id.keyword': id
}
},
{
term: {
'data.parent_location_id.keyword': id
}
},
{
term: {
'data.root_location_id.keyword': id
}
}
]
}
}])
},
available_for_location_id: {
insertion_path: 'query.bool.must', // to be used when areas are added to search
callback: (id) => {
let place;
const filters = [];
return (
App.Areas.send('get', [`/areas/${id}`])
.then(area => place = area)
.then(() => {
// 1) The IDs for any profiles at the zone's level
filters.push([
{ term: { 'data.location_id.keyword': place.id } }
]);
// 2) The id of the location's custom profile, if it exists
if (!_.get(place, 'additional_properties.profile_id.value') || _.get(place, 'additional_properties.profile_id.value') < 0) {
return;
}
filters.push([{ term: { id: _.get(place, 'additional_properties.profile_id.value') } }]);
})
// 3) The IDs for any profiles at the zone's parent (building)
// 4) The IDs for any profiles at the property level
.then(() => App.Areas.send('get', [`/areas/${id}/ancestors`]))
.then((result) => {
result.content.forEach((ancestor) => {
filters.push([
{ term: { 'data.location_id.keyword': ancestor.id } }
]);
});
})
.then(() => [{ bool: { should: filters } }])
);
}
},
all_profiles_by_tree: {
insertion_path: 'query.bool.must',
callback: (ids) => {
const query = [{
bool: {
filter: { terms: { 'data.location_id.keyword': ids.split(',') } },
must_not: [{ exists: { field: 'data.deleted_at' } }],
must: [
{ match: { template: 'profiles' } },
{ term: { 'data.is_default': true } }
]
}
}];
return Promise.resolve(query);
}
},
all_profiles_of_location: {
insertion_path: 'query.bool.must',
callback: (id) => {
const query = [{
bool: {
must_not: [{ exists: { field: 'data.deleted_at' } }],
must: [
{ match: { template: 'profiles' } },
{ term: { 'data.is_default': true } },
{ match: { 'data.location_id.keyword': id } }
]
}
}];
return Promise.resolve(query);
}
}
});