Skip to content

Commit

Permalink
feat: allow abbreviation to be updated
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Feb 14, 2019
1 parent ba48328 commit 2300939
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 149 deletions.
13 changes: 5 additions & 8 deletions lib/role.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/* dependencies */
const path = require('path');
const _ = require('lodash');
const async = require('async');
const { waterfall, parallel } = require('async');
const { abbreviate } = require('@lykmapipo/common');
const { getString, getStrings } = require('@lykmapipo/env');
const { Schema, SchemaTypes } = require('@lykmapipo/mongoose-common');
Expand Down Expand Up @@ -228,13 +228,10 @@ RoleSchema.pre('validate', function (done) {
RoleSchema.methods.preValidate = function preValidate(done) {

//ensure role abbreviation
if (_.isEmpty(this.abbreviation) && !_.isEmpty(this.name)) {
this.abbreviation = abbreviate(this.name);
}
this.abbreviation = (_.trim(this.abbreviation) || abbreviate(this.name));

//ensure description
this.description =
(_.isEmpty(this.description) ? this.name : this.description);
this.description = (_.trim(this.description) || this.name);

//ensure permissions
this.permissions =
Expand Down Expand Up @@ -280,7 +277,7 @@ RoleSchema.statics.upsert = function upsert(role, done) {
const Role = this;

//upsert
async.waterfall([
waterfall([
function findExistingRole(next) {
let criteria = _.merge({}, _role);
criteria = (
Expand Down Expand Up @@ -384,7 +381,7 @@ RoleSchema.statics.seed = function seed(seeds, done) {
});

//seed roles
async.parallel(roles, _done);
parallel(roles, _done);

};

Expand Down
145 changes: 9 additions & 136 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"@lykmapipo/permission": ">=0.7.0",
"async": ">=2.6.2",
"lodash": ">=4.17.11",
"mongoose-rest-actions": ">=0.23.0"
"mongoose-rest-actions": ">=0.23.1"
},
"engines": {
"node": ">=8.11.1",
Expand Down
6 changes: 4 additions & 2 deletions test/integration/role.patch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ describe('Role Patch', () => {
});

it('should be able to patch', (done) => {
role = role.fakeOnly('description');
role = role.fakeOnly('description', 'abbreviation');
Role.patch(role._id, role, (error, updated) => {
expect(error).to.not.exist;
expect(updated).to.exist;
expect(updated._id).to.eql(role._id);
expect(updated.name).to.eql(role.name);
expect(updated.abbreviation).to.be.eql(role.abbreviation);
done(error, updated);
});
});
Expand Down Expand Up @@ -58,12 +59,13 @@ describe('Role Patch', () => {
});

it('should be able to patch', (done) => {
role = role.fakeOnly('description');
role = role.fakeOnly('description', 'abbreviation');
role.patch((error, updated) => {
expect(error).to.not.exist;
expect(updated).to.exist;
expect(updated._id).to.eql(role._id);
expect(updated.name).to.eql(role.name);
expect(updated.abbreviation).to.be.eql(role.abbreviation);
done(error, updated);
});
});
Expand Down
6 changes: 4 additions & 2 deletions test/integration/role.put.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ describe('Role Put', function () {
});

it('should be able to put', (done) => {
role = role.fakeOnly('description');
role = role.fakeOnly('description', 'abbreviation');
Role.put(role._id, role, (error, updated) => {
expect(error).to.not.exist;
expect(updated).to.exist;
expect(updated._id).to.eql(role._id);
expect(updated.name).to.eql(role.name);
expect(updated.abbreviation).to.eql(role.abbreviation);
done(error, updated);
});
});
Expand Down Expand Up @@ -58,12 +59,13 @@ describe('Role Put', function () {
});

it('should be able to put', (done) => {
role = role.fakeOnly('description');
role = role.fakeOnly('description', 'abbreviation');
role.put((error, updated) => {
expect(error).to.not.exist;
expect(updated).to.exist;
expect(updated._id).to.eql(role._id);
expect(updated.name).to.eql(role.name);
expect(updated.abbreviation).to.eql(role.abbreviation);
done(error, updated);
});
});
Expand Down

0 comments on commit 2300939

Please sign in to comment.