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'),
|
2017-10-29 21:04:13 +00:00
|
|
|
_ = require('lodash'),
|
2017-10-31 23:05:50 +00:00
|
|
|
nodemailer = require('nodemailer'),
|
2017-11-01 18:25:24 +00:00
|
|
|
emailNotifications = require('../libs/send-email-notifications'),
|
2017-11-01 19:46:15 +00:00
|
|
|
constants = require('../libs/constants'),
|
2017-11-01 22:13:31 +00:00
|
|
|
helpers = require('./helpers.server.controller'),
|
|
|
|
async = require('async');
|
2017-10-06 04:53:13 +00:00
|
|
|
|
|
|
|
var smtpTransport = nodemailer.createTransport(config.mailer.options);
|
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) {
|
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
|
|
|
}
|
2017-11-21 05:13:55 +00:00
|
|
|
|
|
|
|
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) {
|
2017-03-06 20:30:31 +00:00
|
|
|
|
2017-03-06 19:43:42 +00:00
|
|
|
var timeElapsed = 0;
|
2017-10-05 08:19:13 +00:00
|
|
|
|
2017-06-23 17:54:28 +00:00
|
|
|
if(typeof req.body.timeElapsed === 'number'){
|
2017-03-06 19:43:42 +00:00
|
|
|
timeElapsed = req.body.timeElapsed;
|
|
|
|
}
|
2015-08-07 21:02:44 +00:00
|
|
|
var submission = new FormSubmission({
|
2017-10-05 08:19:13 +00:00
|
|
|
form: req.body._id,
|
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-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)
|
|
|
|
});
|
|
|
|
}
|
2017-11-21 05:04:07 +00:00
|
|
|
var form = req.body;
|
2017-11-01 22:13:31 +00:00
|
|
|
var formFieldDict = emailNotifications.createFieldDict(form.form_fields);
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
function(callback) {
|
2017-11-15 10:46:54 +00:00
|
|
|
if (form.selfNotifications && form.selfNotifications.enabled) {
|
|
|
|
if(form.selfNotifications.fromField){
|
|
|
|
form.selfNotifications.fromEmails = formFieldDict[form.selfNotifications.fromField];
|
|
|
|
} else {
|
|
|
|
form.selfNotifications.fromEmails = config.mailer.options.from;
|
|
|
|
}
|
|
|
|
|
|
|
|
emailNotifications.send(form.selfNotifications, formFieldDict, smtpTransport, function(err){
|
2017-11-01 22:13:31 +00:00
|
|
|
if(err){
|
|
|
|
return callback({
|
|
|
|
message: 'Failure sending submission self-notification email'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback();
|
2017-10-31 23:05:50 +00:00
|
|
|
}
|
2017-11-01 22:13:31 +00:00
|
|
|
},
|
|
|
|
function(callback) {
|
|
|
|
if (form.respondentNotifications && form.respondentNotifications.enabled && form.respondentNotifications.toField) {
|
|
|
|
|
2017-11-02 00:13:55 +00:00
|
|
|
form.respondentNotifications.toEmails = formFieldDict[form.respondentNotifications.toField];
|
2017-11-15 10:46:54 +00:00
|
|
|
emailNotifications.send(form.respondentNotifications, formFieldDict, smtpTransport, function(err){
|
2017-11-01 22:13:31 +00:00
|
|
|
if(err){
|
|
|
|
return callback({
|
|
|
|
message: 'Failure sending submission respondent-notification email'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
], function (err) {
|
|
|
|
if(err){
|
|
|
|
return res.status(400).send(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;
|
|
|
|
|
2017-10-28 09:13:56 +00:00
|
|
|
FormSubmission.find({ form: _form._id }).sort('created').lean().exec(function(err, _submissions) {
|
2015-11-12 21:37:36 +00:00
|
|
|
if (err) {
|
2016-11-09 18:02:12 +00:00
|
|
|
console.error(err);
|
2017-11-06 03:40:28 +00:00
|
|
|
return res.status(500).send({
|
2015-11-12 21:37:36 +00:00
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
}
|
2016-11-09 18:02:12 +00:00
|
|
|
res.json(_submissions);
|
2015-11-12 21:37:36 +00:00
|
|
|
});
|
2015-06-29 22:51:29 +00:00
|
|
|
};
|
|
|
|
|
2017-11-06 03:40:28 +00:00
|
|
|
/**
|
|
|
|
* Get Visitor Analytics Data for a given Form
|
|
|
|
*/
|
|
|
|
exports.getVisitorData = function(req, res) {
|
|
|
|
Form.aggregate([
|
|
|
|
{
|
|
|
|
$match: {
|
|
|
|
_id: mongoose.Types.ObjectId(req.params.formIdNoMiddleware),
|
|
|
|
admin: mongoose.Types.ObjectId(req.user.id)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$facet: {
|
2017-11-21 05:04:07 +00:00
|
|
|
'deviceStatistics': [
|
2017-11-06 03:40:28 +00:00
|
|
|
{
|
|
|
|
$unwind: '$analytics.visitors'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$project: {
|
|
|
|
_id: 0,
|
|
|
|
deviceType: '$analytics.visitors.deviceType',
|
|
|
|
SubmittedTimeElapsed: {
|
|
|
|
$cond: [
|
|
|
|
{
|
|
|
|
$eq: ['$analytics.visitors.isSubmitted', true]
|
|
|
|
},
|
|
|
|
'$analytics.visitors.timeElapsed',
|
|
|
|
0
|
|
|
|
]
|
|
|
|
},
|
|
|
|
SubmittedResponses: {
|
|
|
|
$cond: [
|
|
|
|
{
|
|
|
|
$eq: ['$analytics.visitors.isSubmitted', true]
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
0
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$group: {
|
2017-11-21 05:04:07 +00:00
|
|
|
_id: '$deviceType',
|
|
|
|
total_time: { $sum: '$SubmittedTimeElapsed' },
|
|
|
|
responses: { $sum: '$SubmittedResponses' },
|
2017-11-06 03:40:28 +00:00
|
|
|
visits: { $sum: 1 }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$project: {
|
2017-11-21 05:04:07 +00:00
|
|
|
total_time: '$total_time',
|
|
|
|
responses: '$responses',
|
|
|
|
visits: '$visits',
|
2017-11-06 03:40:28 +00:00
|
|
|
average_time: {
|
2017-11-15 12:31:14 +00:00
|
|
|
$cond: [
|
2017-11-21 05:04:07 +00:00
|
|
|
{ $eq: [ '$responses', 0 ] },
|
2017-11-15 12:31:14 +00:00
|
|
|
0,
|
2017-11-21 05:04:07 +00:00
|
|
|
{ $divide: ['$total_time', '$responses'] }
|
2017-11-15 12:31:14 +00:00
|
|
|
]
|
2017-11-06 03:40:28 +00:00
|
|
|
},
|
|
|
|
conversion_rate: {
|
2017-11-13 23:21:29 +00:00
|
|
|
$multiply: [
|
|
|
|
100,
|
|
|
|
{
|
2017-11-15 12:31:14 +00:00
|
|
|
$cond: [
|
2017-11-21 05:04:07 +00:00
|
|
|
{ $eq: [ '$visits', 0 ] },
|
2017-11-15 12:31:14 +00:00
|
|
|
0,
|
2017-11-21 05:04:07 +00:00
|
|
|
{ $divide: ['$responses', '$visits'] }
|
2017-11-15 12:31:14 +00:00
|
|
|
]
|
2017-11-13 23:21:29 +00:00
|
|
|
}
|
|
|
|
]
|
2017-11-06 03:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2017-11-21 05:04:07 +00:00
|
|
|
'globalStatistics': [
|
2017-11-06 03:40:28 +00:00
|
|
|
{
|
|
|
|
$unwind: '$analytics.visitors'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$project: {
|
|
|
|
_id: 0,
|
|
|
|
deviceType: '$analytics.visitors.deviceType',
|
|
|
|
SubmittedTimeElapsed: {
|
|
|
|
$cond: [
|
|
|
|
{
|
|
|
|
$eq: ['$analytics.visitors.isSubmitted', true]
|
|
|
|
},
|
|
|
|
'$analytics.visitors.timeElapsed',
|
|
|
|
0
|
|
|
|
]
|
|
|
|
},
|
|
|
|
SubmittedResponses: {
|
|
|
|
$cond: [
|
|
|
|
{
|
|
|
|
$eq: ['$analytics.visitors.isSubmitted', true]
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
0
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$group: {
|
|
|
|
_id: null,
|
2017-11-21 05:04:07 +00:00
|
|
|
total_time: { $sum: '$SubmittedTimeElapsed' },
|
|
|
|
responses: { $sum: '$SubmittedResponses' },
|
2017-11-06 03:40:28 +00:00
|
|
|
visits: { $sum: 1 }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$project: {
|
|
|
|
_id: 0,
|
2017-11-21 05:04:07 +00:00
|
|
|
total_time: '$total_time',
|
|
|
|
responses: '$responses',
|
|
|
|
visits: '$visits',
|
2017-11-06 03:40:28 +00:00
|
|
|
average_time: {
|
2017-11-15 12:31:14 +00:00
|
|
|
$cond: [
|
2017-11-21 05:04:07 +00:00
|
|
|
{ $eq: [ '$responses', 0 ] },
|
2017-11-15 12:31:14 +00:00
|
|
|
0,
|
2017-11-21 05:04:07 +00:00
|
|
|
{ $divide: ['$total_time', '$responses'] }
|
2017-11-15 12:31:14 +00:00
|
|
|
]
|
2017-11-06 03:40:28 +00:00
|
|
|
},
|
|
|
|
conversion_rate: {
|
2017-11-13 23:21:29 +00:00
|
|
|
$multiply: [
|
|
|
|
100,
|
|
|
|
{
|
2017-11-15 12:31:14 +00:00
|
|
|
$cond: [
|
2017-11-21 05:04:07 +00:00
|
|
|
{ $eq: [ '$visits', 0 ] },
|
2017-11-15 12:31:14 +00:00
|
|
|
0,
|
2017-11-21 05:04:07 +00:00
|
|
|
{ $divide: ['$responses', '$visits'] }
|
2017-11-15 12:31:14 +00:00
|
|
|
]
|
2017-11-13 23:21:29 +00:00
|
|
|
}
|
|
|
|
]
|
2017-11-06 03:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
], function(err, results){
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
return res.status(500).send({
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json(results);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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){
|
2017-10-29 21:04:13 +00:00
|
|
|
return res.status(400).send({
|
2017-06-23 17:54:28 +00:00
|
|
|
message: 'Invalid Input'
|
2016-08-26 20:43:37 +00:00
|
|
|
});
|
|
|
|
}
|
2017-10-30 18:41:34 +00:00
|
|
|
|
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
|
|
|
|
2017-10-29 21:04:13 +00:00
|
|
|
form.save(function(err, createdForm) {
|
2015-07-03 01:40:57 +00:00
|
|
|
if (err) {
|
2017-10-11 05:07:13 +00:00
|
|
|
return res.status(500).send({
|
2015-07-03 01:40:57 +00:00
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
}
|
2016-08-26 20:43:37 +00:00
|
|
|
|
2017-10-29 21:04:13 +00:00
|
|
|
createdForm = helpers.removeSensitiveModelData('private_form', createdForm);
|
|
|
|
return res.json(createdForm);
|
2015-07-03 01:40:57 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the current form
|
|
|
|
*/
|
|
|
|
exports.read = function(req, res) {
|
2017-07-27 02:53:02 +00:00
|
|
|
if(!req.user || (req.form.admin.id !== req.user.id) ){
|
2017-07-26 22:55:33 +00:00
|
|
|
readForRender(req, res);
|
|
|
|
} else {
|
2017-10-30 03:05:52 +00:00
|
|
|
if(!req.form){
|
2017-07-28 21:47:09 +00:00
|
|
|
return res.status(404).send({
|
|
|
|
message: 'Form Does Not Exist'
|
|
|
|
});
|
2016-11-09 18:02:12 +00:00
|
|
|
}
|
2017-10-29 21:04:13 +00:00
|
|
|
|
2017-10-30 03:05:52 +00:00
|
|
|
var newForm = req.form.toJSON();
|
|
|
|
|
2017-11-06 03:40:28 +00:00
|
|
|
if(newForm.admin === req.user._id){
|
2017-10-30 03:05:52 +00:00
|
|
|
return res.json(newForm);
|
|
|
|
}
|
|
|
|
|
2017-10-29 21:04:13 +00:00
|
|
|
newForm = helpers.removeSensitiveModelData('private_form', newForm);
|
2017-07-28 21:47:09 +00:00
|
|
|
return res.json(newForm);
|
2017-07-26 22:55:33 +00:00
|
|
|
}
|
2015-07-03 01:40:57 +00:00
|
|
|
};
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2017-03-06 20:30:31 +00:00
|
|
|
/**
|
|
|
|
* Show the current form for rendering form live
|
|
|
|
*/
|
2017-07-26 22:55:33 +00:00
|
|
|
var readForRender = exports.readForRender = function(req, res) {
|
2017-09-21 06:08:45 +00:00
|
|
|
var newForm = req.form;
|
2017-04-03 21:26:35 +00:00
|
|
|
if (!newForm.isLive && !req.user) {
|
2017-07-25 22:08:25 +00:00
|
|
|
return res.status(401).send({
|
|
|
|
message: 'Form is Not Public'
|
2017-03-06 20:30:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-29 21:04:13 +00:00
|
|
|
newForm = helpers.removeSensitiveModelData('public_form', newForm);
|
2017-03-06 20:30:31 +00:00
|
|
|
|
2017-10-07 07:27:03 +00:00
|
|
|
if(newForm.startPage && !newForm.startPage.showStart){
|
2017-03-11 19:57:55 +00:00
|
|
|
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) {
|
2017-10-07 07:27:03 +00:00
|
|
|
|
2017-10-05 08:19:13 +00:00
|
|
|
var form = req.form;
|
2017-08-02 20:05:17 +00:00
|
|
|
var updatedForm = req.body.form;
|
2017-10-29 21:04:13 +00:00
|
|
|
|
|
|
|
if(!form.analytics){
|
2017-10-07 07:27:03 +00:00
|
|
|
form.analytics = {
|
|
|
|
visitors: [],
|
|
|
|
gaCode: ''
|
2017-11-01 19:46:15 +00:00
|
|
|
};
|
2017-10-07 07:27:03 +00:00
|
|
|
}
|
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) {
|
2017-10-07 07:27:03 +00:00
|
|
|
diff.applyChange(form._doc, true, change);
|
2016-08-26 22:34:29 +00:00
|
|
|
});
|
|
|
|
} else {
|
2017-10-07 07:27:03 +00:00
|
|
|
|
|
|
|
delete updatedForm.__v;
|
|
|
|
delete updatedForm.created;
|
|
|
|
//Unless we have 'admin' privileges, updating the form's admin is disabled
|
2017-08-02 20:05:17 +00:00
|
|
|
if(updatedForm && req.user.roles.indexOf('admin') === -1) {
|
|
|
|
delete updatedForm.admin;
|
2017-03-01 20:11:20 +00:00
|
|
|
}
|
2017-03-10 19:26:07 +00:00
|
|
|
|
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}$');
|
2017-10-07 07:27:03 +00:00
|
|
|
for(var i=0; i < req.body.form.form_fields.length; i++){
|
2016-08-26 22:34:29 +00:00
|
|
|
var field = req.body.form.form_fields[i];
|
|
|
|
if(!checkForValidId.exec(field._id+'')){
|
|
|
|
delete field._id;
|
|
|
|
}
|
2015-11-13 00:52:14 +00:00
|
|
|
}
|
2017-08-02 20:05:17 +00:00
|
|
|
form = _.extend(form, updatedForm);
|
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) {
|
2017-10-11 05:07:13 +00:00
|
|
|
res.status(500).send({
|
2015-06-29 22:51:29 +00:00
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
} else {
|
2017-10-29 21:04:13 +00:00
|
|
|
savedForm = helpers.removeSensitiveModelData('private_form', savedForm);
|
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 = {};
|
|
|
|
|
2017-09-21 03:53:50 +00:00
|
|
|
Form.find(searchObj)
|
2017-10-02 15:41:34 +00:00
|
|
|
.sort('-created')
|
2017-11-14 00:52:42 +00:00
|
|
|
.select('title language isLive')
|
2017-09-21 03:53:50 +00:00
|
|
|
.lean()
|
|
|
|
.exec(function(err, forms) {
|
2015-06-29 22:51:29 +00:00
|
|
|
if (err) {
|
2017-11-14 00:52:42 +00:00
|
|
|
return res.status(400).send({
|
2015-06-29 22:51:29 +00:00
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
2017-11-14 00:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var form_ids = forms.map(function(form){
|
|
|
|
return form._id;
|
|
|
|
});
|
|
|
|
|
|
|
|
//Get number of submissions for each form
|
|
|
|
FormSubmission.aggregate([
|
|
|
|
{
|
|
|
|
$match: {
|
|
|
|
form: {
|
|
|
|
$in: form_ids
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$group: {
|
|
|
|
_id: '$form',
|
|
|
|
responses: { $sum: 1 }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
], function(err, results){
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
return res.status(500).send({
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-21 05:04:07 +00:00
|
|
|
const result_ids = results.map(function(result){
|
|
|
|
return ''+result._id;
|
|
|
|
});
|
|
|
|
|
2017-11-14 00:52:42 +00:00
|
|
|
var currIndex = -1;
|
|
|
|
|
2017-10-02 15:41:34 +00:00
|
|
|
for(var i=0; i<forms.length; i++){
|
2017-10-29 21:04:13 +00:00
|
|
|
forms[i] = helpers.removeSensitiveModelData('private_form', forms[i]);
|
|
|
|
|
2017-11-21 05:04:07 +00:00
|
|
|
currIndex = result_ids.indexOf(forms[i]._id);
|
2017-11-14 07:45:08 +00:00
|
|
|
if(currIndex > -1){
|
2017-11-14 00:52:42 +00:00
|
|
|
forms[i].submissionNum = results[currIndex].responses;
|
|
|
|
} else {
|
|
|
|
forms[i].submissionNum = 0;
|
2017-10-02 15:41:34 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-14 00:52:42 +00:00
|
|
|
|
2015-07-02 03:50:57 +00:00
|
|
|
res.json(forms);
|
2017-11-14 00:52:42 +00:00
|
|
|
});
|
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-10-29 21:04:13 +00:00
|
|
|
|
2017-09-21 03:53:50 +00:00
|
|
|
Form.findById(id)
|
2017-11-15 17:41:19 +00:00
|
|
|
.select('admin title language form_fields startPage endPage showFooter isLive design analytics.gaCode respondentNotifications selfNotifications')
|
2017-09-21 03:53:50 +00:00
|
|
|
.populate('admin')
|
|
|
|
.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
|
2017-10-29 21:04:13 +00:00
|
|
|
req.form = helpers.removeSensitiveModelData('private_form', form);
|
2017-09-21 03:53:50 +00:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FastForm middleware
|
|
|
|
*/
|
|
|
|
exports.formByIDFast = function(req, res, next, id) {
|
|
|
|
if (!mongoose.Types.ObjectId.isValid(id)) {
|
|
|
|
return res.status(400).send({
|
|
|
|
message: 'Form is invalid'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Form.findById(id)
|
|
|
|
.lean()
|
2017-11-15 17:41:19 +00:00
|
|
|
.select('title language form_fields startPage endPage showFooter isLive design analytics.gaCode selfNotifications respondentNotifications')
|
2017-09-21 03:53:50 +00:00
|
|
|
.exec(function(err, form) {
|
2017-04-21 04:54:02 +00:00
|
|
|
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
|
2017-10-29 21:04:13 +00:00
|
|
|
req.form = helpers.removeSensitiveModelData('public_form', form);
|
2017-04-21 04:54:02 +00:00
|
|
|
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
|
|
|
};
|