2019-11-18 14:04:51 +00:00
|
|
|
const db = require('../core/database');
|
|
|
|
const Email = require('../core/email');
|
|
|
|
const smtp = require('../core/smtp');
|
|
|
|
const config = require('../core/config');
|
|
|
|
|
|
|
|
module.exports = app => {
|
2020-06-02 16:28:13 +00:00
|
|
|
app.get('/api/closure/by-ticket', async function(request, response) {
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/api/closure/all', async function(request, response) {
|
|
|
|
response.status(200).json({
|
|
|
|
message: 'Task executed successfully'
|
|
|
|
});
|
|
|
|
|
2019-11-18 14:04:51 +00:00
|
|
|
const failedtickets = [];
|
|
|
|
const tickets = await db.rawSql(`
|
|
|
|
SELECT
|
|
|
|
t.id,
|
|
|
|
t.clientFk,
|
2020-06-03 09:32:30 +00:00
|
|
|
c.email recipient,
|
|
|
|
c.isToBeMailed,
|
2020-06-03 12:56:34 +00:00
|
|
|
c.salesPersonFk,
|
2020-06-03 09:32:30 +00:00
|
|
|
eu.email salesPersonEmail
|
2019-11-18 14:04:51 +00:00
|
|
|
FROM expedition e
|
|
|
|
JOIN ticket t ON t.id = e.ticketFk
|
|
|
|
JOIN client c ON c.id = t.clientFk
|
2020-06-03 09:32:30 +00:00
|
|
|
JOIN warehouse wh ON wh.id = t.warehouseFk AND wh.hasComission
|
|
|
|
JOIN ticketState ts ON ts.ticketFk = t.id
|
|
|
|
JOIN alertLevel al ON al.alertLevel = ts.alertLevel
|
|
|
|
LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk
|
|
|
|
WHERE al.code = 'PACKED'
|
2019-11-18 14:04:51 +00:00
|
|
|
AND DATE(t.shipped) BETWEEN DATE_ADD(CURDATE(), INTERVAL -2 DAY) AND CURDATE()
|
|
|
|
AND t.refFk IS NULL
|
|
|
|
GROUP BY e.ticketFk`);
|
|
|
|
|
|
|
|
for (const ticket of tickets) {
|
|
|
|
try {
|
2020-06-01 10:50:50 +00:00
|
|
|
await db.rawSql(`CALL vn.ticket_closeByTicket(:ticketId)`, {
|
2019-11-18 14:04:51 +00:00
|
|
|
ticketId: ticket.id
|
|
|
|
});
|
|
|
|
|
2020-06-03 09:32:30 +00:00
|
|
|
if (!ticket.salesPersonFk || !ticket.isToBeMailed) continue;
|
|
|
|
|
|
|
|
if (!ticket.recipient) {
|
|
|
|
const body = `No se ha podido enviar el albarán <strong>${ticket.id}</strong>
|
|
|
|
al cliente <strong>${ticket.clientFk}</strong> porque no tiene un email especificado.<br/><br/>
|
|
|
|
Para dejar de recibir esta notificación, asígnale un email o desactiva la notificación por email para este cliente.`;
|
|
|
|
smtp.send({
|
|
|
|
to: ticket.salesPersonEmail,
|
|
|
|
subject: 'No se ha podido enviar el albarán',
|
|
|
|
html: body
|
|
|
|
});
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-18 14:04:51 +00:00
|
|
|
const args = {
|
|
|
|
ticketId: ticket.id,
|
2020-05-22 13:20:55 +00:00
|
|
|
recipientId: ticket.clientFk,
|
2019-11-18 14:04:51 +00:00
|
|
|
recipient: ticket.recipient
|
|
|
|
};
|
|
|
|
const email = new Email('delivery-note-link', args);
|
|
|
|
await email.send();
|
|
|
|
} catch (error) {
|
|
|
|
// Save tickets on a list of failed ids
|
2019-11-19 10:53:01 +00:00
|
|
|
failedtickets.push({
|
|
|
|
id: ticket.id,
|
|
|
|
stacktrace: error
|
|
|
|
});
|
2019-11-18 14:04:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send email with failed tickets
|
|
|
|
if (failedtickets.length > 0) {
|
2020-06-03 09:32:30 +00:00
|
|
|
let body = 'This following tickets have failed:<br/><br/>';
|
2019-11-19 10:53:01 +00:00
|
|
|
|
|
|
|
for (ticket of failedtickets) {
|
|
|
|
body += `Ticket: <strong>${ticket.id}</strong>
|
|
|
|
<br/> <strong>${ticket.stacktrace}</strong><br/><br/>`;
|
|
|
|
}
|
|
|
|
|
2019-11-18 14:04:51 +00:00
|
|
|
smtp.send({
|
|
|
|
to: config.app.reportEmail,
|
2020-06-03 09:32:30 +00:00
|
|
|
subject: '[API] Nightly ticket closure report',
|
2019-11-19 10:53:01 +00:00
|
|
|
html: body
|
2019-11-18 14:04:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|