From a359ace1b8550b4213a5c620a4cc94329c45a26a Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 26 Feb 2024 12:57:49 +0100 Subject: [PATCH 1/5] fix: refs #6184 saveSign --- .../ticket/back/methods/ticket/saveSign.js | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/modules/ticket/back/methods/ticket/saveSign.js b/modules/ticket/back/methods/ticket/saveSign.js index 14968c203..7eb4b4144 100644 --- a/modules/ticket/back/methods/ticket/saveSign.js +++ b/modules/ticket/back/methods/ticket/saveSign.js @@ -59,19 +59,15 @@ module.exports = Self => { } async function hasSignDms(ticketId) { - const ticketDms = await models.TicketDms.findOne({ - where: {ticketFk: ticketId}, - include: [ - { - relation: 'dms', - fields: ['id'], - scope: { - where: {dmsTypeFk: dmsTypeTicket.id} - } - } - ] - }); - if (ticketDms?.dms()?.id) return true; + const hasTicketDms = await Self.rawSql(` + SELECT d.id + FROM ticketDms td + JOIN dms d ON d.id = td.dmsFk + WHERE td.ticketFk = ? + AND d.dmsTypeFk = ? + `, [ticketId, dmsTypeTicket.id]); + + if (hasTicketDms.length) return true; } async function createGestDoc() { -- 2.40.1 From 817f685621acc760ca74a2df04ef3be46bba0cab Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 26 Feb 2024 13:00:35 +0100 Subject: [PATCH 2/5] fix: refs #6184 saveSign --- modules/ticket/back/methods/ticket/saveSign.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/ticket/back/methods/ticket/saveSign.js b/modules/ticket/back/methods/ticket/saveSign.js index 7eb4b4144..58599716a 100644 --- a/modules/ticket/back/methods/ticket/saveSign.js +++ b/modules/ticket/back/methods/ticket/saveSign.js @@ -65,7 +65,7 @@ module.exports = Self => { JOIN dms d ON d.id = td.dmsFk WHERE td.ticketFk = ? AND d.dmsTypeFk = ? - `, [ticketId, dmsTypeTicket.id]); + `, [ticketId, dmsTypeTicket.id], myOptions); if (hasTicketDms.length) return true; } @@ -108,17 +108,17 @@ module.exports = Self => { fields: ['id'] } }] - }); + }, myOptions); const ticketState = await models.TicketState.findOne({ where: {ticketFk: ticketId}, fields: ['alertLevel'] - }); + }, myOptions); const packedAlertLevel = await models.AlertLevel.findOne({ where: {code: 'PACKED'}, fields: ['id'] - }); + }, myOptions); if (!ticketState) throw new UserError('Ticket does not exist'); @@ -135,7 +135,7 @@ module.exports = Self => { const deliveryState = await models.State.findOne({ where: {code: 'DELIVERED'} - }); + }, myOptions); await models.Ticket.state(ctx, { ticketFk: ticketId, -- 2.40.1 From c1c64d0f89dcbc51e34c2b86318da14ddbdea875 Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 26 Feb 2024 13:39:57 +0100 Subject: [PATCH 3/5] fix: refs #6184 saveSign --- modules/ticket/back/methods/ticket/saveSign.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/modules/ticket/back/methods/ticket/saveSign.js b/modules/ticket/back/methods/ticket/saveSign.js index 58599716a..ea9f715a9 100644 --- a/modules/ticket/back/methods/ticket/saveSign.js +++ b/modules/ticket/back/methods/ticket/saveSign.js @@ -34,6 +34,8 @@ module.exports = Self => { const myOptions = {userId: ctx.req.accessToken.userId}; let tx; let ticket; + let dms; + let isSignUploaded; let externalTickets = []; if (typeof options == 'object') @@ -80,8 +82,8 @@ module.exports = Self => { description: `Firma del cliente - Ruta ${ticket.route().id}`, hasFile: true }; - const dms = await models.Dms.uploadFile(ctxUploadFile, myOptions); - await models.TicketDms.create({ticketFk: ticket.id, dmsFk: dms[0].id}, myOptions); + dms = await models.Dms.uploadFile(ctxUploadFile, myOptions); + isSignUploaded = true; } try { @@ -130,7 +132,9 @@ module.exports = Self => { throw new UserError('Ticket is already signed'); if (location) await setLocation(ticketId); - if (!await hasSignDms(ticketId)) await createGestDoc(ticketId); + if (!await hasSignDms(ticketId) && !isSignUploaded) + await createGestDoc(ticketId); + await models.TicketDms.create({ticketFk: ticket.id, dmsFk: dms[0].id}, myOptions); await ticket.updateAttribute('isSigned', true, myOptions); const deliveryState = await models.State.findOne({ -- 2.40.1 From a4419a2542bb69e89e840d4412d2ebc2a8fd7322 Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 26 Feb 2024 13:57:07 +0100 Subject: [PATCH 4/5] fix: refs #6184 saveSign --- modules/route/back/methods/route/cmrEmail.js | 2 +- modules/ticket/back/methods/ticket/saveSign.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/route/back/methods/route/cmrEmail.js b/modules/route/back/methods/route/cmrEmail.js index d05d72100..0c4cc5061 100644 --- a/modules/route/back/methods/route/cmrEmail.js +++ b/modules/route/back/methods/route/cmrEmail.js @@ -57,7 +57,7 @@ module.exports = Self => { AND dt.code = 'cmr' `, [ticketId]); - if (!dms.lenght) throw new UserError('Cmr file does not exist'); + if (!dms.length) throw new UserError('Cmr file does not exist'); const response = await models.Dms.downloadFile(ctx, dms[0].id); diff --git a/modules/ticket/back/methods/ticket/saveSign.js b/modules/ticket/back/methods/ticket/saveSign.js index ea9f715a9..00211b6b5 100644 --- a/modules/ticket/back/methods/ticket/saveSign.js +++ b/modules/ticket/back/methods/ticket/saveSign.js @@ -134,7 +134,8 @@ module.exports = Self => { if (location) await setLocation(ticketId); if (!await hasSignDms(ticketId) && !isSignUploaded) await createGestDoc(ticketId); - await models.TicketDms.create({ticketFk: ticket.id, dmsFk: dms[0].id}, myOptions); + if (isSignUploaded) + await models.TicketDms.create({ticketFk: ticket.id, dmsFk: dms[0].id}, myOptions); await ticket.updateAttribute('isSigned', true, myOptions); const deliveryState = await models.State.findOne({ -- 2.40.1 From 096987040eeed5c3a37feb8eea4b6e55169baf37 Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 26 Feb 2024 14:00:04 +0100 Subject: [PATCH 5/5] fix: refs #6184 saveSign --- modules/ticket/back/methods/ticket/saveSign.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/ticket/back/methods/ticket/saveSign.js b/modules/ticket/back/methods/ticket/saveSign.js index 00211b6b5..aa970237f 100644 --- a/modules/ticket/back/methods/ticket/saveSign.js +++ b/modules/ticket/back/methods/ticket/saveSign.js @@ -83,6 +83,8 @@ module.exports = Self => { hasFile: true }; dms = await models.Dms.uploadFile(ctxUploadFile, myOptions); + // Si se ha subido ya la firma, no se vuelve a subir, ya que si no + // da un error de deadlock en la db isSignUploaded = true; } -- 2.40.1