From 10b0f94bf65c6b0a37f615d3735f8cc100804ce3 Mon Sep 17 00:00:00 2001 From: Jose Pinto Date: Wed, 26 Dec 2018 16:09:26 -0300 Subject: [PATCH] refactor guardar --- src/js/controller.js | 59 +++++++++++++++++++++++++------------------- src/js/controllerCisterna.js | 44 +++++++++++++++++++++------------ src/js/service.js | 2 +- 3 files changed, 63 insertions(+), 42 deletions(-) diff --git a/src/js/controller.js b/src/js/controller.js index 415c480..d839ed7 100644 --- a/src/js/controller.js +++ b/src/js/controller.js @@ -136,33 +136,22 @@ angular.module('focaAbmVehiculo') $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/' + id); }; $scope.guardar = function() { - if(!$scope.vehiculo.transportista) { - focaModalService.alert('Elija Transportista'); - return; - } - focaAbmVehiculoService.getVehiculosPorTransportista($routeParams.idTransportista) - .then(function(res) { - var existe = res.data.filter(function(vehiculo) { - return vehiculo.codigo === $scope.vehiculo.codigo && - vehiculo.id !== $scope.vehiculo.id; + //Valida si existe numero de unidad + existeCodigoUnidad().then(function() { + delete $scope.vehiculo.transportista; + focaAbmVehiculoService.guardarVehiculo($scope.vehiculo) + .then(function(res){ + if($scope.nuevo) { + $location.path('/vehiculo/' + res.data.id + + '/' + res.data.idTransportista); + } else { + $location.path('/vehiculo'); + } }); + }, function(){ + focaModalService.alert('Código de unidad existente'); + }); - if(existe.length > 0) { - return Promise.reject('Existe codigo de unidad'); - }else { - delete $scope.vehiculo.transportista; - return focaAbmVehiculoService.guerdarVehiculo($scope.vehiculo); - } - }).then(function(res) { - if($scope.nuevo) { - $location.path('/vehiculo/' + res.data.id + - '/' + res.data.idTransportista); - }else { - $location.path('/vehiculo'); - } - }, function(rej) { - focaModalService.alert(rej); - }); }; $scope.solicitarConfirmacionCisterna = function(cisterna) { focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' + @@ -198,5 +187,25 @@ angular.module('focaAbmVehiculo') } ); }; + + function existeCodigoUnidad(){ + return new Promise(function(resolve, reject) { + focaAbmVehiculoService + .getVehiculosPorTransportista($routeParams.idTransportista) + .then(function(res){ + //Valida si existe numero de unidad + var existe = res.data.filter(function(vehiculo) { + return vehiculo.codigo === $scope.vehiculo.codigo && + vehiculo.id !== $scope.vehiculo.id; + }); + + if(existe.length){ + reject(existe); + } else { + resolve(); + } + }); + }); + } } ]); diff --git a/src/js/controllerCisterna.js b/src/js/controllerCisterna.js index 4fd438e..1003ff8 100644 --- a/src/js/controllerCisterna.js +++ b/src/js/controllerCisterna.js @@ -56,24 +56,17 @@ angular.module('focaAbmVehiculo') focaModalService.alert('Ingrese producto'); return; } - focaAbmVehiculoService.getCisternadoPorVehiculo($routeParams.idVehiculo) - .then(function(res) { - var existe = res.data.filter(function(cisterna) { - return cisterna.codigo === $scope.cisterna.codigo; - }); - if(existe.length > 0) { - return Promise.reject('Existe codigo de cisterna'); - }else { - $scope.cisterna.idVehiculo = $routeParams.idVehiculo; - delete $scope.cisterna.unidadMedida; - delete $scope.cisterna.cisternaCarga.articulo; - return focaAbmVehiculoService.guardarCisterna($scope.cisterna); - } - }).then(function() { + existeCodigoCisterna().then(function() { + $scope.cisterna.idVehiculo = $routeParams.idVehiculo; + delete $scope.cisterna.unidadMedida; + delete $scope.cisterna.cisternaCarga.articulo; + focaAbmVehiculoService.guardarCisterna($scope.cisterna).then(function(){ $location.path('/vehiculo/' + $routeParams.idVehiculo); - }, function(rej) { - focaModalService.alert(rej); }); + }, function() { + focaModalService.alert('Código de cisterna existente'); + }); + }; // $scope.seleccionarArticulo = function() { @@ -116,5 +109,24 @@ angular.module('focaAbmVehiculo') $scope.cisterna.unidadMedida = unidaMedida; }); }; + + function existeCodigoCisterna(){ + return new Promise(function(resolve, reject) { + focaAbmVehiculoService + .getCisternadoPorVehiculo($routeParams.idVehiculo) + .then(function(res){ + //Valida si existe numero de unidad + var existe = res.data.filter(function(cisterna) { + return cisterna.codigo === $scope.cisterna.codigo; + }); + + if(existe.length){ + reject(existe); + } else { + resolve(); + } + }); + }); + } } ]); diff --git a/src/js/service.js b/src/js/service.js index b4be7de..8df56f6 100644 --- a/src/js/service.js +++ b/src/js/service.js @@ -10,7 +10,7 @@ angular.module('focaAbmVehiculo') getTransportistas: function() { return $http.get(API_ENDPOINT.URL + '/transportista'); }, - guerdarVehiculo: function(vehiculo) { + guardarVehiculo: function(vehiculo) { return $http.post(API_ENDPOINT.URL + '/vehiculo', {vehiculo: vehiculo}); }, deleteVehiculo: function(id) { -- 1.9.1