diff --git a/package-lock.json b/package-lock.json index 971dea3..5f9b09e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1169,9 +1169,9 @@ } }, "@codetanzania/ewea-common": { - "version": "0.20.0", - "resolved": "https://registry.npmjs.org/@codetanzania/ewea-common/-/ewea-common-0.20.0.tgz", - "integrity": "sha512-Wa1taNOffDFqcEvALsny/ZeDALH9DqHCUu/Y3fI3dCwKeBkhjasWA+3Ttpk2Ov+iLeuIa4Iz6uwfeM9PWDtqyA==", + "version": "0.21.1", + "resolved": "https://registry.npmjs.org/@codetanzania/ewea-common/-/ewea-common-0.21.1.tgz", + "integrity": "sha512-Zkj08L2+Tc6b5f1N29kEB35fD3TCcWETZLN8L2LMlZ7wiuE3/ydm2wVxPrZf/sr7WY6VwXKSxckQxeipDSvKQw==", "requires": { "@codetanzania/emis-stakeholder": ">=2.8.0", "@codetanzania/ewea-internals": ">=0.21.0", @@ -7133,9 +7133,9 @@ } }, "eslint-plugin-jsdoc": { - "version": "27.0.4", - "resolved": "https://registry.npmjs.org/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-27.0.4.tgz", - "integrity": "sha512-gRjtUngFu3j5fwHAi4HHECIT7sDyD0IS1K59+PAg1+Wn/aXCMV+fKkoy1r5ezwBU4hkHxuvq8tOaTTvFNFvjbA==", + "version": "27.0.5", + "resolved": "https://registry.npmjs.org/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-27.0.5.tgz", + "integrity": "sha512-LU2sclCdHe4ykYlb2Jb7Nk7L6xd8PfwANV1KRgwHV78kCo/D8aeTdinNZb8BpZQEmu4HbrY82SPZIvyYumHVTQ==", "dev": true, "requires": { "comment-parser": "^0.7.5", diff --git a/package.json b/package.json index 21cbe19..a17864c 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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", diff --git a/src/case.model.js b/src/case.model.js index 0374477..d1d9c4c 100644 --- a/src/case.model.js +++ b/src/case.model.js @@ -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, @@ -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 diff --git a/src/schema/common.schema.js b/src/schema/common.schema.js index 86dd6f2..3cfe17c 100644 --- a/src/schema/common.schema.js +++ b/src/schema/common.schema.js @@ -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 }), }; /** @@ -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 + * @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 }), }; /** @@ -585,6 +610,7 @@ export const followup = createSubSchema({ follower, followedAt, symptoms: properties, + score, outcome, remarks, }); diff --git a/test/unit/case.model.spec.js b/test/unit/case.model.spec.js index ddc23a8..687de4c 100644 --- a/test/unit/case.model.spec.js +++ b/test/unit/case.model.spec.js @@ -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', () => {