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

159 lines
4.4 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.validatesPresenceOf('socialName', {message: 'Debe especificarse la razón social'});
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'});
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();
};
// 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
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);
}
})
};
// Filters
let Model = Client;
let fields = {
id: false,
fi: false,
name: true,
socialName: true,
city: true,
postcode: false,
email: true,
phone: false
};
Model.remoteMethod('filter', {
description: 'List items using a filter',
accessType: 'READ',
accepts: {
arg: 'filter',
type: 'object',
description: 'Filter defining where'
},
returns: {
arg: 'data',
type: 'Client',
root: true
},
http: {
path: '/filter',
verb: 'get'
}
});
Model.filter = function(filter, cb) {
filter = removeEmpty(filter);
let where = {};
if(filter)
for (let field in fields)
if(filter[field]) {
if(fields[field])
where[field] = {ilike: filter[field]};
else
where[field] = filter[field];
}
Model.find({where: where}, function(err, instances) {
if(!err) {
cb(null, instances);
}
})
};
function removeEmpty(o) {
if(Array.isArray(o)) {
let array = [];
for(let item of o) {
let i = removeEmpty(item);
if(!isEmpty(item))
array.push(item);
};
if(array.length > 0)
return array;
}
else if (typeof o === 'object') {
let object = {};
for(let key in o) {
let i = removeEmpty(o[key]);
if(!isEmpty(i))
object[key] = i;
}
if(Object.keys(object).length > 0)
return object;
}
else if (!isEmpty(o))
return o;
return undefined;
}
function isEmpty(value) {
return value === undefined || value === "";
}
};