Skip to content

Commit

Permalink
made recommended changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lisamdespain committed Sep 14, 2022
1 parent d9119e9 commit bcf8484
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 28 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,3 @@ jspm_packages/
.env.test
.env.local
sendgrid.env
sendgrid.env
sendgrid.env
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ Visual Database Schema: https://dbdesigner.page.link/WTZRbVeTR7EzLvs86 <b>\*Curr
<br />

<h2>Email Service</h2>
In api/email, the emailHelper.js file contains the following functions: sendEmail and addToList. SendEmail sends an API request to SendGrid to send a templated email to the to email address its given. The other function: addToList adds the email and name to a specified SendGrid contact list (they're added to all no matter what, then also to the specified list id). Any function that wants to use sendEmail and addToList will need to import it into their file (see profileRouter.js). Parameters can be passed into it, like the toEmail, name, template_id or list_ids (which is an array so it could have more than 1 list_ids there). The parameters to use are created in the file that's calling the sendEmail function.
In api/email, the emailHelper.js file contains the following functions: sendEmail and addToList. SendEmail sends an API request to SendGrid to send a templated email to the email address its given. The other function: addToList adds the email and name to a specified SendGrid contact list (they're added to all no matter what, then also to the specified list id). Any function that wants to use sendEmail and addToList will need to import it into their file (see profileRouter.js). Parameters can be passed into it, like the toEmail, name, template_id or list_ids (which is an array so it could have more than 1 list_ids there). The parameters to use are created in the file that's calling the sendEmail function.

SendGrid's npm package info and how to set up the API key (also listed below): https://www.npmjs.com/package/@sendgrid/mail. The npm package is @sendgrid/mail. Very lightweight, highly supported, and heavily downloaded. (SendGrid also supports Java)

Expand Down
3 changes: 2 additions & 1 deletion __tests__/routes/emailHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const newStudent = {
name: 'New Student Here',
},
to: '[email protected]',
from: '[email protected]', // verified sender in SendGrid account
// The from email must be the email address of a verified sender in SendGrid account. If/when you verify the domain, an email coming from the domain is likely good enough.
from: '[email protected]',
template_id: 'd-a6dacc6241f9484a96554a13bbdcd971',
};

Expand Down
3 changes: 0 additions & 3 deletions api/email/emailHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ const sendEmail = (data) => {
sgMail
.send(data)
.then((response) => {
// note: the following 2 console logs are SendGrid out of the box. Keep them if you like them. We found the stringify response to be more descriptive.
// console.log(response[0].statusCode);
// console.log(response[0].headers);
console.log(JSON.stringify(response));
})
.catch((error) => {
Expand Down
42 changes: 21 additions & 21 deletions api/profile/profileRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,76 +198,76 @@ router.post('/', checkProfileObject, async (req, res) => {
if (profileExists) {
res.status(400).json({ message: 'profile already exists' });
} else {
const prof = await Profiles.create(profile);
if (!prof) {
const newProfile = await Profiles.create(profile);
if (!newProfile) {
res.status(404).json({
message: 'There was an error saving the profile to the database.',
});
}
if (prof[0].role_id === 3 || prof[0].role_id === '3') {
if (newProfile[0].role_id === 3 || newProfile[0].role_id === '3') {
const instructorWelcomeMessage = {
dynamic_template_data: {
name: prof[0].name,
name: newProfile[0].name,
},
to: prof[0].email,
to: newProfile[0].email,
from: '[email protected]', // verified sender in SendGrid account. Try to put this in env - hardcoded here because it wasn't working there.
template_id: 'd-a4de80911362438bb35d481efa068398',
};
const instructorList = {
list_ids: ['e7b598d9-23ca-48df-a62b-53470b5d1d86'],
email: prof[0].email,
name: prof[0].name,
email: newProfile[0].email,
name: newProfile[0].name,
};
sendEmail(instructorWelcomeMessage);
addToList(instructorList);
res.status(200).json({
message: 'instructor profile created',
profile: prof[0],
profile: newProfile[0],
});
} else if (prof[0].role_id === 4 || prof[0].role_id === '4') {
} else if (newProfile[0].role_id === 4 || newProfile[0].role_id === '4') {
const parentWelcomeMessage = {
dynamic_template_data: {
name: prof[0].name,
name: newProfile[0].name,
},
to: prof[0].email,
to: newProfile[0].email,
from: '[email protected]',
template_id: 'd-19b895416ae74cea97e285c4401fcc1f',
};
const parentList = {
list: 'e7b598d9-23ca-48df-a62b-53470b5d1d86',
email: prof[0].email,
name: prof[0].name,
email: newProfile[0].email,
name: newProfile[0].name,
};
sendEmail(parentWelcomeMessage);
addToList(parentList);
res.status(200).json({
message: 'parent profile created',
profile: prof[0],
profile: newProfile[0],
});
} else if (prof[0].role_id === 5 || prof[0].role_id === '5') {
} else if (newProfile[0].role_id === 5 || newProfile[0].role_id === '5') {
const studentWelcomeMessage = {
dynamic_template_data: {
name: prof[0].name,
name: newProfile[0].name,
},
to: prof[0].email,
to: newProfile[0].email,
from: '[email protected]',
template_id: 'd-a6dacc6241f9484a96554a13bbdcd971',
};
const studentList = {
list: '4dd72555-266f-4f8e-b595-ecc1f7ff8f28',
email: prof[0].email,
name: prof[0].name,
email: newProfile[0].email,
name: newProfile[0].name,
};
sendEmail(studentWelcomeMessage);
addToList(studentList);
res.status(200).json({
message: 'parent profile created',
profile: prof[0],
profile: newProfile[0],
});
} else {
res.status(200).json({
message: 'profile created',
profile: prof[0],
profile: newProfile[0],
});
}
}
Expand Down

0 comments on commit bcf8484

Please sign in to comment.