From 869f29d942d0285706696eda31a237a535e9a93b Mon Sep 17 00:00:00 2001 From: Anna Buianova Date: Tue, 25 Jul 2023 17:36:45 +0300 Subject: [PATCH] Only query unpublish_all data on a relevant tab in the member manager (#19827) * Only query unpublish_all data on a relevant tab in the member manager * Added specs for AuditLog::UnpublishAllsQuery --- app/controllers/admin/users_controller.rb | 7 ++++++- app/queries/audit_log/unpublish_alls_query.rb | 6 ++++++ .../queries/audit_log/unpublish_alls_query_spec.rb | 14 ++++++++++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index aaff393ab..561f3a7e1 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -418,7 +418,12 @@ module Admin def set_unpublish_all_log # in theory, there could be multiple "unpublish all" actions # but let's query and display the last one for now, that should be enough for most cases - @unpublish_all_data = AuditLog::UnpublishAllsQuery.call(@user.id) + @unpublish_all_data = if @current_tab == "unpublish_logs" + AuditLog::UnpublishAllsQuery.call(@user.id) + else + # only find if the data exists for most tabs + AuditLog::UnpublishAllsQuery.new(@user.id).exists? + end end def calculate_countable_flags(reactions) diff --git a/app/queries/audit_log/unpublish_alls_query.rb b/app/queries/audit_log/unpublish_alls_query.rb index fdd45abc6..32aaf6201 100644 --- a/app/queries/audit_log/unpublish_alls_query.rb +++ b/app/queries/audit_log/unpublish_alls_query.rb @@ -12,6 +12,12 @@ class AuditLog @target_comments = [] end + def exists? + exists = AuditLog.where(slug: %w[api_user_unpublish unpublish_all_articles]) + .where("data @> '{\"target_user_id\": ?}'", user_id).present? + Result.new(exists?: exists) + end + def call audit_log = AuditLog.where(slug: %w[api_user_unpublish unpublish_all_articles]) .where("data @> '{\"target_user_id\": ?}'", user_id) diff --git a/spec/queries/audit_log/unpublish_alls_query_spec.rb b/spec/queries/audit_log/unpublish_alls_query_spec.rb index afb72d9af..983039d2e 100644 --- a/spec/queries/audit_log/unpublish_alls_query_spec.rb +++ b/spec/queries/audit_log/unpublish_alls_query_spec.rb @@ -18,15 +18,25 @@ RSpec.describe AuditLog::UnpublishAllsQuery, type: :query do expect(res.audit_log).to eq(audit_log) end - it "exists?" do + it "exists? when data requested" do res = described_class.call(user.id) expect(res.exists?).to be true end + + it "exists? when exists requested" do + res = described_class.new(user.id).exists? + expect(res.exists?).to be true + end end - it "doesn't exist when there is no related audit_log" do + it "doesn't exist when there is no related audit_log and asked for data" do res = described_class.call(user.id) expect(res.exists?).to be false end + + it "doesn't exist when there is no related audit_log and asked for exists?" do + res = described_class.new(user.id).exists? + expect(res.exists?).to be false + end end end