2022-05-11 21:51:26 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-05-30 19:45:51 +08:00
|
|
|
module StepElements
|
2022-05-11 21:51:26 +08:00
|
|
|
class ChecklistItemsController < ApplicationController
|
|
|
|
include ApplicationHelper
|
|
|
|
|
|
|
|
before_action :load_vars
|
2022-07-14 21:09:28 +08:00
|
|
|
before_action :load_checklist_item, only: %i(update toggle destroy)
|
|
|
|
before_action :check_toggle_permissions, only: %i(toggle)
|
2022-05-11 21:51:26 +08:00
|
|
|
before_action :check_manage_permissions, only: %i(create update destroy)
|
|
|
|
|
|
|
|
def create
|
|
|
|
checklist_item = @checklist.checklist_items.build(checklist_item_params.merge!(created_by: current_user))
|
2022-06-02 17:15:32 +08:00
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
checklist_item.save!
|
|
|
|
log_activity(
|
|
|
|
"#{@step.protocol.in_module? ? :task : :protocol}_step_checklist_item_added",
|
|
|
|
{
|
|
|
|
checklist_item: checklist_item.text,
|
|
|
|
checklist_name: @checklist.name
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-06-03 17:52:10 +08:00
|
|
|
render json: checklist_item, serializer: ChecklistItemSerializer, user: current_user
|
2022-05-11 21:51:26 +08:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
2022-06-03 21:26:16 +08:00
|
|
|
render json: checklist_item, serializer: ChecklistItemSerializer,
|
|
|
|
user: current_user,
|
|
|
|
status: :unprocessable_entity
|
2022-05-11 21:51:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2022-07-12 16:41:58 +08:00
|
|
|
@checklist_item.assign_attributes(
|
|
|
|
checklist_item_params.merge(last_modified_by: current_user)
|
|
|
|
)
|
2022-05-11 21:51:26 +08:00
|
|
|
|
2022-06-02 17:15:32 +08:00
|
|
|
if @checklist_item.save!
|
2022-07-14 21:09:28 +08:00
|
|
|
log_activity(
|
|
|
|
"#{@step.protocol.in_module? ? :task : :protocol}_step_checklist_item_edited",
|
|
|
|
checklist_item: @checklist_item.text,
|
|
|
|
checklist_name: @checklist.name
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: @checklist_item, serializer: ChecklistItemSerializer, user: current_user
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
render json: @checklist_item, serializer: ChecklistItemSerializer,
|
|
|
|
user: current_user,
|
|
|
|
status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
def toggle
|
|
|
|
@checklist_item.assign_attributes(
|
|
|
|
checklist_toggle_item_params.merge(last_modified_by: current_user)
|
|
|
|
)
|
|
|
|
|
|
|
|
if @checklist_item.save!
|
|
|
|
completed_items = @checklist_item.checklist.checklist_items.where(checked: true).count
|
|
|
|
all_items = @checklist_item.checklist.checklist_items.count
|
|
|
|
text_activity = smart_annotation_parser(@checklist_item.text).gsub(/\s+/, ' ')
|
|
|
|
type_of = if @checklist_item.saved_change_to_attribute(:checked).last
|
|
|
|
:check_step_checklist_item
|
|
|
|
else
|
|
|
|
:uncheck_step_checklist_item
|
|
|
|
end
|
|
|
|
log_activity(type_of,
|
|
|
|
checkbox: text_activity,
|
|
|
|
num_completed: completed_items.to_s,
|
|
|
|
num_all: all_items.to_s)
|
2022-05-11 21:51:26 +08:00
|
|
|
end
|
|
|
|
|
2022-06-03 17:52:10 +08:00
|
|
|
render json: @checklist_item, serializer: ChecklistItemSerializer, user: current_user
|
2022-05-11 21:51:26 +08:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
2022-06-03 21:26:16 +08:00
|
|
|
render json: @checklist_item, serializer: ChecklistItemSerializer,
|
|
|
|
user: current_user,
|
|
|
|
status: :unprocessable_entity
|
2022-05-11 21:51:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
if @checklist_item.destroy
|
2022-06-02 17:15:32 +08:00
|
|
|
log_activity(
|
|
|
|
"#{@step.protocol.in_module? ? :task : :protocol}_step_checklist_item_deleted",
|
|
|
|
checklist_item: @checklist_item.text,
|
|
|
|
checklist_name: @checklist.name
|
|
|
|
)
|
2022-06-03 17:52:10 +08:00
|
|
|
render json: @checklist_item, serializer: ChecklistItemSerializer, user: current_user
|
2022-05-11 21:51:26 +08:00
|
|
|
else
|
2022-06-03 21:26:16 +08:00
|
|
|
render json: @checklist_item, serializer: ChecklistItemSerializer,
|
|
|
|
user: current_user,
|
|
|
|
status: :unprocessable_entity
|
2022-05-11 21:51:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-26 20:02:02 +08:00
|
|
|
def reorder
|
2022-08-09 20:15:49 +08:00
|
|
|
@checklist.with_lock do
|
2022-05-26 20:02:02 +08:00
|
|
|
params[:checklist_item_positions].each do |id, position|
|
|
|
|
@checklist.checklist_items.find(id).update_column(:position, position)
|
|
|
|
end
|
2022-08-09 20:15:49 +08:00
|
|
|
@checklist.touch
|
2022-05-26 20:02:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: params[:checklist_item_positions], status: :ok
|
|
|
|
end
|
|
|
|
|
2022-05-11 21:51:26 +08:00
|
|
|
private
|
|
|
|
|
2022-07-14 21:09:28 +08:00
|
|
|
def check_toggle_permissions
|
|
|
|
if ActiveModel::Type::Boolean.new.cast(checklist_toggle_item_params[:checked])
|
|
|
|
render_403 unless can_check_my_module_steps?(@step.protocol.my_module)
|
|
|
|
else
|
|
|
|
render_403 unless can_uncheck_my_module_steps?(@step.protocol.my_module)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-11 21:51:26 +08:00
|
|
|
def check_manage_permissions
|
|
|
|
render_403 unless can_manage_step?(@step)
|
|
|
|
end
|
|
|
|
|
|
|
|
def checklist_item_params
|
2022-07-14 21:09:28 +08:00
|
|
|
params.require(:attributes).permit(:text, :position)
|
|
|
|
end
|
|
|
|
|
|
|
|
def checklist_toggle_item_params
|
|
|
|
params.require(:attributes).permit(:checked)
|
2022-05-11 21:51:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def load_vars
|
|
|
|
@step = Step.find_by(id: params[:step_id])
|
|
|
|
return render_404 unless @step
|
|
|
|
|
|
|
|
@checklist = @step.checklists.find_by(id: params[:checklist_id])
|
|
|
|
return render_404 unless @checklist
|
|
|
|
end
|
|
|
|
|
2022-07-14 21:09:28 +08:00
|
|
|
def load_checklist_item
|
2022-05-11 21:51:26 +08:00
|
|
|
@checklist_item = @checklist.checklist_items.find_by(id: params[:id])
|
|
|
|
return render_404 unless @checklist_item
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_activity(type_of, message_items = {})
|
2022-06-02 17:15:32 +08:00
|
|
|
Activities::CreateActivityService.call(
|
|
|
|
activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: @step.protocol,
|
|
|
|
team: @step.protocol.team,
|
2022-06-07 18:10:10 +08:00
|
|
|
project: @step.protocol.in_module? ? @step.protocol.my_module.experiment.project : nil,
|
2022-08-10 16:37:27 +08:00
|
|
|
message_items: message_items.merge(step_message_items)
|
2022-06-02 17:15:32 +08:00
|
|
|
)
|
2022-05-11 21:51:26 +08:00
|
|
|
end
|
2022-08-10 16:37:27 +08:00
|
|
|
|
|
|
|
def step_message_items
|
|
|
|
items = {
|
|
|
|
step: @step.id,
|
|
|
|
step_position: { id: @step.id, value_for: 'position_plus_one' }
|
|
|
|
}
|
|
|
|
|
|
|
|
items[:my_module] = @step.protocol.my_module.id if @step.protocol.in_module?
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
2022-05-11 21:51:26 +08:00
|
|
|
end
|
|
|
|
end
|