2020-08-11 09:12:40 +00:00
|
|
|
const Imap = require('imap');
|
2020-09-22 11:59:00 +00:00
|
|
|
const {NULL} = require('node-sass');
|
2020-08-11 09:12:40 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('checkInbox', {
|
|
|
|
description: 'Check an email inbox and process it',
|
|
|
|
accessType: 'READ',
|
|
|
|
returns:
|
|
|
|
{
|
|
|
|
arg: 'body',
|
|
|
|
type: 'file',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/checkInbox`,
|
|
|
|
verb: 'GET'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.checkInbox = async() => {
|
|
|
|
let imapConfig = await Self.app.models.WorkerTimeControlParams.findOne();
|
|
|
|
let imap = new Imap({
|
|
|
|
user: imapConfig.mailUser,
|
|
|
|
password: imapConfig.mailPass,
|
|
|
|
host: imapConfig.mailHost,
|
|
|
|
port: 993,
|
|
|
|
tls: true
|
|
|
|
});
|
|
|
|
let isEmailOk;
|
2020-09-22 11:59:00 +00:00
|
|
|
let uid;
|
|
|
|
let emailBody;
|
2020-08-11 09:12:40 +00:00
|
|
|
|
|
|
|
function openInbox(cb) {
|
|
|
|
imap.openBox('INBOX', true, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
imap.once('ready', function() {
|
|
|
|
openInbox(function(err, box) {
|
|
|
|
if (err) throw err;
|
2020-09-22 11:59:00 +00:00
|
|
|
let f = imap.seq.fetch('1:*', {
|
2020-08-11 09:12:40 +00:00
|
|
|
bodies: ['HEADER.FIELDS (FROM SUBJECT)', '1'],
|
|
|
|
struct: true
|
|
|
|
});
|
|
|
|
f.on('message', function(msg, seqno) {
|
|
|
|
isEmailOk = false;
|
|
|
|
msg.on('body', function(stream, info) {
|
|
|
|
let buffer = '';
|
2020-09-22 11:59:00 +00:00
|
|
|
let bufferCopy = '';
|
2020-08-11 09:12:40 +00:00
|
|
|
stream.on('data', function(chunk) {
|
2020-09-22 11:59:00 +00:00
|
|
|
// console.log('chunk', chunk);
|
2020-08-11 09:12:40 +00:00
|
|
|
buffer = chunk.toString('utf8');
|
2020-09-22 11:59:00 +00:00
|
|
|
// 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;
|
2020-08-11 09:12:40 +00:00
|
|
|
}
|
|
|
|
});
|
2020-09-22 11:59:00 +00:00
|
|
|
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() {
|
2020-08-11 09:12:40 +00:00
|
|
|
if (info.which === 'HEADER.FIELDS (FROM SUBJECT)') {
|
|
|
|
if (isEmailOk) {
|
2020-09-22 11:59:00 +00:00
|
|
|
imap.move(uid, 'exito', function(err) {
|
2020-08-11 09:12:40 +00:00
|
|
|
});
|
2020-09-22 11:59:00 +00:00
|
|
|
emailConfirm(buffer);
|
2020-08-11 09:12:40 +00:00
|
|
|
} else {
|
2020-09-22 11:59:00 +00:00
|
|
|
imap.move(uid, 'error', function(err) {
|
2020-08-11 09:12:40 +00:00
|
|
|
});
|
2020-09-22 11:59:00 +00:00
|
|
|
emailReply(buffer, emailBody);
|
2020-08-11 09:12:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-09-22 11:59:00 +00:00
|
|
|
f.once('end', function() {
|
|
|
|
imap.end();
|
|
|
|
});
|
2020-08-11 09:12:40 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
imap.connect();
|
2020-09-22 11:59:00 +00:00
|
|
|
return 'Leer emails de gestion horaria';
|
2020-08-11 09:12:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
async function emailConfirm(buffer) {
|
2020-09-22 11:59:00 +00:00
|
|
|
// try {
|
|
|
|
const now = new Date();
|
|
|
|
const from = JSON.stringify(Imap.parseHeader(buffer).from);
|
|
|
|
const subject = JSON.stringify(Imap.parseHeader(buffer).subject);
|
2020-08-11 09:12:40 +00:00
|
|
|
|
2020-09-22 11:59:00 +00:00
|
|
|
const timeControlDate = await getEmailDate(subject);
|
|
|
|
const week = timeControlDate[0];
|
|
|
|
const year = timeControlDate[1];
|
|
|
|
const user = await getUser(from);
|
2020-08-11 09:12:40 +00:00
|
|
|
|
|
|
|
let workerMail = await Self.app.models.WorkerTimeControlMail.findOne({
|
|
|
|
where: {
|
|
|
|
week: week,
|
|
|
|
year: year,
|
2020-09-22 11:59:00 +00:00
|
|
|
workerFk: user.id
|
2020-08-11 09:12:40 +00:00
|
|
|
}
|
|
|
|
});
|
2020-09-22 11:59:00 +00:00
|
|
|
if (workerMail != NULL) {
|
|
|
|
await workerMail.updateAttributes({
|
|
|
|
updated: now,
|
|
|
|
state: 'CONFIRMED'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// } catch (err) {
|
|
|
|
// throw err;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-08-11 09:12:40 +00:00
|
|
|
});
|
2020-09-22 11:59:00 +00:00
|
|
|
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;
|
2020-08-11 09:12:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 11:59:00 +00:00
|
|
|
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
|
|
|
|
});
|
2020-08-11 09:12:40 +00:00
|
|
|
}
|
|
|
|
};
|