Skip to content

Commit

Permalink
refactor: improve existance checks
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Aug 15, 2019
1 parent f7cb9ce commit 633a0d3
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 68 deletions.
6 changes: 3 additions & 3 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 @@ -92,7 +92,7 @@
},
"dependencies": {
"@codetanzania/majifix-common": ">=0.15.0",
"@codetanzania/majifix-jurisdiction": ">=1.6.0",
"@codetanzania/majifix-jurisdiction": ">=1.7.0",
"randomcolor": ">=0.5.4",
"@lykmapipo/common": ">=0.25.1",
"@lykmapipo/env": ">=0.12.1",
Expand Down
64 changes: 2 additions & 62 deletions src/priority.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ import exportable from '@lykmapipo/mongoose-exportable';
import { Jurisdiction } from '@codetanzania/majifix-jurisdiction';
import {
POPULATION_MAX_DEPTH,
MODEL_NAME_JURISDICTION,
MODEL_NAME_PRIORITY,
MODEL_NAME_SERVICEGROUP,
MODEL_NAME_SERVICE,
MODEL_NAME_SERVICEREQUEST,
COLLECTION_NAME_PRIORITY,
PATH_NAME_JURISDICTION,
PATH_NAME_PRIORITY,
checkDependenciesFor,
} from '@codetanzania/majifix-common';
Expand Down Expand Up @@ -88,8 +86,8 @@ const PrioritySchema = createSchema(
*/
jurisdiction: {
type: ObjectId,
ref: MODEL_NAME_JURISDICTION,
exists: true,
ref: Jurisdiction.MODEL_NAME,
exists: { refresh: true, select: Jurisdiction.OPTION_SELECT },
autopopulate: Jurisdiction.OPTION_AUTOPOPULATE,
index: true,
},
Expand Down Expand Up @@ -252,64 +250,6 @@ PrioritySchema.methods.beforeDelete = function beforeDelete(done) {
return checkDependenciesFor(this, { path, dependencies }, done);
};

/**
* @name beforePost
* @function beforePost
* @description pre save priority logics
* @param {function} done callback to invoke on success or error
*
* @since 0.1.0
* @version 1.0.0
* @instance
*/
PrioritySchema.methods.beforePost = function beforePost(done) {
// ensure jurisdiction is pre loaded before post(save)
const jurisdictionId = this.jurisdiction
? this.jurisdiction._id // eslint-disable-line no-underscore-dangle
: this.jurisdiction;

// prefetch existing jurisdiction
if (jurisdictionId) {
Jurisdiction.getById(
jurisdictionId,
function cb(error, jurisdiction) {
// assign existing jurisdiction
if (jurisdiction) {
this.jurisdiction = jurisdiction;
}

// return
done(error, this);
}.bind(this)
);
}

// continue
else {
done();
}
};

/**
* @name afterPost
* @function afterPost
* @description post save priority logics
* @param {function} done callback to invoke on success or error
*
* @since 0.1.0
* @version 1.0.0
* @instance
*/
PrioritySchema.methods.afterPost = function afterPost(done) {
// ensure jurisdiction is populated after post(save)
const population = _.merge(
{},
{ path: PATH_NAME_JURISDICTION },
Jurisdiction.OPTION_AUTOPOPULATE
);
this.populate(population, done);
};

/*
*------------------------------------------------------------------------------
* Statics
Expand Down
4 changes: 2 additions & 2 deletions test/unit/priority.schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('Priority', () => {
expect(jurisdiction.type).to.be.a('function');
expect(jurisdiction.type.name).to.be.equal('ObjectId');
expect(jurisdiction.index).to.be.true;
expect(jurisdiction.exists).to.be.true;
expect(jurisdiction.autopopulate).to.exist;
expect(jurisdiction.exists).to.be.exist.and.be.an('object');
expect(jurisdiction.autopopulate).to.exist.and.be.an('object');
});

describe('name', () => {
Expand Down

0 comments on commit 633a0d3

Please sign in to comment.