Create a note when a users' articles are being unpublished via API (#18545)

* Moved a comment about soft delete from controller to the service

* Create a note when unpublishing all users' articles via api

* Create a note with the default text when unpublishing all via api, added docs
This commit is contained in:
Anna Buianova 2022-10-10 16:25:56 +03:00 committed by GitHub
parent 11f75af401
commit 184ee8039f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 2 deletions

View file

@ -50,9 +50,13 @@ module Api
target_user = User.find(params[:id].to_i)
# Unpublish posts and delete comments w/ boolean attr to allow revert
Moderator::UnpublishAllArticlesWorker.perform_async(target_user.id, @user.id)
note_content = params[:note].presence || "#{@user.username} requested unpublish all articles via API"
Note.create(noteable: target_user, reason: "unpublish_all_articles",
content: note_content, author: @user)
render status: :no_content
end
end

View file

@ -1,3 +1,5 @@
# Unpublish posts and delete comments w/ boolean attr (setting deleted: true) to allow revert
# Create a corresponding audit_log record
module Moderator
class UnpublishAllArticles
# @param target_user_id [Integer] the id of the user whose posts are being unpublished

View file

@ -41,7 +41,7 @@ RSpec.describe "/api/admin/users", type: :request do
let(:api_secret) { create(:api_secret, user: super_admin) }
let(:headers) { v1_headers.merge({ "api-key" => api_secret.secret }) }
it "accepts reqeuest with a super-admin token" do
it "accepts request with a super-admin token" do
expect do
post api_admin_users_path, params: params, headers: headers
end.to change(User, :count).by(1)

View file

@ -129,6 +129,11 @@ will remain."
},
example: 1
parameter name: :note, in: :query, required: false,
description: "Content for the note that's created along with unpublishing",
schema: { type: :string },
example: "Admin requested unpublishing all articles via API"
response "204", "Article successfully unpublished" do
let(:"api-key") { api_secret.secret }
let(:id) { article.id }

View file

@ -263,6 +263,28 @@ RSpec.describe "Api::V1::Users", type: :request do
expect(log.data["target_article_ids"]).to match_array(target_articles.map(&:id))
expect(log.data["target_comment_ids"]).to match_array(target_comments.map(&:id))
end
it "creates a note when note text is passed" do
sidekiq_perform_enqueued_jobs(only: Moderator::UnpublishAllArticlesWorker) do
expect do
put api_user_unpublish_path(id: target_user.id, note: "hehe"), headers: auth_headers
end.to change(Note, :count).by(1)
end
note = target_user.notes.last
expect(note.content).to eq("hehe")
expect(note.reason).to eq("unpublish_all_articles")
end
it "creates a note with the default text when note text is not passed" do
sidekiq_perform_enqueued_jobs(only: Moderator::UnpublishAllArticlesWorker) do
expect do
put api_user_unpublish_path(id: target_user.id), headers: auth_headers
end.to change(Note, :count).by(1)
end
note = target_user.notes.last
expect(note.content).to eq("#{api_secret.user.username} requested unpublish all articles via API")
expect(note.reason).to eq("unpublish_all_articles")
end
end
end
end