Skip to content

Commit

Permalink
feat(survey): add support for additional attributes on model
Browse files Browse the repository at this point in the history
  • Loading branch information
navarroaxel committed Oct 12, 2018
1 parent 5ce0305 commit 3691142
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"plugin:promise/recommended"
],
"parserOptions": {
"ecmaVersion": 2017
"ecmaVersion": 2018
},
"env": {
"es6": true,
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ By default the SurveyAddress has the following Mongoose's schema:
dwellings: [{type: Mixed}],
user: {type: ObjectId, required: true},
address: {type: ObjectId, ref: 'Address', required: true},
surveyAddressState: {type: Number}
surveyAddressState: {type: Number},
state: {type: Number},
valid: {type: Number}
}
```

Expand All @@ -69,6 +71,18 @@ module.exports = {

```

Also, you can add additional attributes to the SurveyAddress's schema defining a `model/surveyAddress.js` as the following:

```js
module.exports = {
visits: [{
order: {type: Number, required: true},
date: {type: Date, required: true},
comments: {type:String}
}]
};
```

### Environment variables

| Required | Variable | Description | Defaults to |
Expand Down
9 changes: 2 additions & 7 deletions src/controllers/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const {map, size, isEmpty} = require('lodash');

const {SurveyAddress, surveyAddressState, SyncLog} = require('../model');
const syncHandlers = require('../helpers/syncHandlers');
const {SyncService} = require('../services');
const {SurveyService, SyncService} = require('../services');

const setSurvey = async (survey, syncLog, user) => {
if (survey.closed) {
Expand All @@ -24,12 +24,7 @@ const setSurvey = async (survey, syncLog, user) => {
if (!surveyAddress) {
return;
}
surveyAddress.surveyAddressState = survey.closed ? surveyAddressState.RESOLVED : surveyAddressState.IN_PROGRESS;
surveyAddress.visits = survey.visits;
surveyAddress.dwellings = survey.dwellings;
if (survey.valid >= 0) {
surveyAddress.valid = survey.valid;
}
SurveyService.assign(surveyAddress, survey);
await syncHandlers.preSaveSurvey(surveyAddress, syncLog);
await surveyAddress.save();
};
Expand Down
2 changes: 1 addition & 1 deletion src/model/dwelling.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const {Mixed} = mongoose;
const dwellingFile = path.resolve('./model/dwelling.js');

if (fs.existsSync(dwellingFile)) {
logger.info('Loading Dwelling model...');
module.exports = require(dwellingFile);
logger.info('Loading dwelling model...');
} else {
logger.warn('Dwelling model not found. Using Mixed type...');
module.exports = {type: Mixed};
Expand Down
3 changes: 2 additions & 1 deletion src/model/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
exports.address = require('./address');
exports.surveyAddressState = require('./surveyAddressState');
exports.SurveyAddress = require('./surveyAddress');
exports.SurveyAddressAdditionalAttributes = require('./surveyAddressAdditionalAttributes');
exports.surveyAddressState = require('./surveyAddressState');
exports.SurveyDump = require('./surveyDump');
exports.SyncLog = require('./syncLog');
8 changes: 6 additions & 2 deletions src/model/surveyAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const mongoose = require('mongoose');

const logger = require('../helpers/logger');
const dwelling = require('./dwelling');
const surveyAddressAdditionalAttributes = require('./surveyAddressAdditionalAttributes');

const {ObjectId, Schema} = mongoose;

Expand All @@ -12,11 +13,14 @@ const surveyAddressSchema = new Schema({
dwellings: [dwelling],
user: {type: ObjectId, required: true},
address: {type: ObjectId, ref: 'Address', required: true},
state: {type: Number}
surveyAddressState: {type: Number},
state: {type: Number},
valid: {type: Number},
...surveyAddressAdditionalAttributes
}, {collection: SURVEYS_COLLECTION, timestamps: true});

const surveysHistory = SURVEYS_HISTORY === true.toString();
logger.info(`Surveys history is turned ${surveysHistory ? 'on' : 'off'}.`);
logger.info(`Surveys history is turned ${surveysHistory ? 'ON' : 'off'}.`);

if (surveysHistory) {
surveyAddressSchema.plugin(require('mongoose-history'));
Expand Down
11 changes: 11 additions & 0 deletions src/model/surveyAddressAdditionalAttributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const path = require('path');
const fs = require('fs');

const logger = require('../helpers/logger');

const surveyAddressFile = path.resolve('./model/surveyAddress.js');

if (fs.existsSync(surveyAddressFile)) {
logger.info('Loading SurveyAddress additional attributes...');
module.exports = require(surveyAddressFile);
}
2 changes: 1 addition & 1 deletion src/routes/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = router => {
logger.info(`Sync mode: ${receiveOnly ? 'receive-only' : 'send+receive'}.`);

const surveysDump = SURVEYS_DUMP === true.toString();
logger.info(`Surveys dump is turned ${surveysDump ? 'on' : 'off'}.`);
logger.info(`Surveys dump is turned ${surveysDump ? 'ON' : 'off'}.`);

const middlewares = [];
if (surveysDump) {
Expand Down
1 change: 1 addition & 0 deletions src/services/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
exports.StatusService = require('./status');
exports.SurveyService = require('./survey');
exports.SyncService = require('./sync');
20 changes: 20 additions & 0 deletions src/services/survey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const {forEach, keys} = require('lodash');

const {SurveyAddressAdditionalAttributes, surveyAddressState} = require('../model');

class SurveyService {
static assign(surveyAddress, survey) {
surveyAddress.surveyAddressState = survey.closed ? surveyAddressState.RESOLVED : surveyAddressState.IN_PROGRESS;
surveyAddress.dwellings = survey.dwellings;
surveyAddress.valid = survey.valid;

forEach(
keys(SurveyAddressAdditionalAttributes),
key => {
surveyAddress[key] = survey[key];
}
);
}
}

module.exports = SurveyService;

0 comments on commit 3691142

Please sign in to comment.