Skip to content

Commit bcf8484

Browse files
committed
made recommended changes
1 parent d9119e9 commit bcf8484

5 files changed

Lines changed: 24 additions & 28 deletions

File tree

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,3 @@ jspm_packages/
4040
.env.test
4141
.env.local
4242
sendgrid.env
43-
sendgrid.env
44-
sendgrid.env

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ Visual Database Schema: https://dbdesigner.page.link/WTZRbVeTR7EzLvs86 <b>\*Curr
303303
<br />
304304

305305
<h2>Email Service</h2>
306-
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.
306+
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.
307307

308308
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)
309309

__tests__/routes/emailHelper.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const newStudent = {
1515
name: 'New Student Here',
1616
},
1717
to: 'someperson@somewhere.com',
18-
from: 'someperson@somewhere.com', // verified sender in SendGrid account
18+
// 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.
19+
from: 'someperson@somewhere.com',
1920
template_id: 'd-a6dacc6241f9484a96554a13bbdcd971',
2021
};
2122

api/email/emailHelper.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ const sendEmail = (data) => {
55
sgMail
66
.send(data)
77
.then((response) => {
8-
// 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.
9-
// console.log(response[0].statusCode);
10-
// console.log(response[0].headers);
118
console.log(JSON.stringify(response));
129
})
1310
.catch((error) => {

api/profile/profileRouter.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -198,76 +198,76 @@ router.post('/', checkProfileObject, async (req, res) => {
198198
if (profileExists) {
199199
res.status(400).json({ message: 'profile already exists' });
200200
} else {
201-
const prof = await Profiles.create(profile);
202-
if (!prof) {
201+
const newProfile = await Profiles.create(profile);
202+
if (!newProfile) {
203203
res.status(404).json({
204204
message: 'There was an error saving the profile to the database.',
205205
});
206206
}
207-
if (prof[0].role_id === 3 || prof[0].role_id === '3') {
207+
if (newProfile[0].role_id === 3 || newProfile[0].role_id === '3') {
208208
const instructorWelcomeMessage = {
209209
dynamic_template_data: {
210-
name: prof[0].name,
210+
name: newProfile[0].name,
211211
},
212-
to: prof[0].email,
212+
to: newProfile[0].email,
213213
from: 'someone@somewhere.com', // verified sender in SendGrid account. Try to put this in env - hardcoded here because it wasn't working there.
214214
template_id: 'd-a4de80911362438bb35d481efa068398',
215215
};
216216
const instructorList = {
217217
list_ids: ['e7b598d9-23ca-48df-a62b-53470b5d1d86'],
218-
email: prof[0].email,
219-
name: prof[0].name,
218+
email: newProfile[0].email,
219+
name: newProfile[0].name,
220220
};
221221
sendEmail(instructorWelcomeMessage);
222222
addToList(instructorList);
223223
res.status(200).json({
224224
message: 'instructor profile created',
225-
profile: prof[0],
225+
profile: newProfile[0],
226226
});
227-
} else if (prof[0].role_id === 4 || prof[0].role_id === '4') {
227+
} else if (newProfile[0].role_id === 4 || newProfile[0].role_id === '4') {
228228
const parentWelcomeMessage = {
229229
dynamic_template_data: {
230-
name: prof[0].name,
230+
name: newProfile[0].name,
231231
},
232-
to: prof[0].email,
232+
to: newProfile[0].email,
233233
from: 'someone@somewhere.com',
234234
template_id: 'd-19b895416ae74cea97e285c4401fcc1f',
235235
};
236236
const parentList = {
237237
list: 'e7b598d9-23ca-48df-a62b-53470b5d1d86',
238-
email: prof[0].email,
239-
name: prof[0].name,
238+
email: newProfile[0].email,
239+
name: newProfile[0].name,
240240
};
241241
sendEmail(parentWelcomeMessage);
242242
addToList(parentList);
243243
res.status(200).json({
244244
message: 'parent profile created',
245-
profile: prof[0],
245+
profile: newProfile[0],
246246
});
247-
} else if (prof[0].role_id === 5 || prof[0].role_id === '5') {
247+
} else if (newProfile[0].role_id === 5 || newProfile[0].role_id === '5') {
248248
const studentWelcomeMessage = {
249249
dynamic_template_data: {
250-
name: prof[0].name,
250+
name: newProfile[0].name,
251251
},
252-
to: prof[0].email,
252+
to: newProfile[0].email,
253253
from: 'someone@somewhere.com',
254254
template_id: 'd-a6dacc6241f9484a96554a13bbdcd971',
255255
};
256256
const studentList = {
257257
list: '4dd72555-266f-4f8e-b595-ecc1f7ff8f28',
258-
email: prof[0].email,
259-
name: prof[0].name,
258+
email: newProfile[0].email,
259+
name: newProfile[0].name,
260260
};
261261
sendEmail(studentWelcomeMessage);
262262
addToList(studentList);
263263
res.status(200).json({
264264
message: 'parent profile created',
265-
profile: prof[0],
265+
profile: newProfile[0],
266266
});
267267
} else {
268268
res.status(200).json({
269269
message: 'profile created',
270-
profile: prof[0],
270+
profile: newProfile[0],
271271
});
272272
}
273273
}

0 commit comments

Comments
 (0)