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

121 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-28 23:26:32 +00:00
pdfFiller = require('node-pdffiller'),
2015-06-29 22:51:29 +00:00
satelize = require('satelize'),
_ = require('lodash'),
config = require('../../config/config'),
path = require('path'),
2015-07-27 18:11:43 +00:00
fs = require('fs-extra'),
FieldSchema = require('./form_field.server.model.js');
2015-06-29 22:51:29 +00:00
/**
* Form Submission Schema
*/
var FormSubmissionSchema = new Schema({
2015-07-27 18:11:43 +00:00
title:{
type: String
2015-06-29 22:51:29 +00:00
},
created: {
type: Date,
default: Date.now
},
admin: {
type: Schema.Types.ObjectId,
ref: 'User',
2015-07-27 18:11:43 +00:00
required: true
2015-06-29 22:51:29 +00:00
},
2015-07-27 18:11:43 +00:00
form_fields: [Schema.Types.Mixed],//[FieldSchema],
form: {
type:Schema.Types.ObjectId,
ref:'Form',
2015-06-29 22:51:29 +00:00
required: true
},
ipAddr: {
type: String,
},
geoLocation: {
type: Schema.Types.Mixed,
},
pdfFilePath: {
type: Schema.Types.Mixed,
},
2015-07-27 18:11:43 +00:00
pdf: {
type: Schema.Types.Mixed,
},
2015-06-29 22:51:29 +00:00
fdfData: {
type: Schema.Types.Mixed,
},
timeElapsed: { //time (in seconds) it took for user to submit form
type: Number,
},
});
2015-07-27 18:11:43 +00:00
//Mongoose Relationship initialization
// FormSubmissionSchema.plugin(relationship, { relationshipPathName:'form' });
2015-06-30 06:12:32 +00:00
//Check for IP Address of submitting person
2015-07-27 18:11:43 +00:00
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) );
this.geoLocation = JSON.parse(geoData);
next();
});
}
}
2015-07-27 18:36:44 +00:00
// console.log('ipAddr check');
2015-07-27 18:11:43 +00:00
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) {
2015-07-27 18:11:43 +00:00
var fdfData, dest_filename, dest_path,
that = this,
_form = this.form;
2015-07-27 18:36:44 +00:00
2015-06-29 22:51:29 +00:00
2015-07-27 18:36:44 +00:00
if(this.pdf && this.pdf.path){
console.log(this.pdf);
2015-07-27 18:11:43 +00:00
dest_filename = that.title.replace(/ /g,'')+'_submission_'+Date.now()+'.pdf';
var __path = this.pdf.path.split('/').slice(0,this.pdf.path.split('/').length-1).join('/');
dest_path = path.join(__path, dest_filename);
2015-06-29 22:51:29 +00:00
2015-07-27 18:11:43 +00:00
that.pdfFilePath = dest_path;
2015-06-30 02:14:43 +00:00
2015-07-27 18:11:43 +00:00
pdfFiller.fillForm(that.pdf.path, dest_path, that.fdfData, function(err){
console.log('fdfData: \n');
console.log(that.fdfData);
2015-06-29 22:51:29 +00:00
2015-07-27 18:11:43 +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-07-27 18:11:43 +00:00
if(err) {
console.log('\n err.message: '+err.message);
next( new Error(err.message) );
}
console.log('Field data from Form: '+that.title.replace(/ /g,'')+' outputed to new PDF: '+dest_path);
2015-06-29 22:51:29 +00:00
next();
2015-07-27 18:11:43 +00:00
});
} else {
next();
}
2015-06-29 22:51:29 +00:00
});
module.exports = FormSubmissionSchema;
// mongoose.model('FormSubmission', FormSubmissionSchema);