diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index 163d51fc5..8369fa906 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -41,6 +41,9 @@ module.exports = Self => { }); async function socialNameIsUnique(err, done) { + if (!this.countryFk) + return done(); + const filter = { include: { relation: 'country', @@ -59,13 +62,11 @@ module.exports = Self => { } }; - const existingClient = await Self.app.models.Client.findOne(filter); + const client = await Self.app.models.Country.findById(this.countryFk, {fields: ['isSocialNameUnique']}); + const existingClient = await Self.findOne(filter); - if (existingClient) { - // eslint-disable-next-line max-len - if (existingClient.country().isSocialNameUnique && this.socialName === existingClient.socialName) - err(); - } + if (existingClient && (existingClient.country().isSocialNameUnique || client.isSocialNameUnique)) + err(); done(); }