diff --git a/app/controllers/api/v1/checklist_items_controller.rb b/app/controllers/api/v1/checklist_items_controller.rb index d8f1c6c5f..f51284962 100644 --- a/app/controllers/api/v1/checklist_items_controller.rb +++ b/app/controllers/api/v1/checklist_items_controller.rb @@ -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 diff --git a/app/controllers/api/v1/steps_controller.rb b/app/controllers/api/v1/steps_controller.rb index c6e8bafe8..10f3c5897 100644 --- a/app/controllers/api/v1/steps_controller.rb +++ b/app/controllers/api/v1/steps_controller.rb @@ -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