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

75 lines
2.4 KiB
JavaScript

var app = require('../../server/server');
module.exports = function(Self) {
var models = app.models;
// Methods
require('../methods/client/activate.js')(Self);
require('../methods/client/addresses.js')(Self);
require('../methods/client/before-save.js')(Self);
require('../methods/client/card.js')(Self);
require('../methods/client/create.js')(Self);
require('../methods/client/employee.js')(Self);
require('../methods/client/filter.js')(Self);
require('../methods/client/roles.js')(Self);
require('../methods/client/salesperson.js')(Self);
require('../methods/client/addressesPropagateRe.js')(Self);
// Validations
Self.validatesUniquenessOf('name', {
message: 'El nombre debe ser único'
});
Self.validatesUniquenessOf('fi', {
message: 'El NIF/CIF debe ser único'
});
Self.validatesPresenceOf('socialName', {
message: 'Debe especificarse la razón social'
});
Self.validatesUniquenessOf('socialName', {
message: 'La razón social debe ser única'
});
Self.validatesFormatOf('postcode', {
message: 'El código postal solo debe contener números',
allowNull: true,
allowBlank: true,
with: /^\d+$/
});
Self.validatesFormatOf('email', {
message: 'Correo electrónico inválido',
allowNull: true,
allowBlank: true,
with: /^[\w|\.|\-]+@\w[\w|\.|\-]*\w(,[\w|\.|\-]+@\w[\w|\.|\-]*\w)*$/
});
Self.validatesLengthOf('postcode', {
allowNull: true,
allowBlank: true,
min: 3, max: 10
});
var validateIban = require('../validations/validateIban');
Self.validateBinded('iban', validateIban, {
message: 'El iban no tiene el formato correcto'
});
Self.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();
}
Self.validateAsync('payMethodFk', hasIban, {
message: 'El método de pago seleccionado requiere que se especifique el IBAN'
});
function hasIban(err, done) {
models.PayMethod.findById(this.payMethodFk, (_, instance) => {
if (instance && instance.ibanRequired && !this.iban)
err();
done();
});
}
};