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

210 lines
4.3 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-11-01 00:32:37 +00:00
pdfFiller = require('pdffiller'),
freegeoip = require('node-freegeoip'),
2015-06-29 22:51:29 +00:00
_ = require('lodash'),
config = require('../../config/config'),
path = require('path'),
2015-07-27 18:11:43 +00:00
fs = require('fs-extra'),
2015-10-06 20:14:38 +00:00
mUtilities = require('mongoose-utilities'),
2015-09-10 22:06:28 +00:00
soap = require('soap'),
2015-10-06 20:14:38 +00:00
async = require('async'),
2015-09-18 16:32:17 +00:00
FieldSchema = require('./form_field.server.model.js'),
2015-09-10 22:06:28 +00:00
OscarSecurity = require('../../scripts/oscarhost/OscarSecurity');
2015-10-06 20:14:38 +00:00
2016-06-05 03:13:42 +00:00
var FieldSchema = require('./form_field.server.model.js');
2016-04-29 06:16:17 +00:00
var newDemoTemplate = {
2015-10-07 01:16:47 +00:00
address: '880-9650 Velit. St.',
city: '',
dateOfBirth: '10',
displayName: 'LITTLE, URIAH',
email: '',
firstName: 'Uriah F.',
hin: '',
lastName: 'Little',
lastUpdateDate: Date.now(),
monthOfBirth: '05',
officialLanguage: 'English',
phone: '250-',
phone2: '',
2015-11-06 17:41:55 +00:00
postal: 'S4M 7T8',
2015-10-07 01:16:47 +00:00
province: 'BC',
sex: 'F',
sexDesc: 'Female',
sin: '',
spokenLanguage: 'English',
title: 'MS.',
2016-04-29 06:16:17 +00:00
yearOfBirth: '2015'
2015-10-07 01:16:47 +00:00
};
2015-06-29 22:51:29 +00:00
2016-06-05 03:13:42 +00:00
// Setter function for form_fields
function formFieldsSetter(form_fields){
for(var i=0; i<form_fields.length; i++){
form_fields[i].isSubmission = true;
form_fields[i].submissionId = form_fields[i]._id;
form_fields[i]._id = new mongoose.mongo.ObjectID();
}
2016-06-07 00:37:09 +00:00
//console.log(form_fields)
2016-06-05 03:13:42 +00:00
return form_fields;
}
2016-04-29 06:16:17 +00:00
/**
2015-06-29 22:51:29 +00:00
* Form Submission Schema
*/
var FormSubmissionSchema = new Schema({
2015-10-06 20:14:38 +00:00
title: {
2015-07-27 18:11:43 +00:00
type: String
2015-06-29 22:51:29 +00:00
},
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
},
2016-06-05 03:13:42 +00:00
form_fields: [FieldSchema],
2016-04-29 06:16:17 +00:00
form: {
type: Schema.Types.ObjectId,
ref: 'Form',
2015-06-29 22:51:29 +00:00
required: true
},
ipAddr: {
2016-04-29 06:16:17 +00:00
type: String
2015-06-29 22:51:29 +00:00
},
geoLocation: {
Country: {
2016-04-29 06:16:17 +00:00
type: String
},
Region: {
2016-04-29 06:16:17 +00:00
type: String
},
City: {
2016-04-29 06:16:17 +00:00
type: String
}
2015-06-29 22:51:29 +00:00
},
device: {
type: {
2016-04-29 06:16:17 +00:00
type: String
},
name: {
2016-04-29 06:16:17 +00:00
type: String
}
},
2015-06-29 22:51:29 +00:00
pdfFilePath: {
2016-04-29 06:16:17 +00:00
type: Schema.Types.Mixed
2015-06-29 22:51:29 +00:00
},
2015-07-27 18:11:43 +00:00
pdf: {
2016-04-29 06:16:17 +00:00
type: Schema.Types.Mixed
2015-07-27 18:11:43 +00:00
},
2015-06-29 22:51:29 +00:00
fdfData: {
2016-04-29 06:16:17 +00:00
type: Schema.Types.Mixed
2015-06-29 22:51:29 +00:00
},
2016-04-29 06:16:17 +00:00
timeElapsed: {
2016-06-05 03:13:42 +00:00
type: Number
2016-04-29 06:16:17 +00:00
},
percentageComplete: {
2016-04-29 06:16:17 +00:00
type: Number
2015-09-10 22:06:28 +00:00
},
//TODO: DAVID: Need to not have this hardcoded
oscarDemoNum: {
2016-04-29 06:16:17 +00:00
type: Number
},
hasPlugins: {
oscarhost: {
type: Boolean,
2016-04-29 06:16:17 +00:00
default: false
}
}
2015-06-29 22:51:29 +00:00
});
2016-06-05 03:13:42 +00:00
FormSubmissionSchema.path('form_fields').set(formFieldsSetter);
2015-10-06 20:14:38 +00:00
FormSubmissionSchema.plugin(mUtilities.timestamp, {
createdPath: 'created',
modifiedPath: 'lastModified',
useVirtual: false
});
2015-09-10 22:06:28 +00:00
2015-10-06 20:14:38 +00:00
//Oscarhost API hook
FormSubmissionSchema.pre('save', function (next) {
2016-04-29 06:16:17 +00:00
2015-10-06 20:14:38 +00:00
var self = this;
if(this.hasPlugins.oscarhost){
mongoose.model('Form').findById(self.form, function(err, _form){
var form_ids = _.map(_.pluck(_form.form_fields, '_id'), function(id){ return ''+id;}),
submission_ids = _.pluck(self.form_fields, '_id');
// console.log('Form form_field ids\n--------');
// console.log(form_ids);
// console.log('FormSubmission [form_field ids]\n--------');
// console.log(submission_ids);
2016-04-29 06:16:17 +00:00
if(err) return next(err);
// console.log(_form);
// console.log('should push to api');
// console.log( (!this.oscarDemoNum && !!_form.plugins.oscarhost.baseUrl && !!_form.plugins.oscarhost.settings.fieldMap) );
2016-08-26 19:31:40 +00:00
return next();
});
}else{
2016-04-29 06:16:17 +00:00
return next();
}
2015-09-10 22:06:28 +00:00
});
2015-07-27 18:11:43 +00:00
2016-04-29 06:16:17 +00:00
//Check for IP Address of submitting person
2015-07-27 18:11:43 +00:00
FormSubmissionSchema.pre('save', function (next){
2015-11-12 22:24:26 +00:00
var self = this;
2015-07-27 18:11:43 +00:00
if(this.ipAddr){
if(this.isModified('ipAddr') || !this.geoLocation){
freegeoip.getLocation(this.ipAddr, function(err, location){
2016-04-29 06:16:17 +00:00
if(err) return next(err);
2016-07-05 22:52:05 +00:00
self.geoLocation = location
2016-04-29 06:16:17 +00:00
return next();
2015-07-27 18:11:43 +00:00
});
}
}
2016-04-29 06:16:17 +00:00
return next();
2015-07-27 18:11:43 +00:00
});
2015-06-29 22:51:29 +00:00
2016-04-29 06:16:17 +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,
2015-11-12 22:24:26 +00:00
self = this,
2015-07-27 18:11:43 +00:00
_form = this.form;
2016-04-29 06:16:17 +00:00
2015-07-27 18:36:44 +00:00
if(this.pdf && this.pdf.path){
2015-11-12 22:24:26 +00:00
dest_filename = self.title.replace(/ /g,'')+'_submission_'+Date.now()+'.pdf';
2015-07-27 18:11:43 +00:00
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-11-12 22:24:26 +00:00
self.pdfFilePath = dest_path;
2015-06-30 02:14:43 +00:00
2015-11-12 22:24:26 +00:00
pdfFiller.fillForm(self.pdf.path, dest_path, self.fdfData, function(err){
2015-07-27 18:11:43 +00:00
if(err) {
console.log('\n err.message: '+err.message);
2016-04-29 06:16:17 +00:00
return next( new Error(err.message) );
2015-07-27 18:11:43 +00:00
}
2015-11-12 22:24:26 +00:00
console.log('Field data from Form: '+self.title.replace(/ /g,'')+' outputed to new PDF: '+dest_path);
2016-04-29 06:16:17 +00:00
return next();
2015-07-27 18:11:43 +00:00
});
} else {
2016-04-29 06:16:17 +00:00
return next();
2015-07-27 18:11:43 +00:00
}
2015-06-29 22:51:29 +00:00
});
module.exports = FormSubmissionSchema;