added backend logic for email notifications and got it to pass server tests

This commit is contained in:
David Baldwynn 2017-11-01 15:13:31 -07:00
parent a7c0cdc056
commit 9faa514ce9
5 changed files with 69 additions and 36 deletions

View file

@ -13,7 +13,8 @@ var mongoose = require('mongoose'),
nodemailer = require('nodemailer'),
emailNotifications = require('../libs/send-email-notifications'),
constants = require('../libs/constants'),
helpers = require('./helpers.server.controller');
helpers = require('./helpers.server.controller'),
async = require('async');
var smtpTransport = nodemailer.createTransport(config.mailer.options);
@ -75,27 +76,52 @@ exports.createSubmission = function(req, res) {
message: errorHandler.getErrorMessage(err)
});
}
var form = req.body
var formFieldDict = emailNotifications.createFieldDict(form.form_fields);
var form = req.body.form;
/*
if (form.selfNotifications && form.selfNotifications.enabled && form.selfNotifications.recipients) {
async.waterfall([
function(callback) {
if (form.selfNotifications && form.selfNotifications.enabled && form.selfNotifications.fromField) {
formFieldDict = emailNotifications.createFieldDict(form.form_fields);
form.selfNotifications.from = formFieldDict[form.selfNotifications.fromField].fieldValue;
form.selfNotifications.fromEmails = formFieldDict[form.selfNotifications.fromField].fieldValue;
emailNotifications.send(form.selfNotifications, formFieldDict, smtpTransport, constants.varFormat, function(err){
if (!err) {
return res.status(200).send('Form submission successfully saved');
emailNotifications.send(form.selfNotifications, formFieldDict, smtpTransport, constants.varFormat, function(err){
if(err){
return callback({
message: 'Failure sending submission self-notification email'
});
}
callback();
});
} else {
callback();
}
return res.status(400).send({
message: 'Failure sending submission email'
});
});
} else {
*/
res.status(200).send('Form submission successfully saved');
//}
},
function(callback) {
if (form.respondentNotifications && form.respondentNotifications.enabled && form.respondentNotifications.toField) {
form.selfNotifications.toEmails = formFieldDict[form.selfNotifications.toField].fieldValue;
emailNotifications.send(form.selfNotifications, formFieldDict, smtpTransport, constants.varFormat, function(err){
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');
});
});
};
@ -326,7 +352,7 @@ exports.formByIDFast = function(req, res, next, id) {
}
Form.findById(id)
.lean()
.select('title language form_fields startPage endPage hideFooter isLive design analytics.gaCode')
.select('title language form_fields startPage endPage hideFooter isLive design analytics.gaCode selfNotifications respondentNotifications')
.exec(function(err, form) {
if (err) {
return next(err);

View file

@ -7,8 +7,8 @@ module.exports = {
var parsedTemplate = this.parseTemplate(emailSettings.htmlTemplate, emailTemplateVars);
var parsedSubject = this.parseTemplate(emailSettings.body, emailTemplateVars);
var mailOptions = {
from: emailSettings.from,
cc: emailSettings.recipients,
from: emailSettings.fromEmails,
cc: emailSettings.toEmails,
subject: parsedSubject,
html: parsedTemplate
};

View file

@ -179,7 +179,7 @@ var FormSchema = new Schema({
toField: {
type: Schema.Types.ObjectId,
},
fromEmail: {
fromEmails: {
type: String,
match: [/.+\@.+\..+/, 'Please fill a valid email address']
},

View file

@ -27,8 +27,8 @@ describe('Form Submission Routes Unit tests', function() {
// Create user credentials
credentials = {
email: 'test@test.com',
username: 'test',
email: 'test423@test.com',
username: 'test534',
password: 'password'
};
@ -44,7 +44,10 @@ describe('Form Submission Routes Unit tests', function() {
// Save a user to the test db and create new Form
user.save(function(err) {
if(err) return done(err);
if(err) {
return done(err);
}
FormObj = new Form({
title: 'Form Title',
language: 'en',
@ -53,7 +56,22 @@ describe('Form Submission Routes Unit tests', function() {
new Field({'fieldType':'textfield', 'title':'First Name', 'fieldValue': ''}),
new Field({'fieldType':'checkbox', 'title':'nascar', 'fieldValue': ''}),
new Field({'fieldType':'checkbox', 'title':'hockey', 'fieldValue': ''})
]
],
selfNotifications: {
fromField: mongoose.Types.ObjectId(),
toEmails: 'john@smith.com',
subject: 'Hello there',
htmlTemplate: '<p> A form was submitted </p>',
enabled: true
},
respondentNotifications: {
toField: mongoose.Types.ObjectId(),
fromEmails: 'john@smith.com',
subject: 'Tellform: Thank you for filling out this TellForm',
htmlTemplate:'Hello, <br><br> Weve received your submission. <br><br> Thank you & have a nice day!',
enabled: true
}
});
FormObj.save(function(formSaveErr, form) {

View file

@ -380,17 +380,6 @@ angular.module('view-form').directive('submitFormDirective', ['$http', 'TimeCoun
if($scope.myform.form_fields[i].fieldType === 'dropdown' && !$scope.myform.form_fields[i].deletePreserved){
$scope.myform.form_fields[i].fieldValue = $scope.myform.form_fields[i].fieldValue.option_value;
}
//Get rid of unnessecary attributes for each form field
delete form.form_fields[i].submissionId;
delete form.form_fields[i].disabled;
delete form.form_fields[i].ratingOptions;
delete form.form_fields[i].fieldOptions;
delete form.form_fields[i].logicJump;
delete form.form_fields[i].description;
delete form.form_fields[i].validFieldTypes;
delete form.form_fields[i].fieldType;
}
setTimeout(function () {