diff --git a/examples/app.js b/examples/app.js index ae5bc75..26ac476 100644 --- a/examples/app.js +++ b/examples/app.js @@ -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'); diff --git a/lib/role.model.js b/lib/role.model.js index ab61458..a08905c 100644 --- a/lib/role.model.js +++ b/lib/role.model.js @@ -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'); @@ -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' @@ -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 - * @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 - * @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); -}; /* *------------------------------------------------------------------------------ diff --git a/test/integration/role.upsert.spec.js b/test/integration/role.upsert.spec.js deleted file mode 100644 index f5c6a94..0000000 --- a/test/integration/role.upsert.spec.js +++ /dev/null @@ -1,92 +0,0 @@ -'use strict'; - -/* dependencies */ -const faker = require('@benmaruchu/faker'); -const { expect } = require('chai'); -const { include } = require('@lykmapipo/include'); -const { clear } = require('@lykmapipo/mongoose-test-helpers'); -const { Permission } = require('@lykmapipo/permission'); -const { Role } = include(__dirname, '..', '..'); - -describe('Role Upsert', () => { - let role; - - before(done => clear(done)); - - before(done => clear(done)); - - before(done => { - Permission.seed(done); - }); - - beforeEach(done => { - role = Role.fakeExcept('description'); - role.post((error, created) => { - role = created; - done(error, created); - }); - }); - - it('should be able upsert non existing', done => { - Role.upsert(role, (error, upserted) => { - expect(error).to.not.exist; - expect(upserted).to.exist; - expect(upserted._id).to.be.eql(role._id); - expect(upserted.name).to.be.eql(role.name); - done(error, upserted); - }); - }); - - it('should be able upsert existing by _id', done => { - const updates = { - _id: role._id, - description: faker.lorem.sentence(), - }; - Role.upsert(updates, (error, upserted) => { - expect(error).to.not.exist; - expect(upserted).to.exist; - expect(upserted._id).to.be.eql(role._id); - expect(upserted.name).to.be.eql(role.name); - expect(upserted.description).to.not.be.eql(role.description); - expect(upserted.description).to.be.eql(updates.description); - expect(upserted.createdAt).to.be.eql(role.createdAt); - done(error, upserted); - }); - }); - - it('should be able upsert existing by fields', done => { - const updates = { - name: role.name, - description: faker.lorem.sentence(), - }; - Role.upsert(updates, (error, upserted) => { - expect(error).to.not.exist; - expect(upserted).to.exist; - expect(upserted._id).to.be.eql(role._id); - expect(upserted.name).to.be.eql(role.name); - expect(upserted.description).to.not.be.eql(role.description); - expect(upserted.description).to.be.eql(updates.description); - expect(upserted.createdAt).to.be.eql(role.createdAt); - done(error, upserted); - }); - }); - - it('should be able upsert Administrator with permissions', done => { - const updates = { - name: 'Administrator', - description: faker.lorem.sentence(), - }; - Role.upsert(updates, (error, upserted) => { - expect(error).to.not.exist; - expect(upserted).to.exist; - expect(upserted.name).to.be.eql('Administrator'); - // expect(upserted.permissions).to.exist; - // expect(upserted.permissions).to.have.length.at.least(1); - done(error, upserted); - }); - }); - - after(done => clear(done)); - - after(done => clear(done)); -});