salix/back/methods/imap-time-control/checkInbox.js

180 lines
6.1 KiB
JavaScript
Raw Normal View History

2020-08-11 09:12:40 +00:00
const Imap = require('imap');
2020-09-23 07:11:00 +00:00
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) {
buffer = chunk.toString('utf8');
2020-09-22 11:59:00 +00:00
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;
});
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
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-09-23 05:35:45 +00:00
let workerMail;
if (user.id != NULL) {
workerMail = await Self.app.models.WorkerTimeControlMail.findOne({
where: {
week: week,
year: year,
workerFk: user.id
}
});
}
2020-09-22 11:59:00 +00:00
if (workerMail != NULL) {
await workerMail.updateAttributes({
updated: now,
state: 'CONFIRMED'
});
}
}
async function emailReply(buffer, emailBody) {
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);
2020-09-23 05:35:45 +00:00
let workerMail;
if (user.id != NULL) {
workerMail = await Self.app.models.WorkerTimeControlMail.findOne({
where: {
week: week,
year: year,
workerFk: user.id
}
2020-09-22 11:59:00 +00:00
});
2020-09-23 05:35:45 +00:00
if (workerMail != NULL) {
await workerMail.updateAttributes({
updated: now,
state: 'REVISE',
emailResponse: emailBody
});
} else
await sendMail(user, subject, emailBody);
}
2020-09-22 11:59:00 +00:00
}
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
}
};