From 60e75d7825cfdc9c5222700ac0f36cf3d92101e0 Mon Sep 17 00:00:00 2001 From: alexm Date: Thu, 20 Jul 2023 08:23:43 +0200 Subject: [PATCH] refs #5837 fix client condition --- modules/client/back/models/client.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) 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(); }