Skip to content

Commit

Permalink
fix(mailcheck): deleting message before finished processing
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Jan 15, 2020
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
Expand Up @@ -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 () {
Expand All @@ -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, {
Expand Down Expand Up @@ -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()
}
)
})
}
],
Expand Down Expand Up @@ -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) &&
Expand Down Expand Up @@ -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)
}
)
}
Expand Down

0 comments on commit 41924ea

Please sign in to comment.