diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 45993bdd5..664a3cf0e 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -290,5 +290,7 @@ "isTaxDataChecked": "Datos comprobados", "comercialId": "Id comercial", "comercialName": "Comercial", - "Invalid NIF for VIES": "Invalid NIF for VIES" -} \ No newline at end of file + "Invalid NIF for VIES": "Invalid NIF for VIES", + "Ticket does not exist": "Este ticket no existe", + "Ticket is already signed": "Este ticket ya ha sido firmado" +} diff --git a/modules/ticket/back/methods/ticket/saveSign.js b/modules/ticket/back/methods/ticket/saveSign.js index aede7de8e..b8f9d5ced 100644 --- a/modules/ticket/back/methods/ticket/saveSign.js +++ b/modules/ticket/back/methods/ticket/saveSign.js @@ -29,8 +29,7 @@ module.exports = Self => { } }); - Self.saveSign = async(ctx, options) => { - const args = Object.assign({}, ctx.args); + Self.saveSign = async(ctx, tickets, location, signedTime, options) => { const models = Self.app.models; const myOptions = {}; let tx; @@ -48,9 +47,9 @@ module.exports = Self => { async function setLocation(ticketId) { await models.Delivery.create({ ticketFk: ticketId, - longitude: args.location.Longitude, - latitude: args.location.Latitude, - dated: args.signedTime || new Date() + longitude: location.Longitude, + latitude: location.Latitude, + dated: signedTime || new Date() }, myOptions); } @@ -107,9 +106,9 @@ module.exports = Self => { } try { - for (let i = 0; i < args.tickets.length; i++) { + for (const ticketId of tickets) { const ticketState = await models.TicketState.findOne( - {where: {ticketFk: args.tickets[i]}, + {where: {ticketFk: ticketId}, fields: ['alertLevel'] }, myOptions); @@ -117,16 +116,19 @@ module.exports = Self => { 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'); - else if (!await gestDocExists(args.tickets[i])) { - if (args.location) setLocation(args.tickets[i]); - if (!gestDocCreated) await createGestDoc(args.tickets[i]); - await models.TicketDms.create({ticketFk: args.tickets[i], dmsFk: dms[0].id}, myOptions); - const ticket = await models.Ticket.findById(args.tickets[i], null, myOptions); - await ticket.updateAttribute('isSigned', true, myOptions); - await Self.rawSql(`CALL vn.ticket_setState(?, ?)`, [args.tickets[i], 'DELIVERED'], myOptions); - } + 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();