From 1d76c21e0a65d5412e2129d5526bb4ebfc15ee3c Mon Sep 17 00:00:00 2001 From: Simon Tharby Date: Mon, 20 Nov 2023 11:01:28 +0000 Subject: [PATCH 1/5] [CL-4247] POC: remove orphaned logic + test --- .../side_fx_custom_field_option_service.rb | 12 ++++++++ ...ide_fx_custom_field_option_service_spec.rb | 29 ++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/back/app/services/side_fx_custom_field_option_service.rb b/back/app/services/side_fx_custom_field_option_service.rb index a5a23783f0d0..114ae3aee337 100644 --- a/back/app/services/side_fx_custom_field_option_service.rb +++ b/back/app/services/side_fx_custom_field_option_service.rb @@ -20,6 +20,8 @@ def before_destroy(custom_field_option, _current_user) end def after_destroy(frozen_custom_field_option, current_user) + remove_logic_option_from_custom_fields(frozen_custom_field_option) + serialized_custom_field_option = clean_time_attributes(frozen_custom_field_option.attributes) LogActivityJob.perform_later( encode_frozen_resource(frozen_custom_field_option), @@ -29,4 +31,14 @@ def after_destroy(frozen_custom_field_option, current_user) payload: { custom_field_option: serialized_custom_field_option } ) end + + def remove_logic_option_from_custom_fields(frozen_custom_field_option) + custom_field = frozen_custom_field_option.custom_field + + return unless custom_field&.input_type == 'select' && custom_field&.logic.present? && + custom_field.logic['rules'].pluck('if').include?(frozen_custom_field_option.id) + + custom_field.logic['rules'].reject! { |rule| rule['if'] == frozen_custom_field_option.id } + custom_field.save! + end end diff --git a/back/spec/services/side_fx_custom_field_option_service_spec.rb b/back/spec/services/side_fx_custom_field_option_service_spec.rb index 66ba9a0fa91c..27d6f3b29c59 100644 --- a/back/spec/services/side_fx_custom_field_option_service_spec.rb +++ b/back/spec/services/side_fx_custom_field_option_service_spec.rb @@ -6,7 +6,7 @@ let(:service) { described_class.new } let(:user) { create(:user) } - describe 'before_delete' do + describe 'before_destroy' do let(:custom_field) { create(:custom_field, :for_custom_form, input_type: 'select') } let(:option1) { create(:custom_field_option, custom_field: custom_field, key: 'option_1') } let!(:user1) { create(:user, custom_field_values: { custom_field.key => 'option_1' }) } @@ -19,4 +19,31 @@ end end end + + describe 'after_destroy' do + let(:custom_field1) { create(:custom_field, :for_custom_form, input_type: 'select') } + let(:custom_field2) { create(:custom_field, :for_custom_form, input_type: 'page') } + let(:custom_field3) { create(:custom_field, :for_custom_form, input_type: 'page') } + let(:option1) { create(:custom_field_option, custom_field: custom_field1, key: 'option_1') } + let(:option2) { create(:custom_field_option, custom_field: custom_field1, key: 'option_2') } + let(:logic) do + { rules: [ + { if: option1.id, goto_page_id: custom_field2.id }, + { if: option2.id, goto_page_id: custom_field3.id } + ] } + end + + before do + custom_field1.update!(logic: logic) + end + + context 'when custom field option that is a survey logic option is destroyed' do + it 'removes the logic option from custom fields that contain it' do + service.after_destroy(option1, user) + + expect(custom_field1.reload.logic['rules'].count).to eq 1 + expect(custom_field1.reload.logic['rules'].pluck('if')).to eq [option2.id] + end + end + end end From 94c8712f558c9958b8403e5f2cfc22ba981c63fa Mon Sep 17 00:00:00 2001 From: Simon Tharby Date: Mon, 20 Nov 2023 11:34:26 +0000 Subject: [PATCH 2/5] [CL-4247] move logic update to form logic service --- back/app/services/form_logic_service.rb | 10 ++++++++++ .../side_fx_custom_field_option_service.rb | 16 +++++----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/back/app/services/form_logic_service.rb b/back/app/services/form_logic_service.rb index 74645d781fe3..34d0585d1d89 100644 --- a/back/app/services/form_logic_service.rb +++ b/back/app/services/form_logic_service.rb @@ -38,6 +38,16 @@ def valid? end end + def remove_select_logic_option_from_custom_fields(frozen_custom_field_option) + custom_field = fields.first + + return unless custom_field&.logic.present? && + custom_field.logic['rules'].pluck('if').include?(frozen_custom_field_option.id) + + custom_field.logic['rules'].reject! { |rule| rule['if'] == frozen_custom_field_option.id } + custom_field.save! + end + private attr_reader :fields, :field_index, :option_index diff --git a/back/app/services/side_fx_custom_field_option_service.rb b/back/app/services/side_fx_custom_field_option_service.rb index 114ae3aee337..9818525b3a2f 100644 --- a/back/app/services/side_fx_custom_field_option_service.rb +++ b/back/app/services/side_fx_custom_field_option_service.rb @@ -20,7 +20,11 @@ def before_destroy(custom_field_option, _current_user) end def after_destroy(frozen_custom_field_option, current_user) - remove_logic_option_from_custom_fields(frozen_custom_field_option) + custom_field = frozen_custom_field_option.custom_field + if custom_field&.resource_type == 'CustomForm' && custom_field&.input_type == 'select' + FormLogicService.new([frozen_custom_field_option.custom_field]) + .remove_select_logic_option_from_custom_fields(frozen_custom_field_option) + end serialized_custom_field_option = clean_time_attributes(frozen_custom_field_option.attributes) LogActivityJob.perform_later( @@ -31,14 +35,4 @@ def after_destroy(frozen_custom_field_option, current_user) payload: { custom_field_option: serialized_custom_field_option } ) end - - def remove_logic_option_from_custom_fields(frozen_custom_field_option) - custom_field = frozen_custom_field_option.custom_field - - return unless custom_field&.input_type == 'select' && custom_field&.logic.present? && - custom_field.logic['rules'].pluck('if').include?(frozen_custom_field_option.id) - - custom_field.logic['rules'].reject! { |rule| rule['if'] == frozen_custom_field_option.id } - custom_field.save! - end end From 705d2cd1d1aa37a53db9df8efe62617020a7b2bc Mon Sep 17 00:00:00 2001 From: Iva Date: Mon, 20 Nov 2023 15:07:15 +0200 Subject: [PATCH 3/5] Skip test --- front/cypress/e2e/sign_up_custom_fields.cy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/front/cypress/e2e/sign_up_custom_fields.cy.ts b/front/cypress/e2e/sign_up_custom_fields.cy.ts index 8e6220d723d8..cc923d67833b 100644 --- a/front/cypress/e2e/sign_up_custom_fields.cy.ts +++ b/front/cypress/e2e/sign_up_custom_fields.cy.ts @@ -1,7 +1,7 @@ import { randomString, randomEmail, logout } from '../support/commands'; describe('Sign up - custom fields step', () => { - describe('No custom fields', () => { + describe.skip('No custom fields', () => { before(() => { const firstName = randomString(); const lastName = randomString(); From fee06b2385b01bfb4845e254f072e42d0ebd0a32 Mon Sep 17 00:00:00 2001 From: Simon Tharby Date: Mon, 20 Nov 2023 14:57:57 +0000 Subject: [PATCH 4/5] [CL-4247] explicitly find custom filed of option, rather than assume it is first field in service fileds instance var --- back/app/services/form_logic_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back/app/services/form_logic_service.rb b/back/app/services/form_logic_service.rb index 34d0585d1d89..e7d02613a614 100644 --- a/back/app/services/form_logic_service.rb +++ b/back/app/services/form_logic_service.rb @@ -39,7 +39,7 @@ def valid? end def remove_select_logic_option_from_custom_fields(frozen_custom_field_option) - custom_field = fields.first + custom_field = frozen_custom_field_option.custom_field return unless custom_field&.logic.present? && custom_field.logic['rules'].pluck('if').include?(frozen_custom_field_option.id) From 0395446b826a24324a7537616257bd2cebf61009 Mon Sep 17 00:00:00 2001 From: EdwinKato Date: Tue, 21 Nov 2023 10:05:01 +0300 Subject: [PATCH 5/5] Bump up cl2-library version --- cl2-component-library/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cl2-component-library/package.json b/cl2-component-library/package.json index 1e819aa364e2..b46f2161f9de 100644 --- a/cl2-component-library/package.json +++ b/cl2-component-library/package.json @@ -1,5 +1,5 @@ { - "version": "0.11.23", + "version": "0.11.24", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts",