Skip to content

Commit

Permalink
feat(model): ensure severity from computed score
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Jun 9, 2020
1 parent cd6a0f1 commit 96d105b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 22 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

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

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"build": "rollup -c",
"lint": "eslint --fix --ext .js src/ test/ rollup.config.js examples/",
"pretest": "npm run lint",
"posttest": "rimraf test/fixtures/*.csv",
"test": "NODE_ENV=test nyc --reporter=html --reporter=text mocha --exit --timeout=20000 --require @babel/register test/**/*.spec.js",
"test:unit": "NODE_ENV=test npm run pretest && NODE_ENV=test mocha --exit --timeout=20000 --require @babel/register test/unit/**/*.spec.js",
"test:integration": "NODE_ENV=test npm run pretest && NODE_ENV=test mocha --exit --timeout=20000 --require @babel/register test/integration/**/*.spec.js",
"posttest": "rimraf test/fixtures/*.spec.csv",
"test": "NODE_ENV=test LOGGER_LOG_ENABLED=false DEFAULT_LOCALE=en LOCALES=en,sw nyc --reporter=html --reporter=text mocha --exit --timeout=80000 --require @babel/register test/**/*.spec.js",
"test:unit": "NODE_ENV=test npm run pretest && NODE_ENV=test DEFAULT_LOCALE=en LOCALES=en,sw mocha --exit --timeout=80000 --require @babel/register test/unit/**/*.spec.js",
"test:integration": "NODE_ENV=test npm run pretest && NODE_ENV=test DEFAULT_LOCALE=en LOCALES=en,sw mocha --exit --timeout=80000 --require @babel/register test/integration/**/*.spec.js",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"docs": "doxdox 'lib/**/*.js' -p package.json -l markdown -o DOCUMENTATION.md",
"cmt": "git add -A && git-cz",
Expand Down Expand Up @@ -84,7 +84,7 @@
"eslint-config-airbnb-base": "^14.1.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.21.1",
"eslint-plugin-jsdoc": "^27.0.4",
"eslint-plugin-jsdoc": "^27.0.5",
"eslint-plugin-mocha": "^7.0.1",
"eslint-plugin-prettier": "^3.1.3",
"generate-changelog": "^1.8.0",
Expand All @@ -106,7 +106,7 @@
},
"dependencies": {
"@codetanzania/emis-stakeholder": ">=2.8.0",
"@codetanzania/ewea-common": ">=0.20.0",
"@codetanzania/ewea-common": ">=0.21.1",
"@codetanzania/ewea-event": ">=0.11.0",
"@codetanzania/ewea-internals": ">=0.21.0",
"@lykmapipo/common": ">=0.34.3",
Expand Down
53 changes: 45 additions & 8 deletions src/case.model.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import {
MODEL_NAME_CASE,
COLLECTION_NAME_CASE,
PREDEFINE_NAMESPACE_CASESTAGE,
PREDEFINE_NAMESPACE_PARTYGENDER,
PREDEFINE_NAMESPACE_PARTYOCCUPATION,
PREDEFINE_NAMESPACE_PARTYNATIONALITY,
PREDEFINE_NAMESPACE_ADMINISTRATIVEAREA,
DEFAULT_SEEDS,
} from '@codetanzania/ewea-internals';
import { pick } from 'lodash';
import { get, pick, set } from 'lodash';
import { mergeObjects, idOf } from '@lykmapipo/common';
import { copyInstance, createSchema, model } from '@lykmapipo/mongoose-common';
import '@lykmapipo/mongoose-sequenceable';
import { copyInstance, createSchema, model } from '@lykmapipo/mongoose-common';
import actions from 'mongoose-rest-actions';
import exportable from '@lykmapipo/mongoose-exportable';
import { caseSeverityFor } from '@codetanzania/ewea-common';

import {
CASE_SCHEMA_OPTIONS,
Expand Down Expand Up @@ -103,17 +110,47 @@ CaseSchema.pre('validate', function onPreValidate(done) {
* @instance
*/
CaseSchema.methods.preValidate = function preValidate(done) {
// ensure started(or reported) date
// TODO: drop reported date & use createdAt
// ensure reported date
this.reportedAt = this.reportedAt || this.createdAt || new Date();

// TODO: ensure victim default gender
// TODO: ensure victim default occupation
// ensure case stage
if (!get(this, 'stage')) {
const staje = DEFAULT_SEEDS[PREDEFINE_NAMESPACE_CASESTAGE];
set(this, 'stage', staje);
}

// TODO: compute score
// TODO: move score to case base schema
// always: ensure case severity from followup score
const score = get(this, 'followup.score');
this.severity = caseSeverityFor({ score });

// ensure victim gender
if (!get(this, 'victim.gender')) {
const gender = DEFAULT_SEEDS[PREDEFINE_NAMESPACE_PARTYGENDER];
set(this, 'victim.gender', gender);
}

// ensure victim default occupation
if (!get(this, 'victim.occupation')) {
const occupation = DEFAULT_SEEDS[PREDEFINE_NAMESPACE_PARTYOCCUPATION];
set(this, 'victim.occupation', occupation);
}

// ensure victim default nationality
if (!get(this, 'victim.nationality')) {
const nationality = DEFAULT_SEEDS[PREDEFINE_NAMESPACE_PARTYNATIONALITY];
set(this, 'victim.nationality', nationality);
}

// ensure victim default area
if (!get(this, 'victim.area')) {
const area = DEFAULT_SEEDS[PREDEFINE_NAMESPACE_ADMINISTRATIVEAREA];
set(this, 'victim.area', area);
}

// TODO: ensure event group and type

// TODO: ensure default values

// TODO: ensure case status

// return
Expand Down
30 changes: 28 additions & 2 deletions src/schema/common.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const age = {
type: Number,
index: true,
exportable: true,
fake: (f) => f.random.number(),
fake: (f) => f.random.number({ min: 1, max: 100 }),
};

/**
Expand All @@ -123,7 +123,32 @@ export const weight = {
type: Number,
index: true,
exportable: true,
fake: (f) => f.random.number(),
fake: (f) => f.random.number({ min: 1, max: 250 }),
};

/**
* @name score
* @description Current score of the case followup.
*
* It used to derive case severity.
*
* @property {object} type - schema(data) type
* @property {boolean} trim - force trimming
* @property {boolean} index - ensure database index
* @property {object} fake - fake data generator options
*
* @author lally elias <lallyelias87@gmail.com>
* @since 0.1.0
* @version 0.1.0
* @instance
* @example
* 4
*/
export const score = {
type: Number,
index: true,
exportable: true,
fake: (f) => f.random.number({ min: 0, max: 5 }),
};

/**
Expand Down Expand Up @@ -585,6 +610,7 @@ export const followup = createSubSchema({
follower,
followedAt,
symptoms: properties,
score,
outcome,
remarks,
});
14 changes: 14 additions & 0 deletions test/unit/case.model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ describe('Case Instance', () => {
done(error);
});
});

it('should set defaults on pre validate', (done) => {
const caze = Case.fake();

caze.preValidate((error) => {
expect(caze.stage).to.exist;
expect(caze.severity).to.exist;
expect(caze.victim.gender).to.exist;
expect(caze.victim.occupation).to.exist;
expect(caze.victim.nationality).to.exist;
expect(caze.victim.area).to.exist;
done(error);
});
});
});

describe.skip('Case Validations', () => {
Expand Down

0 comments on commit 96d105b

Please sign in to comment.