feat: enhance ticket closing process with email notifications and error handling

This commit is contained in:
Alex Moreno 2025-02-25 14:59:49 +01:00
parent 74abd1db32
commit fbdc715fa5
1 changed files with 55 additions and 28 deletions

View File

@ -1,5 +1,6 @@
const smtp = require('vn-print/core/smtp');
const config = require('vn-print/core/config');
const Email = require('vn-print/core/email');
module.exports = Self => {
Self.remoteMethodCtx('closeAll', {
@ -44,8 +45,7 @@ module.exports = Self => {
LIMIT 1`, [toDate, toDate], myOptions);
await Self.rawSql(`
DROP TEMPORARY TABLE IF EXISTS tmp.ticket_close;
CREATE TEMPORARY TABLE tmp.ticket_close
CREATE OR REPLACE TEMPORARY TABLE tmp.ticket_close
ENGINE = MEMORY
WITH wTickets AS(
SELECT t.id ticketFk
@ -63,11 +63,12 @@ module.exports = Self => {
FROM wTicketsTracking wt
JOIN ticketTracking tt ON tt.id = wt.maxTracking
) SELECT tls.ticketFk,
t.clientFk,
t.clientFk clientId,
c.name clientName,
c.email recipient,
c.isToBeMailed,
eu.email salesPersonEmail,
t.addressFk,
t.addressFk addressId,
c.hasDailyInvoice,
c.hasToInvoiceByAddress,
t.totalWithVat,
@ -79,7 +80,7 @@ module.exports = Self => {
JOIN agencyMode am ON am.id = t.agencyModeFk
JOIN client c ON c.id = t.clientFk
LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk
WHERE (al.code = 'PACKED' OR (am.code = 'refund' AND al.code <> 'delivered'));
WHERE al.code = 'PACKED' OR (am.code = 'refund' AND al.code <> 'delivered');
CALL ticket_close();
`, [dateFrom, dateTo], myOptions);
@ -98,21 +99,27 @@ module.exports = Self => {
AND tob.id IS NULL
AND t.routeFk`, [dateFrom, dateTo], myOptions);
const [clients] = await Self.rawSql(`
SELECT clientFk clientId,
clientName,
recipient,
salesPersonEmail,
addressFk addressId,
companyFk,
const clients = await Self.rawSql(`
SELECT *,
SUM(totalWithVat) total,
'quick' serialType
FROM tmp.ticket_close
WHERE hasDailyInvoice
GROUP BY IF (hasToInvoiceByAddress, addressFk, clientFk), companyFk
HAVING total > 0;
GROUP BY IF (hasToInvoiceByAddress, addressId, clientId), companyFk
HAVING total > 0
`, [], myOptions);
const [ticketsToMail] = await Self.rawSql(`
SELECT *
FROM tmp.ticket_close
WHERE NOT hasDailyInvoice
AND isToBeMailed
AND salesPersonEmail IS NOT NULL
AND salesPersonEmail <> ''
AND recipient IS NOT NULL
AND recipient <> '';
DROP TEMPORARY TABLE tmp.ticket_close;
`, [], myOptions);
`, [], myOptions);
if (tx)
await tx.commit();
@ -130,21 +137,23 @@ module.exports = Self => {
if (id)
await Self.app.models.InvoiceOut.makePdfAndNotify(ctx, id, null, nestedTransaction);
} catch (error) {
await Self.rawSql(`
INSERT INTO util.debug (variable, value)
VALUES ('invoicingTicketError', ?)
`, [client.clientId + ' - ' + error]);
await handleInvoicingError(error, client, failedClients);
}
}
if (error.responseCode == 450) {
await invalidEmail(client);
continue;
}
for (const ticket of ticketsToMail) {
const args = {
id: ticket.ticketFk,
recipientId: ticket.clientId,
recipient: ticket.recipient,
replyTo: ticket.salesPersonEmail,
};
failedClients.push({
id: client.clientId,
address: client.addressId,
error
});
try {
const email = new Email('delivery-note-link', args);
await email.send();
} catch (error) {
await handleInvoicingError(error, ticket, failedClients);
}
}
@ -188,4 +197,22 @@ module.exports = Self => {
html: body,
}).catch(err => console.error(err));
}
async function handleInvoicingError(error, entity, failedClients) {
await Self.rawSql(`
INSERT INTO util.debug (variable, value)
VALUES ('invoicingTicketError', ?)
`, [entity.clientId + ' - ' + error]);
if (error.responseCode == 450) {
await invalidEmail(entity);
return;
}
failedClients.push({
id: entity.clientId,
address: entity.addressId,
error
});
}
};