From ebee852de60a0f6a70375c9ee30290f20901fb22 Mon Sep 17 00:00:00 2001 From: joan Date: Wed, 5 Jan 2022 15:41:26 +0100 Subject: [PATCH] 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) {