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'),
|
|
|
|
config = require('../../config/config'),
|
2016-07-06 01:48:25 +00:00
|
|
|
diff = require('deep-diff'),
|
2015-06-29 22:51:29 +00:00
|
|
|
_ = require('lodash');
|
|
|
|
|
2015-07-27 18:11:43 +00:00
|
|
|
/**
|
|
|
|
* Delete a forms submissions
|
|
|
|
*/
|
|
|
|
exports.deleteSubmissions = function(req, res) {
|
2015-08-07 21:02:44 +00:00
|
|
|
|
2015-07-27 18:11:43 +00:00
|
|
|
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){
|
2016-03-30 03:45:16 +00:00
|
|
|
|
2015-07-27 18:11:43 +00:00
|
|
|
if(err){
|
|
|
|
res.status(400).send({
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
2016-06-07 00:37:09 +00:00
|
|
|
return;
|
2015-07-27 18:11:43 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 00:37:09 +00:00
|
|
|
form.analytics.visitors = [];
|
2017-04-21 04:54:02 +00:00
|
|
|
form.save(function(formSaveErr){
|
|
|
|
if(formSaveErr){
|
2016-06-07 00:37:09 +00:00
|
|
|
res.status(400).send({
|
2017-04-21 04:54:02 +00:00
|
|
|
message: errorHandler.getErrorMessage(formSaveErr)
|
2016-06-07 00:37:09 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
res.status(200).send('Form submissions successfully deleted');
|
|
|
|
|
|
|
|
});
|
2015-07-27 18:11:43 +00:00
|
|
|
});
|
|
|
|
};
|
2015-10-30 18:40:02 +00:00
|
|
|
|
2015-06-29 22:51:29 +00:00
|
|
|
/**
|
|
|
|
* Submit a form entry
|
|
|
|
*/
|
|
|
|
exports.createSubmission = function(req, res) {
|
2015-08-07 21:02:44 +00:00
|
|
|
var form = req.form;
|
2017-03-06 20:30:31 +00:00
|
|
|
|
2017-03-06 19:43:42 +00:00
|
|
|
var timeElapsed = 0;
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2017-03-06 19:43:42 +00:00
|
|
|
if(typeof req.body.timeElapsed == "number"){
|
|
|
|
timeElapsed = req.body.timeElapsed;
|
|
|
|
}
|
2015-08-07 21:02:44 +00:00
|
|
|
var submission = new FormSubmission({
|
2017-03-06 21:45:11 +00:00
|
|
|
admin: form.admin._id,
|
|
|
|
form: form._id,
|
|
|
|
title: form.title,
|
2015-07-27 18:11:43 +00:00
|
|
|
form_fields: req.body.form_fields,
|
2017-03-06 19:43:42 +00:00
|
|
|
timeElapsed: timeElapsed,
|
2017-03-06 21:45:11 +00:00
|
|
|
percentageComplete: req.body.percentageComplete,
|
|
|
|
ipAddr: req.body.ipAddr,
|
|
|
|
geoLocation: req.body.geoLocation,
|
|
|
|
device: req.body.device
|
2015-07-27 18:11:43 +00:00
|
|
|
});
|
2017-03-06 20:30:31 +00:00
|
|
|
|
2015-11-13 01:40:23 +00:00
|
|
|
|
2015-08-07 21:02:44 +00:00
|
|
|
submission.save(function(err, submission){
|
2016-11-09 18:02:12 +00:00
|
|
|
if (err) {
|
|
|
|
console.error(err.message);
|
|
|
|
return res.status(500).send({
|
2015-07-27 18:11:43 +00:00
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
}
|
2016-11-09 18:02:12 +00:00
|
|
|
|
|
|
|
form.submissions.push(submission);
|
|
|
|
|
|
|
|
form.save(function (err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
return res.status(500).send({
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
res.status(200).send('Form submission successfully saved');
|
|
|
|
});
|
2015-07-27 18:11:43 +00:00
|
|
|
});
|
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-11-12 21:37:36 +00:00
|
|
|
FormSubmission.find({ form: _form._id }).exec(function(err, _submissions) {
|
|
|
|
if (err) {
|
2016-11-09 18:02:12 +00:00
|
|
|
console.error(err);
|
2015-11-12 21:37:36 +00:00
|
|
|
res.status(400).send({
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
}
|
2016-11-09 18:02:12 +00:00
|
|
|
res.json(_submissions);
|
2016-03-30 03:45:16 +00:00
|
|
|
|
2015-11-12 21:37:36 +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) {
|
2016-08-26 20:43:37 +00:00
|
|
|
|
|
|
|
if(!req.body.form){
|
|
|
|
return res.status(400).send({
|
|
|
|
message: "Invalid Input"
|
|
|
|
});
|
|
|
|
}
|
2015-07-07 01:21:43 +00:00
|
|
|
var form = new Form(req.body.form);
|
|
|
|
|
2015-08-07 21:02:44 +00:00
|
|
|
form.admin = req.user._id;
|
2015-07-03 01:40:57 +00:00
|
|
|
|
|
|
|
form.save(function(err) {
|
|
|
|
if (err) {
|
2016-08-26 20:43:37 +00:00
|
|
|
return res.status(405).send({
|
2015-07-03 01:40:57 +00:00
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
}
|
2016-08-26 20:43:37 +00:00
|
|
|
|
|
|
|
res.json(form);
|
2015-07-03 01:40:57 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the current form
|
|
|
|
*/
|
|
|
|
exports.read = function(req, res) {
|
2016-11-09 18:02:12 +00:00
|
|
|
FormSubmission.find({ form: req.form._id }).exec(function(err, _submissions) {
|
|
|
|
if (err) {
|
|
|
|
res.status(400).send({
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-09 19:04:47 +00:00
|
|
|
var newForm = req.form.toJSON();
|
2016-11-09 18:02:12 +00:00
|
|
|
newForm.submissions = _submissions;
|
|
|
|
|
|
|
|
if (req.userId) {
|
|
|
|
if(req.form.admin._id+'' === req.userId+''){
|
|
|
|
return res.json(newForm);
|
|
|
|
}
|
|
|
|
return res.status(404).send({
|
|
|
|
message: 'Form Does Not Exist'
|
|
|
|
});
|
2016-06-09 03:32:33 +00:00
|
|
|
}
|
2016-11-09 18:02:12 +00:00
|
|
|
return res.json(newForm);
|
|
|
|
});
|
2015-07-03 01:40:57 +00:00
|
|
|
};
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2017-03-10 00:18:18 +00:00
|
|
|
/**
|
|
|
|
* Upload temp file for submissions
|
|
|
|
*/
|
|
|
|
exports.uploadTemp = function(req, res) {
|
|
|
|
//Send uploaded file data back to form
|
|
|
|
res.json(req.file);
|
|
|
|
};
|
|
|
|
|
2017-03-06 20:30:31 +00:00
|
|
|
/**
|
|
|
|
* Show the current form for rendering form live
|
|
|
|
*/
|
|
|
|
exports.readForRender = function(req, res) {
|
|
|
|
var newForm = req.form.toJSON();
|
2017-04-11 07:10:36 +00:00
|
|
|
|
2017-04-03 21:26:35 +00:00
|
|
|
if (!newForm.isLive && !req.user) {
|
2017-03-06 20:30:31 +00:00
|
|
|
return res.status(404).send({
|
|
|
|
message: 'Form Does Not Exist'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//Remove extraneous fields from form object
|
|
|
|
delete newForm.submissions;
|
|
|
|
delete newForm.analytics;
|
|
|
|
delete newForm.isLive;
|
2017-03-06 21:45:11 +00:00
|
|
|
delete newForm.admin;
|
2017-03-06 20:30:31 +00:00
|
|
|
|
2017-03-11 19:57:55 +00:00
|
|
|
if(!newForm.startPage.showStart){
|
|
|
|
delete newForm.startPage;
|
|
|
|
}
|
|
|
|
|
2017-03-06 20:30:31 +00:00
|
|
|
return res.json(newForm);
|
|
|
|
};
|
|
|
|
|
2015-06-29 22:51:29 +00:00
|
|
|
/**
|
|
|
|
* Update a form
|
|
|
|
*/
|
2016-03-30 03:45:16 +00:00
|
|
|
exports.update = function(req, res) {
|
2015-06-29 22:51:29 +00:00
|
|
|
var form = req.form;
|
2016-11-02 18:30:04 +00:00
|
|
|
|
2016-11-09 18:02:12 +00:00
|
|
|
if (req.body.changes) {
|
2016-08-26 22:34:29 +00:00
|
|
|
var formChanges = req.body.changes;
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2016-08-26 22:34:29 +00:00
|
|
|
formChanges.forEach(function (change) {
|
|
|
|
diff.applyChange(form, true, change);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
//Unless we have 'admin' privileges, updating form admin is disabled
|
2017-03-01 20:11:20 +00:00
|
|
|
if(req.body.form && req.user.roles.indexOf('admin') === -1) {
|
2017-03-10 19:26:07 +00:00
|
|
|
delete req.body.form.admin;
|
2017-03-01 20:11:20 +00:00
|
|
|
}
|
2017-03-10 19:26:07 +00:00
|
|
|
|
|
|
|
if(form.analytics == null){
|
|
|
|
form.analytics.visitors = [];
|
|
|
|
form.analytics.gaCode = '';
|
|
|
|
}
|
|
|
|
|
2016-08-26 22:34:29 +00:00
|
|
|
//Do this so we can create duplicate fields
|
|
|
|
var checkForValidId = new RegExp('^[0-9a-fA-F]{24}$');
|
|
|
|
for(var i=0; i<req.body.form.form_fields.length; i++){
|
|
|
|
var field = req.body.form.form_fields[i];
|
|
|
|
if(!checkForValidId.exec(field._id+'')){
|
|
|
|
delete field._id;
|
|
|
|
}
|
2015-11-13 00:52:14 +00:00
|
|
|
}
|
2016-08-26 22:34:29 +00:00
|
|
|
form = _.extend(form, req.body.form);
|
2015-11-13 00:52:14 +00:00
|
|
|
}
|
2016-03-30 03:45:16 +00:00
|
|
|
|
2017-04-21 04:54:02 +00:00
|
|
|
form.save(function(err, savedForm) {
|
2015-06-29 22:51:29 +00:00
|
|
|
if (err) {
|
2016-08-26 20:43:37 +00:00
|
|
|
res.status(405).send({
|
2015-06-29 22:51:29 +00:00
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
} else {
|
2017-04-21 04:54:02 +00:00
|
|
|
res.json(savedForm);
|
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
|
|
|
Form.remove({_id: form._id}, function(err) {
|
2015-06-29 22:51:29 +00:00
|
|
|
if (err) {
|
2015-08-07 21:02:44 +00:00
|
|
|
res.status(400).send({
|
2015-07-03 19:25:02 +00:00
|
|
|
message: errorHandler.getErrorMessage(err)
|
2015-06-29 22:51:29 +00:00
|
|
|
});
|
|
|
|
} else {
|
2015-08-07 21:02:44 +00:00
|
|
|
res.json(form);
|
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-09-15 22:21:49 +00:00
|
|
|
return res.status(400).send({
|
2015-06-29 22:51:29 +00:00
|
|
|
message: 'Form is invalid'
|
|
|
|
});
|
2015-09-15 22:21:49 +00:00
|
|
|
}
|
2017-04-21 04:54:02 +00:00
|
|
|
Form.findById(id).populate('admin').populate('submissions').exec(function(err, form) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
} else if (!form || form === null) {
|
|
|
|
res.status(404).send({
|
|
|
|
message: 'Form not found'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//Remove sensitive information from User object
|
|
|
|
var _form = form;
|
|
|
|
_form.admin.password = null;
|
|
|
|
_form.admin.salt = null;
|
|
|
|
_form.provider = null;
|
|
|
|
|
|
|
|
req.form = _form;
|
|
|
|
return 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-08-07 21:02:44 +00:00
|
|
|
message: 'User '+req.user.username+' is not authorized to edit Form: '+form.title
|
2015-06-29 22:51:29 +00:00
|
|
|
});
|
|
|
|
}
|
2016-04-29 06:00:41 +00:00
|
|
|
return next();
|
2015-06-29 22:51:29 +00:00
|
|
|
};
|