Add activities for step completion and checklist toggling through API [SCI-5139]

This commit is contained in:
Oleksii Kriuchykhin 2020-10-25 22:33:50 +01:00
parent f43d72cd00
commit 4d64883963
2 changed files with 51 additions and 0 deletions

View file

@ -3,6 +3,8 @@
module Api
module V1
class ChecklistItemsController < BaseController
include ApplicationHelper
before_action :load_team, :load_project, :load_experiment, :load_task, :load_protocol, :load_step, :load_checklist
before_action only: :show do
load_checklist_item(:id)
@ -31,6 +33,23 @@ module Api
@checklist_item.assign_attributes(checklist_item_params)
if @checklist_item.changed? && @checklist_item.save!
if @checklist_item.saved_change_to_attribute?(:checked)
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,
my_module: @task.id,
step: @step.id,
step_position: { id: @step.id, value_for: 'position_plus_one' },
checkbox: text_activity,
num_completed: completed_items.to_s,
num_all: all_items.to_s)
end
render jsonapi: @checklist_item, serializer: ChecklistItemSerializer, status: :ok
else
render body: nil, status: :no_content
@ -54,6 +73,18 @@ module Api
@checklist_item = @checklist.checklist_items.find(params.require(:id))
raise PermissionError.new(Protocol, :manage) unless can_manage_protocol_in_module?(@protocol)
end
def log_activity(type_of, 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,
team: @team,
project: @project,
message_items: message_items)
end
end
end
end

View file

@ -41,6 +41,14 @@ module Api
@step.assign_attributes(step_params)
if @step.changed? && @step.save!
if @step.saved_change_to_attribute?(:completed)
completed_steps = @protocol.steps.where(completed: true).count
all_steps = @protocol.steps.count
type_of = @step.saved_change_to_attribute(:completed).last ? :complete_step : :uncomplete_step
log_activity(type_of, my_module: @task.id,
num_completed: completed_steps.to_s,
num_all: all_steps.to_s)
end
render jsonapi: @step, serializer: StepSerializer, status: :ok
else
render body: nil, status: :no_content
@ -68,6 +76,18 @@ module Api
@step = @protocol.steps.find(params.require(:id))
raise PermissionError.new(Protocol, :manage) unless can_manage_protocol_in_module?(@step.protocol)
end
def log_activity(type_of, 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,
team: @team,
project: @project,
message_items: message_items)
end
end
end
end