Modificaciones en el activar del descriptor.

Se envía solo la modificación de la dirección.
Cambios en address. Se comprueba el valor por defecto. Falla pero no da error. Se sube para no perderse los cambios.
This commit is contained in:
nelo 2017-01-26 15:00:13 +01:00
parent d36d619533
commit 87dda33a20
4 changed files with 63 additions and 15 deletions

View File

@ -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,7 +16,8 @@ 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();
@ -33,5 +34,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);

View File

@ -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);

View File

@ -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;
update(ctx, data, next);
});
function update(ctx, data, next){
var address = Address.findOne( {filter: { where: { id: data.id}}}, function (err, address){
if(address)
callbackGetAddress(ctx, data, address, next)
else
next();
});
}
function callbackGetAddress(ctx, data, address, next){
if (data.default){
removeAllDefault(ctx, address.client);
next();
}
else
next(generateErrorDefaultAddress());
}
function getData(ctx){
if(ctx.data)
if (ctx.data)
return ctx.data;
else
return ctx.instance;
}
};
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;
}
};

View File

@ -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});