Add activities for label template actions [SCI-7086]

This commit is contained in:
Oleksii Kriuchykhin 2022-08-22 17:38:52 +02:00
parent a5fc66a6f3
commit bb6e638528
6 changed files with 91 additions and 19 deletions

View file

@ -33,17 +33,29 @@ class LabelTemplatesController < ApplicationController
end
def create
label_template = ZebraLabelTemplate.default.save!
redirect_to label_template_path(label_template, new_label: true)
ActiveRecord::Base.transaction do
label_template = ZebraLabelTemplate.default
label_template.team = current_team
label_template.save!
log_activity(:label_template_created, label_template)
redirect_to label_template_path(label_template, new_label: true)
end
rescue StandardError => e
Rails.logger.error(e.message)
Rails.logger.error(e.backtrace.join("\n"))
flash[:error] = I18n.t('errors.general')
redirect_to label_templates_path
end
def update
if @label_template.update(label_template_params)
render json: @label_template, serializer: LabelTemplateSerializer, user: current_user
else
render json: { error: @label_template.errors.messages }, status: :unprocessable_entity
@label_template.transaction do
@label_template.update!(label_template_params)
log_activity(:label_template_edited, @label_template)
end
render json: @label_template, serializer: LabelTemplateSerializer, user: current_user
rescue StandardError => e
Rails.logger.error e.message
render json: { error: @label_template.errors.messages }, status: :unprocessable_entity
end
def duplicate
@ -53,22 +65,32 @@ class LabelTemplatesController < ApplicationController
new_template.default = false
new_template.name = template.name + '(1)'
new_template.save!
log_activity(
:label_template_copied,
new_template,
message_items: { label_template_new: new_template.id, label_template_original: template.id }
)
end
render json: { message: I18n.t('label_templates.index.templates_duplicated',
count: params[:selected_ids].length) }
rescue ActiveRecord::RecordInvalid => e
Rails.logger.error e.message
render json: { error: I18n.t('errors.general') }, status: :unprocessable_entity
end
rescue StandardError => e
Rails.logger.error(e.message)
Rails.logger.error(e.backtrace.join("\n"))
render json: { error: I18n.t('errors.general') }, status: :unprocessable_entity
end
def delete
ActiveRecord::Base.transaction do
LabelTemplate.where(team_id: current_team.id, id: params[:selected_ids]).each(&:destroy!)
LabelTemplate.where(team_id: current_team.id, id: params[:selected_ids]).each do |template|
log_activity(:label_template_deleted, template)
template.destroy!
end
render json: { message: I18n.t('label_templates.index.templates_deleted') }
end
rescue ActiveRecord::RecordNotDestroyed => e
Rails.logger.error e.message
rescue StandardError => e
Rails.logger.error(e.message)
Rails.logger.error(e.backtrace.join("\n"))
render json: { error: I18n.t('errors.general') }, status: :unprocessable_entity
end
@ -80,8 +102,9 @@ class LabelTemplatesController < ApplicationController
@label_template.update!(default: true)
render json: { message: I18n.t('label_templates.index.template_set_as_default') }
end
rescue ActiveRecord::RecordInvalid => e
Rails.logger.error e.message
rescue StandardError => e
Rails.logger.error(e.message)
Rails.logger.error(e.backtrace.join("\n"))
render json: { error: I18n.t('errors.general') }, status: :unprocessable_entity
end
@ -134,4 +157,15 @@ class LabelTemplatesController < ApplicationController
def label_template_params
params.require(:label_template).permit(:name, :description, :content)
end
def log_activity(type_of, label_template = @label_template, message_items: {})
message_items = { label_template: label_template.id } if message_items.blank?
message_items[:type] = I18n.t("label_templates.types.#{label_template.class.name.underscore}")
Activities::CreateActivityService
.call(activity_type: type_of,
owner: current_user,
subject: label_template,
team: label_template.team,
message_items: message_items)
end
end

View file

@ -155,6 +155,9 @@ class Activity < ApplicationRecord
generate_breadcrumb(subject.my_module)
when Team
breadcrumbs[:team] = subject.name
when LabelTemplate
breadcrumbs[:label_template] = subject.name
generate_breadcrumb(subject.team) if subject.team
when Report
breadcrumbs[:report] = subject.name
generate_breadcrumb(subject.team) if subject.team

View file

@ -0,0 +1,15 @@
<%= render partial: "global_activities/references/team.html.erb",
locals: { team: team, subject: team, breadcrumbs: breadcrumbs, values: values, type_of: type_of } %>
<div class="ga-breadcrumb">
<span class="fas fa-tag"></span>
<% if subject %>
<%= route_to_other_team(label_template_path(subject),
team,
subject.name&.truncate(Constants::NAME_TRUNCATION_LENGTH),
title: subject.name) %>
<% else %>
<span title="<%= breadcrumbs['label_template'] %>">
<%= breadcrumbs['label_template']&.truncate(Constants::NAME_TRUNCATION_LENGTH) %>
</span>
<% end %>
</div>

