Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send email #188

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,907 changes: 1,482 additions & 425 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"homepage": "https://github.com/foundersandcoders/open-tourism-platform#readme",
"dependencies": {
"@sendgrid/mail": "^6.2.1",
"bcrypt": "^1.0.3",
"body-parser": "^1.17.2",
"boom": "^5.2.0",
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const Event = require('../models/Event')
const { rejectIfNull } = require('../db/utils')
const { messages: errMessages } = require('../constants/errors')
const roles = require('../constants/roles')
const sendEmail = require('../helpers/sendEmail')

const eventController = module.exports = {}

Expand Down Expand Up @@ -50,7 +51,7 @@ eventController.create = (req, res, next) => {

const newEvent = new Event(newEventDetails)
newEvent.save()
// TODO: .then(event => req.user.role !== roles.BASIC ? event : sendEmail(event))
.then(event => req.user.role !== roles.BASIC ? event : sendEmail(event))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not put side effects in a ternary! (use an if / else)
Your async flow is cray cray, does sendEmail return a promise?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't see any problem here other than readability maybe?, if i had this in if/else and in ternary it's just going to do the same thing if the user is BASIC then sends an email then the sendEmail function would resolve with the event, if he's not then resolve with the event without sending the email. so whats the reason here to put this in an if statement instead of ternary ?! @des-des

.then(event => res.status(201).send(event))
.catch(next)
}
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/place.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const Place = require('../models/Place')
const { rejectIfNull } = require('../db/utils')
const { messages: errMessages } = require('../constants/errors')
const roles = require('../constants/roles')
const sendEmail = require('../helpers/sendEmail')

const placeController = module.exports = {}

Expand Down Expand Up @@ -35,6 +36,7 @@ placeController.create = (req, res, next) => {

const newPlace = new Place(newPlaceDetails)
newPlace.save()
.then(place => req.user.role !== roles.BASIC ? place : sendEmail(place))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again this is silly

.then(place => res.status(201).send(place))
.catch(next)
}
Expand Down
26 changes: 26 additions & 0 deletions src/helpers/sendEmail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const sgMail = require('@sendgrid/mail')
const User = require('../models/User')
sgMail.setApiKey(process.env.SENDGRID_API_KEY)
sgMail.setSubstitutionWrappers('{{', '}}')

const sendEmails = (emails, content) => {
const msg = {
to: emails,
from: '[email protected]',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put this in env var?

templateId: '0839aec2-dd34-459d-9493-4fd9682b9073',
substitutions: {
name: content.en.name,
id: content._id
}
}

sgMail
.sendMultiple(msg)
}

module.exports = content =>
new Promise((resolve, reject) =>
User.find({ role: { $not: /BASIC/ } }, 'email')
.then(emails => sendEmails(emails, content))
.then(() => resolve(content))
.catch(reject))
8 changes: 4 additions & 4 deletions tests/controllers/event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ tape('POST /events adding event with invalid place field', t => {

tape('POST /events adding valid event', t => {
Promise.all([
User.create(superUser),
makeLoggedInToken(superUser)
User.create([validBasicUser, superUser]), // superUser for sending emails
makeLoggedInToken(validBasicUser)
]).then(([ _, token ]) =>
supertest(server)
.post('/api/v1/events')
Expand All @@ -184,7 +184,7 @@ tape('POST /events adding valid event', t => {
Event.findById(res.body._id)
.then(event => {
t.equal(event.categories[0], res.body.categories[0], 'Event is in the database')
dropCollectionAndEnd(Event, t)
dropCollectionsAndEnd([Event, User], t)
})
.catch(err => {
t.fail(err)
Expand Down Expand Up @@ -297,7 +297,7 @@ tape('PUT /events/:id with invalid id', t => {
)
.then(res => {
t.equal(res.body.message, 'Invalid id', 'Correct message is sent back')
dropCollectionAndEnd(Event, t)
dropCollectionsAndEnd([Event, User], t)
})
.catch(err => t.end(err))
})
Expand Down
4 changes: 2 additions & 2 deletions tests/controllers/place.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ tape('GET /places/:id with id of something in the database', t => {
// Tests for: POST /places
tape('POST /places with valid place data', t => {
Promise.all([
User.create(validBasicUser),
User.create([validBasicUser, superUser]), // superUser for sending emails
makeLoggedInToken(validBasicUser)
]).then(([ _, token ]) => supertest(server)
.post('/api/v1/places')
Expand All @@ -118,7 +118,7 @@ tape('POST /places with valid place data', t => {
})
.catch(err => {
t.fail(err)
dropCollectionAndEnd(Place, t)
dropCollectionsAndEnd([Place, User], t)
})
}).catch(err => t.end(err))
})
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"_id": "5986abad5e2d852cb1ee6bce",
"username": "mantagen",
"password": "pword",
"email": "xxx",
"email": "xxx@xxx.com",
"role": "SUPER",
"isPublic": false,
"en": {
Expand Down