-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
107 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> | ||
* | ||
|
@@ -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'; | ||
|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); |