2015-06-30 02:14:43 +00:00
|
|
|
'use strict';
|
2015-06-29 22:51:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
var mongoose = require('mongoose'),
|
|
|
|
errorHandler = require('./errors.server.controller'),
|
|
|
|
Form = mongoose.model('Form'),
|
|
|
|
FormSubmission = mongoose.model('FormSubmission'),
|
|
|
|
pdfFiller = require( 'pdffiller' ),
|
|
|
|
config = require('../../config/config'),
|
|
|
|
fs = require('fs-extra'),
|
|
|
|
async = require('async'),
|
2015-07-01 23:14:39 +00:00
|
|
|
path = require('path'),
|
2015-06-29 22:51:29 +00:00
|
|
|
_ = require('lodash');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Upload PDF
|
|
|
|
*/
|
2015-07-02 21:49:21 +00:00
|
|
|
exports.uploadPDF = function(req, res, next) {
|
|
|
|
|
|
|
|
console.log('inside uploadPDF');
|
2015-07-27 18:11:43 +00:00
|
|
|
|
|
|
|
// console.log(req.files.file);
|
|
|
|
// console.log('\n\nProperty Descriptor\n-----------');
|
|
|
|
// console.log(Object.getOwnPropertyDescriptor(req.files.file, 'path'));
|
2015-07-02 21:49:21 +00:00
|
|
|
if(req.files){
|
|
|
|
var pdfFile = req.files.file;
|
|
|
|
var _user = req.user;
|
|
|
|
if (req.files.size === 0) {
|
|
|
|
next(new Error('File uploaded is EMPTY'));
|
|
|
|
}else if(req.files.size > 200000000){
|
|
|
|
next(new Error('File uploaded exceeds MAX SIZE of 200MB'));
|
|
|
|
}else {
|
|
|
|
fs.exists(pdfFile.path, function(exists) {
|
|
|
|
//If file exists move to user's tmp directory
|
|
|
|
if(exists) {
|
|
|
|
|
|
|
|
var newDestination = config.tmpUploadPath+_user.username;
|
|
|
|
var stat = null;
|
|
|
|
try {
|
|
|
|
stat = fs.statSync(newDestination);
|
|
|
|
} catch (err) {
|
|
|
|
fs.mkdirSync(newDestination);
|
|
|
|
}
|
|
|
|
if (stat && !stat.isDirectory()) {
|
|
|
|
console.log('Directory cannot be created');
|
|
|
|
next(new Error('Directory cannot be created because an inode of a different type exists at "' + newDestination + '"'));
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.move(pdfFile.path, path.join(newDestination, pdfFile.name), function (err) {
|
|
|
|
if (err) {
|
|
|
|
next(new Error(err.message));
|
|
|
|
}
|
|
|
|
pdfFile.path = path.join(newDestination, pdfFile.name);
|
2015-07-02 23:34:00 +00:00
|
|
|
console.log(pdfFile.name + ' uploaded to ' + pdfFile.path);
|
2015-07-27 18:11:43 +00:00
|
|
|
res.json(pdfFile);
|
2015-07-02 21:49:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
next(new Error('Did NOT get your file!'));
|
|
|
|
}
|
|
|
|
});
|
2015-06-29 22:51:29 +00:00
|
|
|
}
|
2015-07-01 23:14:39 +00:00
|
|
|
}else {
|
2015-07-02 21:49:21 +00:00
|
|
|
next(new Error('Uploaded files were NOT detected'));
|
2015-07-01 23:14:39 +00:00
|
|
|
}
|
2015-06-29 22:51:29 +00:00
|
|
|
};
|
|
|
|
|
2015-07-27 18:11:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a forms submissions
|
|
|
|
*/
|
|
|
|
exports.deleteSubmissions = function(req, res) {
|
|
|
|
console.log(req.body);
|
|
|
|
var submission_id_list = req.body.deleted_submissions,
|
|
|
|
form = req.form;
|
|
|
|
|
|
|
|
FormSubmission.remove({ form: req.form, admin: req.user, _id: {$in: submission_id_list} }, function(err){
|
|
|
|
|
|
|
|
if(err){
|
|
|
|
res.status(400).send({
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
res.status(200).send('Form submissions successfully deleted');
|
|
|
|
});
|
|
|
|
};
|
2015-06-29 22:51:29 +00:00
|
|
|
/**
|
|
|
|
* Submit a form entry
|
|
|
|
*/
|
|
|
|
exports.createSubmission = function(req, res) {
|
|
|
|
|
2015-07-27 18:11:43 +00:00
|
|
|
var submission,
|
2015-06-30 02:14:43 +00:00
|
|
|
form = req.form,
|
|
|
|
fdfData,
|
2015-07-03 19:25:02 +00:00
|
|
|
fdfTemplate,
|
|
|
|
that = this;
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-27 18:11:43 +00:00
|
|
|
submission = new FormSubmission({
|
|
|
|
admin: req.user,
|
|
|
|
form_fields: req.body.form_fields,
|
|
|
|
timeElapsed: req.body.timeElapsed
|
|
|
|
});
|
|
|
|
|
2015-06-29 22:51:29 +00:00
|
|
|
submission.form = form;
|
2015-07-27 18:11:43 +00:00
|
|
|
submission.pdf = form.pdf;
|
|
|
|
submission.title = form.title;
|
2015-06-30 02:14:43 +00:00
|
|
|
// submission.ipAddr = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
|
|
|
|
|
2015-07-02 04:54:46 +00:00
|
|
|
if(form.autofillPDFs){
|
2015-07-03 19:25:02 +00:00
|
|
|
if (form.isGenerated){
|
|
|
|
fdfTemplate = form.generateFDFTemplate();
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
fdfTemplate = pdfFiller.mapForm2PDF(form.generateFDFTemplate(), form.pdfFieldMap);
|
|
|
|
} catch(err){
|
|
|
|
res.status(400).send({
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2015-07-27 18:11:43 +00:00
|
|
|
fdfData = pdfFiller.convFieldJson2FDF(submission.form_fields);
|
2015-07-02 04:54:46 +00:00
|
|
|
submission.fdfData = fdfData;
|
2015-07-27 18:11:43 +00:00
|
|
|
}else{
|
|
|
|
submission.fdfData = undefined;
|
2015-07-02 04:54:46 +00:00
|
|
|
}
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-27 18:11:43 +00:00
|
|
|
submission.save(function(err){
|
|
|
|
|
|
|
|
if(err){
|
|
|
|
res.status(400).send({
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// console.log(results);
|
|
|
|
// console.log(that.form_fields);
|
|
|
|
res.status(200).send('Form submission successfully saved');
|
|
|
|
});
|
2015-06-29 22:51:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-06-30 06:12:32 +00:00
|
|
|
* Get List of Submissions for a given Form
|
2015-06-29 22:51:29 +00:00
|
|
|
*/
|
|
|
|
exports.listSubmissions = function(req, res) {
|
|
|
|
var _form = req.form;
|
2015-07-21 23:25:45 +00:00
|
|
|
var _user = req.user;
|
|
|
|
// console.log(_form);
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-03 23:47:14 +00:00
|
|
|
// if(_form.submissions.length){
|
|
|
|
// res.json(_form.submissions);
|
|
|
|
// }else{
|
2015-07-21 23:25:45 +00:00
|
|
|
FormSubmission.find({ form: req.form, admin: _user }).populate('admin', 'form').exec(function(err, _submissions) {
|
2015-07-03 19:25:02 +00:00
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
res.status(400).send({
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
}
|
2015-07-21 23:25:45 +00:00
|
|
|
|
|
|
|
_form.update({ $set : { submissions: _submissions }}).exec(function(err, form){
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
res.status(400).send({
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
res.json(_submissions);
|
|
|
|
});
|
|
|
|
// res.status(200).send('Updated forms');
|
|
|
|
|
2015-07-03 19:25:02 +00:00
|
|
|
});
|
2015-07-03 23:47:14 +00:00
|
|
|
// }
|
2015-06-29 22:51:29 +00:00
|
|
|
};
|
|
|
|
|
2015-07-03 01:40:57 +00:00
|
|
|
/**
|
|
|
|
* Create a new form
|
|
|
|
*/
|
|
|
|
exports.create = function(req, res) {
|
2015-07-07 01:21:43 +00:00
|
|
|
var form = new Form(req.body.form);
|
|
|
|
|
2015-07-03 01:40:57 +00:00
|
|
|
form.admin = req.user;
|
2015-07-27 18:11:43 +00:00
|
|
|
// console.log(form);
|
|
|
|
// console.log(req.user);
|
2015-07-03 01:40:57 +00:00
|
|
|
|
|
|
|
form.save(function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
res.status(400).send({
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
} else {
|
2015-07-27 18:11:43 +00:00
|
|
|
res.json(form);
|
2015-07-03 01:40:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the current form
|
|
|
|
*/
|
|
|
|
exports.read = function(req, res) {
|
|
|
|
res.json(req.form);
|
|
|
|
};
|
2015-06-29 22:51:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a form
|
|
|
|
*/
|
2015-07-03 23:47:14 +00:00
|
|
|
exports.update = function(req, res) {
|
2015-07-03 00:49:23 +00:00
|
|
|
console.log('in form.update()');
|
|
|
|
|
2015-07-27 18:11:43 +00:00
|
|
|
// console.log(req.body.form.form_fields);
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2015-06-29 22:51:29 +00:00
|
|
|
var form = req.form;
|
2015-07-21 23:25:45 +00:00
|
|
|
delete req.body.form.__v;
|
|
|
|
delete req.body.form._id;
|
|
|
|
delete req.body.form.created;
|
|
|
|
delete req.body.form.lastModified;
|
2015-07-27 18:11:43 +00:00
|
|
|
delete req.body.form.admin;
|
|
|
|
|
|
|
|
// console.log(form.admin);
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
//Unless we have 'admin' priviledges, updating form admin is disabled
|
|
|
|
if(req.user.roles.indexOf('admin') === -1) delete req.body.form.admin;
|
|
|
|
|
|
|
|
form = _.extend(form, req.body.form);
|
|
|
|
// console.log(req.body.form);
|
|
|
|
// form.form_fields = req.body.form.form_fields;
|
|
|
|
|
2015-07-27 18:11:43 +00:00
|
|
|
form.save(function(err, form) {
|
2015-06-29 22:51:29 +00:00
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
2015-07-02 03:50:57 +00:00
|
|
|
res.status(400).send({
|
2015-06-29 22:51:29 +00:00
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log('updated form');
|
2015-07-27 18:11:43 +00:00
|
|
|
res.json(form);
|
2015-06-29 22:51:29 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a form
|
|
|
|
*/
|
|
|
|
exports.delete = function(req, res) {
|
|
|
|
var form = req.form;
|
2015-07-02 03:50:57 +00:00
|
|
|
console.log('deleting form');
|
|
|
|
Form.remove({_id: form._id}, function(err) {
|
2015-06-29 22:51:29 +00:00
|
|
|
if (err) {
|
2015-07-02 04:54:46 +00:00
|
|
|
res.status(500).send({
|
2015-07-03 19:25:02 +00:00
|
|
|
message: errorHandler.getErrorMessage(err)
|
2015-06-29 22:51:29 +00:00
|
|
|
});
|
|
|
|
} else {
|
2015-07-02 03:50:57 +00:00
|
|
|
console.log('Form successfully deleted');
|
|
|
|
res.status(200).send('Form successfully deleted');
|
2015-06-29 22:51:29 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-07-02 02:49:35 +00:00
|
|
|
* Get All of Users' Forms
|
2015-06-29 22:51:29 +00:00
|
|
|
*/
|
|
|
|
exports.list = function(req, res) {
|
2015-07-02 02:49:35 +00:00
|
|
|
//Allow 'admin' user to view all forms
|
|
|
|
var searchObj = {admin: req.user};
|
2015-07-02 03:50:57 +00:00
|
|
|
if(req.user.isAdmin()) searchObj = {};
|
|
|
|
|
2015-07-07 01:56:38 +00:00
|
|
|
Form.find(searchObj).sort('-created').populate('admin.username', 'admin._id').exec(function(err, forms) {
|
2015-06-29 22:51:29 +00:00
|
|
|
if (err) {
|
2015-07-02 03:50:57 +00:00
|
|
|
res.status(400).send({
|
2015-06-29 22:51:29 +00:00
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
} else {
|
2015-07-02 03:50:57 +00:00
|
|
|
res.json(forms);
|
2015-06-29 22:51:29 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Form middleware
|
|
|
|
*/
|
|
|
|
exports.formByID = function(req, res, next, id) {
|
|
|
|
|
|
|
|
if (!mongoose.Types.ObjectId.isValid(id)) {
|
2015-07-02 03:50:57 +00:00
|
|
|
res.status(400).send({
|
2015-06-29 22:51:29 +00:00
|
|
|
message: 'Form is invalid'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-28 22:29:07 +00:00
|
|
|
Form.findById(id).populate('admin').exec(function(err, form) {
|
2015-07-02 04:54:46 +00:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
2015-07-28 22:29:07 +00:00
|
|
|
} else if (form === undefined || form === null) {
|
|
|
|
res.status(400).send({
|
2015-06-29 22:51:29 +00:00
|
|
|
message: 'Form not found'
|
|
|
|
});
|
|
|
|
}
|
2015-07-02 04:54:46 +00:00
|
|
|
else {
|
2015-07-28 22:29:07 +00:00
|
|
|
if(!form.admin.username){
|
2015-07-27 18:11:43 +00:00
|
|
|
form.admin = req.user;
|
|
|
|
}
|
2015-07-28 22:29:07 +00:00
|
|
|
console.log(form.admin);
|
2015-07-27 18:11:43 +00:00
|
|
|
|
2015-07-02 04:54:46 +00:00
|
|
|
//Remove sensitive information from User object
|
|
|
|
form.admin.password = null;
|
|
|
|
form.admin.created = null;
|
|
|
|
form.admin.salt = null;
|
2015-07-28 22:29:07 +00:00
|
|
|
form.provider = null;
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-02 04:54:46 +00:00
|
|
|
req.form = form;
|
|
|
|
next();
|
|
|
|
}
|
2015-06-29 22:51:29 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Form authorization middleware
|
|
|
|
*/
|
|
|
|
exports.hasAuthorization = function(req, res, next) {
|
|
|
|
|
|
|
|
var form = req.form;
|
2015-07-02 03:50:57 +00:00
|
|
|
if (req.form.admin.id !== req.user.id && req.user.roles.indexOf('admin') === -1) {
|
|
|
|
res.status(403).send({
|
2015-07-02 02:49:35 +00:00
|
|
|
message: 'User '+req.user.username+' is not authorized'
|
2015-06-29 22:51:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
};
|