From ca41b93995fc9d563482aaca7011bd96a0238bbc Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Tue, 22 Sep 2020 13:59:00 +0200 Subject: [PATCH] mail models and fix checkInbox --- back/methods/imap-time-control/checkInbox.js | 165 +++++++++++++------ back/model-config.json | 3 + back/models/mail-forward.json | 26 +++ back/models/mail.json | 3 +- db/tests/vn/workerTimeControlCheck.spec.js | 2 +- modules/worker/back/models/worker.json | 3 + 6 files changed, 145 insertions(+), 57 deletions(-) create mode 100644 back/models/mail-forward.json diff --git a/back/methods/imap-time-control/checkInbox.js b/back/methods/imap-time-control/checkInbox.js index ff0b0a46e..4f3cb06b0 100644 --- a/back/methods/imap-time-control/checkInbox.js +++ b/back/methods/imap-time-control/checkInbox.js @@ -1,5 +1,5 @@ const Imap = require('imap'); - +const {NULL} = require('node-sass'); module.exports = Self => { Self.remoteMethod('checkInbox', { description: 'Check an email inbox and process it', @@ -26,7 +26,8 @@ module.exports = Self => { tls: true }); let isEmailOk; - let emailText = ''; + let uid; + let emailBody; function openInbox(cb) { imap.openBox('INBOX', true, cb); @@ -35,7 +36,7 @@ module.exports = Self => { imap.once('ready', function() { openInbox(function(err, box) { if (err) throw err; - let f = imap.seq.fetch('1:3', { + let f = imap.seq.fetch('1:*', { bodies: ['HEADER.FIELDS (FROM SUBJECT)', '1'], struct: true }); @@ -43,87 +44,143 @@ module.exports = Self => { isEmailOk = false; msg.on('body', function(stream, info) { let buffer = ''; + let bufferCopy = ''; stream.on('data', function(chunk) { + // console.log('chunk', chunk); buffer = chunk.toString('utf8'); - if (info.which === '1') { - emailText = buffer.split('\n')[0]; - emailText = emailText.toUpperCase(); - emailText = emailText.replace(/\s/g, ''); - if (emailText === 'OK') - isEmailOk = true; // isEmailOk + // console.log('buffer', buffer); + if (info.which === '1' && bufferCopy.length == 0) + bufferCopy = buffer.replace(/\s/g, ' '); + }); + stream.on('end', function() { + if (bufferCopy.length > 0) { + emailBody = bufferCopy.toUpperCase().trim(); + + const bodyPositionOK = emailBody.match(/\bOK\b/i); + + if (bodyPositionOK != null && (bodyPositionOK.index == 0 || bodyPositionOK.index == 122)) + isEmailOk = true; + else + isEmailOk = false; } }); - stream.once('end', function() { + 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)') { - console.log('isEmailOk', isEmailOk); if (isEmailOk) { + imap.move(uid, 'exito', function(err) { + }); emailConfirm(buffer); - imap.seq.move(seqno, 'exito', function(err) { - console.log('Move correcte: ' + err); - console.log('Move error: ' + seqno); - }); } else { - emailReply(buffer); - imap.seq.move(seqno, 'error', function(err) { - console.log('Move error: ' + err); - console.log('Move error: ' + seqno); + imap.move(uid, 'error', function(err) { }); + emailReply(buffer, emailBody); } } }); }); }); - // msg.on('attributes', function(attrs) { - // console.log('ATRIBUTO', attrs.struct[0]); - // console.log('ATRIBUTO', attrs.struct[0].params); - // }); + f.once('end', function() { + imap.end(); + }); }); }); imap.connect(); - return 'pepinillos'; + return 'Leer emails de gestion horaria'; }; - async function getUser(workerEmail) { - let firstPosition = workerEmail.search('<') + 1; - let lastPosition = workerEmail.search('@') - firstPosition; - let userName = workerEmail.substr(firstPosition, lastPosition); - let user = await Self.app.models.Account.findOne({where: {name: userName}}); - return user; - } - - async function getEmailDate(subject) { - let date = subject.match(/\d+/g); - return date; - } async function emailConfirm(buffer) { - let now = new Date(); - let from = JSON.stringify(Imap.parseHeader(buffer).from); - let subject = JSON.stringify(Imap.parseHeader(buffer).subject); + // try { + const now = new Date(); + const from = JSON.stringify(Imap.parseHeader(buffer).from); + const subject = JSON.stringify(Imap.parseHeader(buffer).subject); - let timeControlDate = await getEmailDate(subject); - let week = timeControlDate[0]; - let year = timeControlDate[1]; - let user = await getUser(from); + const timeControlDate = await getEmailDate(subject); + const week = timeControlDate[0]; + const year = timeControlDate[1]; + const user = await getUser(from); let workerMail = await Self.app.models.WorkerTimeControlMail.findOne({ where: { week: week, year: year, - worker: user.id + workerFk: user.id } }); - await workerMail.updateAttributes({ - updated: now, - state: 'CONFIRMED' - }); - // MOVER A CARPETA EXITO + if (workerMail != NULL) { + await workerMail.updateAttributes({ + updated: now, + state: 'CONFIRMED' + }); + } + // } catch (err) { + // throw err; + // } } - async function emailReply(subject, workerEmail) { - console.log('bbbbb'); - // INSERT EN VN.MAIL - // UPDATE EN WORKERTIMECONTROLMAIL - // MOVER A CARPETA ERROR + async function emailReply(buffer, emailBody) { + // try { + const now = new Date(); + const from = JSON.stringify(Imap.parseHeader(buffer).from); + const subject = JSON.stringify(Imap.parseHeader(buffer).subject); + + const timeControlDate = await getEmailDate(subject); + const week = timeControlDate[0]; + const year = timeControlDate[1]; + const user = await getUser(from); + + 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' + }); + } + + await sendMail(user, subject, emailBody); + // } catch (err) { + // throw err; + // } + } + + async function getUser(workerEmail) { + const userEmail = workerEmail.match(/(?<=<)(.*?)(?=>)/); + + let [user] = await Self.rawSql(`SELECT u.id,u.name FROM account.user u + LEFT JOIN account.mailForward m on m.account = u.id + WHERE forwardTo =? OR + CONCAT(u.name,'@verdnatura.es') = ?`, + [userEmail[0], userEmail[0]]); + + return user; + } + + async function getEmailDate(subject) { + const date = subject.match(/\d+/g); + return date; + } + + async function sendMail(user, subject, emailBody) { + const sendTo = 'rrhh@verdnatura.es'; + const emailSubject = subject + ' ' + user.name; + + await Self.app.models.Mail.create({ + sender: sendTo, + subject: emailSubject, + body: emailBody + }); } }; diff --git a/back/model-config.json b/back/model-config.json index 46733696f..07900448c 100644 --- a/back/model-config.json +++ b/back/model-config.json @@ -79,6 +79,9 @@ }, "Mail": { "dataSource": "vn" + }, + "MailForward": { + "dataSource": "vn" } } diff --git a/back/models/mail-forward.json b/back/models/mail-forward.json new file mode 100644 index 000000000..40b695a7a --- /dev/null +++ b/back/models/mail-forward.json @@ -0,0 +1,26 @@ +{ + "name": "MailForward", + "base": "VnModel", + "options": { + "mysql": { + "table": "account.mailForward" + } + }, + "properties": { + "account": { + "id": true, + "type": "Number" + }, + "forwardTo": { + "type": "String" + } + }, + "acls": [ + { + "accessType": "READ", + "principalType": "ROLE", + "principalId": "$everyone", + "permission": "ALLOW" + } + ] +} diff --git a/back/models/mail.json b/back/models/mail.json index cf5c11993..3861460f3 100644 --- a/back/models/mail.json +++ b/back/models/mail.json @@ -9,8 +9,7 @@ "properties": { "id": { "id": true, - "type": "Number", - "required": true + "type": "Number" }, "sender": { "type": "String" diff --git a/db/tests/vn/workerTimeControlCheck.spec.js b/db/tests/vn/workerTimeControlCheck.spec.js index 4869e38a6..f70a0b1c3 100644 --- a/db/tests/vn/workerTimeControlCheck.spec.js +++ b/db/tests/vn/workerTimeControlCheck.spec.js @@ -1,7 +1,7 @@ const app = require('vn-loopback/server/server'); const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; -// #2257 xdescribe dbtest workerTimeControl_check() +// #2261 xdescribe dbtest workerTimeControl_check() xdescribe('worker workerTimeControl_check()', () => { it(`should throw an error if the worker can't sign on that tablet`, async() => { let stmts = []; diff --git a/modules/worker/back/models/worker.json b/modules/worker/back/models/worker.json index 6af7e8608..45eee23bf 100644 --- a/modules/worker/back/models/worker.json +++ b/modules/worker/back/models/worker.json @@ -31,6 +31,9 @@ "userFk": { "type" : "Number", "required": true + }, + "bossFk": { + "type" : "Number" } }, "relations": {