62 lines
1.9 KiB
JavaScript
62 lines
1.9 KiB
JavaScript
module.exports = function(Client) {
|
|
//validations
|
|
Client.validatesUniquenessOf('name', {message: 'el nombre debe ser unico'});
|
|
Client.validatesUniquenessOf('fi', {message: 'el nif/cif debe ser unico'});
|
|
|
|
Client.validate('payMethod',hasCC,{message: 'Introduzca el iban del cliente'});
|
|
function hasCC(err) {
|
|
if (this.payMethod == 2 && !this.iban) err();
|
|
};
|
|
|
|
Client.validate('payMethod',hasSalesMan,{message: 'No se puede cambiar la forma de pago si no hay comercial asignado'});
|
|
function hasSalesMan(err) {
|
|
if (this.payMethod && !this.salesPerson) err();
|
|
};
|
|
|
|
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'
|
|
},
|
|
http: {
|
|
path: '/:id/activate', verb: 'put'
|
|
}
|
|
});
|
|
|
|
Client.activate = function(id, cb){
|
|
Client.findById(id, function(err, client) {
|
|
if (!err) {
|
|
Client.update({id: client.id}, {active: !client.active});
|
|
cb(null, !client.active);
|
|
}
|
|
})
|
|
};
|
|
|
|
// Hooks
|
|
Client.observe('before save', function (ctx, next) {
|
|
if (ctx.instance) {
|
|
if (!ctx.instance.dueDay){
|
|
ctx.instance.dueDay = 5;
|
|
}
|
|
next();
|
|
} else {
|
|
Client.findById(ctx.where.id,
|
|
function(err, item) {
|
|
if (!err) {
|
|
if (item.payMethod != ctx.data.payMethod && item.dueDay == ctx.data.dueDay) {
|
|
ctx.data.dueDay = 5;
|
|
}
|
|
}
|
|
next();
|
|
}
|
|
);
|
|
}
|
|
});
|
|
}; |