Skip to content

Commit

Permalink
fix(mailcheck): deleting message before finished processing
Browse files Browse the repository at this point in the history
polonel committed Jan 15, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent b383ad5 commit 41924ea
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/mailer/mailCheck.js
Original file line number Diff line number Diff line change
@@ -127,8 +127,9 @@ function bindImapError () {
function bindImapReady () {
try {
mailCheck.Imap.on('end', function () {
handleMessages(mailCheck.messages)
mailCheck.Imap.destroy()
handleMessages(mailCheck.messages, function () {
mailCheck.Imap.destroy()
})
})

mailCheck.Imap.on('ready', function () {
@@ -148,17 +149,13 @@ function bindImapReady () {
return next()
}

winston.debug('Processed %s Mail > Ticket', _.size(results))
winston.debug('Processing %s Mail', _.size(results))

var flag = '\\Seen'
if (mailCheck.fetchMailOptions.deleteMessage) {
flag = '\\Deleted'
}

mailCheck.Imap.addFlags(results, flag, function (err) {
if (err) winston.warn(err)
})

var message = {}

var f = mailCheck.Imap.fetch(results, {
@@ -201,11 +198,20 @@ function bindImapReady () {
})

f.on('end', function () {
mailCheck.Imap.closeBox(true, function (err) {
if (err) winston.warn(err)

return next()
})
async.series(
[
function (cb) {
mailCheck.Imap.addFlags(results, flag, cb)
},
function (cb) {
mailCheck.Imap.closeBox(true, cb)
}
],
function (err) {
if (err) winston.warn(err)
return next()
}
)
})
}
],
@@ -233,7 +239,8 @@ mailCheck.fetchMail = function () {
}
}

function handleMessages (messages) {
function handleMessages (messages, done) {
var count = 0
messages.forEach(function (message) {
if (
!_.isUndefined(message.from) &&
@@ -376,16 +383,17 @@ function handleMessages (messages) {
ticket: ticket
})

count++
return callback()
}
)
}
]
},
function (err) {
if (err) {
winston.warn(err)
}
winston.debug('Created %s tickets from mail', count)
if (err) winston.warn(err)
return done(err)
}
)
}

0 comments on commit 41924ea

Please sign in to comment.