diff --git a/modules/ticket/back/methods/ticket/saveSign.js b/modules/ticket/back/methods/ticket/saveSign.js index 9667847d2..9888328e7 100644 --- a/modules/ticket/back/methods/ticket/saveSign.js +++ b/modules/ticket/back/methods/ticket/saveSign.js @@ -44,40 +44,6 @@ module.exports = Self => { myOptions.transaction = tx; } - try { - for (const ticketId of tickets) { - 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'); - if (ticketState.alertLevel < packedAlertLevel.id) - throw new UserError('This ticket cannot be signed because it has not been boxed'); - if (await gestDocExists(ticketId)) - throw new UserError('Ticket is already signed'); - - if (location) setLocation(ticketId); - if (!gestDocCreated) await createGestDoc(ticketId); - await models.TicketDms.create({ticketFk: ticketId, dmsFk: dms[0].id}, myOptions); - const ticket = await models.Ticket.findById(ticketId, null, myOptions); - await ticket.updateAttribute('isSigned', true, myOptions); - const params = {ticketFk: ticketId, code: 'DELIVERED'}; - await models.Ticket.state(ctx, params, options); - } - - if (tx) await tx.commit(); - return; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - async function setLocation(ticketId) { await models.Delivery.create({ ticketFk: ticketId, @@ -137,9 +103,41 @@ module.exports = Self => { description: `Firma del cliente - Ruta ${ticket.route().id}`, hasFile: false }; - dms = await models.Dms.uploadFile(ctxUploadFile, myOptions); gestDocCreated = true; } + + try { + for (const ticketId of tickets) { + 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'); + if (ticketState.alertLevel < packedAlertLevel.id) + throw new UserError('This ticket cannot be signed because it has not been boxed'); + if (await gestDocExists(ticketId)) + throw new UserError('Ticket is already signed'); + + if (location) setLocation(ticketId); + if (!gestDocCreated) await createGestDoc(ticketId); + await models.TicketDms.create({ticketFk: ticketId, dmsFk: dms[0].id}, myOptions); + const ticket = await models.Ticket.findById(ticketId, null, myOptions); + await ticket.updateAttribute('isSigned', true, myOptions); + await Self.rawSql(`CALL vn.ticket_setState(?, ?)`, [ticketId, 'DELIVERED'], myOptions); + } + + if (tx) await tx.commit(); + return; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; };