@trusthab/composable-resources
Version:
migrating https://github.com/knetikmedia/hab-api/tree/integration/app/resources/composable
192 lines (176 loc) • 4.61 kB
JavaScript
const composer = require('../mixin_loader');
module.exports = (App) => {
const { underscore } = require('inflected');
class AlertResource {
static mixins() {
return [
// Functionality
'Validatable',
'Templatable',
// ORMish
'Persistable',
'KnetikCloudPersistence',
'ObjectCommon',
'Nameable'
];
}
static fields() {
return {
id: {
type: 'integer'
},
message: {
type: 'text',
minLength: 1,
path: 'name'
},
status: {
type: 'text',
minLength: 1,
path: 'data.status'
},
statuses: {
type: 'list',
path: 'data.statuses'
},
// device_id: {
// type: 'text',
// minLength: 1,
// path: 'data.device_id'
// },
metric_id: {
type: 'integer',
or: ['null'],
path: 'data.metric_id'
},
machine_name: {
type: 'text',
path: 'data.machine_name'
},
value: {
type: 'integer',
or: ['null'],
path: 'data.value'
},
value_type: {
type: 'text',
or: ['null'],
path: 'data.value_type'
},
evaluation: {
type: 'text',
or: ['null'],
path: 'data.evaluation'
},
threshold: {
type: 'integer',
or: ['null'],
path: 'data.threshold'
},
archived: {
type: 'boolean',
path: 'data.archived',
default_value: false
},
resolved_at: {
type: 'integer',
or: ['null'],
path: 'data.resolved_at'
},
template: {
type: 'integer',
path: 'template',
schema_ignore: true
}
};
}
static knetikCloudMethods() {
return {
search: {
index: 'objects',
template: 'alerts'
},
find: {
api: 'ObjectsApi',
method: 'getObjectItem',
paramsBuilder: id => ['alerts', id]
},
create: {
api: 'ObjectsApi',
method: 'createObjectItem',
paramsBuilder: params => ['alerts', { objectItem: params }]
},
update: {
api: 'ObjectsApi',
method: 'updateObjectItem',
paramsBuilder: (id, params) => ['alerts', id, { cascade: false, objectItem: params }]
}
};
}
static customFilters() {
return {
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
}
}
]
}
}])
},
for_multiple_locations: {
insertion_path: 'query.bool.should',
callback: (ids) => {
ids = ids.split(',');
const query = ids.map(id => ({ match: { 'data.location_id.keyword': id } }));
return Promise.resolve(query);
}
},
not_resolved: {
insertion_path: 'query.bool.must_not',
callback: () => {
const must_not = [
{ exists: { field: 'data.resolved_at' } },
{ match: { 'data.status': 'resolved' } }
];
return Promise.resolve(must_not);
}
},
filter_by_status: {
insertion_path: 'query.bool.must_not',
callback: (statuses_string) => {
const statuses = statuses_string.split(' ');
const must_not = [];
statuses.forEach((status) => { must_not.push({ match: { 'data.status': status } }); });
return Promise.resolve(must_not);
}
},
not_archived: {
insertion_path: 'query.bool.must_not',
callback: () => {
const must_not = [
{ match: { 'data.archived': true } }
];
return Promise.resolve(must_not);
}
}
};
}
}
return composer(AlertResource, App);
};