tellform/app/models/form_field.server.model.js

183 lines
3.8 KiB
JavaScript
Raw Normal View History

2015-06-29 22:51:29 +00:00
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
2016-06-01 01:06:45 +00:00
util = require('util'),
2017-10-11 05:07:13 +00:00
timeStampPlugin = require('../libs/timestamp.server.plugin'),
2015-09-18 16:32:17 +00:00
_ = require('lodash'),
2016-08-23 21:45:59 +00:00
Schema = mongoose.Schema,
2017-10-11 05:07:13 +00:00
LogicJumpSchema = require('./logic_jump.server.model'),
2017-11-01 19:17:35 +00:00
tokgen = require('../libs/tokenGenerator'),
constants = require('../libs/constants');
2017-07-29 00:04:16 +00:00
2015-10-06 20:14:38 +00:00
var FieldOptionSchema = new Schema({
option_id: {
2016-04-29 06:16:17 +00:00
type: Number
2015-10-06 20:14:38 +00:00
},
option_title: {
2016-04-29 06:16:17 +00:00
type: String
2015-10-06 20:14:38 +00:00
},
option_value: {
type: String,
2016-04-29 06:16:17 +00:00
trim: true
}
2015-10-06 20:14:38 +00:00
});
2016-06-01 01:06:45 +00:00
var RatingFieldSchema = new Schema({
steps: {
2016-06-01 01:16:24 +00:00
type: Number,
min: 1,
max: 10
2016-06-01 01:06:45 +00:00
},
shape: {
type: String,
2017-11-01 19:17:35 +00:00
enum: constants.ratingShapeTypes
2016-06-01 01:06:45 +00:00
},
validShapes: {
type: [String]
}
});
2015-06-29 22:51:29 +00:00
/**
2015-10-06 20:14:38 +00:00
* FormField Schema
2015-06-29 22:51:29 +00:00
*/
2016-06-01 01:06:45 +00:00
function BaseFieldSchema(){
Schema.apply(this, arguments);
2015-09-03 18:21:56 +00:00
2016-06-01 01:06:45 +00:00
this.add({
newOptionSchema: {
type: Boolean,
default: false
},
2017-07-29 00:04:16 +00:00
globalId: {
type: String,
},
2016-06-05 03:13:42 +00:00
isSubmission: {
type: Boolean,
default: false
},
submissionId: {
type: Schema.Types.ObjectId
},
2016-06-01 01:06:45 +00:00
title: {
type: String,
2017-10-07 07:27:03 +00:00
trim: true
2016-06-01 01:06:45 +00:00
},
description: {
type: String,
default: ''
},
2015-09-03 18:21:56 +00:00
2016-08-23 21:45:59 +00:00
logicJump: LogicJumpSchema,
2015-07-27 18:11:43 +00:00
2017-03-07 00:38:37 +00:00
ratingOptions: RatingFieldSchema,
2016-06-01 01:06:45 +00:00
fieldOptions: [FieldOptionSchema],
2016-06-01 01:06:45 +00:00
required: {
type: Boolean,
default: true
},
disabled: {
type: Boolean,
default: false
},
deletePreserved: {
type: Boolean,
default: false
},
validFieldTypes: {
type: [String]
},
fieldType: {
type: String,
2017-11-01 19:17:35 +00:00
enum: constants.fieldTypes
2016-06-01 01:06:45 +00:00
},
2017-11-14 05:06:30 +00:00
fieldValue: {
type: Schema.Types.Mixed,
default: ''
}
2016-06-01 01:06:45 +00:00
});
2017-10-11 05:07:13 +00:00
this.plugin(timeStampPlugin, {
2016-06-01 01:06:45 +00:00
createdPath: 'created',
modifiedPath: 'lastModified',
useVirtual: false
});
this.pre('save', function (next) {
this.validFieldTypes = mongoose.model('Field').schema.path('fieldType').enumValues;
if(this.fieldType === 'rating' && this.ratingOptions.validShapes.length === 0){
2017-11-01 19:17:35 +00:00
this.ratingOptions.validShapes = constants.ratingShapeTypes;
2016-06-01 01:06:45 +00:00
}
2016-06-05 03:13:42 +00:00
2016-06-01 01:06:45 +00:00
next();
});
}
util.inherits(BaseFieldSchema, Schema);
var FormFieldSchema = new BaseFieldSchema();
FormFieldSchema.pre('validate', function(next) {
var error = new mongoose.Error.ValidationError(this);
//If field is rating check that it has ratingOptions
if(this.fieldType !== 'rating'){
if(this.ratingOptions && this.ratingOptions.steps && this.ratingOptions.shape){
error.errors.ratingOptions = new mongoose.Error.ValidatorError({path: 'ratingOptions', message: 'ratingOptions is only allowed for type \'rating\' fields.', type: 'notvalid', value: this.ratingOptions});
2016-08-23 21:45:59 +00:00
console.error(error);
2016-06-01 01:06:45 +00:00
return(next(error));
}
2017-11-14 05:06:30 +00:00
} else {
2016-06-01 01:06:45 +00:00
//Setting default values for ratingOptions
2017-11-14 05:06:30 +00:00
if(!this.ratingOptions.steps) {
2016-06-01 01:06:45 +00:00
this.ratingOptions.steps = 10;
}
if(!this.ratingOptions.shape){
this.ratingOptions.shape = 'Star';
}
}
//If field is multiple choice check that it has field
if(this.fieldType !== 'dropdown' && this.fieldType !== 'radio' && this.fieldType !== 'checkbox'){
2016-08-23 21:45:59 +00:00
if(this.fieldOptions && this.fieldOptions.length > 0){
2016-06-01 01:06:45 +00:00
error.errors.ratingOptions = new mongoose.Error.ValidatorError({path:'fieldOptions', message: 'fieldOptions are only allowed for type dropdown, checkbox or radio fields.', type: 'notvalid', value: this.ratingOptions});
2016-08-23 21:45:59 +00:00
console.error(error);
2016-06-01 01:06:45 +00:00
return(next(error));
}
}
2016-06-05 03:13:42 +00:00
return next();
});
2016-08-23 21:45:59 +00:00
//LogicJump Save
2016-06-05 03:13:42 +00:00
FormFieldSchema.pre('save', function(next) {
2017-07-29 00:04:16 +00:00
if(!this.globalId){
2017-10-11 05:07:13 +00:00
this.globalId = tokgen();
2017-07-29 00:04:16 +00:00
}
2016-08-23 21:45:59 +00:00
next();
});
2016-06-05 03:13:42 +00:00
2016-08-23 21:45:59 +00:00
//Submission fieldValue correction
FormFieldSchema.pre('save', function(next) {
2016-06-07 00:37:09 +00:00
if(this.fieldType === 'dropdown' && this.isSubmission){
2016-06-05 03:13:42 +00:00
this.fieldValue = this.fieldValue.option_value;
}
2016-06-01 01:06:45 +00:00
return next();
2015-10-06 20:14:38 +00:00
});
2015-08-18 21:44:36 +00:00
2016-06-05 03:13:42 +00:00
2016-06-01 01:06:45 +00:00
var Field = mongoose.model('Field', FormFieldSchema);
var RatingOptions = mongoose.model('RatingOptions', RatingFieldSchema);
2015-09-18 16:32:17 +00:00
2015-10-06 20:14:38 +00:00
module.exports = FormFieldSchema;
2015-09-18 16:32:17 +00:00