From a60720d1ed3a82644546a52026b368b9236abb50 Mon Sep 17 00:00:00 2001 From: Joan Date: Fri, 17 Aug 2018 14:53:25 +0200 Subject: [PATCH] Ticket packaging refactor + front test updated --- client/ticket/src/package/index.html | 21 ++++----- client/ticket/src/package/index.js | 30 ++++-------- client/ticket/src/package/index.spec.js | 46 ++++++++++++++++--- services/loopback/common/locale/en.json | 1 - services/loopback/common/locale/es.json | 2 +- .../methods/packaging/crudTicketPackaging.js | 3 -- .../common/methods/packaging/listPackaging.js | 8 ++-- .../ticket/common/models/ticket-packaging.js | 4 +- 8 files changed, 66 insertions(+), 49 deletions(-) delete mode 100644 services/ticket/common/methods/packaging/crudTicketPackaging.js diff --git a/client/ticket/src/package/index.html b/client/ticket/src/package/index.html index 462dec31d..8d3230832 100644 --- a/client/ticket/src/package/index.html +++ b/client/ticket/src/package/index.html @@ -1,11 +1,13 @@ - - + + @@ -13,28 +15,25 @@ Packages - + - {{id}} : {{name}} + {{itemFk}} : {{name}} { - this.$.index.accept(); + this.$scope.watcher.notifySaved(); + this.$scope.model.refresh(); }); } @@ -54,14 +52,11 @@ class Controller { } getPackages() { - this.packages = this.$.index.model; + this.packages = this.$scope.model.data; this.setOldPackages(); } setOldPackages() { - if (this.oldPackages && !this.$.watcher.dataChanged()) - return; - this.oldPackages = []; this.removedPackages = []; this.packages.forEach(item => { @@ -72,16 +67,9 @@ class Controller { packageEquals(newPackage, oldPackage) { return newPackage.packagingFk === oldPackage.packagingFk && newPackage.quantity == oldPackage.quantity; } - - hasChanges(packagesObj) { - if (packagesObj.create.length || packagesObj.update.length || packagesObj.delete.length) - return true; - - return false; - } } -Controller.$inject = ['$http', '$scope', '$translate', 'vnApp']; +Controller.$inject = ['$http', '$scope', '$stateParams', '$translate', 'vnApp']; ngModule.component('vnTicketPackageIndex', { template: require('./index.html'), diff --git a/client/ticket/src/package/index.spec.js b/client/ticket/src/package/index.spec.js index f9d0ec606..3222c58c8 100644 --- a/client/ticket/src/package/index.spec.js +++ b/client/ticket/src/package/index.spec.js @@ -16,10 +16,13 @@ describe('Ticket', () => { $httpBackend = _$httpBackend_; $httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({}); $scope = { - index: { - accept: function() {} + model: { + refresh: () => {} }, - form: {} + watcher: { + check: () => {}, + notifySaved: () => {} + } }; controller = $componentController('vnTicketPackageIndex', {$scope: $scope}); })); @@ -35,8 +38,9 @@ describe('Ticket', () => { describe('submit()', () => { it('should perform a post', () => { - spyOn(controller.$.index, 'accept'); - let query = '/ticket/api/TicketPackagings/crudTicketPackaging'; + spyOn(controller.$scope.watcher, 'notifySaved'); + spyOn(controller.$scope.model, 'refresh'); + controller.removedPackages = []; controller.oldPackages = []; controller.oldPackages[1] = {id: 1, packagingFk: 1, quantity: 5, ticketFk: 1}; @@ -45,9 +49,39 @@ describe('Ticket', () => { {id: 1, packagingFk: 1, quantity: 25, ticketFk: 1} ]; - $httpBackend.expectPOST(query, {delete: [], create: [{quantity: 5, packagingFk: 2, ticketFk: 1}], update: [{id: 1, packagingFk: 1, quantity: 25, ticketFk: 1}]}).respond('ok'); + let data = { + delete: [], + create: [{quantity: 5, packagingFk: 2, ticketFk: 1}], + update: [{id: 1, packagingFk: 1, quantity: 25, ticketFk: 1}] + }; + + $httpBackend.when('POST', '/ticket/api/TicketPackagings/crud', data).respond(200); + $httpBackend.expect('POST', '/ticket/api/TicketPackagings/crud', data).respond('ok'); controller.submit(); $httpBackend.flush(); + + expect(controller.$scope.model.refresh).toHaveBeenCalledWith(); + expect(controller.$scope.watcher.notifySaved).toHaveBeenCalledWith(); + }); + }); + + describe('packageEquals()', () => { + it('should return true if the old package and the new one matches', () => { + let oldPackage = {quantity: 5, packagingFk: 2}; + let newPackage = {quantity: 5, packagingFk: 2}; + + let result = controller.packageEquals(oldPackage, newPackage); + + expect(result).toBeTruthy(); + }); + + it(`should return false if the old package and the new one doesn't match`, () => { + let oldPackage = {quantity: 5, packagingFk: 2}; + let newPackage = {quantity: 6, packagingFk: 2}; + + let result = controller.packageEquals(oldPackage, newPackage); + + expect(result).toBeFalsy(); }); }); }); diff --git a/services/loopback/common/locale/en.json b/services/loopback/common/locale/en.json index c6e8298c7..78b24b9f1 100644 --- a/services/loopback/common/locale/en.json +++ b/services/loopback/common/locale/en.json @@ -11,7 +11,6 @@ "Is invalid": "Is invalid", "Quantity cannot be zero": "Quantity cannot be zero", "Enter an integer different to zero": "Enter an integer different to zero", - "Package cannot be blank": "Package cannot be blank", "The company name must be unique": "The company name must be unique", "Invalid email": "Invalid email", "The IBAN does not have the correct format": "The IBAN does not have the correct format", diff --git a/services/loopback/common/locale/es.json b/services/loopback/common/locale/es.json index a2eba7d3c..3536a10b8 100644 --- a/services/loopback/common/locale/es.json +++ b/services/loopback/common/locale/es.json @@ -11,7 +11,7 @@ "Is invalid": "Is invalid", "Quantity cannot be zero": "La cantidad no puede ser cero", "Enter an integer different to zero": "Introduce un entero distinto de cero", - "Package cannot be blank": "Embalaje no puede estar en blanco", + "The package cannot be blank": "El embalaje no puede estar en blanco", "The company name must be unique": "La razón social debe ser única", "Invalid email": "Correo electrónico inválido", "The IBAN does not have the correct format": "El IBAN no tiene el formato correcto", diff --git a/services/ticket/common/methods/packaging/crudTicketPackaging.js b/services/ticket/common/methods/packaging/crudTicketPackaging.js deleted file mode 100644 index b5c4bc875..000000000 --- a/services/ticket/common/methods/packaging/crudTicketPackaging.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = Self => { - Self.installCrudModel('crudTicketPackaging'); -}; diff --git a/services/ticket/common/methods/packaging/listPackaging.js b/services/ticket/common/methods/packaging/listPackaging.js index 8e83a60a5..117b51000 100644 --- a/services/ticket/common/methods/packaging/listPackaging.js +++ b/services/ticket/common/methods/packaging/listPackaging.js @@ -24,11 +24,13 @@ module.exports = Self => { Self.listPackaging = async filter => { let stmt = new ParameterizedSQL( - `SELECT i.name, i.id, p.id packagingFk - FROM item i JOIN packaging p ON i.id = p.itemFk` + `SELECT name, itemFk, packagingFk + FROM (SELECT i.name, i.id itemFk, p.id packagingFk + FROM item i + JOIN packaging p ON i.id = p.itemFk) p` ); - stmt.merge(Self.buildSuffix(filter, 'i')); + stmt.merge(Self.buildSuffix(filter)); return Self.rawStmt(stmt); }; }; diff --git a/services/ticket/common/models/ticket-packaging.js b/services/ticket/common/models/ticket-packaging.js index 8edf743d5..01cb3ee75 100644 --- a/services/ticket/common/models/ticket-packaging.js +++ b/services/ticket/common/models/ticket-packaging.js @@ -1,6 +1,4 @@ module.exports = function(Self) { - require('../methods/packaging/crudTicketPackaging')(Self); - Self.validateBinded('quantity', validateQuantity, { message: 'Enter an integer different to zero', allowNull: false, @@ -11,5 +9,5 @@ module.exports = function(Self) { return !isNaN(quantity) && quantity != 0; } - Self.validatesPresenceOf('packagingFk', {message: 'Package cannot be blank'}); + Self.validatesPresenceOf('packagingFk', {message: 'The package cannot be blank'}); };