Skip to content

Commit

Permalink
refactor(model): re-implement seed to use new API
Browse files Browse the repository at this point in the history
  • Loading branch information
mkumbobeaty committed Jun 2, 2019
1 parent d66d5f8 commit e3d40c3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 223 deletions.
2 changes: 0 additions & 2 deletions examples/app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

/* dependencies */
const path = require('path');
const async = require('async');
const { include } = require('@lykmapipo/include');
const { get, start, mount } = require('@lykmapipo/express-common');
const { connect } = require('@lykmapipo/mongoose-common');
Expand Down
146 changes: 17 additions & 129 deletions lib/role.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
*/

/* dependencies */
const path = require('path');
const _ = require('lodash');
const { waterfall, parallel } = require('async');
const { abbreviate } = require('@lykmapipo/common');
const { getString, getStrings } = require('@lykmapipo/env');
const { Schema, ObjectId } = require('@lykmapipo/mongoose-common');
const { getString, } = require('@lykmapipo/env');
const { Schema, ObjectId, copyInstance } = require('@lykmapipo/mongoose-common');
const { model, SCHEMA_OPTIONS } = require('@lykmapipo/mongoose-common');
const actions = require('mongoose-rest-actions');
const exportable = require('@lykmapipo/mongoose-exportable');
Expand All @@ -34,7 +32,6 @@ const { Permission } = require('@lykmapipo/permission');
/* constants */
const ROLE_MODEL_NAME = getString('ROLE_MODEL_NAME', 'Role');
const ROLE_COLLECTION_NAME = getString('ROLE_COLLECTION_NAME', 'roles');
const ROLE_SEED = getString('ROLE_SEED', 'roles');
const ADMINISTRATOR_ROLE_NAME = getString(
'ADMINISTRATOR_ROLE_NAME',
'Administrator'
Expand Down Expand Up @@ -244,138 +241,29 @@ RoleSchema.statics.ADMINISTRATOR_ROLE_NAME = ADMINISTRATOR_ROLE_NAME;
RoleSchema.statics.OPTION_AUTOPOPULATE = OPTION_AUTOPOPULATE;

/**
* @name upsert
* @function upsert
* @description upsert role
* @param {role} role valid role details
* @param {function} done callback to invoke on success or error
* @name prepareSeedCriteria
* @function prepareSeedCriteria
* @description prepare role seeding upsert criteria
* @param {Object} seed plain object role seed
* @return {Object} criteria used to upsert role
*
* @author lally elias <lallyelias87@gmail.com>
* @since 0.1.0
* @since 1.5.0
* @version 0.1.0
* @public
*/
RoleSchema.statics.upsert = function upsert(role, done) {
//normalize arguments
const _role = _.isFunction(role.toObject) ? role.toObject() : role;

//refs
const Role = this;

//upsert
waterfall(
[
function findExistingRole(next) {
let criteria = _.merge({}, _role);
/* jshint ignore:start */
criteria = criteria._id
? _.pick(criteria, '_id')
: _.pick(criteria, 'name');
/* jshint ignore:end */
Role.findOne(criteria, next);
},

function upsertRole(found, next) {
//instantiate if not found
if (!found) {
found = new Role(_role);
}

//upsert
found.updatedAt = new Date();
found.put(_role, next);
},

function setPermissions(role, next) {
//TODO refactor
// assign administrator all permissions
if (role.name === ADMINISTRATOR_ROLE_NAME) {
Permission.find()
.select({ _id: 1 })
.exec(function(error, permissions) {
role.permissions = [].concat(permissions);
role.put(next);
});
} else {
next(null, role);
}
},
],
done
RoleSchema.statics.prepareSeedCriteria = seed => {
// prepare role upsert criteria by _id or fields
let criteria = copyInstance(seed);
criteria = (
criteria._id ?
_.pick(criteria, '_id') :
_.pick(criteria, 'name')
);
// return role upsert criteria
return criteria;
};

/**
* @name seed
* @function seed
* @description seed roles into database. On duplicate last changes will
* override existing one.
* @param {Role[]} [roles] set of permission(s) to seed
* @param {function} done callback to invoke on success or error
*
* @author lally elias <lallyelias87@gmail.com>
* @since 0.1.0
* @version 0.1.0
* @public
*/
RoleSchema.statics.seed = function seed(seeds, done) {
//normalize arguments
let _seeds = _.isFunction(seeds) ? [] : [].concat(seeds);
const _done = _.isFunction(seeds) ? seeds : done;

//obtain .env roles to seed
let ROLE_SEEDS = getStrings('ROLE_SEEDS');
ROLE_SEEDS = _.uniq(
_.compact([].concat(ADMINISTRATOR_ROLE_NAME).concat(ROLE_SEEDS))
);

//refs
const Role = this;

//prepare roles
let roles = _.map(ROLE_SEEDS, function(role) {
return {
name: role,
description: role,
};
});

/* jshint ignore:start */
//merge provided roles
_seeds = _.map([].concat(_seeds), function(_seed) {
const __seed = _.isString(_seed)
? { name: _seed, description: _seed }
: _seed;
return __seed;
});
/* jshint ignore:end */

// try load seeds from environment
const BASE_PATH = getString('BASE_PATH', process.cwd());
const SEEDS_PATH = getString('SEEDS_PATH', path.join(BASE_PATH, 'seeds'));
const SEED_PATH = path.join(SEEDS_PATH, ROLE_SEED);
try {
const seed = require(SEED_PATH);
_seeds = [].concat(_seeds).concat(seed);
} catch (e) {
/* ignore */
}

// collect unique roles from seeds
_seeds = _.compact(_seeds);
_seeds = _.uniqWith(_seeds, _.isEqual);

//upsert roles
roles = [].concat(roles).concat(_seeds);
roles = _.map(roles, function(role) {
return function upsertRoles(next) {
Role.upsert(role, next);
};
});

//seed roles
parallel(roles, _done);
};

/*
*------------------------------------------------------------------------------
Expand Down
92 changes: 0 additions & 92 deletions test/integration/role.upsert.spec.js

This file was deleted.

0 comments on commit e3d40c3

Please sign in to comment.