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'),
|
2015-09-10 22:06:28 +00:00
|
|
|
Field = mongoose.model('Field'),
|
|
|
|
soap = require('soap'),
|
|
|
|
OscarSecurity = require('../../scripts/oscarhost/OscarSecurity');
|
|
|
|
|
|
|
|
var newDemoTemplate = {
|
|
|
|
"activeCount": 0,
|
|
|
|
"address": "",
|
|
|
|
"alias": "",
|
|
|
|
"anonymous": "",
|
|
|
|
"chartNo": "",
|
|
|
|
"children":"",
|
|
|
|
"citizenship":"",
|
|
|
|
"city": "",
|
|
|
|
"dateJoined": null,
|
|
|
|
"dateOfBirth": "",
|
|
|
|
"email": "",
|
|
|
|
"firstName": "",
|
|
|
|
"hin": 9146509343,
|
|
|
|
"lastName": "",
|
|
|
|
"lastUpdateDate": null,
|
|
|
|
"monthOfBirth": "",
|
|
|
|
"officialLanguage": "",
|
|
|
|
"phone": "",
|
|
|
|
"phone2": "",
|
|
|
|
"providerNo": 0,
|
|
|
|
"province": "",
|
|
|
|
"sex": "",
|
|
|
|
"spokenLanguage": "",
|
|
|
|
"postal": "",
|
|
|
|
"yearOfBirth": ""
|
|
|
|
};
|
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-09-10 22:06:28 +00:00
|
|
|
form_fields: [Field],
|
2015-07-27 18:11:43 +00:00
|
|
|
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-30 00:22:53 +00:00
|
|
|
percentageComplete: {
|
|
|
|
type: Number,
|
2015-09-10 22:06:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
//TODO: DAVID: Need to not have this hardcoded
|
|
|
|
oscarDemoNum: {
|
|
|
|
type: Number,
|
2015-07-30 00:22:53 +00:00
|
|
|
}
|
2015-06-29 22:51:29 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-09-10 22:06:28 +00:00
|
|
|
//Oscarhost API hook
|
|
|
|
FormSubmissionSchema.post('save', function (next) {
|
|
|
|
if(this.form.plugins.oscarhost.baseUrl){
|
|
|
|
|
|
|
|
var url_login = this.form.plugins.oscarhost.baseUrl+'/LoginService?wsdl',
|
|
|
|
url_demo = this.form.plugins.oscarhost.baseUrl+'/DemographicService?wsdl';
|
|
|
|
var options = {
|
|
|
|
ignoredNamespaces: {
|
|
|
|
namespaces: ['targetNamespace', 'typedNamespace'],
|
|
|
|
override: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Generate demographics from hashmap
|
|
|
|
var generateDemo = function(formFields, conversionMap, templateDemo){
|
|
|
|
var _generatedDemo = {};
|
|
|
|
for(var field in formFields){
|
|
|
|
if(templateDemo.hasOwnProperty(conversionMap[field._id])){
|
|
|
|
var propertyName = conversionMap[field._id];
|
|
|
|
|
|
|
|
if(propertyName === "unparsedDOB"){
|
|
|
|
var date = Date.parse(field.fieldValue);
|
|
|
|
generatedDemo['dateOfBirth'] = date.getDate();
|
|
|
|
generatedDemo['yearOfBirth'] = date.getFullYear();
|
|
|
|
generatedDemo['monthOfBirth'] = date.getMonth();
|
|
|
|
}else{
|
|
|
|
generatedDemo[propertyName] = field.fieldValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _generatedDemo;
|
|
|
|
}
|
|
|
|
|
|
|
|
var submissionDemographic = generateDemo(this.form_fields, this.form.plugin.oscarhost.settings.fieldMap);
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
function (callback) {
|
|
|
|
//Authenticate with API
|
|
|
|
soap.createClient(url_login, options, function(err, client) {
|
|
|
|
client.login(args_login, function (err, result) {
|
|
|
|
if(err) callback(err);
|
|
|
|
callback(null, result.return);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
function (security_obj, callback) {
|
|
|
|
//Force Add
|
|
|
|
if(this.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: exampleDemo }, function (err, result) {
|
|
|
|
if(err) callback(err);
|
|
|
|
|
|
|
|
callback(null, result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
], function(err, result) {
|
|
|
|
if(err) throw err;
|
|
|
|
console.log(result);
|
|
|
|
this.oscarDemoNum = parseInt(result.return, 10);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
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){
|
|
|
|
if(this.ipAddr){
|
2015-07-30 00:22:53 +00:00
|
|
|
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) );
|
|
|
|
|
|
|
|
this.geoLocation = JSON.parse(geoData);
|
|
|
|
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,
|
|
|
|
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){
|
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){
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
module.exports = FormSubmissionSchema;
|
|
|
|
|
2015-09-10 22:06:28 +00:00
|
|
|
//mongoose.model('FormSubmission', FormSubmissionSchema);
|