salix/services/client/common/models/Address.js

23 lines
622 B
JavaScript

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) {
var data = getData(ctx);
if (data.enabled && data.default) {
ctx.Model.update({client: data.client}, {default: false});
}
next();
});
function getData(ctx){
if(ctx.data)
return ctx.data;
else
return ctx.instance;
}
};