From 7c3c3a2d66ff836ccaa2f93e9f71ca4fb4763cb1 Mon Sep 17 00:00:00 2001 From: alexandre Date: Thu, 9 Mar 2023 08:31:38 +0100 Subject: [PATCH] refs #4632 added userError --- loopback/locale/es.json | 3 +- .../back/methods/ticket-dms/saveSign.js | 25 ++++++------ .../methods/ticket-dms/specs/saveSign.spec.js | 39 ++++--------------- 3 files changed, 21 insertions(+), 46 deletions(-) diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 507cc9003..0c624ed3d 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -266,5 +266,6 @@ "There is no assigned email for this client": "No hay correo asignado para este cliente", "This locker has already been assigned": "Esta taquilla ya ha sido asignada", "Tickets with associated refunds": "No se pueden borrar tickets con abonos asociados. Este ticket está asociado al abono Nº {{id}}", - "Not exist this branch": "La rama no existe" + "Not exist this branch": "La rama no existe", + "This ticket cannot be signed because it has not been boxed": "Este ticket no puede firmarse porque no ha sido encajado" } diff --git a/modules/ticket/back/methods/ticket-dms/saveSign.js b/modules/ticket/back/methods/ticket-dms/saveSign.js index 9f1684539..60dd03be1 100644 --- a/modules/ticket/back/methods/ticket-dms/saveSign.js +++ b/modules/ticket/back/methods/ticket-dms/saveSign.js @@ -1,3 +1,5 @@ +const UserError = require('vn-loopback/util/user-error'); + module.exports = Self => { Self.remoteMethodCtx('saveSign', { description: 'Save sign', @@ -46,21 +48,17 @@ module.exports = Self => { } async function setLocation(ticketId) { - if (args.signedTime) { - await models.Delivery.create({ - ticketFk: ticketId, - longitude: args.location.Longitude, - latitude: args.location.Latitude, - dated: args.signedTime || new Date() - }, myOptions); - } + await models.Delivery.create({ + ticketFk: ticketId, + longitude: args.location.Longitude, + latitude: args.location.Latitude, + dated: args.signedTime || new Date() + }, myOptions); } async function gestDocExists(ticketId) { const ticketDms = await models.TicketDms.findOne({ - where: { - ticketFk: ticketId - }, + where: {ticketFk: ticketId}, fields: ['dmsFk'] }, myOptions); @@ -120,12 +118,13 @@ module.exports = Self => { if (args.location) setLocation(args.tickets[i]); await createGestDoc(args.tickets[i]); await Self.rawSql(`CALL vn.ticket_setState(?, ?)`, [args.tickets[i], 'DELIVERED'], myOptions); - } + } else if (ticketState.alertLevel < 2) + throw new UserError('This ticket cannot be signed because it has not been boxed'); } if (tx) await tx.commit(); - return 'Signs uploaded correctly'; + return 'Signs uploaded successfully'; } catch (e) { if (tx) await tx.rollback(); throw e; diff --git a/modules/ticket/back/methods/ticket-dms/specs/saveSign.spec.js b/modules/ticket/back/methods/ticket-dms/specs/saveSign.spec.js index b640fefb8..47902e257 100644 --- a/modules/ticket/back/methods/ticket-dms/specs/saveSign.spec.js +++ b/modules/ticket/back/methods/ticket-dms/specs/saveSign.spec.js @@ -1,57 +1,32 @@ const models = require('vn-loopback/server/server').models; -fdescribe('TicketDms saveSign()', () => { +describe('TicketDms saveSign()', () => { const FormData = require('form-data'); const data = new FormData(); let ctx = {req: { accessToken: {userId: 9}, headers: { ...data.getHeaders() - }, - on: (param, cb) => {} + } + }}; - it(`should not save sign if the ticket's alert level is lower than 2`, async() => { + it(`should throw error if the ticket's alert level is lower than 2`, async() => { const tx = await models.TicketDms.beginTransaction({}); const ticketWithOkState = 12; - + let error; try { const options = {transaction: tx}; ctx.args = {tickets: [ticketWithOkState]}; await models.TicketDms.saveSign(ctx, options); - const ticket = await models.Ticket.findById(ticketWithOkState, {fields: ['isSigned']}, options); - const ticketDms = await models.TicketDms.findOne({where: {ticketFk: ticketWithOkState}}, options); - - expect(ticket.isSigned).toEqual(false); - expect(ticketDms).toEqual(null); await tx.rollback(); } catch (e) { + error = e; await tx.rollback(); - throw e; } - }); - it(`should save sign if the ticket's alert level is greater than 2`, async() => { - const tx = await models.TicketDms.beginTransaction({}); - const ticketWithPackedState = 7; - - try { - const options = {transaction: tx}; - ctx.args = {tickets: [ticketWithPackedState]}; - - await models.TicketDms.saveSign(ctx, options); - const ticket = await models.Ticket.findById(ticketWithPackedState, {fields: ['isSigned']}, options); - const ticketDms = await models.TicketDms.findOne({where: {ticketFk: ticketWithPackedState}}, options); - - expect(ticket.isSigned).toEqual(true); - expect(ticketDms).toBeDefined(); - - await tx.rollback(); - } catch (e) { - await tx.rollback(); - throw e; - } + expect(error).toBeDefined(); }); });