diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index 5ccc1ec64..549b0d627 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -46,10 +46,6 @@ module.exports = Self => { message: 'TIN must be unique' }); - Self.validatesUniquenessOf('socialName', { - message: 'The company name must be unique' - }); - Self.validatesFormatOf('email', { message: 'Invalid email', allowNull: true, @@ -63,17 +59,37 @@ module.exports = Self => { min: 3, max: 10 }); + Self.validateAsync('socialName', socialNameIsUnique, { + message: 'The company name must be unique' + }); + + async function socialNameIsUnique(err, done) { + const filter = { + where: { + and: [ + {socialName: this.socialName}, + {fi: this.fi}, + {id: {neq: this.id}} + ] + } + }; + const client = await Self.app.models.Client.findOne(filter); + if (client) + err(); + done(); + } + Self.validateAsync('iban', ibanNeedsValidation, { message: 'The IBAN does not have the correct format' }); async function ibanNeedsValidation(err, done) { - let filter = { + const filter = { fields: ['code'], where: {id: this.countryFk} }; - let country = await Self.app.models.Country.findOne(filter); - let code = country ? country.code.toLowerCase() : null; + const country = await Self.app.models.Country.findOne(filter); + const code = country ? country.code.toLowerCase() : null; if (code != 'es') return done(); @@ -90,12 +106,12 @@ module.exports = Self => { if (!this.isTaxDataChecked) return done(); - let filter = { + const filter = { fields: ['code'], where: {id: this.countryFk} }; - let country = await Self.app.models.Country.findOne(filter); - let code = country ? country.code.toLowerCase() : null; + const country = await Self.app.models.Country.findOne(filter); + const code = country ? country.code.toLowerCase() : null; if (!this.fi || !validateTin(this.fi, code)) err(); @@ -118,8 +134,8 @@ module.exports = Self => { function cannotHaveET(err) { if (!this.fi) return; - let tin = this.fi.toUpperCase(); - let cannotHaveET = /^[A-B]/.test(tin); + const tin = this.fi.toUpperCase(); + const cannotHaveET = /^[A-B]/.test(tin); if (cannotHaveET && this.isEqualizated) err();