2015-06-29 15:51:29 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
var mongoose = require('mongoose'),
|
|
|
|
Schema = mongoose.Schema,
|
2015-10-31 17:32:37 -07:00
|
|
|
pdfFiller = require('pdffiller'),
|
2015-06-29 15:51:29 -07:00
|
|
|
_ = require('lodash'),
|
|
|
|
config = require('../../config/config'),
|
|
|
|
path = require('path'),
|
2015-10-06 13:14:38 -07:00
|
|
|
mUtilities = require('mongoose-utilities'),
|
2015-06-30 07:21:53 -07:00
|
|
|
fs = require('fs-extra'),
|
2015-07-27 11:11:43 -07:00
|
|
|
async = require('async'),
|
2015-08-04 14:06:16 -07:00
|
|
|
util = require('util');
|
|
|
|
|
|
|
|
//Mongoose Models
|
2015-09-18 09:32:17 -07:00
|
|
|
var FieldSchema = require('./form_field.server.model.js');
|
|
|
|
var Field = mongoose.model('Field');
|
|
|
|
|
2015-09-10 15:06:28 -07:00
|
|
|
var FormSubmissionSchema = require('./form_submission.server.model.js'),
|
|
|
|
FormSubmission = mongoose.model('FormSubmission', FormSubmissionSchema);
|
2015-06-29 15:51:29 -07:00
|
|
|
|
2015-09-18 09:32:17 -07:00
|
|
|
|
2015-08-18 14:44:36 -07:00
|
|
|
var ButtonSchema = new Schema({
|
|
|
|
url: {
|
|
|
|
type: String,
|
|
|
|
match: [/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/],
|
|
|
|
},
|
|
|
|
action: String,
|
|
|
|
text: String,
|
|
|
|
bgColor: {
|
|
|
|
type: String,
|
|
|
|
match: [/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/],
|
|
|
|
default: '#5bc0de'
|
|
|
|
},
|
|
|
|
color: {
|
|
|
|
type: String,
|
|
|
|
match: [/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/],
|
|
|
|
default: '#ffffff'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-06-29 15:51:29 -07:00
|
|
|
/**
|
|
|
|
* Form Schema
|
|
|
|
*/
|
2015-09-10 15:06:28 -07:00
|
|
|
var FormSchema = new Schema({
|
2015-06-29 15:51:29 -07:00
|
|
|
title: {
|
|
|
|
type: String,
|
|
|
|
trim: true,
|
2015-07-28 15:29:07 -07:00
|
|
|
required: 'Form Title cannot be blank',
|
2015-06-29 15:51:29 -07:00
|
|
|
},
|
2015-07-06 18:21:43 -07:00
|
|
|
language: {
|
|
|
|
type: String,
|
|
|
|
enum: ['english', 'french', 'spanish'],
|
2015-07-13 12:00:10 -07:00
|
|
|
required: 'Form must have a language',
|
|
|
|
default: 'english'
|
2015-07-06 18:21:43 -07:00
|
|
|
},
|
2015-06-29 15:51:29 -07:00
|
|
|
description: {
|
|
|
|
type: String,
|
|
|
|
default: '',
|
|
|
|
},
|
2015-10-06 18:16:47 -07:00
|
|
|
form_fields: {
|
|
|
|
type: [FieldSchema],
|
|
|
|
},
|
2015-06-29 15:51:29 -07:00
|
|
|
|
2015-06-29 19:14:43 -07:00
|
|
|
submissions: [{
|
2015-06-29 15:51:29 -07:00
|
|
|
type: Schema.Types.ObjectId,
|
|
|
|
ref: 'FormSubmission'
|
|
|
|
}],
|
|
|
|
|
|
|
|
admin: {
|
|
|
|
type: Schema.Types.ObjectId,
|
2015-07-21 16:25:45 -07:00
|
|
|
ref: 'User',
|
|
|
|
required: 'Form must have an Admin'
|
2015-06-29 15:51:29 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
pdf: {
|
|
|
|
type: Schema.Types.Mixed
|
|
|
|
},
|
|
|
|
pdfFieldMap: {
|
|
|
|
type: Schema.Types.Mixed
|
|
|
|
},
|
2015-07-21 16:25:45 -07:00
|
|
|
|
2015-08-07 14:02:44 -07:00
|
|
|
startPage: {
|
|
|
|
showStart:{
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
2015-08-18 14:44:36 -07:00
|
|
|
introTitle:{
|
2015-08-07 14:02:44 -07:00
|
|
|
type: String,
|
2015-08-18 14:44:36 -07:00
|
|
|
default: 'Welcome to Form'
|
2015-08-07 14:02:44 -07:00
|
|
|
},
|
2015-08-18 14:44:36 -07:00
|
|
|
introParagraph:{
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
buttons:[ButtonSchema]
|
2015-08-05 22:52:59 -07:00
|
|
|
},
|
2015-08-18 14:44:36 -07:00
|
|
|
|
2015-06-30 04:05:44 -07:00
|
|
|
hideFooter: {
|
|
|
|
type: Boolean,
|
2015-07-06 18:21:43 -07:00
|
|
|
default: false,
|
2015-06-30 04:05:44 -07:00
|
|
|
},
|
2015-06-29 15:51:29 -07:00
|
|
|
isGenerated: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
2015-07-01 19:49:35 -07:00
|
|
|
isLive: {
|
|
|
|
type: Boolean,
|
2015-07-06 18:21:43 -07:00
|
|
|
default: false,
|
2015-07-01 19:49:35 -07:00
|
|
|
},
|
2015-06-29 15:51:29 -07:00
|
|
|
autofillPDFs: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
2015-08-07 14:02:44 -07:00
|
|
|
|
2015-07-29 17:22:53 -07:00
|
|
|
design: {
|
|
|
|
colors:{
|
2015-08-07 15:30:41 -07:00
|
|
|
backgroundColor: {
|
|
|
|
type: String,
|
2015-10-30 14:40:22 -07:00
|
|
|
match: [/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/],
|
|
|
|
default: '#fff'
|
2015-08-07 15:30:41 -07:00
|
|
|
},
|
|
|
|
questionColor: {
|
|
|
|
type: String,
|
2015-10-30 14:40:22 -07:00
|
|
|
match: [/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/],
|
|
|
|
default: '#333',
|
2015-08-07 15:30:41 -07:00
|
|
|
},
|
|
|
|
answerColor: {
|
|
|
|
type: String,
|
2015-10-30 14:40:22 -07:00
|
|
|
match: [/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/],
|
|
|
|
default: '#333',
|
2015-08-07 15:30:41 -07:00
|
|
|
},
|
|
|
|
buttonColor: {
|
|
|
|
type: String,
|
|
|
|
match: [/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/]
|
|
|
|
},
|
2015-07-29 17:22:53 -07:00
|
|
|
},
|
|
|
|
font: String,
|
|
|
|
backgroundImage: { type: Schema.Types.Mixed }
|
2015-09-10 15:06:28 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
plugins: {
|
|
|
|
oscarhost: {
|
|
|
|
baseUrl: {
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
settings: {
|
|
|
|
lookupField: {
|
|
|
|
type: Schema.Types.ObjectId,
|
|
|
|
ref: 'Field'
|
|
|
|
},
|
|
|
|
updateType: {
|
|
|
|
type: String,
|
|
|
|
enum: ['upsert', 'force_add', 'force_update', 'fetch'],
|
|
|
|
},
|
|
|
|
fieldMap: {
|
|
|
|
type: Schema.Types.Mixed,
|
2015-10-30 11:40:02 -07:00
|
|
|
},
|
|
|
|
validUpdateTypes: {
|
|
|
|
type: [String]
|
|
|
|
},
|
|
|
|
validFields : {
|
|
|
|
type: [String],
|
|
|
|
default: [
|
|
|
|
'address',
|
|
|
|
'city',
|
|
|
|
'email',
|
|
|
|
'firstName',
|
|
|
|
'hin',
|
|
|
|
'lastName',
|
|
|
|
'phone',
|
|
|
|
'postal',
|
|
|
|
'province',
|
|
|
|
'sex',
|
|
|
|
'spokenLanguage',
|
|
|
|
'title',
|
|
|
|
'DOB']
|
2015-09-10 15:06:28 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
auth: {
|
|
|
|
user: {
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
pass: {
|
|
|
|
type: String,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-28 15:29:07 -07:00
|
|
|
}
|
2015-06-29 15:51:29 -07:00
|
|
|
});
|
|
|
|
|
2015-10-06 13:14:38 -07:00
|
|
|
FormSchema.plugin(mUtilities.timestamp, {
|
|
|
|
createdPath: 'created',
|
|
|
|
modifiedPath: 'lastModified',
|
|
|
|
useVirtual: false
|
|
|
|
});
|
2015-11-23 12:12:10 -08:00
|
|
|
FormSchema.pre('init', function (next) {
|
2015-10-30 11:40:02 -07:00
|
|
|
var validUpdateTypes= mongoose.model('Form').schema.path('plugins.oscarhost.settings.updateType').enumValues;
|
|
|
|
this.plugins.oscarhost.settings.validUpdateTypes = validUpdateTypes;
|
2015-10-06 13:14:38 -07:00
|
|
|
|
2015-11-23 12:12:10 -08:00
|
|
|
// this.plugins.oscarhost.settings.validFields = [
|
|
|
|
// 'address',
|
|
|
|
// 'city',
|
|
|
|
// 'email',
|
|
|
|
// 'firstName',
|
|
|
|
// 'hin',
|
|
|
|
// 'lastName',
|
|
|
|
// 'phone',
|
|
|
|
// 'postal',
|
|
|
|
// 'province',
|
|
|
|
// 'sex',
|
|
|
|
// 'spokenLanguage',
|
|
|
|
// 'title',
|
|
|
|
// 'DOB'];
|
2015-10-30 13:14:48 -07:00
|
|
|
next();
|
2015-10-30 11:40:02 -07:00
|
|
|
});
|
2015-07-02 17:49:23 -07:00
|
|
|
//Delete template PDF of current Form
|
2015-09-10 15:06:28 -07:00
|
|
|
FormSchema.pre('remove', function (next) {
|
2015-07-29 17:32:46 -07:00
|
|
|
if(this.pdf && process.env.NODE_ENV === 'development'){
|
2015-07-02 17:49:23 -07:00
|
|
|
//Delete template form
|
|
|
|
fs.unlink(this.pdf.path, function(err){
|
|
|
|
if (err) throw err;
|
|
|
|
console.log('successfully deleted', this.pdf.path);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-08-14 10:24:14 -07:00
|
|
|
|
2015-07-28 15:29:07 -07:00
|
|
|
var _original;
|
2015-07-02 17:49:23 -07:00
|
|
|
|
2015-07-27 11:11:43 -07:00
|
|
|
//Set _original
|
2015-09-10 15:06:28 -07:00
|
|
|
FormSchema.pre('save', function (next) {
|
2015-09-18 09:32:17 -07:00
|
|
|
|
|
|
|
this.constructor
|
2015-07-28 15:29:07 -07:00
|
|
|
.findOne({_id: this._id}).exec(function(err, original){
|
|
|
|
if(err) {
|
|
|
|
console.log(err);
|
|
|
|
next(err);
|
|
|
|
} else {
|
2015-07-27 11:11:43 -07:00
|
|
|
_original = original;
|
2015-09-18 09:32:17 -07:00
|
|
|
//console.log('_original');
|
2015-07-28 15:29:07 -07:00
|
|
|
// console.log(_original);
|
2015-07-27 11:11:43 -07:00
|
|
|
next();
|
|
|
|
}
|
2015-07-28 15:29:07 -07:00
|
|
|
});
|
2015-07-27 11:11:43 -07:00
|
|
|
});
|
|
|
|
|
2015-07-21 16:25:45 -07:00
|
|
|
function getDeletedIndexes(needle, haystack){
|
|
|
|
var deletedIndexes = [];
|
|
|
|
|
|
|
|
if(haystack.length > 0){
|
|
|
|
for(var i = 0; i < needle.length; i++){
|
2015-07-27 11:11:43 -07:00
|
|
|
if(haystack.indexOf(needle[i]) === -1){
|
2015-07-21 16:25:45 -07:00
|
|
|
deletedIndexes.push(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return deletedIndexes;
|
|
|
|
}
|
|
|
|
|
2015-07-01 20:50:57 -07:00
|
|
|
//Move PDF to permanent location after new template is uploaded
|
2015-09-10 15:06:28 -07:00
|
|
|
FormSchema.pre('save', function (next) {
|
2015-06-29 15:51:29 -07:00
|
|
|
if(this.pdf){
|
2015-07-02 17:49:23 -07:00
|
|
|
var that = this;
|
|
|
|
async.series([
|
|
|
|
function(callback){
|
2015-07-27 11:11:43 -07:00
|
|
|
if(that.isModified('pdf') && that.pdf.path){
|
2015-07-02 17:49:23 -07:00
|
|
|
|
|
|
|
var new_filename = that.title.replace(/ /g,'')+'_template.pdf';
|
|
|
|
|
|
|
|
var newDestination = path.join(config.pdfUploadPath, that.admin.username.replace(/ /g,''), that.title.replace(/ /g,'')),
|
|
|
|
stat = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
stat = fs.statSync(newDestination);
|
|
|
|
} catch (err) {
|
|
|
|
fs.mkdirSync(newDestination);
|
|
|
|
}
|
|
|
|
if (stat && !stat.isDirectory()) {
|
2015-07-27 11:11:43 -07:00
|
|
|
return callback( new Error('Directory cannot be created because an inode of a different type exists at "' + config.pdfUploadPath + '"'), null);
|
2015-07-02 17:49:23 -07:00
|
|
|
}
|
|
|
|
|
2015-07-27 11:11:43 -07:00
|
|
|
var old_path = that.pdf.path;
|
|
|
|
fs.move(old_path, path.join(newDestination, new_filename), {clobber: true}, function (err) {
|
2015-07-02 17:49:23 -07:00
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
2015-07-27 11:11:43 -07:00
|
|
|
callback( new Error(err.message), 'task1');
|
|
|
|
}else {
|
|
|
|
that.pdf.path = path.join(newDestination, new_filename);
|
|
|
|
that.pdf.name = new_filename;
|
2015-07-02 17:49:23 -07:00
|
|
|
|
2015-07-27 11:11:43 -07:00
|
|
|
callback(null,'task1');
|
|
|
|
}
|
2015-07-02 17:49:23 -07:00
|
|
|
});
|
2015-07-27 11:11:43 -07:00
|
|
|
}else {
|
|
|
|
callback(null,'task1');
|
2015-07-02 17:49:23 -07:00
|
|
|
}
|
2015-07-27 11:11:43 -07:00
|
|
|
|
2015-07-02 17:49:23 -07:00
|
|
|
},
|
|
|
|
function(callback){
|
|
|
|
if(that.isGenerated){
|
|
|
|
that.pdf.path = path.join(config.pdfUploadPath, that.admin.username.replace(/ /g,''), that.title.replace(/ /g,''), that.title.replace(/ /g,'')+'_template.pdf');
|
|
|
|
that.pdf.name = that.title.replace(/ /g,'')+'_template.pdf';
|
|
|
|
var _typeConvMap = {
|
|
|
|
'Multiline': 'textarea',
|
|
|
|
'Text': 'textfield',
|
|
|
|
'Button': 'checkbox',
|
|
|
|
'Choice': 'radio',
|
|
|
|
'Password': 'password',
|
|
|
|
'FileSelect': 'filefield',
|
|
|
|
'Radio': 'radio'
|
|
|
|
};
|
|
|
|
|
2015-11-05 14:07:56 -08:00
|
|
|
// console.log('autogenerating form');
|
2015-07-03 13:15:13 -07:00
|
|
|
// console.log(that.pdf.path);
|
2015-07-02 17:49:23 -07:00
|
|
|
|
2015-07-03 12:25:02 -07:00
|
|
|
pdfFiller.generateFieldJson(that.pdf.path, function(err, _form_fields){
|
|
|
|
if(err){
|
2015-07-21 16:25:45 -07:00
|
|
|
callback( new Error(err.message), null);
|
|
|
|
}else if(!_form_fields.length || _form_fields === undefined || _form_fields === null){
|
|
|
|
callback( new Error('Generated formfields is empty'), null);
|
2015-07-03 12:25:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//Map PDF field names to FormField field names
|
|
|
|
for(var i = 0; i < _form_fields.length; i++){
|
|
|
|
var field = _form_fields[i];
|
|
|
|
|
|
|
|
//Convert types from FDF to 'FormField' types
|
|
|
|
if(_typeConvMap[ field.fieldType+'' ]){
|
|
|
|
field.fieldType = _typeConvMap[ field.fieldType+'' ];
|
2015-07-02 17:49:23 -07:00
|
|
|
}
|
|
|
|
|
2015-10-06 13:14:38 -07:00
|
|
|
// field = new Field(field);
|
2015-07-21 16:25:45 -07:00
|
|
|
field.required = false;
|
2015-07-03 12:25:02 -07:00
|
|
|
_form_fields[i] = field;
|
|
|
|
}
|
|
|
|
|
2015-07-03 13:15:13 -07:00
|
|
|
// console.log('NEW FORM_FIELDS: ');
|
|
|
|
// console.log(_form_fields);
|
2015-07-02 17:49:23 -07:00
|
|
|
|
2015-07-27 11:11:43 -07:00
|
|
|
that.form_fields = that.form_fields.concat(_form_fields);
|
|
|
|
|
2015-07-03 13:15:13 -07:00
|
|
|
// console.log('\n\nOLD FORM_FIELDS: ');
|
|
|
|
// console.log(that.form_fields);
|
2015-07-27 11:11:43 -07:00
|
|
|
that.isGenerated = false;
|
2015-07-03 13:15:13 -07:00
|
|
|
callback(null, 'task2');
|
2015-07-03 12:25:02 -07:00
|
|
|
});
|
2015-07-03 13:15:13 -07:00
|
|
|
}else{
|
|
|
|
callback(null, 'task2');
|
|
|
|
}
|
2015-07-02 17:49:23 -07:00
|
|
|
}
|
|
|
|
], function(err, results) {
|
|
|
|
if(err){
|
|
|
|
next(new Error({
|
|
|
|
message: err.message
|
|
|
|
}));
|
|
|
|
}
|
2015-07-03 13:15:13 -07:00
|
|
|
console.log('ending form save');
|
|
|
|
next();
|
|
|
|
|
2015-07-01 19:49:35 -07:00
|
|
|
});
|
2015-11-23 12:12:10 -08:00
|
|
|
}else if(_original.hasOwnProperty('pdf')){
|
|
|
|
fs.remove(_original.pdf.path, function (err) {
|
|
|
|
if(err) next(err);
|
|
|
|
console.log('file at '+_original.pdf.path+' successfully deleted');
|
|
|
|
next();
|
|
|
|
});
|
2015-07-01 19:49:35 -07:00
|
|
|
}
|
2015-07-28 15:29:07 -07:00
|
|
|
next();
|
2015-07-01 19:49:35 -07:00
|
|
|
});
|
|
|
|
|
2015-09-10 15:06:28 -07:00
|
|
|
FormSchema.pre('save', function (next) {
|
2015-11-05 14:07:56 -08:00
|
|
|
|
2015-07-28 15:29:07 -07:00
|
|
|
// console.log('_original\n------------');
|
2015-07-27 11:11:43 -07:00
|
|
|
// console.log(_original);
|
2015-10-06 13:14:38 -07:00
|
|
|
//console.log('field has been deleted: ');
|
|
|
|
//console.log(this.isModified('form_fields') && !!this.form_fields && !!_original);
|
2015-07-27 11:36:44 -07:00
|
|
|
|
2015-07-28 15:29:07 -07:00
|
|
|
if(this.isModified('form_fields') && this.form_fields.length >= 0 && _original){
|
2015-07-27 11:11:43 -07:00
|
|
|
|
|
|
|
var old_form_fields = _original.form_fields,
|
|
|
|
new_ids = _.map(_.pluck(this.form_fields, '_id'), function(id){ return ''+id;}),
|
|
|
|
old_ids = _.map(_.pluck(old_form_fields, '_id'), function(id){ return ''+id;}),
|
|
|
|
deletedIds = getDeletedIndexes(old_ids, new_ids),
|
|
|
|
that = this;
|
|
|
|
|
2015-10-31 19:37:24 -07:00
|
|
|
// console.log('deletedId Indexes\n--------');
|
|
|
|
// console.log(deletedIds);
|
|
|
|
// console.log('old_ids\n--------');
|
|
|
|
// console.log(old_ids);
|
|
|
|
// console.log('new_ids\n--------');
|
|
|
|
// console.log(new_ids);
|
2015-07-27 11:11:43 -07:00
|
|
|
|
|
|
|
//Preserve fields that have at least one submission
|
|
|
|
if( deletedIds.length > 0 ){
|
|
|
|
|
|
|
|
var modifiedSubmissions = [];
|
|
|
|
|
|
|
|
async.forEachOfSeries(deletedIds,
|
|
|
|
function (deletedIdIndex, key, callback) {
|
|
|
|
|
|
|
|
var deleted_id = old_ids[deletedIdIndex];
|
|
|
|
|
2015-10-06 13:14:38 -07:00
|
|
|
//Find FormSubmissions that contain field with _id equal to 'deleted_id'
|
2015-07-27 11:11:43 -07:00
|
|
|
FormSubmission.
|
2015-10-06 13:14:38 -07:00
|
|
|
find({ form: that._id, admin: that.admin, form_fields: {$elemMatch: {_id: deleted_id} } }).
|
2015-07-27 11:11:43 -07:00
|
|
|
exec(function(err, submissions){
|
|
|
|
if(err){
|
|
|
|
console.error(err);
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2015-10-06 13:14:38 -07:00
|
|
|
//Delete field if there are no submission(s) found
|
|
|
|
if(submissions.length) {
|
2015-10-31 19:37:24 -07:00
|
|
|
// console.log('adding submissions');
|
|
|
|
// console.log(submissions);
|
2015-10-06 13:14:38 -07:00
|
|
|
//Add submissions
|
|
|
|
modifiedSubmissions.push.apply(modifiedSubmissions, submissions);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
});
|
2015-07-27 11:11:43 -07:00
|
|
|
// }
|
|
|
|
},
|
|
|
|
function (err) {
|
|
|
|
if(err){
|
|
|
|
console.error(err.message);
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// console.log('modifiedSubmissions\n---------\n\n');
|
|
|
|
// console.log(modifiedSubmissions);
|
|
|
|
|
2015-10-06 13:14:38 -07:00
|
|
|
//Iterate through all submissions with modified form_fields
|
2015-07-27 11:11:43 -07:00
|
|
|
async.forEachOfSeries(modifiedSubmissions, function (submission, key, callback) {
|
|
|
|
|
2015-10-06 13:14:38 -07:00
|
|
|
//Iterate through ids of deleted fields
|
2015-07-27 11:11:43 -07:00
|
|
|
for(var i = 0; i < deletedIds.length; i++){
|
|
|
|
|
2015-10-06 13:14:38 -07:00
|
|
|
//Get index of deleted field
|
2015-07-27 11:11:43 -07:00
|
|
|
var index = _.findIndex(submission.form_fields, function(field) {
|
|
|
|
var tmp_id = field._id+'';
|
|
|
|
return tmp_id === old_ids[ deletedIds[i] ];
|
|
|
|
});
|
|
|
|
|
2015-10-06 13:14:38 -07:00
|
|
|
var deletedField = submission.form_fields[index];
|
2015-07-27 11:11:43 -07:00
|
|
|
|
2015-10-06 13:14:38 -07:00
|
|
|
//Hide field if it exists
|
|
|
|
if(deletedField){
|
2015-10-31 19:37:24 -07:00
|
|
|
// console.log('deletedField\n-------\n\n');
|
|
|
|
// console.log(deletedField);
|
2015-07-27 11:11:43 -07:00
|
|
|
//Delete old form_field
|
|
|
|
submission.form_fields.splice(index, 1);
|
|
|
|
|
2015-10-06 13:14:38 -07:00
|
|
|
deletedField.deletePreserved = true;
|
2015-07-27 11:11:43 -07:00
|
|
|
|
2015-10-06 13:14:38 -07:00
|
|
|
//Move deleted form_field to start
|
|
|
|
submission.form_fields.unshift(deletedField);
|
|
|
|
that.form_fields.unshift(deletedField);
|
2015-07-27 11:11:43 -07:00
|
|
|
// console.log('form.form_fields\n--------\n\n');
|
|
|
|
// console.log(that.form_fields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
submission.save(function (err) {
|
|
|
|
if(err) callback(err);
|
|
|
|
else callback(null);
|
|
|
|
});
|
|
|
|
}, function (err) {
|
|
|
|
if(err){
|
|
|
|
console.error(err.message);
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
// console.log('form.form_fields\n--------\n\n');
|
|
|
|
// console.log(that.form_fields);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
2015-06-29 15:51:29 -07:00
|
|
|
|
|
|
|
|
2015-09-10 15:06:28 -07:00
|
|
|
mongoose.model('Form', FormSchema);
|
2015-06-29 15:51:29 -07:00
|
|
|
|