Skip to content

Conversation

@paulvalderama
Copy link

@paulvalderama paulvalderama commented Apr 16, 2020

The function receives the email input for who to send the email to, configures the mail to send including html, sends confirmation email to user. Function is stored in mailgunModule and exported to be used in server file.


const mailgunModule = {}

mailgunModule.sendEmail = async (req, res) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

mailer.sendConfirmationEmail = async ( { recipient, confirmationToken }) => {

html: '<h1>Congratulations! Confirm your E-Mail with C0D3</h1><button>Confirm</button>'
}, (error) => {
if (error) {
console.log(`error sending email ${error}`)
Copy link
Contributor

Choose a reason for hiding this comment

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

use logger

}
})
} catch (error) {
console.log(`mailgun did not send email successful ${error}`)
Copy link
Contributor

Choose a reason for hiding this comment

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

use logger

package.json Outdated
},
"dependencies": {
"pg": "^8.0.0"
"pg": "^8.0.2"

Choose a reason for hiding this comment

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

mailgun should appear in dependencies, add $ npm i --save mailgun-js

Copy link

@allopez7 allopez7 Apr 23, 2020

Choose a reason for hiding this comment

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

Also you don't have the latest devDependencies and dependencies. You need to go to master and $ git pull then on your branch $ git pull origin master so you can get all the latest changes.

require('dotenv').config()

const sendConfirmationEmail = () => {
const mailgun = require('mailgun-js');
Copy link

@allopez7 allopez7 Apr 23, 2020

Choose a reason for hiding this comment

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

place mailgun on the first line. Typically we place our modules at the top.

if(error){
console.log(error)
}
console.log(body)
Copy link

@allopez7 allopez7 Apr 23, 2020

Choose a reason for hiding this comment

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

inside of logger we also have logger.info which is equivalent to console.log. Use logger.info('email successfully sent')

to: process.env.RECEIVER_EMAIL,
subject: 'Congratulations!',
text: 'Welcome to C0D3',
html: "<h1>Confirm your E-mail</h1><button>Confirm</button>"
Copy link
Contributor

Choose a reason for hiding this comment

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

html: .... <a href="https://learndb.dev/emailConfirmation/ token from parameter>Click to confirm</a>

Copy link
Contributor

@songz songz Apr 24, 2020

Choose a reason for hiding this comment

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

const link = `https://learndatabases.dev/emailConfirmation/${token}`
html: `
<h1> Confirm your Email </h1>
<p>
  <a href="${link}">Click Here</a>
</p>
<p> Or visit this link: <a href="${link}">${link}</a></p>
`

};
mg.messages().send(data, function(error, body){
if(error){
logger.error('email failed to send')
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't want to be calling both error and info. So you need return.

text: 'Welcome to C0D3',
html: "<h1>Confirm your E-mail</h1><button>Confirm</button>"
};
mg.messages().send(data, function(error, body){
Copy link
Contributor

Choose a reason for hiding this comment

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

there's no way for caller to see whether email was successful or not. You should return a promise that resolves or rejects. From the documentation, it seems like the library returns promises: https://www.npmjs.com/package/mailgun-js#promises


const mg = mailgun({apiKey: process.env.MAILGUN_API_KEY, domain: process.env.MAILGUN_DOMAIN});

const sendConfirmationEmail = () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

takes in 2 arguments. receiver, token

logger.error('error')
})
}
sendConfirmationEmail()
Copy link
Contributor

Choose a reason for hiding this comment

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

why are you running your own function.

}
sendConfirmationEmail()

module.exports = sendConfirmationEmail No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

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

you may be sending other types of email in the future, so please export an object.

module.exports = {
  sendConfirmationEmail
}

package.json Outdated
"dependencies": {
"mailgun-js": "^0.22.0",
"pg": "^8.0.2",
"promisify-call": "^2.0.4",
Copy link
Contributor

Choose a reason for hiding this comment

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

is this module necessary? I don't see you using it anywhere. if not, please remove it.

};

return mg.messages().send(data).then((returnedData) => {
logger.info('results')
Copy link
Contributor

@songz songz Apr 28, 2020

Choose a reason for hiding this comment

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

logger.info('Confirmation Email successfully sent', returnedData)

return mg.messages().send(data).then((returnedData) => {
logger.info('results')
}).catch((error) => {
logger.error('error')
Copy link
Contributor

Choose a reason for hiding this comment

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

logger.info('Confirmation Email Error:', error)

return mg.messages().send(data).then((returnedData) => {
logger.info('Confirmation Email successfully sent', returnedData)
}).catch((error) => {
logger.info('Confirmation Email Error:', error)
Copy link
Contributor

Choose a reason for hiding this comment

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

logger.error

mgModule.sendConfirmationEmail = (receiver, token) => {
const link = `https://learndatabases.dev/emailConfirmation/${token}`
const data = {
from: process.env.SENDER_EMAIL,
Copy link
Contributor

Choose a reason for hiding this comment

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

admin@learndatabases.dev

log: jest.fn()
}

logGen.mockReturnValue(logger)
Copy link
Contributor

Choose a reason for hiding this comment

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

Test the logger or remove logGen

Copy link
Contributor

@songz songz left a comment

Choose a reason for hiding this comment

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

  1. Handle logger functions when send function resolves or throws.

@songz
Copy link
Contributor

songz commented May 7, 2020

replaced: #41 (review)

@songz songz closed this May 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants