added field concatenation logic to form.pre('save')

This commit is contained in:
David Baldwynn 2015-07-03 13:15:13 -07:00
parent fe574995b4
commit 74de814ac9
3 changed files with 47 additions and 21 deletions

View file

@ -189,7 +189,6 @@ exports.update = function(req, res) {
console.log('in form.update()'); console.log('in form.update()');
var form = req.form; var form = req.form;
// console.log(req.body.form);
form = _.extend(form, req.body.form); form = _.extend(form, req.body.form);
form.admin = req.user; form.admin = req.user;

View file

@ -38,7 +38,13 @@ var FormSchema = new Schema({
type: String, type: String,
default: '', default: '',
}, },
form_fields: [{type: Schema.Types.Mixed}], form_fields: {
type: [Schema.Types.Mixed],
set: function(form_fields) {
this._previousFormFields = this.form_fields;
return form_fields;
}
},
submissions: [{ submissions: [{
type: Schema.Types.ObjectId, type: Schema.Types.ObjectId,
@ -96,7 +102,7 @@ FormSchema.pre('save', function (next) {
fs.mkdirSync(newDestination); fs.mkdirSync(newDestination);
} }
if (stat && !stat.isDirectory()) { if (stat && !stat.isDirectory()) {
console.log('Directory cannot be created'); // console.log('Directory cannot be created');
next( new Error('Directory cannot be created because an inode of a different type exists at "' + newDestination + '"') ); next( new Error('Directory cannot be created because an inode of a different type exists at "' + newDestination + '"') );
}else{ }else{
next(); next();
@ -113,6 +119,20 @@ FormSchema.pre('save', function (next) {
next(); next();
}); });
//Concatenate submission and form's form_fields
FormSchema.pre('save', function (next) {
if(this.isModified('form_fields')){
if(this.submissions.length){
for(var i=0; i<this.submissions.length; i++){
submission = submissions[i];
submission.form_fields = submission.form_fields.concat(_.difference(this.form_fields, this._previousFormFields));
}
}
this.form_fields = this._previousFormFields.concat(_.difference(this.form_fields, this._previousFormFields));
}
next();
});
//Move PDF to permanent location after new template is uploaded //Move PDF to permanent location after new template is uploaded
FormSchema.pre('save', function (next) { FormSchema.pre('save', function (next) {
@ -121,7 +141,7 @@ FormSchema.pre('save', function (next) {
async.series([ async.series([
function(callback){ function(callback){
if(that.isModified('pdf')){ if(that.isModified('pdf')){
console.log('about to move PDF'); // console.log('about to move PDF');
var new_filename = that.title.replace(/ /g,'')+'_template.pdf'; var new_filename = that.title.replace(/ /g,'')+'_template.pdf';
@ -134,11 +154,11 @@ FormSchema.pre('save', function (next) {
fs.mkdirSync(newDestination); fs.mkdirSync(newDestination);
} }
if (stat && !stat.isDirectory()) { if (stat && !stat.isDirectory()) {
console.log('Directory '+newDestination+' cannot be created'); // console.log('Directory '+newDestination+' cannot be created');
callback( new Error('Directory cannot be created because an inode of a different type exists at "' + config.pdfUploadPath + '"') ); callback( new Error('Directory cannot be created because an inode of a different type exists at "' + config.pdfUploadPath + '"') );
} }
console.log('about to move PDF'); // console.log('about to move PDF');
fs.move(that.pdf.path, path.join(newDestination, new_filename), function (err) { fs.move(that.pdf.path, path.join(newDestination, new_filename), function (err) {
if (err) { if (err) {
@ -148,9 +168,9 @@ FormSchema.pre('save', function (next) {
that.pdf.path = path.join(newDestination, new_filename); that.pdf.path = path.join(newDestination, new_filename);
that.pdf.name = new_filename; that.pdf.name = new_filename;
console.log('\n\n PDF file:'+that.pdf.name+' successfully moved to: '+that.pdf.path); // console.log('\n\n PDF file:'+that.pdf.name+' successfully moved to: '+that.pdf.path);
callback(null,null); callback(null,'task1');
}); });
} }
callback(null,null); callback(null,null);
@ -169,8 +189,8 @@ FormSchema.pre('save', function (next) {
'Radio': 'radio' 'Radio': 'radio'
}; };
console.log('autogenerating form'); // console.log('autogenerating form');
console.log(that.pdf.path); // console.log(that.pdf.path);
pdfFiller.generateFieldJson(that.pdf.path, function(err, _form_fields){ pdfFiller.generateFieldJson(that.pdf.path, function(err, _form_fields){
if(err){ if(err){
@ -204,17 +224,18 @@ FormSchema.pre('save', function (next) {
_form_fields[i] = field; _form_fields[i] = field;
} }
console.log('NEW FORM_FIELDS: '); // console.log('NEW FORM_FIELDS: ');
console.log(_form_fields); // console.log(_form_fields);
console.log('\n\nOLD FORM_FIELDS: '); // console.log('\n\nOLD FORM_FIELDS: ');
console.log(that.form_fields); // console.log(that.form_fields);
that.form_fields = _form_fields; that.form_fields.concat(_form_fields);
callback(null, 'pdfFiller'); callback(null, 'task2');
}); });
} }else{
callback(null, that); callback(null, 'task2');
}
} }
], function(err, results) { ], function(err, results) {
if(err){ if(err){
@ -222,9 +243,9 @@ FormSchema.pre('save', function (next) {
message: err.message message: err.message
})); }));
} }
console.log(results); console.log('ending form save');
console.log(that.form_fields); next();
next(results[1]);
}); });
}else{ }else{
next(); next();

View file

@ -28,6 +28,9 @@
<table class="table table-striped table-hover table-condensed"> <table class="table table-striped table-hover table-condensed">
<thead> <thead>
<tr> <tr>
<th>
<input ng-checked="table.masterChecker" type="checkbox" ng-init="table.masterChecker = false"/>
</th>
<th>#</th> <th>#</th>
<th data-ng-repeat="(key, value) in submissions[0].form_fields"> <th data-ng-repeat="(key, value) in submissions[0].form_fields">
{{value.title}} {{value.title}}
@ -45,6 +48,9 @@
</thead> </thead>
<tbody> <tbody>
<tr data-ng-repeat="submission in submissions"> <tr data-ng-repeat="submission in submissions">
<td>
<input ng-checked="table.rows[$index].selected" type="checkbox" ng-init="table.masterChecker = false"/>
</td>
<th class="scope">{{$index+1}}</th> <th class="scope">{{$index+1}}</th>
<td data-ng-repeat="(key, value) in submission.form_fields"> <td data-ng-repeat="(key, value) in submission.form_fields">
{{value.fieldValue}} {{value.fieldValue}}