From c5b8280403fe2ec6a5689c4ca18cfa62282fd592 Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 5 Jan 2022 12:44:18 +0100 Subject: [PATCH 1/6] feat(client): add isWorker condition in hasBusinessType --- modules/client/back/models/client.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index b4961771d..d9007e95a 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -55,13 +55,16 @@ module.exports = Self => { with: /^[\w|.|-]+@[\w|-]+(\.[\w|-]+)*(,[\w|.|-]+@[\w|-]+(\.[\w|-]+)*)*$/ }); - Self.validate('businessTypeFk', hasBusinessType, { + Self.validateAsync('businessTypeFk', hasBusinessType, { message: `The type of business must be filled in basic data` }); - function hasBusinessType(err) { - if (!this.businessTypeFk) + async function hasBusinessType(err, done) { + const isWorker = await Self.app.models.UserAccount.findById(this.id); + + if (!this.businessTypeFk && !isWorker) err(); + done(); } Self.validatesLengthOf('postcode', { From eae140003d54442b77af1db412bbbcb3a5e2f1ea Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 5 Jan 2022 14:29:03 +0100 Subject: [PATCH 2/6] feat(client): add more condition for businessType Validation --- modules/client/back/models/client.js | 32 +++++++++++++++++----------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index d9007e95a..ea87a49ee 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -55,18 +55,6 @@ module.exports = Self => { with: /^[\w|.|-]+@[\w|-]+(\.[\w|-]+)*(,[\w|.|-]+@[\w|-]+(\.[\w|-]+)*)*$/ }); - Self.validateAsync('businessTypeFk', hasBusinessType, { - message: `The type of business must be filled in basic data` - }); - - async function hasBusinessType(err, done) { - const isWorker = await Self.app.models.UserAccount.findById(this.id); - - if (!this.businessTypeFk && !isWorker) - err(); - done(); - } - Self.validatesLengthOf('postcode', { allowNull: true, allowBlank: true, @@ -192,6 +180,24 @@ module.exports = Self => { return regexp.test(value); } + Self.observe('before save', async ctx => { + const changes = ctx.data || ctx.instance; + const orgData = ctx.currentInstance; + const hasChanges = orgData && changes; + + const businessTypeFk = changes && changes.businessTypeFk || orgData && orgData.businessTypeFk; + + const isTaxDataChecked = changes && changes.isTaxDataChecked || orgData && orgData.isTaxDataChecked; + const isTaxDataCheckedChanged = hasChanges && orgData.isTaxDataChecked != isTaxDataChecked; + + let isWorker = false; + if (!ctx.isNewInstance) + isWorker = await Self.app.models.UserAccount.findById(orgData.id); + + if (!businessTypeFk && !isTaxDataChecked && !isTaxDataCheckedChanged && !isWorker) + throw new UserError(`The type of business must be filled in basic data`); + }); + Self.observe('before save', async function(ctx) { const changes = ctx.data || ctx.instance; const orgData = ctx.currentInstance; @@ -209,7 +215,7 @@ module.exports = Self => { && orgData.isTaxDataChecked != isTaxDataChecked; if ((socialNameChanged || dataCheckedChanged) && !isAlpha(socialName)) - throw new UserError('The socialName has an invalid format'); + throw new UserError(`The socialName has an invalid format`); if (changes.salesPerson === null) { changes.credit = 0; From 7406c59f16f642a651c76f7be3b60e84ef5233be Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 5 Jan 2022 14:53:29 +0100 Subject: [PATCH 3/6] update condition --- modules/client/back/models/client.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index ea87a49ee..06453da36 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -183,18 +183,15 @@ module.exports = Self => { Self.observe('before save', async ctx => { const changes = ctx.data || ctx.instance; const orgData = ctx.currentInstance; - const hasChanges = orgData && changes; const businessTypeFk = changes && changes.businessTypeFk || orgData && orgData.businessTypeFk; - const isTaxDataChecked = changes && changes.isTaxDataChecked || orgData && orgData.isTaxDataChecked; - const isTaxDataCheckedChanged = hasChanges && orgData.isTaxDataChecked != isTaxDataChecked; let isWorker = false; if (!ctx.isNewInstance) isWorker = await Self.app.models.UserAccount.findById(orgData.id); - if (!businessTypeFk && !isTaxDataChecked && !isTaxDataCheckedChanged && !isWorker) + if (!businessTypeFk && !isTaxDataChecked && !isWorker) throw new UserError(`The type of business must be filled in basic data`); }); From d85f95a8f4a2eb468fe46ead45e078f4322e22fc Mon Sep 17 00:00:00 2001 From: joan Date: Wed, 5 Jan 2022 15:17:21 +0100 Subject: [PATCH 4/6] Prevent businessTypeFk validation for other invalid fields --- modules/client/back/models/client.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index 06453da36..0c378ec25 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -184,15 +184,17 @@ module.exports = Self => { const changes = ctx.data || ctx.instance; const orgData = ctx.currentInstance; - const businessTypeFk = changes && changes.businessTypeFk || orgData && orgData.businessTypeFk; - const isTaxDataChecked = changes && changes.isTaxDataChecked || orgData && orgData.isTaxDataChecked; + if (!ctx.isNewInstance) { + const businessTypeFk = changes && changes.businessTypeFk || orgData && orgData.businessTypeFk; + const isTaxDataChecked = changes && changes.isTaxDataChecked || orgData && orgData.isTaxDataChecked; + const changedFields = Object.keys(changes); + const hasChangedOtherFields = changedFields.some(key => key !== 'businessTypeFk'); - let isWorker = false; - if (!ctx.isNewInstance) - isWorker = await Self.app.models.UserAccount.findById(orgData.id); + const isWorker = await Self.app.models.UserAccount.findById(orgData.id); - if (!businessTypeFk && !isTaxDataChecked && !isWorker) - throw new UserError(`The type of business must be filled in basic data`); + if (!businessTypeFk && !isTaxDataChecked && !isWorker && !hasChangedOtherFields) + throw new UserError(`The type of business must be filled in basic data`); + } }); Self.observe('before save', async function(ctx) { From ebee852de60a0f6a70375c9ee30290f20901fb22 Mon Sep 17 00:00:00 2001 From: joan Date: Wed, 5 Jan 2022 15:41:26 +0100 Subject: [PATCH 5/6] Throw invalid businessType for new clients --- modules/client/back/models/client.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index 0c378ec25..bb6a8d863 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -184,17 +184,26 @@ module.exports = Self => { const changes = ctx.data || ctx.instance; const orgData = ctx.currentInstance; + const businessTypeFk = changes && changes.businessTypeFk || orgData && orgData.businessTypeFk; + const isTaxDataChecked = changes && changes.isTaxDataChecked || orgData && orgData.isTaxDataChecked; + + let invalidBusinessType = false; if (!ctx.isNewInstance) { - const businessTypeFk = changes && changes.businessTypeFk || orgData && orgData.businessTypeFk; - const isTaxDataChecked = changes && changes.isTaxDataChecked || orgData && orgData.isTaxDataChecked; + const isWorker = await Self.app.models.UserAccount.findById(orgData.id); const changedFields = Object.keys(changes); const hasChangedOtherFields = changedFields.some(key => key !== 'businessTypeFk'); - const isWorker = await Self.app.models.UserAccount.findById(orgData.id); - if (!businessTypeFk && !isTaxDataChecked && !isWorker && !hasChangedOtherFields) - throw new UserError(`The type of business must be filled in basic data`); + invalidBusinessType = true; } + + if (ctx.isNewInstance) { + if (!businessTypeFk && !isTaxDataChecked) + invalidBusinessType = true; + } + + if (invalidBusinessType) + throw new UserError(`The type of business must be filled in basic data`); }); Self.observe('before save', async function(ctx) { From dc572a6e2e5b9ddce37362b8fcbdf6f39a4907cf Mon Sep 17 00:00:00 2001 From: joan Date: Wed, 5 Jan 2022 16:23:58 +0100 Subject: [PATCH 6/6] Updated translations --- loopback/locale/es.json | 2 +- modules/client/back/models/client.js | 2 +- modules/supplier/back/models/supplier.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 2611ee0dd..3fdbd43d8 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -104,7 +104,7 @@ "Weekday cannot be blank": "El día de la semana no puede quedar en blanco", "You can't delete a confirmed order": "No puedes borrar un pedido confirmado", "Can't create stowaway for this ticket": "No se puede crear un polizon para este ticket", - "The socialName has an invalid format": "El nombre fiscal tiene un formato incorrecto", + "The social name has an invalid format": "El nombre fiscal tiene un formato incorrecto", "Invalid quantity": "Cantidad invalida", "This postal code is not valid": "This postal code is not valid", "is invalid": "is invalid", diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index bb6a8d863..3dc1e1f8d 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -223,7 +223,7 @@ module.exports = Self => { && orgData.isTaxDataChecked != isTaxDataChecked; if ((socialNameChanged || dataCheckedChanged) && !isAlpha(socialName)) - throw new UserError(`The socialName has an invalid format`); + throw new UserError(`The social name has an invalid format`); if (changes.salesPerson === null) { changes.credit = 0; diff --git a/modules/supplier/back/models/supplier.js b/modules/supplier/back/models/supplier.js index 1ac6e3bd2..c5c79f079 100644 --- a/modules/supplier/back/models/supplier.js +++ b/modules/supplier/back/models/supplier.js @@ -97,6 +97,6 @@ module.exports = Self => { && orgData.socialName != socialName; if ((socialNameChanged) && !isAlpha(socialName)) - throw new UserError('The socialName has an invalid format'); + throw new UserError('The social name has an invalid format'); }); };