Skip to content

Commit

Permalink
fix(events): #294 server crash on sending mail
Browse files Browse the repository at this point in the history
Mailer respects MAILER_ENABLED flag during ticket creation/comment added.
  • Loading branch information
polonel committed Jan 2, 2020
1 parent 6aa507b commit b126338
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/emitter/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ var notifications = require('../notifications') // Load Push Events
if (err) return false

settingsSchema.getSettingsByName(
['tps:enable', 'tps:username', 'tps:apikey', 'gen:siteurl', 'beta:email'],
['tps:enable', 'tps:username', 'tps:apikey', 'gen:siteurl', 'mailer:enable', 'beta:email'],
function (err, tpsSettings) {
if (err) return false
var tpsEnabled = _.head(_.filter(tpsSettings, ['name', 'tps:enable']))
var tpsUsername = _.head(_.filter(tpsSettings, ['name', 'tps:username']))
var tpsApiKey = _.head(_.filter(tpsSettings, ['name', 'tps:apikey']))
var baseUrl = _.head(_.filter(tpsSettings, ['name', 'gen:siteurl'])).value
var betaEnabled = _.head(_.filter(tpsSettings, ['name', 'beta:email']))
var mailerEnabled = _.head(_.filter(tpsSettings, ['name', 'mailer:enable']))
mailerEnabled = !mailerEnabled ? false : mailerEnabled.value

var betaEnabled = _.head(_.filter(tpsSettings, ['name', 'beta:email']))
betaEnabled = !betaEnabled ? false : betaEnabled.value

if (!tpsEnabled || !tpsUsername || !tpsApiKey) {
Expand Down Expand Up @@ -118,6 +120,7 @@ var notifications = require('../notifications') // Load Push Events
},
function (err) {
if (err) return c(err)
if (!mailerEnabled) return c()

emails = _.uniq(emails)

Expand Down Expand Up @@ -165,15 +168,15 @@ var notifications = require('../notifications') // Load Push Events

mailer.sendMail(mailOptions, function (err) {
if (err) winston.warn('[trudesk:events:ticket:created] - ' + err)

winston.debug('SentMail')
return c()
})
})
.catch(function (err) {
winston.warn('[trudesk:events:ticket:created] - ' + err)
return c(err)
})
.finally(function () {
return c()
})
})
}
)
Expand Down Expand Up @@ -389,12 +392,17 @@ var notifications = require('../notifications') // Load Push Events
// Goes to client
io.sockets.emit('updateComments', ticket)

settingsSchema.getSettingsByName(['tps:enable', 'tps:username', 'tps:apikey'], function (err, tpsSettings) {
settingsSchema.getSettingsByName(['tps:enable', 'tps:username', 'tps:apikey', 'mailer:enable'], function (
err,
tpsSettings
) {
if (err) return false

var tpsEnabled = _.head(_.filter(tpsSettings, ['name', 'tps:enable']))
var tpsUsername = _.head(_.filter(tpsSettings, ['name', 'tps:username']))
var tpsApiKey = _.head(_.filter(tpsSettings), ['name', 'tps:apikey'])
var mailerEnabled = _.head(_.filter(tpsSettings), ['name', 'mailer:enable'])
mailerEnabled = !mailerEnabled ? false : mailerEnabled.value

if (!tpsEnabled || !tpsUsername || !tpsApiKey) {
tpsEnabled = false
Expand Down Expand Up @@ -456,6 +464,8 @@ var notifications = require('../notifications') // Load Push Events
},
// Send email to subscribed users
function (c) {
if (!mailerEnabled) return c()

var mailer = require('../mailer')
var emails = []
async.each(
Expand Down Expand Up @@ -505,18 +515,15 @@ var notifications = require('../notifications') // Load Push Events
}

mailer.sendMail(mailOptions, function (err) {
if (err) {
winston.warn('[trudesk:events:sendSubscriberEmail] - ' + err)
}
if (err) winston.warn('[trudesk:events:sendSubscriberEmail] - ' + err)
winston.debug('SentMail')
return c()
})
})
.catch(function (err) {
winston.warn('[trudesk:events:sendSubscriberEmail] - ' + err)
return c(err)
})
.finally(function () {
return c()
})
})
}
)
Expand Down

0 comments on commit b126338

Please sign in to comment.