From 73eca9905e91bb33a2e0f712edb837acd2d09b9e Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Wed, 25 Sep 2019 11:26:12 +0200 Subject: [PATCH] fixed #1734 canHaveStowaway validation --- loopback/locale/en.json | 3 ++- ...{canBeStowawayed.js => canHaveStowaway.js} | 13 +++++----- .../methods/ticket/getPossibleStowaways.js | 9 ++++--- ...awayed.spec.js => canHaveStowaway.spec.js} | 6 ++--- modules/ticket/back/models/stowaway.js | 9 ++++--- modules/ticket/back/models/ticket.js | 2 +- .../ticket/front/descriptor/addStowaway.html | 11 +++++--- .../ticket/front/descriptor/addStowaway.js | 17 ++++--------- modules/ticket/front/descriptor/index.html | 5 +++- modules/ticket/front/descriptor/index.js | 10 +------- modules/ticket/front/descriptor/index.spec.js | 25 +++++++++++++++++-- 11 files changed, 64 insertions(+), 46 deletions(-) rename modules/ticket/back/methods/ticket/{canBeStowawayed.js => canHaveStowaway.js} (55%) rename modules/ticket/back/methods/ticket/specs/{canBeStowawayed.spec.js => canHaveStowaway.spec.js} (66%) diff --git a/loopback/locale/en.json b/loopback/locale/en.json index 4c29d0a70..862dd999e 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -55,5 +55,6 @@ "This ticket can not be modified": "This ticket can not be modified", "You can't delete a confirmed order": "You can't delete a confirmed order", "Value has an invalid format": "Value has an invalid format", - "The postcode doesn't exists. Ensure you put the correct format": "The postcode doesn't exists. Ensure you put the correct format" + "The postcode doesn't exists. Ensure you put the correct format": "The postcode doesn't exists. Ensure you put the correct format", + "Can't create stowaway for this ticket": "Can't create stowaway for this ticket" } \ No newline at end of file diff --git a/modules/ticket/back/methods/ticket/canBeStowawayed.js b/modules/ticket/back/methods/ticket/canHaveStowaway.js similarity index 55% rename from modules/ticket/back/methods/ticket/canBeStowawayed.js rename to modules/ticket/back/methods/ticket/canHaveStowaway.js index 72b6d7f46..0d64c2d11 100644 --- a/modules/ticket/back/methods/ticket/canBeStowawayed.js +++ b/modules/ticket/back/methods/ticket/canHaveStowaway.js @@ -1,7 +1,7 @@ module.exports = Self => { - Self.remoteMethod('canBeStowawayed', { - description: 'Returns if a ticket can be stowawayed', + Self.remoteMethod('canHaveStowaway', { + description: 'Returns if a ticket can have stowaway', accessType: 'READ', accepts: [{ arg: 'id', @@ -14,14 +14,15 @@ module.exports = Self => { root: true }, http: { - path: `/:id/canBeStowawayed`, + path: `/:id/canHaveStowaway`, verb: 'GET' } }); - Self.canBeStowawayed = async id => { - const ticket = await Self.app.models.Ticket.findById(id); - const warehouse = await Self.app.models.Warehouse.findById(ticket.warehouseFk); + Self.canHaveStowaway = async id => { + const models = Self.app.models; + const ticket = await models.Ticket.findById(id); + const warehouse = await models.Warehouse.findById(ticket.warehouseFk); if (warehouse && warehouse.hasStowaway) return true; diff --git a/modules/ticket/back/methods/ticket/getPossibleStowaways.js b/modules/ticket/back/methods/ticket/getPossibleStowaways.js index 2b3194f86..f692c9f23 100644 --- a/modules/ticket/back/methods/ticket/getPossibleStowaways.js +++ b/modules/ticket/back/methods/ticket/getPossibleStowaways.js @@ -21,12 +21,13 @@ module.exports = Self => { }); Self.getPossibleStowaways = async ticketFk => { - let canStowaway = await Self.app.models.Ticket.canBeStowawayed(ticketFk); + const models = Self.app.models; + const canHaveStowaway = await models.Ticket.canHaveStowaway(ticketFk); - if (!canStowaway) + if (!canHaveStowaway) throw new UserError(`Can't create stowaway for this ticket`); - let ship = await Self.app.models.Ticket.findById(ticketFk); + let ship = await models.Ticket.findById(ticketFk); if (!ship || !ship.shipped) return []; @@ -38,7 +39,7 @@ module.exports = Self => { highestDate.setHours(23, 59, 59); - let possibleStowaways = await Self.app.models.Ticket.find({ + let possibleStowaways = await models.Ticket.find({ where: { id: {neq: ticketFk}, clientFk: ship.clientFk, diff --git a/modules/ticket/back/methods/ticket/specs/canBeStowawayed.spec.js b/modules/ticket/back/methods/ticket/specs/canHaveStowaway.spec.js similarity index 66% rename from modules/ticket/back/methods/ticket/specs/canBeStowawayed.spec.js rename to modules/ticket/back/methods/ticket/specs/canHaveStowaway.spec.js index 49a775225..231f2581b 100644 --- a/modules/ticket/back/methods/ticket/specs/canBeStowawayed.spec.js +++ b/modules/ticket/back/methods/ticket/specs/canHaveStowaway.spec.js @@ -1,16 +1,16 @@ const app = require('vn-loopback/server/server'); -describe('ticket canBeStowawayed()', () => { +describe('ticket canHaveStowaway()', () => { it('should return true if the ticket warehouse have hasStowaway equal 1', async() => { const ticketId = 16; - let canStowaway = await app.models.Ticket.canBeStowawayed(ticketId); + let canStowaway = await app.models.Ticket.canHaveStowaway(ticketId); expect(canStowaway).toBeTruthy(); }); it('should return false if the ticket warehouse dont have hasStowaway equal 0', async() => { const ticketId = 10; - let canStowaway = await app.models.Ticket.canBeStowawayed(ticketId); + let canStowaway = await app.models.Ticket.canHaveStowaway(ticketId); expect(canStowaway).toBeFalsy(); }); diff --git a/modules/ticket/back/models/stowaway.js b/modules/ticket/back/models/stowaway.js index a8f967d24..aa21e8680 100644 --- a/modules/ticket/back/models/stowaway.js +++ b/modules/ticket/back/models/stowaway.js @@ -3,21 +3,22 @@ const UserError = require('vn-loopback/util/user-error'); module.exports = function(Self) { Self.observe('before save', async function(ctx) { - let isStowaway = await Self.app.models.Ticket.canBeStowawayed(ctx.instance.id); + const models = Self.app.models; + const canHaveStowaway = await models.Ticket.canHaveStowaway(ctx.instance.shipFk); - if (!isStowaway) + if (!canHaveStowaway) throw new UserError(`Can't create stowaway for this ticket`); if (ctx.isNewInstance) { let where = { code: 'BOARDING' }; - let state = await Self.app.models.State.findOne({where}); + let state = await models.State.findOne({where}); let params = {ticketFk: ctx.instance.id, stateFk: state.id}; const loopBackContext = LoopBackContext.getCurrentContext(); let httpCtx = {req: loopBackContext.active}; - await Self.app.models.TicketTracking.changeState(httpCtx, params); + await models.TicketTracking.changeState(httpCtx, params); } }); }; diff --git a/modules/ticket/back/models/ticket.js b/modules/ticket/back/models/ticket.js index 21a41783f..ed3a85da2 100644 --- a/modules/ticket/back/models/ticket.js +++ b/modules/ticket/back/models/ticket.js @@ -25,7 +25,7 @@ module.exports = Self => { require('../methods/ticket/uploadFile')(Self); require('../methods/ticket/addSale')(Self); require('../methods/ticket/transferSales')(Self); - require('../methods/ticket/canBeStowawayed')(Self); + require('../methods/ticket/canHaveStowaway')(Self); Self.observe('before save', async function(ctx) { if (ctx.isNewInstance) return; diff --git a/modules/ticket/front/descriptor/addStowaway.html b/modules/ticket/front/descriptor/addStowaway.html index 349ae137e..74554d2be 100644 --- a/modules/ticket/front/descriptor/addStowaway.html +++ b/modules/ticket/front/descriptor/addStowaway.html @@ -1,13 +1,18 @@ + + + on-open="model.reload()">
Stowaways to add
- + Ticket id @@ -18,7 +23,7 @@ - + {{ticket.id}} {{ticket.landed | dateTime: 'dd/MM/yyyy'}} {{ticket.agencyMode.name}} diff --git a/modules/ticket/front/descriptor/addStowaway.js b/modules/ticket/front/descriptor/addStowaway.js index b5f6d82fe..ad6750fdf 100644 --- a/modules/ticket/front/descriptor/addStowaway.js +++ b/modules/ticket/front/descriptor/addStowaway.js @@ -1,23 +1,16 @@ import ngModule from '../module'; class Controller { - constructor($state, $, $http, vnApp, $translate) { + constructor($stateParams, $, $http, vnApp, $translate) { this.vnApp = vnApp; this.$translate = $translate; this.$ = $; - this.$state = $state; + this.$stateParams = $stateParams; this.$http = $http; } - getPossibleStowaways() { - this.$http.get(`/api/Tickets/${this.ticket.id}/getPossibleStowaways`) - .then(res => { - this.possibleStowaways = res.data; - }); - } - - addStowaway(index) { - let params = {id: this.possibleStowaways[index].id, shipFk: this.ticket.id}; + addStowaway(stowaway) { + let params = {id: stowaway.id, shipFk: this.ticket.id}; this.$http.post(`/api/Stowaways/`, params) .then(() => { this.cardReload(); @@ -35,7 +28,7 @@ class Controller { } } -Controller.$inject = ['$state', '$scope', '$http', 'vnApp', '$translate']; +Controller.$inject = ['$stateParams', '$scope', '$http', 'vnApp', '$translate']; ngModule.component('vnAddStowaway', { template: require('./addStowaway.html'), diff --git a/modules/ticket/front/descriptor/index.html b/modules/ticket/front/descriptor/index.html index 07e0217c0..c377a23b1 100644 --- a/modules/ticket/front/descriptor/index.html +++ b/modules/ticket/front/descriptor/index.html @@ -158,7 +158,10 @@ question="You are going to delete this ticket" message="This ticket will be removed from current route! Continue anyway?"> - + + { + this.$http.get(`/api/Tickets/${this.ticket.id}/canHaveStowaway`).then(response => { if (response.data === true) return this.canShowStowaway = true; diff --git a/modules/ticket/front/descriptor/index.spec.js b/modules/ticket/front/descriptor/index.spec.js index 57ae5abe7..6fc167a41 100644 --- a/modules/ticket/front/descriptor/index.spec.js +++ b/modules/ticket/front/descriptor/index.spec.js @@ -135,12 +135,33 @@ describe('Ticket Component vnTicketDescriptor', () => { }); }); + + describe('showAddStowaway()', () => { + it('should show a dialog with a list of tickets available for an stowaway', () => { + controller.$scope.addStowaway = {}; + controller.$scope.addStowaway.show = jasmine.createSpy('show'); + controller.showAddStowaway(); + + expect(controller.$scope.addStowaway.show).toHaveBeenCalledWith(); + }); + }); + + describe('showRemoveStowaway()', () => { + it('should show a dialog for an stowaway removal', () => { + controller.$scope.removeStowaway = {}; + controller.$scope.removeStowaway.show = jasmine.createSpy('show'); + controller.showRemoveStowaway(); + + expect(controller.$scope.removeStowaway.show).toHaveBeenCalledWith(); + }); + }); + describe('canStowaway()', () => { it('should make a query and return if the ticket can be stowawayed', () => { controller.ticket.id = 16; spyOn(controller, 'isTicketModule').and.callThrough(); - $httpBackend.when('GET', '/api/Tickets/16/canBeStowawayed').respond(true); - $httpBackend.expect('GET', '/api/Tickets/16/canBeStowawayed').respond(true); + $httpBackend.when('GET', '/api/Tickets/16/canHaveStowaway').respond(true); + $httpBackend.expect('GET', '/api/Tickets/16/canHaveStowaway').respond(true); controller.canStowaway(); $httpBackend.flush();