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

246 lines
6.1 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'),
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-09-10 22:06:28 +00:00
var newDemoTemplate = {
2015-10-06 20:14:38 +00:00
'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-09-10 22:06:28 +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
},
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,
},
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,
},
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');
console.log('Form form_field ids\n--------');
console.log(form_ids);
console.log('FormSubmission form_field ids\n--------');
console.log(submission_ids);
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
}
};
//Generate demographics from hashmap
var generateDemo = function(formFields, conversionMap, demographicsTemplate){
var _generatedDemo = {};
for(var field in formFields){
if(demographicsTemplate.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(self.form_fields, _form.plugins.oscarhost.settings.fieldMap, newDemoTemplate);
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-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) {
if(err) callback(err);
callback(null, result);
});
});
}
},
], function(err, result) {
if(err) next(err);
console.log(result);
console.log('hello');
this.oscarDemoNum = parseInt(result.return, 10);
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){
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) );
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
});
module.exports = FormSubmissionSchema;
2015-09-10 22:06:28 +00:00
//mongoose.model('FormSubmission', FormSubmissionSchema);