From ff047559e3548fca4249f3f3c652874715d960f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Wed, 21 Mar 2018 11:08:59 +0100 Subject: [PATCH] Ticket packaging --- client/ticket/routes.json | 2 +- .../ticket/src/package/list/package-list.html | 116 ++++++++++-------- .../ticket/src/package/list/package-list.js | 56 ++++++++- .../src/package/list/package-list.spec.js | 59 +++++++++ services/loopback/common/locale/en.json | 3 +- services/loopback/common/locale/es.json | 5 +- .../methods/packaging/crudTicketPackaging.js | 3 + .../ticket/common/models/ticketPackaging.js | 15 +++ 8 files changed, 201 insertions(+), 58 deletions(-) create mode 100644 client/ticket/src/package/list/package-list.spec.js create mode 100644 services/ticket/common/methods/packaging/crudTicketPackaging.js create mode 100644 services/ticket/common/models/ticketPackaging.js diff --git a/client/ticket/routes.json b/client/ticket/routes.json index 8f88f9684..18c8632ae 100644 --- a/client/ticket/routes.json +++ b/client/ticket/routes.json @@ -2,7 +2,7 @@ "module": "ticket", "name": "Tickets", "icon": "icon-ticket", - "validations": false, + "validations": true, "routes": [ { "url": "/ticket", diff --git a/client/ticket/src/package/list/package-list.html b/client/ticket/src/package/list/package-list.html index 6a895361e..4a4430eeb 100644 --- a/client/ticket/src/package/list/package-list.html +++ b/client/ticket/src/package/list/package-list.html @@ -1,56 +1,68 @@ + options="mgIndex" actions="$ctrl.getPackages()"> - - Packages - - - - {{id}} : {{name}} - - - - - - - - - - - - - - - - + + + +
+ + Packages + + + + {{id}} : {{name}} + + + + + + + + + + + + + + + + + + + +
\ No newline at end of file diff --git a/client/ticket/src/package/list/package-list.js b/client/ticket/src/package/list/package-list.js index f77a77418..0bf6903a2 100644 --- a/client/ticket/src/package/list/package-list.js +++ b/client/ticket/src/package/list/package-list.js @@ -2,9 +2,63 @@ import ngModule from '../../module'; class Controller { - construct($http, $scope) { + constructor($http, $scope, $translate, vnApp) { this.$http = $http; this.$ = $scope; + this.$translate = $translate; + this.vnApp = vnApp; + this.removedPackages = []; + this.updatedPackages = []; + } + + submit() { + let query = `/ticket/api/TicketPackagings/crudTicketPackaging`; + let packagesObj = { + delete: this.removedPackages, + create: [], + update: [] + }; + + this.packages.forEach(item => { + if (typeof item.id === 'undefined') + packagesObj.create.push(item); + + if (typeof item.id !== 'undefined' && angular.equals(item, this.oldPackages[item.id])) + packagesObj.update.push(item); + }); + + this.$http.post(query, packagesObj).then(res => { + this.$.index.accept(); + }); + } + + removePackage(index) { + if (this.packages[index] && this.packages[index].id) + this.removedPackages.push(this.packages[index].id); + + this.packages.splice(index, 1); + } + + addPackage() { + let data = { + packagingFk: null, + quantity: null, + created: Date.now(), + ticketFk: this.ticket.id + }; + this.packages.push(data); + } + + getPackages() { + this.packages = this.$.index.model; + this.setOldPackages(); + } + + setOldPackages() { + this.oldPackages = []; + this.packages.forEach(item => { + this.oldPackages[item.id] = item; + }); } } diff --git a/client/ticket/src/package/list/package-list.spec.js b/client/ticket/src/package/list/package-list.spec.js new file mode 100644 index 000000000..f6c38cc49 --- /dev/null +++ b/client/ticket/src/package/list/package-list.spec.js @@ -0,0 +1,59 @@ +import './package-list.js'; + +describe('Ticket', () => { + describe('Component vnTicketPackageList', () => { + let $componentController; + let controller; + let $httpBackend; + let $scope; + + beforeEach(() => { + angular.mock.module('ticket'); + }); + + beforeEach(angular.mock.inject((_$componentController_, _$httpBackend_, $rootScope) => { + $componentController = _$componentController_; + $httpBackend = _$httpBackend_; + $scope = { + index: { + accept: function() {} + } + }; + controller = $componentController('vnTicketPackageList', {$scope: $scope}); + })); + + describe('removePackage()', () => { + it('should push a package to removedPackages in the controller', () => { + controller.packages = [{id: 1}, {id: 2}]; + controller.removePackage(0); + + expect(controller.removedPackages).toEqual([1]); + }); + }); + + describe('submit()', () => { + it('should perform a post', () => { + spyOn(angular, 'equals').and.returnValue(true); + let query = '/ticket/api/TicketPackagings/crudTicketPackaging'; + controller.removedPackages = []; + controller.oldPackages = [ + {id: 1, quantity: 5, ticketFk: 1} + ]; + controller.packages = [ + {quantity: 5, ticketFk: 1}, + {id: 1, quantity: 25, ticketFk: 1} + ]; + let packagesObj = { + delete: controller.removedPackages, + create: [], + update: [] + }; + + $httpBackend.whenPOST(query, packagesObj).respond('omg YEAH'); + $httpBackend.expectPOST(query, packagesObj); + controller.submit(); + $httpBackend.flush(); + }); + }); + }); +}); diff --git a/services/loopback/common/locale/en.json b/services/loopback/common/locale/en.json index ce4eb95f2..35acd8506 100644 --- a/services/loopback/common/locale/en.json +++ b/services/loopback/common/locale/en.json @@ -5,5 +5,6 @@ "The default consignee can not be unchecked": "The default consignee can not be unchecked", "Unable to default a disabled consignee": "Unable to default a disabled consignee", "El método de pago seleccionado requiere que se especifique el IBAN": "El método de pago seleccionado requiere que se especifique el IBAN", - "Ya existe un usuario con ese nombre": "Ya existe un usuario con ese nombre" + "Ya existe un usuario con ese nombre": "Ya existe un usuario con ese nombre", + "Quantity cannot be zero": "Quantity cannot be zero" } \ No newline at end of file diff --git a/services/loopback/common/locale/es.json b/services/loopback/common/locale/es.json index edf12882c..791d0bc53 100644 --- a/services/loopback/common/locale/es.json +++ b/services/loopback/common/locale/es.json @@ -8,7 +8,6 @@ "can't be blank": "can't be blank", "DNI Incorrecto": "DNI Incorrecto", "Ya existe un usuario con ese nombre": "Ya existe un usuario con ese nombre", - "ValidationError: The `Item` instance is not valid. Details: `originFk` Cannot be blank (value: undefined).": "ValidationError: The `Item` instance is not valid. Details: `originFk` Cannot be blank (value: undefined).", - "Error: ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails (`vn2008`.`Articles`, CONSTRAINT `Articles_ibfk_5` FOREIGN KEY (`tipo_id`) REFERENCES `Tipos` (`tipo_id`) ON UPDATE CASCADE)": "Error: ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails (`vn2008`.`Articles`, CONSTRAINT `Articles_ibfk_5` FOREIGN KEY (`tipo_id`) REFERENCES `Tipos` (`tipo_id`) ON UPDATE CASCADE)", - "Error: ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails (`vn2008`.`Articles`, CONSTRAINT `expenceFk` FOREIGN KEY (`expenceFk`) REFERENCES `Gastos` (`Id_Gasto`) ON UPDATE CASCADE)": "Error: ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails (`vn2008`.`Articles`, CONSTRAINT `expenceFk` FOREIGN KEY (`expenceFk`) REFERENCES `Gastos` (`Id_Gasto`) ON UPDATE CASCADE)" + "is invalid": "is invalid", + "Quantity cannot be zero": "La cantidad no puede ser cero" } \ No newline at end of file diff --git a/services/ticket/common/methods/packaging/crudTicketPackaging.js b/services/ticket/common/methods/packaging/crudTicketPackaging.js new file mode 100644 index 000000000..b5c4bc875 --- /dev/null +++ b/services/ticket/common/methods/packaging/crudTicketPackaging.js @@ -0,0 +1,3 @@ +module.exports = Self => { + Self.installCrudModel('crudTicketPackaging'); +}; diff --git a/services/ticket/common/models/ticketPackaging.js b/services/ticket/common/models/ticketPackaging.js new file mode 100644 index 000000000..55e13b494 --- /dev/null +++ b/services/ticket/common/models/ticketPackaging.js @@ -0,0 +1,15 @@ +module.exports = function(Self) { + require('../methods/packaging/crudTicketPackaging')(Self); + + Self.validateBinded('quantity', validateQuantity, { + message: 'Quantity cannot be zero', + allowNull: false, + allowBlank: false + }); + + function validateQuantity(quantity) { + return quantity != 0; + } + + Self.validatesPresenceOf('packagingFk', {message: 'Package cannot be blank'}); +};