diff --git a/@salix/crud/src/client/addresses-data-edit/index.js b/@salix/crud/src/client/addresses-data-edit/index.js index f1af9095e8..6ad6cab97a 100644 --- a/@salix/crud/src/client/addresses-data-edit/index.js +++ b/@salix/crud/src/client/addresses-data-edit/index.js @@ -5,7 +5,7 @@ export const NAME = 'vnClientAddressesDataEdit'; export const COMPONENT = { template: template, controllerAs: 'addressData', - controller: function($http, $stateParams, copyObject, equalsObject, $window) { + controller: function($http, $stateParams, copyObject, equalsObject, $window, modified) { this.address = {}; $http.get(`/client/api/Addresses/${$stateParams.addressId}`).then( json => { @@ -16,14 +16,13 @@ export const COMPONENT = { this.submit = function() { if (!equalsObject(this.address, this.addressOld)) { - $http.put('/client/api/Addresses', this.address).then( + var newAddress = modified(this.address, this.addressOld); + $http.put(`/client/api/Addresses/${this.addressOld.id}`, newAddress).then( json => { - this.address = json.data; - this.copyAddress(); + $window.history.back(); } ); } - $window.history.back(); }; this.copyAddress = () => { @@ -33,5 +32,5 @@ export const COMPONENT = { } }; -COMPONENT.controller.$inject = ['$http', '$stateParams', 'copyObject', 'equalsObject', '$window']; +COMPONENT.controller.$inject = ['$http', '$stateParams', 'copyObject', 'equalsObject', '$window', 'getDataModified']; module.component(NAME, COMPONENT); diff --git a/@salix/crud/src/client/descriptor/descriptor.js b/@salix/crud/src/client/descriptor/descriptor.js index ca4bf7e94b..89a66763b2 100644 --- a/@salix/crud/src/client/descriptor/descriptor.js +++ b/@salix/crud/src/client/descriptor/descriptor.js @@ -9,7 +9,7 @@ export const COMPONENT = { bindings: { client: '<' }, - controller: function($http, $scope, copyObject) { + controller: function($http, $scope) { var self = this; $scope.$watch('descriptor.client.active', function(newValue, oldValue) { if (oldValue !== undefined) @@ -17,5 +17,5 @@ export const COMPONENT = { }); } }; -COMPONENT.controller.$inject = ['$http', '$scope', 'copyObject']; +COMPONENT.controller.$inject = ['$http', '$scope']; module.component(NAME, COMPONENT); diff --git a/services/client/common/models/Address.js b/services/client/common/models/Address.js index 75ef213ba4..411ed0b874 100644 --- a/services/client/common/models/Address.js +++ b/services/client/common/models/Address.js @@ -5,19 +5,61 @@ module.exports = function(Address) { if (!this.enabled && this.default) err(); } - Address.observe('before save', function (ctx, next) { - var data = getData(ctx); - if (data.enabled && data.default) { - ctx.Model.update({client: data.client}, {default: false}); - } - next(); + Address.beforeRemote('create',function(ctx, modelInstance, next){ + var data = ctx.req.body; + create(data, next); }); + function create(data, next){ + if(data.default){ + removeAllDefault(data.client); + } + next(); + } + + Address.beforeRemote('prototype.updateAttributes',function(ctx, modelInstance, next){ + var data = ctx.req.body; + data.id = ctx.req.params.id; + getAddress(ctx, data, next); + }); + + function getAddress(ctx, data, next){ + var address = Address.findOne( {where: { id: data.id}}, function (err, address){ + if(address) + callbackGetAddress(ctx, data, address, next) + }); + } + + function callbackGetAddress(ctx, data, address, next){ + if (data.default){ + removeAllDefault(address.client); + next(); + } + else if (address.default && !data.default) + next(generateErrorDefaultAddress()); + else + next(); + } + function getData(ctx){ - if(ctx.data) + if (ctx.data) return ctx.data; else return ctx.instance; } -}; \ No newline at end of file + function removeAllDefault(cl){ + Address.update({client: cl}, {default: false}); + } + + + function generateErrorDefaultAddress(){ + var error = new Error(); + error.message = "No se puede desmarcar el consignatario predeterminado"; + error.status = 500; + return error; + } + + +}; + diff --git a/services/client/common/models/Client.js b/services/client/common/models/Client.js index 593e5f31ee..49b0ed0fdb 100644 --- a/services/client/common/models/Client.js +++ b/services/client/common/models/Client.js @@ -15,6 +15,12 @@ module.exports = function(Client) { Client.remoteMethod('activate', { + description: 'Activate or deactive client', + accepts: {arg: 'id', type: 'number', required: true, http: function(ctx) { + var id = ctx && ctx.req && ctx.req.params.id + return id; + } + }, returns: { arg: 'active', type: 'boolean' @@ -24,8 +30,7 @@ module.exports = function(Client) { } }); - Client.activate = function(cb){ - var id = 12; + Client.activate = function(id, cb){ Client.findById(id, function(err, client) { if (!err) { Client.update({id: client.id}, {active: !client.active});