2017-10-11 13:36:47 +00:00
|
|
|
module.exports = function(Self) {
|
2017-10-19 07:12:27 +00:00
|
|
|
Self.validate('default', isEnabled, {message: 'No se puede poner predeterminado un consignatario desactivado'});
|
2017-01-18 09:42:53 +00:00
|
|
|
function isEnabled(err) {
|
2017-10-19 07:12:27 +00:00
|
|
|
if (!this.isEnabled && this.isDefaultAddress) err();
|
2017-01-18 09:42:53 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 07:12:27 +00:00
|
|
|
Self.beforeRemote('create', function(ctx, modelInstance, next) {
|
2017-01-26 14:00:13 +00:00
|
|
|
var data = ctx.req.body;
|
|
|
|
create(data, next);
|
|
|
|
});
|
|
|
|
|
2017-10-19 07:12:27 +00:00
|
|
|
function create(data, next) {
|
|
|
|
if (data.isDefaultAddress) {
|
2017-11-09 13:27:01 +00:00
|
|
|
removeAllDefault(data, next);
|
2017-10-19 07:12:27 +00:00
|
|
|
} else {
|
2017-10-03 07:16:02 +00:00
|
|
|
next();
|
|
|
|
}
|
2017-01-26 14:00:13 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 07:12:27 +00:00
|
|
|
Self.beforeRemote('prototype.patchAttributes', function(ctx, modelInstance, next) {
|
2017-09-28 12:34:18 +00:00
|
|
|
let newData = ctx.req.body;
|
|
|
|
newData.id = ctx.req.params.id;
|
|
|
|
getAddress(ctx, newData, next);
|
2017-01-18 09:42:53 +00:00
|
|
|
});
|
2017-09-28 09:29:01 +00:00
|
|
|
|
2017-10-11 13:36:47 +00:00
|
|
|
Self.beforeRemote('findById', function(ctx, modelInstance, next) {
|
2017-09-28 09:29:01 +00:00
|
|
|
ctx.args.filter = {
|
2017-11-15 11:11:35 +00:00
|
|
|
include: [{
|
2017-11-09 12:22:54 +00:00
|
|
|
relation: "province",
|
|
|
|
scope: {
|
|
|
|
fields: ["id", "name"]
|
2017-09-28 09:29:01 +00:00
|
|
|
}
|
2017-11-15 11:11:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
relation: "defaultAgency",
|
|
|
|
scope: {
|
|
|
|
fields: ["id", "name"]
|
|
|
|
}
|
2017-09-28 09:29:01 +00:00
|
|
|
}
|
2017-11-15 11:11:35 +00:00
|
|
|
]
|
2017-09-28 09:29:01 +00:00
|
|
|
};
|
|
|
|
next();
|
|
|
|
});
|
2017-01-18 13:48:35 +00:00
|
|
|
|
2017-10-19 07:12:27 +00:00
|
|
|
function getAddress(ctx, newData, next) {
|
|
|
|
Self.findOne({where: {id: newData.id}}, (_, oldData) => {
|
|
|
|
if (oldData)
|
2017-09-28 12:34:18 +00:00
|
|
|
callbackGetAddress(ctx, newData, oldData, next);
|
2017-10-19 07:12:27 +00:00
|
|
|
});
|
2017-01-26 14:00:13 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 07:12:27 +00:00
|
|
|
function callbackGetAddress(ctx, newData, oldData, next) {
|
|
|
|
if (newData.isDefaultAddress) {
|
2017-11-09 12:22:54 +00:00
|
|
|
removeAllDefault(oldData, next);
|
2017-10-19 07:12:27 +00:00
|
|
|
} else if (oldData.isDefaultAddress && newData.hasOwnProperty('isDefaultAddress') && !newData.isDefaultAddress) {
|
2017-01-26 14:00:13 +00:00
|
|
|
next(generateErrorDefaultAddress());
|
2017-10-19 07:12:27 +00:00
|
|
|
} else
|
2017-01-30 07:23:18 +00:00
|
|
|
next();
|
2017-01-26 14:00:13 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 07:12:27 +00:00
|
|
|
function removeAllDefault(client, next) {
|
2017-11-09 13:27:01 +00:00
|
|
|
if (client && client.clientFk)
|
|
|
|
Self.updateAll({clientFk: client.clientFk, isDefaultAddress: {neq: 0}}, {isDefaultAddress: false}, next);
|
2017-11-09 12:22:54 +00:00
|
|
|
else
|
|
|
|
next();
|
2017-01-26 14:00:13 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 07:12:27 +00:00
|
|
|
function generateErrorDefaultAddress() {
|
2017-01-26 14:00:13 +00:00
|
|
|
var error = new Error();
|
|
|
|
error.message = "No se puede desmarcar el consignatario predeterminado";
|
|
|
|
error.status = 500;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|