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

293 lines
7.4 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
2016-10-23 04:31:18 +00:00
function formFieldsSetter(form_fields) {
for (var i = 0; i < form_fields.length; i++) {
2016-06-05 03:13:42 +00:00
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;
2016-10-23 04:31:18 +00:00
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-10-23 04:31:18 +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-10-23 04:31:18 +00:00
if (!this.oscarDemoNum && _form.plugins.oscarhost.baseUrl && _form.plugins.oscarhost.settings.fieldMap) {
console.log('OSCARHOST API HOOK');
2016-10-23 04:31:18 +00:00
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
}
};
// console.log(self.form_fields);
//Generate demographics from hashmap
var generateDemo = function (formFields, conversionMap, demographicsTemplate) {
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 + '';
} else if (propertyName === 'DOB') {
var date = new Date(currField.fieldValue);
_generatedDemo.dateOfBirth = date.getDate() + '';
_generatedDemo.yearOfBirth = date.getFullYear() + '';
_generatedDemo.monthOfBirth = date.getMonth() + '';
}
}
var currDate = new Date();
var dateString = currDate.toISOString().split('T')[0] + ' ' + currDate.toISOString().split('T')[1].slice(0, 8);
_generatedDemo.lastUpdateDate = currDate.toISOString();
return _generatedDemo;
};
var submissionDemographic = generateDemo(self.form_fields, _form.plugins.oscarhost.settings.fieldMap, newDemoTemplate);
console.log(submissionDemographic);
async.waterfall([
2016-04-29 06:16:17 +00:00
function (callback) {
//Authenticate with API
2016-10-23 04:31:18 +00:00
soap.createClient(url_login, options, function (err, client) {
client.login(args_login, function (err, result) {
2016-10-23 04:31:18 +00:00
if (err) return callback(err);
console.log('SOAP authenticated');
2016-04-29 06:16:17 +00:00
return callback(null, result.return);
2015-10-06 20:14:38 +00:00
});
});
},
function (security_obj, callback) {
//Force Add Demographic
2016-10-23 04:31:18 +00:00
if (_form.plugins.oscarhost.settings.updateType === 'force_add') {
soap.createClient(url_demo, options, function (err, client) {
if (err) return callback(err);
client.setSecurity(new OscarSecurity(security_obj.securityId, security_obj.securityTokenKey));
2016-10-23 04:31:18 +00:00
client.addDemographic({arg0: submissionDemographic}, function (err, result) {
console.log('FORCE ADDING DEMOGRAPHIC \n');
// console.log(result.return);
2016-10-23 04:31:18 +00:00
if (err) return callback(err);
2016-04-29 06:16:17 +00:00
return callback(null, result);
});
});
}
2016-06-05 03:13:42 +00:00
}
2015-10-06 20:14:38 +00:00
2016-10-23 04:31:18 +00:00
], function (err, result) {
if (err) return next(err);
2015-10-07 01:16:47 +00:00
self.oscarDemoNum = parseInt(result.return, 10);
2016-10-23 04:31:18 +00:00
console.log('self.oscarDemoNum: ' + self.oscarDemoNum);
2016-04-29 06:16:17 +00:00
return next();
});
2016-10-23 04:31:18 +00:00
} else {
2016-04-29 06:16:17 +00:00
return next();
}
});
2016-10-23 04:31:18 +00:00
} 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
2016-10-23 04:31:18 +00:00
FormSubmissionSchema.pre('save', function (next) {
2015-11-12 22:24:26 +00:00
var self = this;
2016-10-23 04:31:18 +00:00
if (this.ipAddr) {
if (this.isModified('ipAddr') || !this.geoLocation) {
freegeoip.getLocation(this.ipAddr, function (err, location) {
if (err) return next(err);
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
2016-10-23 04:31:18 +00:00
if (this.pdf && this.pdf.path) {
dest_filename = self.title.replace(/ /g, '') + '_submission_' + Date.now() + '.pdf';
var __path = this.pdf.path.split('/').slice(0, this.pdf.path.split('/').length - 1).join('/');
2015-07-27 18:11:43 +00:00
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
2016-10-23 04:31:18 +00:00
pdfFiller.fillForm(self.pdf.path, dest_path, self.fdfData, function (err) {
if (err) {
console.log('\n err.message: ' + err.message);
return next(new Error(err.message));
2015-07-27 18:11:43 +00:00
}
2016-10-23 04:31:18 +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;