got form deletion working

This commit is contained in:
David Baldwynn 2015-07-01 20:50:57 -07:00
parent 3843a93e4b
commit 0c8c320a71
7 changed files with 107 additions and 75 deletions

View file

@ -24,11 +24,11 @@ exports.create = function(req, res) {
form.save(function(err) {
if (err) {
console.log(err);
return res.status(400).send({
res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
return res.json(form);
res.json(form);
}
});
};
@ -127,11 +127,12 @@ exports.createSubmission = function(req, res) {
submission.save(function(err){
if (err) {
console.error(err);
return res.status(400).send({
res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
return res.status(200);
console.log('Form Submission CREATED');
res.status(200).send('Form submission successfully saved');
}
});
};
@ -145,11 +146,11 @@ exports.listSubmissions = function(req, res) {
FormSubmission.find({ form: req.form }).populate('admin', 'form').exec(function(err, submissions) {
if (err) {
return res.status(400).send({
res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
return res.json(submissions);
res.json(submissions);
}
});
};
@ -167,12 +168,12 @@ exports.update = function(req, res) {
form.save(function(err) {
if (err) {
console.log(err);
return res.status(400).send({
res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
console.log('updated form');
return res.json(form);
res.json(form);
}
});
};
@ -182,14 +183,15 @@ exports.update = function(req, res) {
*/
exports.delete = function(req, res) {
var form = req.form;
form.remove(function(err) {
console.log('deleting form');
Form.remove({_id: form._id}, function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
res.status(400).send({
message: err.message
});
} else {
return res.status(200);
console.log('Form successfully deleted');
res.status(200).send('Form successfully deleted');
}
});
};
@ -200,16 +202,15 @@ exports.delete = function(req, res) {
exports.list = function(req, res) {
//Allow 'admin' user to view all forms
var searchObj = {admin: req.user};
if(req.user.isAdmin()){
searchObj = {};
}
if(req.user.isAdmin()) searchObj = {};
Form.find({}).sort('-created').populate('admin').exec(function(err, forms) {
if (err) {
return res.status(400).send({
res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
return res.json(forms);
res.json(forms);
}
});
};
@ -221,7 +222,7 @@ exports.list = function(req, res) {
exports.formByID = function(req, res, next, id) {
if (!mongoose.Types.ObjectId.isValid(id)) {
return res.status(400).send({
res.status(400).send({
message: 'Form is invalid'
});
}
@ -229,10 +230,29 @@ exports.formByID = function(req, res, next, id) {
Form.findById(id).populate('admin').exec(function(err, form) {
if (err) return next(err);
if (!form) {
return res.status(404).send({
res.status(404).send({
message: 'Form not found'
});
}
if(!form.admin){
form.admin = req.user;
form.save(function(err) {
if (err) {
console.log(err);
res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
//Remove sensitive information from User object
form.admin.password = null;
form.admin.created = null;
form.admin.salt = null;
req.form = form;
next();
}
});
}
//Remove sensitive information from User object
form.admin.password = null;
@ -250,11 +270,8 @@ exports.formByID = function(req, res, next, id) {
exports.hasAuthorization = function(req, res, next) {
var form = req.form;
// console.log(req.form.admin);
// console.log(req.user);
if (req.form.admin.id !== req.user.id || req.user.roles.indexOf('admin') === -1) {
return res.status(403).send({
if (req.form.admin.id !== req.user.id && req.user.roles.indexOf('admin') === -1) {
res.status(403).send({
message: 'User '+req.user.username+' is not authorized'
});
}

View file

@ -22,6 +22,10 @@ var FormSchema = new Schema({
type: Date,
default: Date.now
},
lastModified: {
type: Date,
default: Date.now
},
title: {
type: String,
default: '',
@ -69,10 +73,14 @@ var FormSchema = new Schema({
},
});
//Move PDF to permanent location after first save
//Update lastModified everytime we save
FormSchema.pre('save', function (next) {
this.lastModified = Date.now();
next();
});
//Move PDF to permanent location after new template is uploaded
FormSchema.pre('save', function (next) {
// console.log(this.pdf);
// debugger;
if(this.pdf){
if(this.pdf.modified){
@ -209,5 +217,4 @@ FormSchema.methods.convertToFDF = function (cb) {
return jsonObj;
};
mongoose.model('Form', FormSchema);

View file

@ -31,27 +31,21 @@ angular.module('forms').controller('ViewFormController', ['$scope', '$stateParam
// Remove existing Form
$scope.remove = function() {
if (CurrentForm.getForm()) {
var form = CurrentForm.getForm();
form.$remove();
$http.delete('/forms/'+$scope.form._id).
success(function(data, status, headers){
console.log('hello');
var form = CurrentForm.getForm()
if(!form){
form = $scope.form
}
$http.delete('/forms/'+$scope.form._id)
.success(function(data, status, headers){
console.log('form deleted successfully');
alert('Form deleted..');
$state.go('listForms');
}).error(function(error){
console.log('ERROR: Form could not be deleted.');
console.error(error);
});
} else{
$scope.form.$remove(function() {
$http.delete('/forms/'+$scope.form._id).
success(function(data, status, headers){
console.log('form deleted successfully');
alert('Form deleted..');
$state.go('listForms');
});
});
}
};

View file

@ -1,25 +1,27 @@
'use strict';
angular.module('forms').directive('formDirective', ['$http', '$timeout', 'timeCounter',
function ($http, $timeout, timeCounter) {
angular.module('forms').directive('formDirective', ['$http', '$timeout', 'timeCounter', 'Auth',
function ($http, $timeout, timeCounter, Auth) {
return {
controller: function($scope){
timeCounter.startClock();
$scope.submit = function(){
var _timeElapsed = timeCounter.stopClock();
$scope.form.timeElapsed = _timeElapsed;
console.log($scope.form.timeElapsed);
// console.log($scope.form.form_fields[7]);
// console.log($scope.form.timeElapsed);
$scope.authentication = Auth;
console.log($scope.authentication.isAuthenticated());
$http.post('/forms/'+$scope.form._id,$scope.form).
success(function(data, status, headers){
console.log('form submitted successfully');
alert('Form submitted..');
$scope.form.submitted = true;
})
.error(function(error){
console.log(error);
});
};

View file

@ -8,13 +8,13 @@ angular.module('forms').service('timeCounter', [
this.startClock = function(){
_startTime = Date.now();
console.log('Clock Started');
// console.log('Clock Started');
};
this.stopClock = function(){
_endTime = Date.now();
that.timeSpent = Math.abs(_endTime.valueOf() - _startTime.valueOf())/1000;
console.log('Clock Ended');
// console.log('Clock Ended');
return that.timeSpent;
};

View file

@ -1,9 +1,7 @@
<h2>{{ form.form_name }}</h2>
<div ng-show="!form.submitted">
<div ng-hide="form.submitted">
<div class="field row">
<div class="col-sm-11 col-sm-offset-1"><h1>{{ form.title }}</h1>
<div class="col-sm-10 col-sm-offset-1"><h1>{{ form.title }}</h1>
<hr>
</div>
</div>
@ -11,31 +9,46 @@
<br>
<br>
<form class="field row" name="form" ng-model="form" ng-repeat="field in form.form_fields" >
<!-- <ul class=" col-sm-11 col-sm-offset-1" style="margin-top: 50px; margin-bottom: 50px;" ng-repeat="field in form.form_fields" > -->
<field-directive field="field" >
</field-directive>
<!-- </ul> -->
</form>
<div class="row">
<form class="field col-sm-offset-1 col-sm-10" name="form" ng-model="form" ng-repeat="field in form.form_fields" >
<!-- <ul class=" col-sm-11 col-sm-offset-1" style="margin-top: 50px; margin-bottom: 50px;" ng-repeat="field in form.form_fields" > -->
<field-directive field="field" >
</field-directive>
<!-- </ul> -->
</form>
</div>
<div class="row form-actions">
<p class="text-left col-sm-2 col-sm-offset-5">
<button class="btn btn-success right" type="button" ng-disabled="myForm.$valid" ng-click="submit()">
<i class="icon-edit icon-white"></i> Submit Form
<button class="btn btn-success col-sm-2 col-sm-offset-5" type="button" ng-disabled="myForm.$valid" ng-click="submit()" style="font-size: 1.6em;">
<i class="icon-edit icon-white"></i> submit
</button>
</p>
</div>
</div>
<div ng-show="form.submitted">
<h3>Form Successfully submitted</h3>
<br><br><br>
<div class="field row">
<div class="col-sm-11 col-sm-offset-1"><h1>{{ form.title }}</h1>
<hr>
</div>
</div>
<br>
<br>
<div class="field row text-center">
<h4 class="col-xs-6 col-xs-offset-1 text-left">Form entry successfully submitted!<br> What would you like to do next?</h4>
</div>
<br><br><br><br><br>
<div class="row form-actions">
<p class="text-left col-sm-2">
<button class="btn btn-primary left" type="button">
<a href="/form/{{form.id}}" style="color:white;"> Submit again?</a>
<p class="text-center col-xs-3 col-xs-offset-2">
<button class="btn btn-success left" type="button">
<a href="/#!/form/{{form._id}}" style="color:white;"> Submit again?</a>
</button>
</p>
<p class="text-center col-xs-2" ng-if="authentication.isAuthenticated()">
<button class="btn btn-caution left" type="button">
<a href="/form/{{form.id}}/admin" style="color:white;">Edit Form</a>
</button>
</p>
</div>

View file

@ -1,8 +1,7 @@
<link rel="stylesheet" href="./modules/forms/css/form.css">
<section data-ng-controller="SubmitFormController">
<form-directive form="form"></form-directive><hr>
<form-directive form="form"></form-directive>
<section ng-if="!form.hideFooter" class="navbar navbar-fixed-bottom" style="background-color:rgba(242,242,242,0.5); padding-top:15px;">
<div class="container" >