discourse/plugins/automation/app/services/discourse_automation/destroy_automation.rb
Joffrey JAFFEUX 932bd6ba85
Some checks are pending
Licenses / run (push) Waiting to run
Linting / run (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (frontend, themes) (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (system, chat) (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (system, core) (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (system, plugins) (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (system, themes) (push) Waiting to run
Tests / core frontend (${{ matrix.browser }}) (Chrome) (push) Waiting to run
Tests / core frontend (${{ matrix.browser }}) (Firefox ESR) (push) Waiting to run
Tests / core frontend (${{ matrix.browser }}) (Firefox Evergreen) (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (annotations, core) (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (backend, core) (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (backend, plugins) (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (frontend, plugins) (push) Waiting to run
UX: logs when an automation is destroyed (#29565)
A `UserHistory` entry will now be created when an automation is destroyed. This is visible in `/admin/logs/staff_action_logs`. id, name, trigger and script will be logged.

This commit also creates a service `DestroyAutomation` to hold all the destroy automation logic.

---------
Co-authored-by: Martin Brennan <mjrbrennan@gmail.com>
2024-11-04 11:12:18 +09:00

49 lines
1.1 KiB
Ruby

# frozen_string_literal: true
module DiscourseAutomation
class DestroyAutomation
include ::Service::Base
# @!method self.call(guardian:, params:)
# @param [Guardian] guardian
# @param [Hash] params
# @option params [Integer] :automation_id
# @return [Service::Base::Context]
params do
attribute :automation_id, :integer
validates :automation_id, presence: true
end
model :automation
policy :can_destroy_automation
transaction do
step :log_action
step :destroy_automation
end
private
def fetch_automation(params:)
DiscourseAutomation::Automation.find_by(id: params.automation_id)
end
def can_destroy_automation(guardian:)
guardian.is_admin?
end
def log_action(automation:, guardian:)
StaffActionLogger.new(guardian.user).log_custom(
"delete_automation",
id: automation.id,
name: automation.name,
script: automation.script,
trigger: automation.trigger,
)
end
def destroy_automation(automation:)
automation.destroy!
end
end
end