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

258 lines
6.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-11-01 00:32:37 +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'),
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
2015-10-07 01:16:47 +00:00
var newDemoTemplate = {
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.',
yearOfBirth: '2015'
};
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
},
2015-10-06 20:14:38 +00:00
form_fields: {
type: [Schema.Types.Mixed],
},
2015-07-27 18:11:43 +00:00
form: {
2015-10-06 20:14:38 +00:00
type: Schema.Types.ObjectId,
ref: 'Form',
2015-06-29 22:51:29 +00:00
required: true
},
ipAddr: {
type: String,
},
geoLocation: {
type: Schema.Types.Mixed,
},
device: {
type: {
type: String,
},
name: {
type: String,
}
},
2015-06-29 22:51:29 +00:00
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: {
2015-06-29 22:51:29 +00:00
type: Number,
},
percentageComplete: {
type: Number,
2015-09-10 22:06:28 +00:00
},
//TODO: DAVID: Need to not have this hardcoded
oscarDemoNum: {
type: Number,
}
2015-06-29 22:51:29 +00:00
});
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) {
var self = this;
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');
2015-10-07 01:16:47 +00:00
// console.log('Form form_field ids\n--------');
// console.log(form_ids);
// console.log('FormSubmission [form_field ids]\n--------');
// console.log(submission_ids);
2015-10-06 20:14:38 +00:00
if(err) next(err);
// console.log(_form);
// console.log('should push to api');
// console.log( (!this.oscarDemoNum && !!_form.plugins.oscarhost.baseUrl && !!_form.plugins.oscarhost.settings.fieldMap) );
if(!this.oscarDemoNum && _form.plugins.oscarhost.baseUrl && _form.plugins.oscarhost.settings.fieldMap){
console.log('OSCARHOST API HOOK');
var url_login = _form.plugins.oscarhost.baseUrl+'/LoginService?wsdl',
url_demo = _form.plugins.oscarhost.baseUrl+'/DemographicService?wsdl';
var args_login = {arg0: config.oscarhost.auth.user, arg1: config.oscarhost.auth.pass};
var options = {
ignoredNamespaces: {
namespaces: ['targetNamespace', 'typedNamespace'],
override: true
}
};
2015-10-07 01:28:11 +00:00
// console.log(self.form_fields);
2015-10-06 20:14:38 +00:00
//Generate demographics from hashmap
var generateDemo = function(formFields, conversionMap, demographicsTemplate){
2015-10-07 01:16:47 +00:00
console.log('generating Demo fields');
console.log(conversionMap);
var _generatedDemo = {}, currField, propertyName;
for(var y=0; y<formFields.length; y++){
currField = formFields[y];
propertyName = conversionMap[currField._id];
if(demographicsTemplate.hasOwnProperty(conversionMap[currField._id])){
_generatedDemo[propertyName] = currField.fieldValue+'';
2015-10-30 18:40:02 +00:00
}else if(propertyName === 'DOB'){
2015-10-07 01:16:47 +00:00
var date = new Date(currField.fieldValue);
_generatedDemo.dateOfBirth = date.getDate()+'';
_generatedDemo.yearOfBirth = date.getFullYear()+'';
_generatedDemo.monthOfBirth = date.getMonth()+'';
2015-10-06 20:14:38 +00:00
}
}
2015-10-30 20:14:48 +00:00
var currDate = new Date();
var dateString = currDate.toISOString().split('T')[0] + ' ' + currDate.toISOString().split('T')[1].slice(0,8);
_generatedDemo.lastUpdateDate = currDate.toISOString();
2015-10-06 20:14:38 +00:00
return _generatedDemo;
};
2015-10-07 01:16:47 +00:00
2015-10-06 20:14:38 +00:00
var submissionDemographic = generateDemo(self.form_fields, _form.plugins.oscarhost.settings.fieldMap, newDemoTemplate);
2015-10-07 01:16:47 +00:00
console.log(submissionDemographic);
2015-10-06 20:14:38 +00:00
async.waterfall([
function (callback) {
//Authenticate with API
soap.createClient(url_login, options, function(err, client) {
client.login(args_login, function (err, result) {
2015-09-10 22:06:28 +00:00
if(err) callback(err);
2015-10-07 01:16:47 +00:00
console.log('SOAP authenticated');
2015-10-06 20:14:38 +00:00
callback(null, result.return);
2015-09-10 22:06:28 +00:00
});
});
2015-10-06 20:14:38 +00:00
},
function (security_obj, callback) {
//Force Add Demographic
if(_form.plugins.oscarhost.settings.updateType === 'force_add'){
soap.createClient(url_demo, options, function(err, client) {
client.setSecurity(new OscarSecurity(security_obj.securityId, security_obj.securityTokenKey) );
client.addDemographic({ arg0: submissionDemographic }, function (err, result) {
2015-10-07 01:16:47 +00:00
console.log('FORCE ADDING DEMOGRAPHIC \n');
// console.log(result.return);
2015-10-06 20:14:38 +00:00
if(err) callback(err);
callback(null, result);
});
});
}
},
], function(err, result) {
if(err) next(err);
2015-10-07 01:16:47 +00:00
self.oscarDemoNum = parseInt(result.return, 10);
2015-10-30 20:14:48 +00:00
console.log('self.oscarDemoNum: '+self.oscarDemoNum);
2015-10-06 20:14:38 +00:00
next();
});
}else{
next();
}
});
2015-09-10 22:06:28 +00:00
});
2015-07-27 18:11:43 +00:00
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){
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')){
2015-07-27 18:11:43 +00:00
satelize.satelize({ip: this.ipAddr}, function(err, geoData){
if (err) next( new Error(err.message) );
2015-11-12 22:24:26 +00:00
self.geoLocation = JSON.parse(geoData);
2015-07-27 18:11:43 +00:00
next();
});
}
}
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,
2015-11-12 22:24:26 +00:00
self = this,
2015-07-27 18:11:43 +00:00
_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){
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);
next( new Error(err.message) );
}
2015-11-12 22:24:26 +00:00
console.log('Field data from Form: '+self.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;