2022-04-29 18:29:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class StepOrderableElementsController < ApplicationController
|
|
|
|
before_action :load_vars_nested
|
2022-05-30 19:45:51 +08:00
|
|
|
before_action :check_manage_permissions
|
2022-04-29 18:29:42 +08:00
|
|
|
|
2022-05-25 20:23:35 +08:00
|
|
|
def reorder
|
2022-08-09 20:15:49 +08:00
|
|
|
@step.with_lock do
|
2022-05-30 19:45:51 +08:00
|
|
|
params[:step_orderable_element_positions].each do |id, position|
|
|
|
|
@step.step_orderable_elements.find(id).update_column(:position, position)
|
|
|
|
end
|
2022-06-01 18:30:09 +08:00
|
|
|
|
|
|
|
if @protocol.in_module?
|
|
|
|
log_activity(:task_step_content_rearranged, @my_module.experiment.project, my_module: @my_module.id)
|
|
|
|
else
|
|
|
|
log_activity(:protocol_step_content_rearranged, nil, protocol: @protocol.id)
|
|
|
|
end
|
2022-08-09 20:15:49 +08:00
|
|
|
@step.touch
|
2022-05-30 19:45:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: params[:step_orderable_element_positions], status: :ok
|
2022-05-03 21:15:49 +08:00
|
|
|
end
|
|
|
|
|
2022-04-29 18:29:42 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def load_vars_nested
|
|
|
|
@step = Step.find_by(id: params[:step_id])
|
|
|
|
return render_404 unless @step
|
|
|
|
|
|
|
|
@protocol = @step.protocol
|
2022-06-01 18:30:09 +08:00
|
|
|
@my_module = @protocol.my_module
|
2022-04-29 18:29:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_view_permissions
|
|
|
|
render_403 unless can_read_protocol_in_module?(@protocol) || can_read_protocol_in_repository?(@protocol)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_manage_permissions
|
|
|
|
render_403 unless can_manage_step?(@step)
|
|
|
|
end
|
2022-06-01 18:30:09 +08:00
|
|
|
|
|
|
|
def log_activity(type_of, project = nil, message_items = {})
|
|
|
|
default_items = { step: @step.id,
|
|
|
|
step_position: { id: @step.id, value_for: 'position_plus_one' } }
|
|
|
|
message_items = default_items.merge(message_items)
|
|
|
|
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: @protocol,
|
2022-11-24 22:19:17 +08:00
|
|
|
team: @protocol.team,
|
2022-06-01 18:30:09 +08:00
|
|
|
project: project,
|
|
|
|
message_items: message_items)
|
|
|
|
end
|
2022-04-29 18:29:42 +08:00
|
|
|
end
|