Skip to content

Commit

Permalink
Merge pull request #6427 from CitizenLabDotCo/CL-4247-remove-logic-wh…
Browse files Browse the repository at this point in the history
…en-survey-select-option-deleted

[CL-4247] Remove related logic when survey select option is deleted
  • Loading branch information
jinjagit authored Nov 20, 2023
2 parents ca83702 + fee06b2 commit 0de97e3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
10 changes: 10 additions & 0 deletions back/app/services/form_logic_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ def valid?
end
end

def remove_select_logic_option_from_custom_fields(frozen_custom_field_option)
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)

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
Expand Down
6 changes: 6 additions & 0 deletions back/app/services/side_fx_custom_field_option_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ def before_destroy(custom_field_option, _current_user)
end

def after_destroy(frozen_custom_field_option, current_user)
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(
encode_frozen_resource(frozen_custom_field_option),
Expand Down
29 changes: 28 additions & 1 deletion back/spec/services/side_fx_custom_field_option_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }) }
Expand All @@ -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

0 comments on commit 0de97e3

Please sign in to comment.