Skip to content

Commit 96d105b

Browse files
committed
feat(model): ensure severity from computed score
1 parent cd6a0f1 commit 96d105b

File tree

5 files changed

+99
-22
lines changed

5 files changed

+99
-22
lines changed

package-lock.json

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
"build": "rollup -c",
1111
"lint": "eslint --fix --ext .js src/ test/ rollup.config.js examples/",
1212
"pretest": "npm run lint",
13-
"posttest": "rimraf test/fixtures/*.csv",
14-
"test": "NODE_ENV=test nyc --reporter=html --reporter=text mocha --exit --timeout=20000 --require @babel/register test/**/*.spec.js",
15-
"test:unit": "NODE_ENV=test npm run pretest && NODE_ENV=test mocha --exit --timeout=20000 --require @babel/register test/unit/**/*.spec.js",
16-
"test:integration": "NODE_ENV=test npm run pretest && NODE_ENV=test mocha --exit --timeout=20000 --require @babel/register test/integration/**/*.spec.js",
13+
"posttest": "rimraf test/fixtures/*.spec.csv",
14+
"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",
15+
"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",
16+
"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",
1717
"coverage": "nyc report --reporter=text-lcov | coveralls",
1818
"docs": "doxdox 'lib/**/*.js' -p package.json -l markdown -o DOCUMENTATION.md",
1919
"cmt": "git add -A && git-cz",
@@ -84,7 +84,7 @@
8484
"eslint-config-airbnb-base": "^14.1.0",
8585
"eslint-config-prettier": "^6.11.0",
8686
"eslint-plugin-import": "^2.21.1",
87-
"eslint-plugin-jsdoc": "^27.0.4",
87+
"eslint-plugin-jsdoc": "^27.0.5",
8888
"eslint-plugin-mocha": "^7.0.1",
8989
"eslint-plugin-prettier": "^3.1.3",
9090
"generate-changelog": "^1.8.0",
@@ -106,7 +106,7 @@
106106
},
107107
"dependencies": {
108108
"@codetanzania/emis-stakeholder": ">=2.8.0",
109-
"@codetanzania/ewea-common": ">=0.20.0",
109+
"@codetanzania/ewea-common": ">=0.21.1",
110110
"@codetanzania/ewea-event": ">=0.11.0",
111111
"@codetanzania/ewea-internals": ">=0.21.0",
112112
"@lykmapipo/common": ">=0.34.3",

src/case.model.js

+45-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import {
22
MODEL_NAME_CASE,
33
COLLECTION_NAME_CASE,
4+
PREDEFINE_NAMESPACE_CASESTAGE,
5+
PREDEFINE_NAMESPACE_PARTYGENDER,
6+
PREDEFINE_NAMESPACE_PARTYOCCUPATION,
7+
PREDEFINE_NAMESPACE_PARTYNATIONALITY,
8+
PREDEFINE_NAMESPACE_ADMINISTRATIVEAREA,
9+
DEFAULT_SEEDS,
410
} from '@codetanzania/ewea-internals';
5-
import { pick } from 'lodash';
11+
import { get, pick, set } from 'lodash';
612
import { mergeObjects, idOf } from '@lykmapipo/common';
7-
import { copyInstance, createSchema, model } from '@lykmapipo/mongoose-common';
813
import '@lykmapipo/mongoose-sequenceable';
14+
import { copyInstance, createSchema, model } from '@lykmapipo/mongoose-common';
915
import actions from 'mongoose-rest-actions';
1016
import exportable from '@lykmapipo/mongoose-exportable';
17+
import { caseSeverityFor } from '@codetanzania/ewea-common';
1118

1219
import {
1320
CASE_SCHEMA_OPTIONS,
@@ -103,17 +110,47 @@ CaseSchema.pre('validate', function onPreValidate(done) {
103110
* @instance
104111
*/
105112
CaseSchema.methods.preValidate = function preValidate(done) {
106-
// ensure started(or reported) date
107-
// TODO: drop reported date & use createdAt
113+
// ensure reported date
108114
this.reportedAt = this.reportedAt || this.createdAt || new Date();
109115

110-
// TODO: ensure victim default gender
111-
// TODO: ensure victim default occupation
116+
// ensure case stage
117+
if (!get(this, 'stage')) {
118+
const staje = DEFAULT_SEEDS[PREDEFINE_NAMESPACE_CASESTAGE];
119+
set(this, 'stage', staje);
120+
}
121+
122+
// TODO: compute score
123+
// TODO: move score to case base schema
124+
// always: ensure case severity from followup score
125+
const score = get(this, 'followup.score');
126+
this.severity = caseSeverityFor({ score });
127+
128+
// ensure victim gender
129+
if (!get(this, 'victim.gender')) {
130+
const gender = DEFAULT_SEEDS[PREDEFINE_NAMESPACE_PARTYGENDER];
131+
set(this, 'victim.gender', gender);
132+
}
133+
134+
// ensure victim default occupation
135+
if (!get(this, 'victim.occupation')) {
136+
const occupation = DEFAULT_SEEDS[PREDEFINE_NAMESPACE_PARTYOCCUPATION];
137+
set(this, 'victim.occupation', occupation);
138+
}
139+
140+
// ensure victim default nationality
141+
if (!get(this, 'victim.nationality')) {
142+
const nationality = DEFAULT_SEEDS[PREDEFINE_NAMESPACE_PARTYNATIONALITY];
143+
set(this, 'victim.nationality', nationality);
144+
}
145+
146+
// ensure victim default area
147+
if (!get(this, 'victim.area')) {
148+
const area = DEFAULT_SEEDS[PREDEFINE_NAMESPACE_ADMINISTRATIVEAREA];
149+
set(this, 'victim.area', area);
150+
}
112151

113152
// TODO: ensure event group and type
114-
115153
// TODO: ensure default values
116-
117154
// TODO: ensure case status
118155

119156
// return

src/schema/common.schema.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export const age = {
9999
type: Number,
100100
index: true,
101101
exportable: true,
102-
fake: (f) => f.random.number(),
102+
fake: (f) => f.random.number({ min: 1, max: 100 }),
103103
};
104104

105105
/**
@@ -123,7 +123,32 @@ export const weight = {
123123
type: Number,
124124
index: true,
125125
exportable: true,
126-
fake: (f) => f.random.number(),
126+
fake: (f) => f.random.number({ min: 1, max: 250 }),
127+
};
128+
129+
/**
130+
* @name score
131+
* @description Current score of the case followup.
132+
*
133+
* It used to derive case severity.
134+
*
135+
* @property {object} type - schema(data) type
136+
* @property {boolean} trim - force trimming
137+
* @property {boolean} index - ensure database index
138+
* @property {object} fake - fake data generator options
139+
*
140+
* @author lally elias <lallyelias87@gmail.com>
141+
* @since 0.1.0
142+
* @version 0.1.0
143+
* @instance
144+
* @example
145+
* 4
146+
*/
147+
export const score = {
148+
type: Number,
149+
index: true,
150+
exportable: true,
151+
fake: (f) => f.random.number({ min: 0, max: 5 }),
127152
};
128153

129154
/**
@@ -585,6 +610,7 @@ export const followup = createSubSchema({
585610
follower,
586611
followedAt,
587612
symptoms: properties,
613+
score,
588614
outcome,
589615
remarks,
590616
});

test/unit/case.model.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ describe('Case Instance', () => {
2020
done(error);
2121
});
2222
});
23+
24+
it('should set defaults on pre validate', (done) => {
25+
const caze = Case.fake();
26+
27+
caze.preValidate((error) => {
28+
expect(caze.stage).to.exist;
29+
expect(caze.severity).to.exist;
30+
expect(caze.victim.gender).to.exist;
31+
expect(caze.victim.occupation).to.exist;
32+
expect(caze.victim.nationality).to.exist;
33+
expect(caze.victim.area).to.exist;
34+
done(error);
35+
});
36+
});
2337
});
2438

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

0 commit comments

Comments
 (0)