From 28dc557e2fe14da7f57af59c3d70b2c069b708cb Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Wed, 23 Sep 2020 07:35:45 +0200 Subject: [PATCH] fix checkInbox --- back/methods/imap-time-control/checkInbox.js | 64 +++++++++----------- back/models/worker-time-control-mail.json | 3 + 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/back/methods/imap-time-control/checkInbox.js b/back/methods/imap-time-control/checkInbox.js index 4f3cb06b0..a79fe4418 100644 --- a/back/methods/imap-time-control/checkInbox.js +++ b/back/methods/imap-time-control/checkInbox.js @@ -46,9 +46,7 @@ module.exports = Self => { let buffer = ''; let bufferCopy = ''; stream.on('data', function(chunk) { - // console.log('chunk', chunk); buffer = chunk.toString('utf8'); - // console.log('buffer', buffer); if (info.which === '1' && bufferCopy.length == 0) bufferCopy = buffer.replace(/\s/g, ' '); }); @@ -66,10 +64,6 @@ module.exports = Self => { }); msg.once('attributes', function(attrs) { uid = attrs.uid; - // let structure = attrs.struct[2][0]; - // console.log('attrs.struct', structure); - // console.log('attrs', structure.params); - // console.log('attrs.struct', structure.find(item => item.subtype === 'html')); }); msg.once('end', function() { if (info.which === 'HEADER.FIELDS (FROM SUBJECT)') { @@ -97,7 +91,7 @@ module.exports = Self => { }; async function emailConfirm(buffer) { - // try { + console.log('buffer', buffer); const now = new Date(); const from = JSON.stringify(Imap.parseHeader(buffer).from); const subject = JSON.stringify(Imap.parseHeader(buffer).subject); @@ -106,27 +100,28 @@ module.exports = Self => { const week = timeControlDate[0]; const year = timeControlDate[1]; const user = await getUser(from); + let workerMail; - let workerMail = await Self.app.models.WorkerTimeControlMail.findOne({ - where: { - week: week, - year: year, - workerFk: user.id - } - }); + if (user.id != NULL) { + workerMail = await Self.app.models.WorkerTimeControlMail.findOne({ + where: { + week: week, + year: year, + workerFk: user.id + } + }); + } if (workerMail != NULL) { await workerMail.updateAttributes({ updated: now, state: 'CONFIRMED' }); } - // } catch (err) { - // throw err; - // } } async function emailReply(buffer, emailBody) { - // try { + console.log('buffer', buffer); + console.log('emailBody', emailBody); const now = new Date(); const from = JSON.stringify(Imap.parseHeader(buffer).from); const subject = JSON.stringify(Imap.parseHeader(buffer).subject); @@ -135,25 +130,26 @@ module.exports = Self => { const week = timeControlDate[0]; const year = timeControlDate[1]; const user = await getUser(from); + let workerMail; - let workerMail = await Self.app.models.WorkerTimeControlMail.findOne({ - where: { - week: week, - year: year, - workerFk: user.id - } - }); - if (workerMail != NULL) { - await workerMail.updateAttributes({ - updated: now, - state: 'REVISE' + if (user.id != NULL) { + workerMail = await Self.app.models.WorkerTimeControlMail.findOne({ + where: { + week: week, + year: year, + workerFk: user.id + } }); - } - await sendMail(user, subject, emailBody); - // } catch (err) { - // throw err; - // } + if (workerMail != NULL) { + await workerMail.updateAttributes({ + updated: now, + state: 'REVISE', + emailResponse: emailBody + }); + } else + await sendMail(user, subject, emailBody); + } } async function getUser(workerEmail) { diff --git a/back/models/worker-time-control-mail.json b/back/models/worker-time-control-mail.json index 4a0fd31f1..81ca235d7 100644 --- a/back/models/worker-time-control-mail.json +++ b/back/models/worker-time-control-mail.json @@ -26,6 +26,9 @@ }, "updated": { "type": "Date" + }, + "emailResponse": { + "type": "String" } }, "acls": [