FIX: perform `agree_and_keep` action only if possible. (#13967)

FIX: perform agree_and_keep action only if possible. (#13967)

While deleting spammers from flag modal it’s trying to perform agree_and_keep action where it’s not possible (or already performed).

diff --git a/app/services/user_destroyer.rb b/app/services/user_destroyer.rb
index 82ac26a..bc02627 100644
--- a/app/services/user_destroyer.rb
+++ b/app/services/user_destroyer.rb
@@ -125,7 +125,7 @@ class UserDestroyer
 
   def agree_with_flags(user)
     ReviewableFlaggedPost.where(target_created_by: user).find_each do |reviewable|
-      reviewable.perform(@actor, :agree_and_keep)
+      reviewable.perform(@actor, :agree_and_keep) if reviewable.actions_for(@guardian).has?(:agree_and_keep)
     end
   end
 
diff --git a/spec/requests/admin/users_controller_spec.rb b/spec/requests/admin/users_controller_spec.rb
index d32b6be..1ad1867 100644
--- a/spec/requests/admin/users_controller_spec.rb
+++ b/spec/requests/admin/users_controller_spec.rb
@@ -665,6 +665,16 @@ RSpec.describe Admin::UsersController do
         expect(Topic.where(id: topic.id).count).to eq(0)
         expect(User.where(id: delete_me.id).count).to eq(0)
       end
+
+      context "user has reviewable flagged post which was handled" do
+        let!(:reviewable) { Fabricate(:reviewable_flagged_post, created_by: admin, target_created_by: delete_me, target: post, topic: topic, status: 4) }
+
+        it "deletes the user record" do
+          delete "/admin/users/#{delete_me.id}.json", params: { delete_posts: true, delete_as_spammer: true }
+          expect(response.status).to eq(200)
+          expect(User.where(id: delete_me.id).count).to eq(0)
+        end
+      end
     end
 
     it "deletes the user record" do

GitHub sha: 0c777825b31dfb688fbe350808a9eb67c30bf61e

This commit appears in #13967 which was approved by romanrizzi. It was merged by vinothkannans.