2017-01-18 09:42:53 +00:00
|
|
|
module.exports = function(Address) {
|
|
|
|
|
|
|
|
Address.validate('default',isEnabled,{message: 'No se puede poner predeterminado un consignatario desactivado'});
|
|
|
|
function isEnabled(err) {
|
|
|
|
if (!this.enabled && this.default) err();
|
|
|
|
}
|
|
|
|
|
|
|
|
Address.observe('before save', function (ctx, next) {
|
2017-01-18 13:48:35 +00:00
|
|
|
var data = getData(ctx);
|
|
|
|
if (isEnabled(data) && isDefault(data)) {
|
|
|
|
updateData(ctx);
|
2017-01-18 09:42:53 +00:00
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
2017-01-18 13:48:35 +00:00
|
|
|
|
|
|
|
function getData(ctx){
|
|
|
|
if(ctx.data)
|
|
|
|
return ctx.data;
|
|
|
|
else
|
|
|
|
return ctx.instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isEnabled(data){
|
|
|
|
return data.isEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isDefault(data){
|
|
|
|
return data.default;
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateData(ctx){
|
|
|
|
ctx.Model.update({client: ctx.data.client}, {default: false});
|
|
|
|
}
|
2017-01-18 09:42:53 +00:00
|
|
|
};
|