Last active
February 13, 2017 14:53
-
-
Save crizstian/bcc7f4b7eee44c57cec3859626455366 to your computer and use it in GitHub Desktop.
Example of di for notifications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = ({repo}, app) => { | |
// this our endpoint where is going to validate our email, and the create and finally send it | |
app.post('/notifiaction/sendEmail', (req, res, next) => { | |
const {validate} = req.container.cradle | |
validate(req.body.payload, 'notification') | |
.then(payload => { | |
return repo.sendEmail(payload) | |
}) | |
.then(ok => { | |
res.status(status.OK).json({msg: 'ok'}) | |
}) | |
.catch(next) | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sendEmail = (payload) => { | |
return new Promise((resolve, reject) => { | |
const {smtpSettings, smtpTransport, nodemailer} = container.cradle | |
const transporter = nodemailer.createTransport( | |
smtpTransport({ | |
service: smtpSettings.service, | |
auth: { | |
user: smtpSettings.user, | |
pass: smtpSettings.pass | |
} | |
})) | |
const mailOptions = { | |
from: '"Do Not Reply, Cinemas Company 👥" <[email protected]>', | |
to: `${payload.user.email}`, | |
subject: `Tickects for movie ${payload.movie.title}`, | |
html: ` | |
<h1>Tickest for ${payload.movie.title}</h1> | |
<p>Cinem: ${payload.cinema.name}</p> | |
<p>Room: ${payload.cinema.room}</p> | |
<p>Seats: ${payload.cinema.seats}</p> | |
<p>description: ${payload.description}</p> | |
<p>Total: ${payload.totalAmount}</p> | |
<p>Total: ${payload.orderId}</p> | |
<h3>Cinemas Microserivce 2017, Enjoy your movie !</h3> | |
` | |
} | |
transporter.sendMail(mailOptions, (err, info) => { | |
if (err) { | |
reject(new Error('An error occured sending an email, err:' + err)) | |
} | |
transporter.close() | |
resolve(info) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment