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-10-06 20:24:02 +00:00
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 ( {
2017-11-12 05:44:23 +00:00
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 ] ,
2017-11-12 05:44:23 +00:00
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
} ,
fieldValue : Schema . Types . Mixed
} ) ;
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 ) ) ;
}
} else {
//Setting default values for ratingOptions
if ( ! this . ratingOptions . steps ) {
this . ratingOptions . steps = 10 ;
}
if ( ! this . ratingOptions . shape ) {
this . ratingOptions . shape = 'Star' ;
}
2015-06-29 22:51:29 +00:00
2016-06-01 01:06:45 +00:00
//Checking that the fieldValue is between 0 and ratingOptions.steps
if ( this . fieldValue + 0 > this . ratingOptions . steps || this . fieldValue + 0 < 0 ) {
this . fieldValue = 1 ;
}
}
2016-06-04 07:00:31 +00:00
//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