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

123 lines
2.5 KiB
JavaScript
Raw Normal View History

2015-06-29 22:51:29 +00:00
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
2015-07-11 00:13:40 +00:00
pdfFiller = require('pdffiller'),
2015-06-29 22:51:29 +00:00
satelize = require('satelize'),
_ = require('lodash'),
config = require('../../config/config'),
path = require('path'),
Form = mongoose.model('Form'),
2015-06-30 17:36:51 +00:00
FieldSchema = require('./form_field.server.model.js'),
Field = mongoose.model('Field', FieldSchema),
2015-06-29 22:51:29 +00:00
fs = require('fs-extra');
/**
* Form Submission Schema
*/
var FormSubmissionSchema = new Schema({
title: {
type: String,
required: true,
},
created: {
type: Date,
default: Date.now
},
admin: {
type: Schema.Types.ObjectId,
ref: 'User',
},
2015-06-30 17:36:51 +00:00
form_fields: [{type: Schema.Types.Mixed}],
2015-06-29 22:51:29 +00:00
form: {
type: Schema.Types.ObjectId,
ref: 'Form',
required: true
},
ipAddr: {
type: String,
required: false
},
geoLocation: {
type: Schema.Types.Mixed,
},
pdfFilePath: {
type: Schema.Types.Mixed,
},
fdfData: {
type: Schema.Types.Mixed,
},
timeElapsed: { //time (in seconds) it took for user to submit form
type: Number,
},
});
2015-06-30 06:12:32 +00:00
//Check for IP Address of submitting person
// FormSubmissionSchema.pre('save', function (next){
// if(this.ipAddr){
// if(this.ipAddr.modified){
// satelize.satelize({ip: this.ipAddr}, function(err, geoData){
// if (err) next( new Error(err.message) );
2015-06-29 22:51:29 +00:00
2015-06-30 06:12:32 +00:00
// this.geoLocation = JSON.parse(geoData);
// next();
// });
// }
// }
// console.log('ipAddr check');
// next();
// });
2015-06-29 22:51:29 +00:00
2015-06-30 02:14:43 +00:00
//Generate autofilled PDF if flags are set
2015-06-29 22:51:29 +00:00
FormSubmissionSchema.pre('save', function (next) {
// debugger;
var fdfData, dest_filename, dest_path;
var that = this;
var _form = this.form;
2015-06-29 22:51:29 +00:00
// Form.findById(that.form, function(err, _form){
// if(err) next( new Error(err.mesasge) );
2015-06-29 22:51:29 +00:00
2015-07-02 04:54:46 +00:00
that.title = _form.title;
2015-06-30 02:14:43 +00:00
// console.log(_form);
2015-06-29 22:51:29 +00:00
2015-07-02 04:54:46 +00:00
if(_form.autofillPDFs){
2015-06-30 02:14:43 +00:00
2015-07-03 00:49:23 +00:00
dest_filename = _form.title.replace(/ /g,'')+'_submission_'+Date.now()+'.pdf';
2015-06-30 02:14:43 +00:00
dest_path = path.join(config.pdfUploadPath, dest_filename);
2015-06-29 22:51:29 +00:00
this.pdfFilePath = dest_path;
2015-06-30 02:14:43 +00:00
pdfFiller.fillForm(_form.pdf.path, dest_path, that.fdfData, function(err){
2015-06-30 02:45:25 +00:00
console.log('fdfData: \n');
2015-06-30 02:14:43 +00:00
console.log(that.fdfData);
2015-06-29 22:51:29 +00:00
2015-06-30 02:45:25 +00:00
// console.log('_form.pdf.path: '+_form.pdf.path);
// console.log('dest_path: '+dest_path);
2015-06-29 22:51:29 +00:00
2015-06-30 02:14:43 +00:00
if(err) {
2015-06-30 02:45:25 +00:00
console.log('\n err.message: '+err.message);
2015-06-30 02:14:43 +00:00
next( new Error(err.message) );
}
2015-07-03 00:49:23 +00:00
console.log('Field data from Form: '+_form.title.replace(/ /g,'')+' outputed to new PDF: '+dest_path);
2015-06-29 22:51:29 +00:00
next();
});
} else {
next();
}
// });
2015-06-29 22:51:29 +00:00
});
2015-07-11 00:13:40 +00:00
mongoose.model('FormSubmission', FormSubmissionSchema);