From 115e6deae89a7f11e0d7ac48fecc10f38c908be4 Mon Sep 17 00:00:00 2001 From: Oluwatoyosi Oyegoke <34948675+Tooyosi@users.noreply.github.com> Date: Tue, 9 Jul 2024 17:31:04 +0100 Subject: [PATCH] add check to prevent soft deleted subject added to subject set (#4355) --- app/controllers/api/v1/subject_sets_controller.rb | 2 +- spec/controllers/api/v1/subject_sets_controller_spec.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/subject_sets_controller.rb b/app/controllers/api/v1/subject_sets_controller.rb index 5684483c2..23cb2fb10 100644 --- a/app/controllers/api/v1/subject_sets_controller.rb +++ b/app/controllers/api/v1/subject_sets_controller.rb @@ -112,7 +112,7 @@ def add_relation(resource, relation, value) if relation == :subjects && value.is_a?(Array) #ids is returning duplicates even though the AR Relations were uniq subject_ids_to_link = new_items(resource, relation, value).distinct.ids - unless Subject.where(id: subject_ids_to_link).count == value.count + unless Subject.where(id: subject_ids_to_link, activated_state: 'active').count == value.count raise BadLinkParams.new("Error: check the subject set and all the subjects exist.") end new_sms_values = subject_ids_to_link.map do |subject_id| diff --git a/spec/controllers/api/v1/subject_sets_controller_spec.rb b/spec/controllers/api/v1/subject_sets_controller_spec.rb index 360db0c9c..e9f40cc8e 100644 --- a/spec/controllers/api/v1/subject_sets_controller_spec.rb +++ b/spec/controllers/api/v1/subject_sets_controller_spec.rb @@ -227,6 +227,15 @@ expect(response).to have_http_status(:ok) end end + + context 'when a linking resource has been soft deleted' do + it 'returns a 422 with a missing subject' do + last_subject = subjects.last + last_subject.update(activated_state: 'inactive') + run_update_links + expect(response).to have_http_status(:unprocessable_entity) + end + end end context 'with illegal link properties' do