View file

@ -193,7 +193,7 @@ class Extends
ACTIVITY_SUBJECT_TYPES = %w(
Team RepositoryBase Project Experiment MyModule Result Protocol Report RepositoryRow
ProjectFolder Asset Step
ProjectFolder Asset Step LabelTemplate
).freeze
SEARCHABLE_ACTIVITY_SUBJECT_TYPES = %w(
@ -415,7 +415,11 @@ class Extends
protocol_step_checklist_deleted: 212,
protocol_step_checklist_item_added: 213,
protocol_step_checklist_item_edited: 214,
protocol_step_checklist_item_deleted: 215
protocol_step_checklist_item_deleted: 215,
label_template_created: 216,
label_template_edited: 217,
label_template_deleted: 218,
label_template_copied: 219
}
ACTIVITY_GROUPS = {
@ -433,7 +437,8 @@ class Extends
protocol_repository: [80, 103, 89, 87, 79, 90, 91, 88, 85, 86, 84, 81, 82,
83, 101, 112, 123, 125, 117, 119, 129, 131, 170, 173, 179, 187, 186,
190, 191, *204..215],
team: [92, 94, 93, 97, 104]
team: [92, 94, 93, 97, 104],
label_repository: [*216..219]
}
SHARED_INVENTORIES_PERMISSION_LEVELS = {

View file

@ -825,9 +825,16 @@ en:
can_add_user_to_project: "Can not add user to the project."
label_templates:
types:
fluics_label_template: 'Fluics'
zebra_label_template: 'ZPL'
default_zebra_name: 'SciNote Item (ZPL)'
default_fluics_name: 'SciNote Item (Fluics)'
new_label_template: 'New label'
repository_row:
errors:
unsupported_key: "Key %{key} is not supported by this label template renderer."
column_not_found: "Column '%{column}' not found on inventory item."
index:
head_title: 'Label templates'
search_templates: 'Filter templates'

View file

@ -186,7 +186,6 @@ en:
export_inventory_snapshot_items_assigned_to_task_html: "%{user} exported inventory item(s) assigned to task %{my_module} from inventory %{repository_snapshot}: Snapshot of %{created_at}."
change_status_on_task_flow_html: "%{user} changed status from <strong>%{my_module_status_old}</strong> to <strong>%{my_module_status_new}</strong> for task %{my_module}."
move_project_html: "%{user} moved project %{project} from folder %{project_folder_from} to folder %{project_folder_to}."
create_project_folder_html: "%{user} created project folder %{project_folder}."
rename_project_folder_html: "%{user} renamed project folder %{project_folder}."
delete_project_folder_html: "%{user} deleted project folder %{project_folder}."
@ -241,6 +240,10 @@ en:
protocol_step_checklist_item_added_html: "%{user} created checklist item <strong>%{checklist_item}</strong> in checklist <strong>%{checklist_name}</strong> in protocol's step %{step_position} <strong>%{step}</strong> in Protocol repository"
protocol_step_checklist_item_edited_html: "%{user} edited checklist item <strong>%{checklist_item}</strong> in checklist <strong>%{checklist_name}</strong> in protocol's step %{step_position} <strong>%{step}</strong> in Protocol repository"
protocol_step_checklist_item_deleted_html: "%{user} deleted checklist item <strong>%{checklist_item}</strong> in checklist <strong>%{checklist_name}</strong> in protocol's step %{step_position} <strong>%{step}</strong> in Protocol repository"
label_template_created_html: "%{user} created %{type} label template %{label_template} in Label templates."
label_template_edited_html: "%{user} edited %{type} label template %{label_template} in Label templates."
label_template_deleted_html: "%{user} deleted %{type} label template %{label_template} in Label templates."
label_template_copied_html: "%{user} copied %{type} label template %{label_template_new} from %{label_template_original} in Label templates."
activity_name:
create_project: "Project created"
rename_project: "Project renamed"
@ -435,6 +438,10 @@ en:
protocol_step_checklist_item_added: "Protocol step checklist item added"
protocol_step_checklist_item_edited: "Protocol step checklist item edited"
protocol_step_checklist_item_deleted: "Protocol step checklist item deleted"
label_template_created: "Label template created"
label_template_edited: "Label template edited"
label_template_deleted: "Label template deleted"
label_template_copied: "Label template copied"
@ -450,6 +457,7 @@ en:
protocol_repository: "Protocol repository"
team: "Team"
exports: "Exports"
label_repository: "Label repository"
subject_name:
repository: "Inventory"
project: "Project"