2019-11-18 14:04:51 +00:00
|
|
|
const db = require('../core/database');
|
|
|
|
const Email = require('../core/email');
|
2021-10-25 09:58:23 +00:00
|
|
|
const Report = require('../core/report');
|
2019-11-18 14:04:51 +00:00
|
|
|
const smtp = require('../core/smtp');
|
|
|
|
const config = require('../core/config');
|
|
|
|
|
|
|
|
module.exports = app => {
|
2020-06-16 06:17:01 +00:00
|
|
|
app.get('/api/closure/all', async function(req, res, next) {
|
|
|
|
try {
|
2021-03-15 10:07:44 +00:00
|
|
|
const reqArgs = req.args;
|
|
|
|
if (!reqArgs.to)
|
|
|
|
throw new Error('The argument to is required');
|
|
|
|
|
2020-06-16 06:17:01 +00:00
|
|
|
res.status(200).json({
|
|
|
|
message: 'Task executed successfully'
|
|
|
|
});
|
|
|
|
|
2020-09-14 06:51:46 +00:00
|
|
|
const tickets = await db.rawSql(`
|
2020-06-16 06:17:01 +00:00
|
|
|
SELECT
|
2020-09-14 06:51:46 +00:00
|
|
|
t.id
|
2020-06-16 06:17:01 +00:00
|
|
|
FROM expedition e
|
|
|
|
JOIN ticket t ON t.id = e.ticketFk
|
|
|
|
JOIN warehouse wh ON wh.id = t.warehouseFk AND wh.hasComission
|
|
|
|
JOIN ticketState ts ON ts.ticketFk = t.id
|
2021-06-29 16:01:33 +00:00
|
|
|
JOIN alertLevel al ON al.id = ts.alertLevel
|
2020-06-16 06:17:01 +00:00
|
|
|
WHERE al.code = 'PACKED'
|
2021-04-27 08:29:09 +00:00
|
|
|
AND DATE(t.shipped) BETWEEN DATE_ADD(?, INTERVAL -2 DAY)
|
|
|
|
AND util.dayEnd(?)
|
2020-06-16 06:17:01 +00:00
|
|
|
AND t.refFk IS NULL
|
2021-04-27 08:29:09 +00:00
|
|
|
GROUP BY e.ticketFk`, [reqArgs.to, reqArgs.to]);
|
2020-09-14 06:51:46 +00:00
|
|
|
const ticketIds = tickets.map(ticket => ticket.id);
|
2020-09-07 08:54:39 +00:00
|
|
|
|
2020-09-14 06:51:46 +00:00
|
|
|
await closeAll(ticketIds, req.args);
|
2020-09-07 08:54:39 +00:00
|
|
|
await db.rawSql(`
|
2020-09-14 06:51:46 +00:00
|
|
|
UPDATE ticket t
|
|
|
|
JOIN ticketState ts ON t.id = ts.ticketFk
|
2021-06-29 16:01:33 +00:00
|
|
|
JOIN alertLevel al ON al.id = ts.alertLevel
|
2020-09-14 06:51:46 +00:00
|
|
|
JOIN agencyMode am ON am.id = t.agencyModeFk
|
|
|
|
JOIN deliveryMethod dm ON dm.id = am.deliveryMethodFk
|
|
|
|
JOIN zone z ON z.id = t.zoneFk
|
|
|
|
SET t.routeFk = NULL
|
2021-04-27 08:29:09 +00:00
|
|
|
WHERE DATE(t.shipped) BETWEEN DATE_ADD(?, INTERVAL -2 DAY)
|
|
|
|
AND util.dayEnd(?)
|
2020-09-14 06:51:46 +00:00
|
|
|
AND al.code NOT IN('DELIVERED','PACKED')
|
|
|
|
AND t.routeFk
|
2021-04-27 08:29:09 +00:00
|
|
|
AND z.name LIKE '%MADRID%'`, [reqArgs.to, reqArgs.to]);
|
2020-06-16 06:17:01 +00:00
|
|
|
} catch (error) {
|
|
|
|
next(error);
|
|
|
|
}
|
2020-06-02 16:28:13 +00:00
|
|
|
});
|
|
|
|
|
2020-06-16 06:17:01 +00:00
|
|
|
app.get('/api/closure/by-ticket', async function(req, res, next) {
|
|
|
|
try {
|
|
|
|
const reqArgs = req.args;
|
|
|
|
if (!reqArgs.ticketId)
|
|
|
|
throw new Error('The argument ticketId is required');
|
2020-06-02 16:28:13 +00:00
|
|
|
|
2020-06-16 06:17:01 +00:00
|
|
|
res.status(200).json({
|
|
|
|
message: 'Task executed successfully'
|
|
|
|
});
|
|
|
|
|
2020-09-14 06:51:46 +00:00
|
|
|
const tickets = await db.rawSql(`
|
2020-06-16 06:17:01 +00:00
|
|
|
SELECT
|
2020-09-14 06:51:46 +00:00
|
|
|
t.id
|
2020-06-16 06:17:01 +00:00
|
|
|
FROM expedition e
|
|
|
|
JOIN ticket t ON t.id = e.ticketFk
|
|
|
|
JOIN ticketState ts ON ts.ticketFk = t.id
|
2021-06-29 16:01:33 +00:00
|
|
|
JOIN alertLevel al ON al.id = ts.alertLevel
|
2020-06-16 06:17:01 +00:00
|
|
|
WHERE al.code = 'PACKED'
|
2021-04-27 08:29:09 +00:00
|
|
|
AND t.id = ?
|
2020-06-16 06:17:01 +00:00
|
|
|
AND t.refFk IS NULL
|
2021-04-27 08:29:09 +00:00
|
|
|
GROUP BY e.ticketFk`, [reqArgs.ticketId]);
|
2020-09-14 06:51:46 +00:00
|
|
|
const ticketIds = tickets.map(ticket => ticket.id);
|
2020-06-16 06:17:01 +00:00
|
|
|
|
2020-09-14 06:51:46 +00:00
|
|
|
await closeAll(ticketIds, reqArgs);
|
2020-06-16 06:17:01 +00:00
|
|
|
} catch (error) {
|
|
|
|
next(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-01-04 08:22:32 +00:00
|
|
|
app.get('/api/closure/by-agency', async function(req, res, next) {
|
2020-06-16 06:17:01 +00:00
|
|
|
try {
|
|
|
|
const reqArgs = req.args;
|
|
|
|
if (!reqArgs.agencyModeId)
|
|
|
|
throw new Error('The argument agencyModeId is required');
|
|
|
|
|
|
|
|
if (!reqArgs.warehouseId)
|
|
|
|
throw new Error('The argument warehouseId is required');
|
|
|
|
|
|
|
|
if (!reqArgs.to)
|
|
|
|
throw new Error('The argument to is required');
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
message: 'Task executed successfully'
|
|
|
|
});
|
|
|
|
|
2021-01-04 08:22:32 +00:00
|
|
|
const agenciesId = reqArgs.agencyModeId.split(',');
|
2020-09-14 06:51:46 +00:00
|
|
|
const tickets = await db.rawSql(`
|
2020-06-16 06:17:01 +00:00
|
|
|
SELECT
|
2020-09-14 06:51:46 +00:00
|
|
|
t.id
|
2020-06-16 06:17:01 +00:00
|
|
|
FROM expedition e
|
|
|
|
JOIN ticket t ON t.id = e.ticketFk
|
|
|
|
JOIN ticketState ts ON ts.ticketFk = t.id
|
2021-06-29 16:01:33 +00:00
|
|
|
JOIN alertLevel al ON al.id = ts.alertLevel
|
2020-06-16 06:17:01 +00:00
|
|
|
WHERE al.code = 'PACKED'
|
2021-04-27 08:29:09 +00:00
|
|
|
AND t.agencyModeFk IN(?)
|
|
|
|
AND t.warehouseFk = ?
|
2021-03-15 11:03:19 +00:00
|
|
|
AND DATE(t.shipped) BETWEEN DATE_ADD(:to, INTERVAL -2 DAY)
|
2021-04-27 08:29:09 +00:00
|
|
|
AND util.dayEnd(?)
|
2020-06-16 06:17:01 +00:00
|
|
|
AND t.refFk IS NULL
|
2021-04-27 08:29:09 +00:00
|
|
|
GROUP BY e.ticketFk`, [
|
|
|
|
agenciesId,
|
|
|
|
reqArgs.warehouseId,
|
|
|
|
reqArgs.to,
|
|
|
|
reqArgs.to
|
|
|
|
]);
|
2020-09-14 06:51:46 +00:00
|
|
|
const ticketIds = tickets.map(ticket => ticket.id);
|
2020-06-16 06:17:01 +00:00
|
|
|
|
2020-09-14 06:51:46 +00:00
|
|
|
await closeAll(ticketIds, reqArgs);
|
2020-06-16 06:17:01 +00:00
|
|
|
} catch (error) {
|
|
|
|
next(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-01-04 08:22:32 +00:00
|
|
|
app.get('/api/closure/by-route', async function(req, res, next) {
|
2020-06-16 06:17:01 +00:00
|
|
|
try {
|
|
|
|
const reqArgs = req.args;
|
|
|
|
if (!reqArgs.routeId)
|
|
|
|
throw new Error('The argument routeId is required');
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
message: 'Task executed successfully'
|
|
|
|
});
|
|
|
|
|
2020-09-14 06:51:46 +00:00
|
|
|
const tickets = await db.rawSql(`
|
2020-06-16 06:17:01 +00:00
|
|
|
SELECT
|
2020-09-14 06:51:46 +00:00
|
|
|
t.id
|
2020-06-16 06:17:01 +00:00
|
|
|
FROM expedition e
|
|
|
|
JOIN ticket t ON t.id = e.ticketFk
|
|
|
|
JOIN ticketState ts ON ts.ticketFk = t.id
|
2021-06-29 16:01:33 +00:00
|
|
|
JOIN alertLevel al ON al.id = ts.alertLevel
|
2020-06-16 06:17:01 +00:00
|
|
|
WHERE al.code = 'PACKED'
|
2021-04-27 08:29:09 +00:00
|
|
|
AND t.routeFk = ?
|
2020-06-16 06:17:01 +00:00
|
|
|
AND t.refFk IS NULL
|
2021-04-27 08:29:09 +00:00
|
|
|
GROUP BY e.ticketFk`, [reqArgs.routeId]);
|
2020-09-14 06:51:46 +00:00
|
|
|
const ticketIds = tickets.map(ticket => ticket.id);
|
|
|
|
await closeAll(ticketIds, reqArgs);
|
2021-09-10 08:38:19 +00:00
|
|
|
|
|
|
|
// Send route report to the agency
|
|
|
|
const agencyMail = await db.findValue(`
|
|
|
|
SELECT am.reportMail
|
|
|
|
FROM route r
|
|
|
|
JOIN agencyMode am ON am.id = r.agencyModeFk
|
|
|
|
WHERE r.id = ?`, [reqArgs.routeId]);
|
|
|
|
|
|
|
|
if (agencyMail) {
|
|
|
|
const args = Object.assign({
|
|
|
|
routeId: reqArgs.routeId,
|
|
|
|
recipient: agencyMail
|
|
|
|
}, reqArgs);
|
|
|
|
|
|
|
|
const email = new Email('driver-route', args);
|
|
|
|
await email.send();
|
|
|
|
}
|
2020-06-16 06:17:01 +00:00
|
|
|
} catch (error) {
|
|
|
|
next(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-09-14 06:51:46 +00:00
|
|
|
async function closeAll(ticketIds, reqArgs) {
|
2019-11-18 14:04:51 +00:00
|
|
|
const failedtickets = [];
|
|
|
|
const tickets = await db.rawSql(`
|
|
|
|
SELECT
|
|
|
|
t.id,
|
|
|
|
t.clientFk,
|
2020-12-22 13:56:21 +00:00
|
|
|
c.name clientName,
|
2020-06-03 09:32:30 +00:00
|
|
|
c.email recipient,
|
2020-06-03 12:56:34 +00:00
|
|
|
c.salesPersonFk,
|
2020-06-16 06:17:01 +00:00
|
|
|
c.isToBeMailed,
|
|
|
|
c.hasToInvoice,
|
|
|
|
co.hasDailyInvoice,
|
2020-06-03 09:32:30 +00:00
|
|
|
eu.email salesPersonEmail
|
2020-09-14 06:51:46 +00:00
|
|
|
FROM ticket t
|
2019-11-18 14:04:51 +00:00
|
|
|
JOIN client c ON c.id = t.clientFk
|
2020-06-16 06:17:01 +00:00
|
|
|
JOIN province p ON p.id = c.provinceFk
|
|
|
|
JOIN country co ON co.id = p.countryFk
|
2020-09-14 06:51:46 +00:00
|
|
|
LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk
|
|
|
|
WHERE t.id IN(?)`, [ticketIds]);
|
2019-11-18 14:04:51 +00:00
|
|
|
|
|
|
|
for (const ticket of tickets) {
|
|
|
|
try {
|
2021-07-20 11:06:31 +00:00
|
|
|
await db.rawSql(`CALL vn.ticket_closeByTicket(?)`, [ticket.id]);
|
2019-11-18 14:04:51 +00:00
|
|
|
|
2021-10-19 11:46:17 +00:00
|
|
|
if (!ticket.salesPersonFk || !ticket.isToBeMailed) continue;
|
2020-06-03 09:32:30 +00:00
|
|
|
|
|
|
|
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/>
|
2021-10-25 09:58:23 +00:00
|
|
|
Para dejar de recibir esta notificación, asígnale un email o desactiva
|
|
|
|
la notificación por email para este cliente.`;
|
2020-06-03 09:32:30 +00:00
|
|
|
smtp.send({
|
|
|
|
to: ticket.salesPersonEmail,
|
|
|
|
subject: 'No se ha podido enviar el albarán',
|
|
|
|
html: body
|
|
|
|
});
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-10-19 11:46:17 +00:00
|
|
|
const hasToInvoice = ticket.hasToInvoice && ticket.hasDailyInvoice;
|
|
|
|
if (hasToInvoice) {
|
2021-10-25 09:58:23 +00:00
|
|
|
const invoice = await db.findOne(`
|
|
|
|
SELECT io.id, io.ref, io.serial, cny.code companyCode
|
2021-10-19 11:46:17 +00:00
|
|
|
FROM ticket t
|
|
|
|
JOIN invoiceOut io ON io.ref = t.refFk
|
2021-10-25 09:58:23 +00:00
|
|
|
JOIN company cny ON cny.id = io.companyFk
|
2021-10-19 11:46:17 +00:00
|
|
|
WHERE t.id = ?
|
|
|
|
`, [ticket.id]);
|
|
|
|
|
|
|
|
const args = Object.assign({
|
2021-10-25 09:58:23 +00:00
|
|
|
invoiceId: invoice.id,
|
2021-10-19 11:46:17 +00:00
|
|
|
recipientId: ticket.clientFk,
|
|
|
|
recipient: ticket.recipient,
|
|
|
|
replyTo: ticket.salesPersonEmail
|
|
|
|
}, reqArgs);
|
|
|
|
|
2021-10-25 09:58:23 +00:00
|
|
|
let mailOptions = {};
|
|
|
|
if (invoice.serial == 'E' && invoice.companyCode == 'VNL') {
|
|
|
|
const exportation = new Report('exportation', args);
|
|
|
|
const stream = await exportation.toPdfStream();
|
|
|
|
const fileName = `exportation-${invoice.ref}.pdf`;
|
|
|
|
mailOptions = {
|
|
|
|
overrideAttachments: false,
|
|
|
|
attachments: [{
|
|
|
|
filename: fileName,
|
|
|
|
content: stream
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-19 11:46:17 +00:00
|
|
|
const email = new Email('invoice', args);
|
2021-10-25 09:58:23 +00:00
|
|
|
await email.send(mailOptions);
|
2021-10-19 11:46:17 +00:00
|
|
|
} else {
|
|
|
|
const args = Object.assign({
|
|
|
|
ticketId: ticket.id,
|
|
|
|
recipientId: ticket.clientFk,
|
|
|
|
recipient: ticket.recipient,
|
|
|
|
replyTo: ticket.salesPersonEmail
|
|
|
|
}, reqArgs);
|
|
|
|
|
|
|
|
const email = new Email('delivery-note-link', args);
|
|
|
|
await email.send();
|
|
|
|
}
|
2019-11-18 14:04:51 +00:00
|
|
|
} catch (error) {
|
2020-12-22 13:56:21 +00:00
|
|
|
// Domain not found
|
|
|
|
if (error.responseCode == 450)
|
|
|
|
return invalidEmail(ticket);
|
|
|
|
|
2019-11-18 14:04:51 +00:00
|
|
|
// 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
|
|
|
});
|
|
|
|
}
|
2020-06-16 06:17:01 +00:00
|
|
|
}
|
2020-12-22 13:56:21 +00:00
|
|
|
|
|
|
|
async function invalidEmail(ticket) {
|
2021-04-27 08:29:09 +00:00
|
|
|
await db.rawSql(`UPDATE client SET email = NULL WHERE id = ?`, [
|
|
|
|
ticket.clientFk
|
|
|
|
]);
|
2020-12-22 13:56:21 +00:00
|
|
|
|
|
|
|
const oldInstance = `{"email": "${ticket.recipient}"}`;
|
|
|
|
const newInstance = `{"email": ""}`;
|
|
|
|
await db.rawSql(`
|
|
|
|
INSERT INTO clientLog (originFk, userFk, action, changedModel, oldInstance, newInstance)
|
2021-04-27 08:29:09 +00:00
|
|
|
VALUES (?, NULL, 'UPDATE', 'Client', ?, ?)`, [
|
|
|
|
ticket.clientFk,
|
|
|
|
oldInstance,
|
|
|
|
newInstance
|
|
|
|
]);
|
2020-12-22 13:56:21 +00:00
|
|
|
|
|
|
|
const body = `No se ha podido enviar el albarán <strong>${ticket.id}</strong>
|
|
|
|
al cliente <strong>${ticket.clientFk} - ${ticket.clientName}</strong>
|
|
|
|
porque la dirección de email <strong>"${ticket.recipient}"</strong> no es correcta o no está disponible.<br/><br/>
|
|
|
|
Para evitar que se repita este error, se ha eliminado la dirección de email de la ficha del cliente.
|
|
|
|
Actualiza la dirección de email con una correcta.`;
|
|
|
|
|
|
|
|
smtp.send({
|
|
|
|
to: ticket.salesPersonEmail,
|
|
|
|
subject: 'No se ha podido enviar el albarán',
|
|
|
|
html: body
|
|
|
|
});
|
|
|
|
}
|
2019-11-18 14:04:51 +00:00
|
|
|
};
|