From c0db6a59b275cd7f2474e9774095a0f5cffe28ff Mon Sep 17 00:00:00 2001 From: Caio <117518+caiosba@users.noreply.github.com> Date: Sun, 23 Feb 2025 23:18:24 -0300 Subject: [PATCH] Fixed cache clearing for bulk-accepted suggestions. Previously, the id was incorrectly used as target_ids. Now, all relevant fields are included, not just a subset. Fixes: CV2-6191. --- app/models/concerns/relationship_bulk.rb | 18 +++++------------- test/models/relationship_test.rb | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/app/models/concerns/relationship_bulk.rb b/app/models/concerns/relationship_bulk.rb index a2af22921..b999225e2 100644 --- a/app/models/concerns/relationship_bulk.rb +++ b/app/models/concerns/relationship_bulk.rb @@ -20,7 +20,6 @@ def bulk_update(ids, updates, team) end relationships = Relationship.where(id: ids, source_id: source_id) relationships.update_all(update_columns) - delete_cached_field(pm_source.id, ids) # Run callbacks in background extra_options = { team_id: team&.id, @@ -29,6 +28,7 @@ def bulk_update(ids, updates, team) action: updates[:action] } self.delay.run_update_callbacks(ids.to_json, extra_options.to_json) + delete_cached_fields(pm_source.id, relationships.map(&:target_id)) { source_project_media: pm_source } end end @@ -41,7 +41,7 @@ def bulk_destroy(ids, updates, team) relationships.find_each{ |r| relationship_target[r.id] = r.target_id} relationships.delete_all target_ids = relationship_target.values - delete_cached_field(pm_source.id, target_ids) + delete_cached_fields(pm_source.id, target_ids) # Run callbacks in background extra_options = { team_id: team&.id, @@ -52,17 +52,9 @@ def bulk_destroy(ids, updates, team) { source_project_media: pm_source } end - def delete_cached_field(source_id, target_ids) - # Clear cached fields - # List fields with `model: Relationship` - cached_fields = [ - 'is_suggested', 'is_confirmed', 'linked_items_count', 'suggestions_count','report_status','related_count', - 'demand', 'last_seen', 'sources_as_sentence', 'added_as_similar_by_name', 'confirmed_as_similar_by_name' - ] - cached_fields.each do |name| - Rails.cache.delete("check_cached_field:ProjectMedia:#{source_id}:#{name}") - target_ids.each { |id| Rails.cache.delete("check_cached_field:ProjectMedia:#{id}:#{name}") } - end + def delete_cached_fields(source_id, target_ids) + ids = [source_id, target_ids].flatten + ProjectMedia.where(id: ids).each { |pm| pm.clear_cached_fields } end def run_update_callbacks(ids_json, extra_options_json) diff --git a/test/models/relationship_test.rb b/test/models/relationship_test.rb index f52b1e337..92b49e047 100644 --- a/test/models/relationship_test.rb +++ b/test/models/relationship_test.rb @@ -428,4 +428,23 @@ def setup assert_equal 4, Relationship.where(source_id: pm1.id, relationship_type: Relationship.suggested_type).count assert_equal 0, Relationship.where(source_id: pm2.id, relationship_type: Relationship.suggested_type).count end + + test "should update media origin when bulk-accepting suggestion" do + Sidekiq::Testing.inline! + RequestStore.store[:skip_cached_field_update] = false + u = create_user is_admin: true + + # Create suggestion + t = create_team + pm1 = create_project_media team: t + pm2 = create_project_media team: t + r = create_relationship source_id: pm1.id, target_id: pm2.id, relationship_type: Relationship.suggested_type + assert_equal CheckMediaClusterOrigins::OriginCodes::AUTO_MATCHED, pm2.media_cluster_origin + + # Accept suggestion + with_current_user_and_team u, t do + Relationship.bulk_update([r.id], { source_id: pm1.id, action: 'accept' }, t) + end + assert_equal CheckMediaClusterOrigins::OriginCodes::USER_MATCHED, pm2.media_cluster_origin + end end