UNPKG

@trusthab/composable-resources

Version:

migrating https://github.com/knetikmedia/hab-api/tree/integration/app/resources/composable

252 lines (242 loc) 5.77 kB
const composer = require('../mixin_loader'); const moment = require('moment'); const _ = require('lodash'); module.exports = (App) => { const { underscore } = require('inflected'); class UserResource { static mixins() { return [ // Functionality 'Validatable', 'Templatable', // ORMish 'Persistable', 'KnetikCloudPersistence', // Mapping and helpers 'Common', 'Nameable', 'Alertable', 'Sourceable' ]; } static fields() { return { id: { weight: -1000, type: 'integer', or: ['null'] }, name: { weight: -999, type: 'text', or: ['null'], path: 'first_name' }, address: { type: 'text', path: 'address', or: ['null'] }, address2: { type: 'text', path: 'address2', or: ['null'] }, avatar_url: { type: 'text', path: 'avatar_url', or: ['null'] }, city: { type: 'text', path: 'city', or: ['null'] }, country_code: { type: 'text', path: 'country_code', or: ['null'] }, currency_code: { type: 'text', path: 'currency_code', or: ['null'] }, date_of_birth: { type: 'text', path: 'date_of_birth', or: ['null'] }, description: { type: 'text', path: 'description', or: ['null'] }, display_name: { type: 'text', path: 'display_name', or: ['null'] }, email: { type: 'text', path: 'email' }, first_name: { type: 'text', path: 'first_name', or: ['null'] }, fullname: { type: 'text', path: 'fullname', or: ['null'] }, gender: { type: 'text', path: 'gender', or: ['null'] }, language_code: { type: 'text', path: 'language_code', or: ['null'] }, last_name: { type: 'text', path: 'last_name', or: ['null'] }, mobile_number: { type: 'text', path: 'mobile_number', or: ['null'] }, password: { type: 'text', path: 'password' }, postal_code: { type: 'text', path: 'postal_code', or: ['null'] }, status: { type: 'boolean', path: 'additional_properties.status.value', or: ['null'] }, state: { type: 'text', path: 'state', or: ['null'] }, tags: { type: 'list', items: { type: 'string' }, path: 'tags' }, contact_methods: { type: 'list', items: { type: 'string' }, path: 'additional_properties.contact_methods.values' }, timezone_code: { type: 'text', path: 'timezone_code', or: ['null'] }, username: { type: 'text', path: 'username' }, persona_id: { type: 'text', path: 'additional_properties.persona_id.value' }, persona_name: { type: 'text', path: 'additional_properties.persona_name.value', or: ['null'] }, personas: { type: 'list', path: 'additional_properties.personas.values', or: ['null'] }, created_at: { weight: 1000, type: 'integer', or: ['null'], path: 'member_since' }, updated_at: { weight: 1001, type: 'integer', or: ['null'], path: 'additional_properties.updated_at.value' }, verified: { weight: 1002, type: 'boolean', or: ['null'], path: 'additional_properties.verified.value' }, template: { type: 'string', path: 'template', schema_ignore: true } }; } static knetikCloudMethods() { return { search: { index: 'users', template: 'users' }, find: { api: 'UsersApi', method: 'getUser' }, create: { api: 'UsersApi', method: 'registerUser', paramsBuilder: (params) => { params.template = 'users'; _.set( params, "additional_properties.updated_at", { type: "integer", value: moment().unix() } ); return [{ userResource: params }]; } }, update: { api: 'UsersApi', method: 'updateUser', paramsBuilder: (id, params) => { params.template = 'users'; _.set( params, "additional_properties.updated_at", { type: "integer", value: moment().unix() } ); return [id, { userResource: params }]; } }, destroy: { api: 'UsersApi', method: 'updateUser', paramsBuilder: (id, params) => { params.template = 'users'; return [id, { userResource: params }]; } } }; } static customFilters() { return require('./user_filters')(App); } } return composer(UserResource, App); };