Skip to content

Commit

Permalink
Add email capabilities to public comment page #656
Browse files Browse the repository at this point in the history
  • Loading branch information
nclairesays committed Jan 14, 2021
1 parent c24d327 commit 9327b72
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 8 deletions.
9 changes: 5 additions & 4 deletions client/src/components/PublicComment/PublicCommentPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,20 @@ const PublicCommentForm = () => {
await new Promise(r => setTimeout(r, 500));

try {
const result = await postPublicComment({
const response = await postPublicComment({
name,
email,
comment,
forwardToWebTeam
});

if (result.status === 201) {
if (response.status === 201) {
toast.add("Comment delivered successfully");
resetForm({});
}
} catch (e) {
toast.add(e);
} catch (err) {
toast.add("Something went wrong");
console.error(err);
}
setSubmitting(false);
};
Expand Down
45 changes: 45 additions & 0 deletions server/.env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Instructions: Create a .env file in the server directory. Copy and paste values from Google Docs.

# Node server settings
PORT=

JWT_SECRET=

# Shared Development Database - Azure
SQL_SERVER_NAME=
SQL_SERVER_PORT=
SQL_DATABASE_NAME=
SQL_USER_NAME=
SQL_PASSWORD=
SQL_ENCRYPT=

# Local Development Database - Docker Container
# Example for SQL Server Express on Docker
# SQL_SERVER_NAME=
# SQL_SERVER_INSTANCE
# SQL_SERVER_PORT=
# SQL_DATABASE_NAME=
# SQL_USER_NAME=
# SQL_PASSWORD=
# SQL_ENCRYPT=

# Local Development Database - Windows Native
# SQL_SERVER_NAME=
# SQL_SERVER_INSTANCE=
# SQL_DATABASE_NAME=
# SQL_USER_NAME=
# SQL_PASSWORD=
# SQL_ENCRYPT=

# To support email interaction
CLIENT_URL=
SERVER_URL=
SENDGRID_API_KEY=
EMAIL_SENDER=

# Email addresses for public comment
EMAIL_PUBLIC_COMMENT_LA_CITY=
EMAIL_PUBLIC_COMMENT_WEB_TEAM=

# For metrics on Azure
AZURE_INSIGHTS_INSTRUMENTATION_KEY=b04010da-8444-48d2-a8e1-fcc931a8330d
4 changes: 4 additions & 0 deletions server/app/services/public-comment.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { pool, poolConnect } = require("./tedious-pool");
const mssql = require("mssql");
const { sendPublicComment } = require("./sendgrid-service");

const postPublicComment = async publicComment => {
try {
Expand All @@ -16,6 +17,9 @@ const postPublicComment = async publicComment => {
request.output("id", mssql.Int, null);

const response = await request.execute("PublicComment_Insert");

await sendPublicComment(publicComment);

return response.returnStatus;
} catch (err) {
return Promise.reject(err);
Expand Down
39 changes: 35 additions & 4 deletions server/app/services/sendgrid-service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const sgMail = require("@sendgrid/mail");
const clientUrl = process.env.CLIENT_URL;
const emailUser = process.env.EMAIL_USER;
const sendgridKey = process.env.SENDGRID_API_KEY;
const senderEmail = process.env.EMAIL_SENDER;
const laCityEmail = process.env.EMAIL_PUBLIC_COMMENT_LA_CITY;
const webTeamEmail = process.env.EMAIL_PUBLIC_COMMENT_WEB_TEAM;

sgMail.setApiKey(sendgridKey);

Expand All @@ -24,7 +26,7 @@ const send = async (emailTo, emailFrom, subject, textBody, htmlBody) => {
const sendRegistrationConfirmation = async (email, token) => {
const msg = {
to: `${email}`,
from: emailUser,
from: senderEmail,
subject: "Verify your account",
text: "Verify your account",
html: `<p>Hello, please click the following link to verify your account.</p>
Expand All @@ -45,7 +47,7 @@ const sendRegistrationConfirmation = async (email, token) => {
const sendResetPasswordConfirmation = async (email, token) => {
const msg = {
to: `${email}`,
from: emailUser,
from: senderEmail,
subject: "Confirm Password Reset for TDM Calculator",
text: "Confirm Password Reset for TDM Calculator",
html: `<p>Hello, please click the following link to reset your password for TDM Calculator.</p>
Expand All @@ -66,8 +68,37 @@ const sendResetPasswordConfirmation = async (email, token) => {
});
};

const sendPublicComment = async (publicCommentData) => {
try {
const {name, email, comment, forwardToWebTeam} = publicCommentData
const msg = {
to: laCityEmail,
cc: forwardToWebTeam ? webTeamEmail : "",
from: senderEmail,
subject: `TDM_Calc Comment - ${name}`,
text: `TDM_Calc Comment - ${name}`,
html: ` <p><strong>Name:</strong> ${name}</p>
<p><strong>Email</strong>: ${email ? email : "Anonymous"}</p>
<p><strong>Comment</strong>: ${comment}</p>
<p><strong>Forward To Website Team</strong>: ${forwardToWebTeam ? "Yes" : "No"} </p>`
};
return sgMail.send(msg, false, err => {
if (err) {
return Promise.reject(
"Sending email to LA City or Website team failed."
);
} else {
return Promise.resolve(true);
}
});
} catch(err) {
console.error(err)
}
};

module.exports = {
send,
sendRegistrationConfirmation,
sendResetPasswordConfirmation
sendResetPasswordConfirmation,
sendPublicComment
};

0 comments on commit 9327b72

Please sign in to comment.