Skip to content

Commit

Permalink
refactor: remove type and responsibilities
Browse files Browse the repository at this point in the history
- Add release stage on heroku Procfile
- Extract seed into separate file
- Force latest dependencies & audit fix
  • Loading branch information
lykmapipo committed Feb 14, 2019
1 parent 77e1bfd commit 2bef8ac
Show file tree
Hide file tree
Showing 14 changed files with 531 additions and 346 deletions.
11 changes: 11 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ module.exports = function (grunt) {
'test/integration/**/*.js',
'!test/unit/**/*.js'
]
},
http: {
options: {
reporter: 'spec',
timeout: 20000
},
src: [
'test/integration/bootstrap.spec.js',
'test/integration/**/*.http.spec.js'
]
}
},
jshint: {
Expand Down Expand Up @@ -81,6 +91,7 @@ module.exports = function (grunt) {
grunt.registerTask('default', ['jshint', 'mochaTest', 'watch']);
grunt.registerTask('test', ['jshint', 'mochaTest']);
grunt.registerTask('integration', ['jshint', 'mochaTest:integration']);
grunt.registerTask('http', ['jshint', 'mochaTest:http']);
grunt.registerTask('unit', ['jshint', 'mochaTest:unit']);
grunt.registerTask('doc', ['jshint', 'apidoc:api']);

Expand Down
3 changes: 2 additions & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
web: BASE_PATH=./examples node ./examples/app.js
web: BASE_PATH=./examples node ./examples/app.js
release: BASE_PATH=./examples node ./examples/seed.js
34 changes: 15 additions & 19 deletions examples/app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
'use strict';


/* ensure mongodb uri */
process.env.MONGODB_URI =
(process.env.MONGODB_URI || 'mongodb://localhost/emis-role');


/* dependencies */
const path = require('path');
const async = require('async');
Expand All @@ -15,22 +10,23 @@ const { Role, info, app } = include(__dirname, '..');


// establish mongodb connection
connect((error) => {

// seed roles
Role.seed((error, results) => {

// expose module info
app.get('/', (request, response) => {
response.status(200);
response.json(info);
});
connect(error => {
// re-throw if error
if (error) { throw error; }

// expose module info
app.get('/', (request, response) => {
response.status(200);
response.json(info);
});

// fire the app
app.start((error, env) => {
console.log(`visit http://0.0.0.0:${env.PORT}`);
});
// fire the app
app.start((error, env) => {
// re-throw if error
if (error) { throw error; }

// start http server
console.log(`visit http://0.0.0.0:${env.PORT}`);
});

});
60 changes: 60 additions & 0 deletions examples/seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';


/* dependencies */
const _ = require('lodash');
const { waterfall } = require('async');
const { include } = require('@lykmapipo/include');
const { connect } = require('@lykmapipo/mongoose-common');
const { Permission } = require('@lykmapipo/permission');
const { Role } = include(__dirname, '..');


// naive logger
const log = (stage, error, result) => {
if (error) {
console.error(`${stage} seed error`, error);
}
if (result) {
const val = _.isArray(result) ? result.length : result;
console.info(`${stage} seed result`, val);
}
};


// refs
let seedStart;
let seedEnd;

// seed permissions
const seedPermission = done => {
Permission.seed((error, seeded) => {
log('permissions', error, seeded);
done(error);
});
};

// seed roles
const seedRole = done => {
Role.seed((error, seeded) => {
log('roles', error, seeded);
done(error);
});
};

// do seed
const seed = done => {
seedStart = Date.now();
connect(error => {
if (error) { return done(error); }
waterfall([seedPermission, seedRole], done);
});
};

// do seeding
seed((error, results = [true]) => {
seedEnd = Date.now();
log('time', null, seedEnd - seedStart);
log('final', error, results);
process.exit(0);
});
16 changes: 8 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@


/* dependencies */
const path = require('path');
const _ = require('lodash');
const { pkg } = require('@lykmapipo/common');
const { include } = require('@lykmapipo/include');
const { apiVersion } = require('@lykmapipo/env');
const app = require('@lykmapipo/express-common');
const { Permission, permissionRouter } = require('@lykmapipo/permission');
const pkg = include(__dirname, 'package.json');
const Role = include(__dirname, 'lib', 'role.model');
const roleRouter = include(__dirname, 'lib', 'role.http.router');


Expand All @@ -39,13 +39,13 @@ const roleRouter = include(__dirname, 'lib', 'role.http.router');
* @type {Object}
*
* @author lally elias <lallyelias87@gmail.com>
* @since 0.1.0
* @since 1.0.0
* @version 0.1.0
*/
exports.info = _.merge({}, _.pick(pkg, [
exports.info = pkg(
'name', 'description', 'version', 'license',
'homepage', 'repository', 'bugs', 'sandbox', 'contributors'
]));
);


/**
Expand All @@ -69,7 +69,7 @@ exports.Permission = Permission;
* @since 0.1.0
* @version 0.1.0
*/
exports.Role = require(path.join(__dirname, 'lib', 'role.model'));
exports.Role = Role;


/**
Expand Down Expand Up @@ -105,7 +105,7 @@ exports.roleRouter = roleRouter;
* @since 0.1.0
* @version 0.1.0
*/
exports.apiVersion = roleRouter.apiVersion;
exports.apiVersion = apiVersion();


/**
Expand Down
10 changes: 5 additions & 5 deletions lib/role.http.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
* @apiSuccess {String} [data.abbreviation] Human readable short form of a role.
* @apiSuccess {String} [data.description] A brief summary about a role if
* available i.e additional details that clarify what a role for
* @apiSuccess {Object[]} [data.permissions] List of defined permits(access rights)
* of a role
* @apiSuccess {String[]} [data.responsibilities] A duties, obligation or functions
* performed(or assigned) to a role.
* @apiSuccess {Object[]} [data.permissions] List of defined
* permits(access rights) of a role
* @apiSuccess {String[]} [data.responsibilities] A duties, obligation or
* functions performed(or assigned) to a role.
* @apiSuccess {Date} data.createdAt Date when role was created
* @apiSuccess {Date} data.updatedAt Date when role was last updated
* @apiSuccess {Number} total Total number of role
Expand Down Expand Up @@ -142,7 +142,7 @@ const router = new Router({
router.get(PATH_LIST, function getRoles(request, response, next) {

//obtain request options
const options = _.merge({}, request.mquery);
const options = _.merge({}, { select: { permissions: 0 } }, request.mquery);

Role
.get(options, function onGetRoles(error, results) {
Expand Down
79 changes: 4 additions & 75 deletions lib/role.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
const path = require('path');
const _ = require('lodash');
const async = require('async');
const { abbreviate } = require('@lykmapipo/common');
const { getString, getStrings } = require('@lykmapipo/env');
const { Schema, SchemaTypes } = require('@lykmapipo/mongoose-common');
const { model, SCHEMA_OPTIONS } = require('@lykmapipo/mongoose-common');
Expand All @@ -36,8 +37,6 @@ const { ObjectId } = SchemaTypes;
/* constants */
const ROLE_MODEL_NAME = getString('ROLE_MODEL_NAME', 'Role');
const ROLE_COLLECTION_NAME = getString('ROLE_COLLECTION_NAME', 'roles');
const ROLE_TYPES = ['System', 'Assignable'];
const DEFAULT_ROLE_TYPE = getString('DEFAULT_ROLE_TYPE', 'System');
const ROLE_SEED = getString('ROLE_SEED', 'roles');
const ADMINISTRATOR_ROLE_NAME =
getString('ADMINISTRATOR_ROLE_NAME', 'Administrator');
Expand All @@ -56,38 +55,6 @@ const OPTION_AUTOPOPULATE = ({
* @private
*/
const RoleSchema = new Schema({
/**
* @name type
* @description Human readable category of a role.
*
* @type {object}
* @property {object} type - schema(data) type
* @property {boolean} trim - force trimming
* @property {boolean} enum - list of acceptable values
* @property {boolean} index - ensure database index
* @property {boolean} searchable - allow for searching
* @property {boolean} taggable - allow field use for tagging
* @property {object} fake - fake data generator options
*
* @author lally elias <lallyelias87@gmail.com>
* @since 0.1.0
* @version 0.1.0
* @instance
* @example
* System, Assignable etc.
*/
type: {
type: String,
trim: true,
enum: ROLE_TYPES,
index: true,
searchable: true,
taggable: true,
hide: true,
fake: true
},


/**
* @name name
* @description Unique human readable name of a role.
Expand Down Expand Up @@ -133,7 +100,6 @@ const RoleSchema = new Schema({
* @type {object}
* @property {object} type - schema(data) type
* @property {boolean} trim - force trimming
* @property {boolean} uppercase - force upper-casing
* @property {boolean} index - ensure database index
* @property {boolean} searchable - allow for searching
* @property {boolean} taggable - allow field use for tagging
Expand All @@ -148,7 +114,6 @@ const RoleSchema = new Schema({
abbreviation: {
type: String,
trim: true,
uppercase: true,
index: true,
searchable: true,
taggable: true,
Expand Down Expand Up @@ -188,34 +153,6 @@ const RoleSchema = new Schema({
},


/**
* @name responsibilities
* @description A duties, obligation or functions performed(or assigned) to
* a role.
*
* @type {object}
* @property {object} type - schema(data) type
* @property {boolean} index - ensure database index
* @property {boolean} searchable - allow for searching
* @property {object} fake - fake data generator options
*
* @author lally elias <lallyelias87@gmail.com>
* @since 0.1.0
* @version 0.1.0
* @instance
*/
responsibilities: {
type: [String],
index: true,
default: undefined,
searchable: true,
fake: {
generator: 'lorem',
type: 'sentence'
}
},


/**
* @name permissions
* @description List of defined permits(access rights) of a role
Expand Down Expand Up @@ -290,15 +227,9 @@ RoleSchema.pre('validate', function (done) {
*/
RoleSchema.methods.preValidate = function preValidate(done) {

//ensure role type
this.type =
(_.isEmpty(this.type) ? DEFAULT_ROLE_TYPE : this.type);

//ensure role abbreviation
if (!this.abbreviation && !_.isEmpty(this.name)) {
this.abbreviation = _.reduce(_.words(this.name), function (abbr, word) {
return _.toUpper(abbr + _.first(word));
}, '');
if (_.isEmpty(this.abbreviation) && !_.isEmpty(this.name)) {
this.abbreviation = abbreviate(this.name);
}

//ensure description
Expand All @@ -325,8 +256,6 @@ RoleSchema.methods.preValidate = function preValidate(done) {
RoleSchema.statics.MODEL_NAME = ROLE_MODEL_NAME;
RoleSchema.statics.COLLECTION_NAME = ROLE_COLLECTION_NAME;
RoleSchema.statics.ADMINISTRATOR_ROLE_NAME = ADMINISTRATOR_ROLE_NAME;
RoleSchema.statics.DEFAULT_ROLE_TYPE = DEFAULT_ROLE_TYPE;
RoleSchema.statics.ROLE_TYPES = ROLE_TYPES;
RoleSchema.statics.OPTION_AUTOPOPULATE = OPTION_AUTOPOPULATE;


Expand Down Expand Up @@ -357,7 +286,7 @@ RoleSchema.statics.upsert = function upsert(role, done) {
criteria = (
criteria._id ?
_.pick(criteria, '_id') :
_.pick(criteria, 'type', 'name')
_.pick(criteria, 'name')
);
Role.findOne(criteria, next);
},
Expand Down
Loading

0 comments on commit 2bef8ac

Please sign in to comment.