2017-02-21 10:36:43 +00:00
|
|
|
|
2017-01-12 10:02:22 +00:00
|
|
|
module.exports = function(Client) {
|
2017-02-08 11:45:55 +00:00
|
|
|
// Validations
|
|
|
|
|
2017-04-28 13:04:29 +00:00
|
|
|
Client.validatesUniquenessOf('name', {message: 'El nombre debe ser único'});
|
|
|
|
Client.validatesUniquenessOf('fi', {message: 'El NIF/CIF debe ser único'});
|
2017-01-31 13:13:06 +00:00
|
|
|
Client.validatesPresenceOf('socialName', {message: 'Debe especificarse la razón social'});
|
2017-02-07 11:58:25 +00:00
|
|
|
Client.validatesFormatOf('postcode', {allowNull: true, with: /^\d+$/, message: 'El código postal solo debe contener números'});
|
|
|
|
Client.validatesLengthOf('postcode', {allowNull: true, min: 3, max: 10});
|
|
|
|
Client.validatesFormatOf('email', {allowNull: true, with: /^[\w|\.|\-]+@\w[\w|\.|\-]*\w$/, message: 'Correo electrónico inválido'});
|
2017-01-12 10:02:22 +00:00
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
Client.validate('payMethod', hasCC, {message: 'Introduzca el iban del cliente'});
|
2017-01-12 10:02:22 +00:00
|
|
|
function hasCC(err) {
|
2017-02-08 11:45:55 +00:00
|
|
|
if (this.payMethod == 2 && !this.iban) err();
|
2017-01-25 10:46:15 +00:00
|
|
|
};
|
2017-01-12 10:02:22 +00:00
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
Client.validate('payMethod', hasSalesMan, {message: 'No se puede cambiar la forma de pago si no hay comercial asignado'});
|
2017-01-16 08:57:06 +00:00
|
|
|
function hasSalesMan(err) {
|
2017-01-31 13:13:06 +00:00
|
|
|
if(this.payMethod && !this.salesPerson) err();
|
2017-01-25 10:46:15 +00:00
|
|
|
};
|
|
|
|
|
2017-02-08 11:45:55 +00:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Methods
|
|
|
|
|
2017-02-07 11:58:25 +00:00
|
|
|
Client.remoteMethod('activate', {
|
2017-01-26 14:00:13 +00:00
|
|
|
description: 'Activate or deactive client',
|
2017-02-07 11:58:25 +00:00
|
|
|
accepts: {
|
|
|
|
arg: 'id',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
2017-02-21 15:21:55 +00:00
|
|
|
description: 'Model id',
|
|
|
|
http: {source: 'path'}
|
2017-02-07 11:58:25 +00:00
|
|
|
},
|
2017-01-25 10:46:15 +00:00
|
|
|
returns: {
|
|
|
|
arg: 'active',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
http: {
|
2017-02-21 15:21:55 +00:00
|
|
|
verb: 'put',
|
|
|
|
path: '/:id/activate'
|
2017-01-25 10:46:15 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-02-08 11:45:55 +00:00
|
|
|
Client.activate = function(id, cb) {
|
2017-02-21 15:21:55 +00:00
|
|
|
console.log(id);
|
2017-01-25 10:46:15 +00:00
|
|
|
Client.findById(id, function(err, client) {
|
2017-01-31 13:13:06 +00:00
|
|
|
if(!err) {
|
2017-01-25 10:46:15 +00:00
|
|
|
Client.update({id: client.id}, {active: !client.active});
|
|
|
|
cb(null, !client.active);
|
|
|
|
}
|
2017-05-11 11:01:33 +00:00
|
|
|
});
|
2017-01-25 10:46:15 +00:00
|
|
|
};
|
2017-01-12 10:02:22 +00:00
|
|
|
|
2017-02-21 10:36:43 +00:00
|
|
|
// Basic filter
|
2017-02-08 11:45:55 +00:00
|
|
|
|
2017-02-28 17:10:11 +00:00
|
|
|
Client.installMethod('filter', filterClients);
|
2017-02-28 10:46:18 +00:00
|
|
|
function filterClients(p) {
|
2017-02-21 10:36:43 +00:00
|
|
|
return {
|
|
|
|
where: {
|
|
|
|
id: p.id,
|
2017-05-05 10:54:47 +00:00
|
|
|
name: {regexp: p.name},
|
2017-02-21 10:36:43 +00:00
|
|
|
cif: p.cif,
|
2017-05-05 10:54:47 +00:00
|
|
|
socialName: {regexp: p.socialName},
|
|
|
|
city: {regexp: p.city},
|
2017-02-21 10:36:43 +00:00
|
|
|
postcode: p.postcode,
|
2017-05-05 10:54:47 +00:00
|
|
|
email: {regexp: p.email},
|
2017-02-21 10:36:43 +00:00
|
|
|
phone: p.phone
|
|
|
|
},
|
|
|
|
skip: (p.page - 1) * p.size,
|
|
|
|
limit: p.size
|
|
|
|
};
|
2017-02-08 11:45:55 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 15:21:55 +00:00
|
|
|
// Client addresses
|
|
|
|
|
|
|
|
Client.remoteMethod('addressesList', {
|
|
|
|
description: 'List items using a filter',
|
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'id',
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
description: 'Model id',
|
|
|
|
http: {source: 'path'}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'filter',
|
|
|
|
type: 'object',
|
|
|
|
required: true,
|
|
|
|
description: 'Filter defining where',
|
|
|
|
http: function(ctx) {
|
|
|
|
return ctx.req.query;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
arg: 'data',
|
|
|
|
type: ['Address'],
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/addressesList`,
|
|
|
|
verb: 'get'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Client.addressesList = function(id, p, cb) {
|
|
|
|
let filter = {
|
2017-02-21 10:36:43 +00:00
|
|
|
where: {
|
2017-05-09 12:04:44 +00:00
|
|
|
clientFk: id,
|
2017-02-21 10:36:43 +00:00
|
|
|
},
|
|
|
|
skip: (p.page - 1) * p.size,
|
|
|
|
limit: p.size
|
|
|
|
};
|
2017-02-21 15:21:55 +00:00
|
|
|
|
|
|
|
Client.app.models.Address.find(filter, function(err, instances) {
|
|
|
|
if(!err) {
|
|
|
|
cb(null, instances);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
2017-01-12 10:02:22 +00:00
|
|
|
};
|