Skip to content

Commit

Permalink
feat: implement getOneOrDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Aug 16, 2019
1 parent 790a79c commit a1638ed
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 5 deletions.
55 changes: 50 additions & 5 deletions src/priority.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* in order of their importance.
*
* @requires https://github.com/CodeTanzania/majifix-jurisdiction
* @see {@link https://github.com/CodeTanzania/majifix-jurisdiction|Jurisdiction}
* @author Benson Maruchu <[email protected]>
* @author lally elias <[email protected]>
*
Expand All @@ -16,7 +15,7 @@
* @public
*/
import _ from 'lodash';
import { randomColor } from '@lykmapipo/common';
import { randomColor, compact, mergeObjects } from '@lykmapipo/common';
import { createSchema, model, ObjectId } from '@lykmapipo/mongoose-common';
import { localize, localizedIndexesFor } from 'mongoose-locale-schema';
import actions from 'mongoose-rest-actions';
Expand Down Expand Up @@ -299,15 +298,61 @@ PrioritySchema.statics.OPTION_AUTOPOPULATE = OPTION_AUTOPOPULATE;
* @version 1.0.0
* @static
*/
PrioritySchema.statics.findDefault = function findDefault(done) {
// reference priority
const Priority = this;
PrioritySchema.statics.findDefault = done => {
// refs
const Priority = model(MODEL_NAME_PRIORITY);

// sort priority by weight descending and take one
return Priority.findOne()
.sort({ weight: 'asc' })
.exec(done);
};

/**
* @name getOneOrDefault
* @function getOneOrDefault
* @description Find existing priority or default based on given criteria
* @param {Object} criteria valid query criteria
* @param {Function} done callback to invoke on success or error
* @returns {Object|Error} found priority or error
*
* @author lally elias <[email protected]>
* @since 1.5.0
* @version 0.1.0
* @static
* @example
*
* const criteria = { _id: '...'};
* Priority.getOneOrDefault(criteria, (error, found) => { ... });
*
*/
PrioritySchema.statics.getOneOrDefault = (criteria, done) => {
// normalize criteria
const { _id, ...filters } = mergeObjects(criteria);

const allowDefault = true;
const allowId = !_.isEmpty(_id);
const allowFilters = !_.isEmpty(filters);

const byDefault = mergeObjects({ default: true });
const byId = mergeObjects({ _id });
const byFilters = mergeObjects(filters);

const or = compact([
allowId ? byId : undefined,
allowFilters ? byFilters : undefined,
allowDefault ? byDefault : undefined,
]);
const filter = { $or: or };

// refs
const Priority = model(MODEL_NAME_PRIORITY);

// query
return Priority.findOne(filter)
.orFail()
.exec(done);
};

/* export priority model */
export default model(MODEL_NAME_PRIORITY, PrioritySchema);
57 changes: 57 additions & 0 deletions test/integration/priority.getOneOrDefault.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { expect, clear } from '@lykmapipo/mongoose-test-helpers';
import { Priority } from '../../src/index';

describe('Priority getOneOrDefault', () => {
before(done => clear(done));

let priority = Priority.fake();
priority.default = true;

before(done => {
priority.post((error, created) => {
priority = created;
done(error, created);
});
});

it('should be able to get existing by id', done => {
const { _id } = priority;
Priority.getOneOrDefault({ _id }, (error, found) => {
expect(error).to.not.exist;
expect(found).to.exist;
expect(found._id).to.eql(priority._id);
done(error, found);
});
});

it('should be able to get existing with criteria', done => {
const name = priority.name.en;
Priority.getOneOrDefault({ 'name.en': name }, (error, found) => {
expect(error).to.not.exist;
expect(found).to.exist;
expect(found._id).to.eql(priority._id);
done(error, found);
});
});

it('should be able to get default with criteria', done => {
Priority.getOneOrDefault({}, (error, found) => {
expect(error).to.not.exist;
expect(found).to.exist;
expect(found._id).to.eql(priority._id);
done(error, found);
});
});

it('should not throw if not exists', done => {
const { _id } = Priority.fake();
Priority.getOneOrDefault({ _id }, (error, found) => {
expect(error).to.not.exist;
expect(found).to.exist;
expect(found._id).to.eql(priority._id);
done(error, found);
});
});

after(done => clear(done));
});

0 comments on commit a1638ed

Please sign in to comment.