@trusthab/composable-resources
Version:
migrating https://github.com/knetikmedia/hab-api/tree/integration/app/resources/composable
163 lines (151 loc) • 3.58 kB
JavaScript
const composer = require('../mixin_loader');
module.exports = (App) => {
const { underscore } = require('inflected');
class ProfileResource {
static mixins() {
return [
// Functionality
'Validatable',
'Templatable',
// ORMish
'Persistable',
'KnetikCloudPersistence',
'ObjectCommon'
];
}
static simpleProperties() {
return true;
}
static fields() {
return {
template: {
type: 'text',
or: ['null'],
path: 'template'
},
id: {
type: 'integer',
or: ['null'],
path: 'id'
},
name: {
type: 'text',
path: 'name'
},
description: {
type: 'text',
or: ['null'],
path: 'short_description'
},
active: {
type: 'boolean',
or: ['null'],
path: 'data.active'
},
off: {
type: 'boolean',
or: ['null'],
path: 'data.off'
},
is_default: {
type: 'boolean',
path: 'data.is_default'
},
locked: {
type: 'boolean',
or: ['null'],
path: 'data.locked'
},
climate_mode: {
type: 'text',
path: 'data.climate_mode'
},
heat_min: {
type: 'integer',
path: 'data.heat_min'
},
heat_max: {
type: 'integer',
path: 'data.heat_max'
},
heat_target: {
type: 'integer',
path: 'data.heat_target'
},
cool_min: {
type: 'integer',
path: 'data.cool_min'
},
cool_max: {
type: 'integer',
path: 'data.cool_max'
},
cool_target: {
type: 'integer',
path: 'data.cool_target'
},
auto_min: {
type: 'integer',
path: 'data.auto_min'
},
auto_max: {
type: 'integer',
path: 'data.auto_max'
},
auto_mode: {
type: 'text',
or: ['null'],
path: 'data.auto_mode'
},
scope: {
type: 'text',
path: 'data.scope',
persist: false,
get_method: 'getScope'
},
schedule: {
schema_ignore: true,
type: 'map',
path: 'data.schedule'
},
command_source: {
type: 'text',
or: ['null'],
path: 'data.command_source'
}
};
}
getScope() {
const { location_type, parent_location_name, location_name } = this;
if (location_type === 'unit') { return parent_location_name; }
return location_name;
}
static knetikCloudMethods() {
return {
search: {
index: 'objects',
template: 'profiles'
},
find: {
api: 'ObjectsApi',
method: 'getObjectItem',
paramsBuilder: id => ['profiles', id]
},
create: {
api: 'ObjectsApi',
method: 'createObjectItem',
paramsBuilder: params => ['profiles', { objectItem: params }]
},
update: {
api: 'ObjectsApi',
method: 'updateObjectItem',
paramsBuilder: (id, params) => ['profiles', id, { objectItem: params }]
}
};
}
static customFilters() {
return require('./profile_filters')(App);
}
}
return composer(ProfileResource, App);
};