Skip to content

Instantly share code, notes, and snippets.

@crizstian
Last active February 13, 2017 14:53
Show Gist options
  • Save crizstian/bcc7f4b7eee44c57cec3859626455366 to your computer and use it in GitHub Desktop.
Save crizstian/bcc7f4b7eee44c57cec3859626455366 to your computer and use it in GitHub Desktop.
Example of di for notifications
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)
})
}
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