From 6d8b3e346dc99043d23af1c771d3483b80aef386 Mon Sep 17 00:00:00 2001 From: carlossa Date: Fri, 14 Jul 2023 09:40:16 +0200 Subject: [PATCH 1/4] refs #5837 fix filter existingClient France --- modules/client/back/models/client.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index ca279ef71..dc48f9d3d 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -50,9 +50,14 @@ module.exports = Self => { ] } }; - const client = await Self.app.models.Client.findOne(filter); - if (client) - err(); + + const existingClient = await Self.app.models.Client.findOne(filter); + + if (existingClient) { + if (this.countryFk !== 19 && this.socialName === existingClient.socialName) + err(); + } + done(); } From 765c22df969a5ebd8c73907cd30f503a7934ca5e Mon Sep 17 00:00:00 2001 From: carlossa Date: Fri, 14 Jul 2023 11:30:13 +0200 Subject: [PATCH 2/4] refs #5837 new table --- back/models/country.json | 5 ++++- db/changes/233001/00-noUniqueSocialName.sql | 2 ++ modules/client/back/models/client.js | 3 ++- modules/client/back/models/client.json | 5 +++++ 4 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 db/changes/233001/00-noUniqueSocialName.sql diff --git a/back/models/country.json b/back/models/country.json index 8fa25b88e..fd540d819 100644 --- a/back/models/country.json +++ b/back/models/country.json @@ -22,6 +22,9 @@ }, "isUeeMember": { "type": "boolean" + }, + "isSocialNameUnique": { + "type": "boolean" } }, "relations": { @@ -39,4 +42,4 @@ "permission": "ALLOW" } ] -} \ No newline at end of file +} diff --git a/db/changes/233001/00-noUniqueSocialName.sql b/db/changes/233001/00-noUniqueSocialName.sql new file mode 100644 index 000000000..0dc4c832f --- /dev/null +++ b/db/changes/233001/00-noUniqueSocialName.sql @@ -0,0 +1,2 @@ +ALTER TABLE `vn`.`country` +ADD COLUMN `isSocialNameUnique` tinyint(1) NOT NULL DEFAULT 1; diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index dc48f9d3d..089500149 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -54,7 +54,8 @@ module.exports = Self => { const existingClient = await Self.app.models.Client.findOne(filter); if (existingClient) { - if (this.countryFk !== 19 && this.socialName === existingClient.socialName) + console.log(this.isSocialNameUnique); + if (!this.isSocialNameUnique && this.socialName === existingClient.socialName) err(); } diff --git a/modules/client/back/models/client.json b/modules/client/back/models/client.json index 5f56a1ed2..66a67ec2e 100644 --- a/modules/client/back/models/client.json +++ b/modules/client/back/models/client.json @@ -181,6 +181,11 @@ "model": "Country", "foreignKey": "countryFk" }, + "isSocialNameUnique": { + "type": "belongsTo", + "model": "Country", + "foreignKey": "countryFk" + }, "contactChannel": { "type": "belongsTo", "model": "ContactChannel", From 93387cae62aed6a12f860fe3207bf656b84d73ac Mon Sep 17 00:00:00 2001 From: carlossa Date: Fri, 14 Jul 2023 13:07:40 +0200 Subject: [PATCH 3/4] refs #5837 if isSocialNameUnique --- modules/client/back/models/client.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index 089500149..163d51fc5 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -42,6 +42,14 @@ module.exports = Self => { async function socialNameIsUnique(err, done) { const filter = { + include: { + relation: 'country', + scope: { + fields: { + isSocialNameUnique: true, + }, + }, + }, where: { and: [ {socialName: this.socialName}, @@ -54,8 +62,8 @@ module.exports = Self => { const existingClient = await Self.app.models.Client.findOne(filter); if (existingClient) { - console.log(this.isSocialNameUnique); - if (!this.isSocialNameUnique && this.socialName === existingClient.socialName) + // eslint-disable-next-line max-len + if (existingClient.country().isSocialNameUnique && this.socialName === existingClient.socialName) err(); } From 60e75d7825cfdc9c5222700ac0f36cf3d92101e0 Mon Sep 17 00:00:00 2001 From: alexm Date: Thu, 20 Jul 2023 08:23:43 +0200 Subject: [PATCH 4/4] 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(); }