Fix step elements interactions for technician role [SCI-6973]

This commit is contained in:
Oleksii Kriuchykhin 2022-07-14 15:09:28 +02:00
parent c85612ee4e
commit a40b218f97
8 changed files with 99 additions and 38 deletions

View file

@ -5,7 +5,8 @@ module StepElements
include ApplicationHelper
before_action :load_vars
before_action :load_checklist, only: %i(update destroy)
before_action :load_checklist_item, only: %i(update toggle destroy)
before_action :check_toggle_permissions, only: %i(toggle)
before_action :check_manage_permissions, only: %i(create update destroy)
def create
@ -35,26 +36,38 @@ module StepElements
)
if @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,
checkbox: text_activity,
num_completed: completed_items.to_s,
num_all: all_items.to_s)
else
log_activity(
"#{@step.protocol.in_module? ? :task : :protocol}_step_checklist_item_edited",
checklist_item: @checklist_item.text,
checklist_name: @checklist.name
)
end
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)
end
render json: @checklist_item, serializer: ChecklistItemSerializer, user: current_user
@ -91,12 +104,24 @@ module StepElements
private
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
def check_manage_permissions
render_403 unless can_manage_step?(@step)
end
def checklist_item_params
params.require(:attributes).permit(:checked, :text, :position)
params.require(:attributes).permit(:text, :position)
end
def checklist_toggle_item_params
params.require(:attributes).permit(:checked)
end
def load_vars
@ -107,7 +132,7 @@ module StepElements
return render_404 unless @checklist
end
def load_checklist
def load_checklist_item
@checklist_item = @checklist.checklist_items.find_by(id: params[:id])
return render_404 unless @checklist_item
end

View file

@ -4,7 +4,7 @@
<div class="title">
<h3>{{ i18n.t('protocols.steps.files', {count: attachments.length}) }}</h3>
</div>
<div class="actions">
<div class="actions" v-if="step.attributes.attachments_manageble">
<div class="dropdown sci-dropdown">
<button class="btn btn-light dropdown-toggle" type="button" id="dropdownAttachmentsOptions" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span>{{ i18n.t("protocols.steps.attachments.manage") }}</span>

View file

@ -53,7 +53,7 @@
{{ step.attributes.name }}
</span>
</div>
<i v-if="!editingName" class="step-name-edit-icon fas fa-pen" @click="editingName = true"></i>
<i v-if="urls.update_url && !editingName" class="step-name-edit-icon fas fa-pen" @click="editingName = true"></i>
</div>
<div class="step-actions-container">
<div ref="actionsDropdownButton" v-if="urls.update_url" class="dropdown">
@ -201,7 +201,7 @@
required: true
},
reorderStepUrl: {
required: true
required: false
}
},
data() {

View file

@ -54,6 +54,7 @@
@editStart="editingItem = true"
@editEnd="editingItem = false"
@update="saveItem"
@toggle="saveItemChecked"
@removeItem="removeItem"
@component:delete="removeItem"
@multilinePaste="handleMultilinePaste"
@ -178,6 +179,20 @@
}
this.update();
},
saveItemChecked(item) {
$.ajax({
url: item.attributes.urls.toggle_url,
type: 'PATCH',
data: { attributes: { checked: item.attributes.checked } },
success: (result) => {
let updatedItem = this.checklistItems[item.attributes.position]
updatedItem.attributes = result.data.attributes
updatedItem.attributes.id = item.attributes.id
this.$set(this.checklistItems, item.attributes.position, updatedItem)
},
error: () => HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger')
});
},
addItem() {
this.checklistItems.push(
{

View file

@ -5,7 +5,7 @@
<i class="fas fa-grip-vertical"></i>
</div>
<div class="step-element-name" :class="{ 'done': checklistItem.attributes.checked }">
<div class="sci-checkbox-container" :class="{ 'disabled': !updateUrl || inRepository}">
<div class="sci-checkbox-container" :class="{ 'disabled': !toggleUrl || inRepository}">
<input ref="checkbox"
type="checkbox"
class="sci-checkbox"
@ -94,6 +94,11 @@
return this.checklistItem.attributes.urls.update_url;
},
toggleUrl() {
if (!this.checklistItem.attributes.urls) return
return this.checklistItem.attributes.urls.toggle_url;
},
deleteUrl() {
if (!this.checklistItem.attributes.urls) return
@ -114,9 +119,9 @@
this.$emit('editEnd');
},
toggleChecked(e) {
if (!this.updateUrl) return
if (!this.toggleUrl) return
this.checklistItem.attributes.checked = this.$refs.checkbox.checked;
this.update();
this.$emit('toggle', this.checklistItem);
},
updateText(text) {
if (text.length === 0) {

View file

@ -17,13 +17,23 @@ class ChecklistItemSerializer < ActiveModel::Serializer
end
def urls
return {} if object.destroyed? ||
!object.persisted? ||
!can_manage_step?(scope[:user] || @instance_options[:user], object.checklist.step)
urls_list = {}
return urls_list if object.destroyed? || !object.persisted?
{
update_url: step_checklist_checklist_item_path(object.checklist.step, object.checklist, object),
delete_url: step_checklist_checklist_item_path(object.checklist.step, object.checklist, object)
}
step = object.checklist.step
my_module = object.checklist.step.protocol.my_module
if !object.checked && can_check_my_module_steps?(scope[:user] || @instance_options[:user], my_module) ||
object.checked && can_uncheck_my_module_steps?(scope[:user] || @instance_options[:user], my_module)
urls_list[:toggle_url] =
toggle_step_checklist_checklist_item_path(step, object.checklist, object)
end
if can_manage_step?(scope[:user] || @instance_options[:user], step)
urls_list[:update_url] = step_checklist_checklist_item_path(step, object.checklist, object)
urls_list[:delete_url] = step_checklist_checklist_item_path(step, object.checklist, object)
end
urls_list
end
end

View file

@ -6,7 +6,7 @@ class StepSerializer < ActiveModel::Serializer
include ApplicationHelper
include CommentHelper
attributes :name, :position, :completed, :urls, :assets_view_mode, :assets_order,
attributes :name, :position, :completed, :attachments_manageble, :urls, :assets_view_mode, :assets_order,
:marvinjs_enabled, :bio_eddie_service_enabled, :bio_eddie_context, :marvinjs_context,
:wopi_enabled, :wopi_context, :comments_count, :unseen_comments, :storage_limit
@ -64,16 +64,21 @@ class StepSerializer < ActiveModel::Serializer
object.current_view_state(@instance_options[:user]).state.dig('assets', 'sort') unless object.destroyed?
end
def attachments_manageble
can_manage_step?(object)
end
def urls
urls_list = {
elements_url: elements_step_path(object),
attachments_url: attachments_step_path(object)
}
urls_list[:state_url] = toggle_step_state_step_path(object) if can_complete_my_module_steps?(object.my_module)
if can_manage_step?(object)
urls_list.merge!({
delete_url: step_path(object),
state_url: toggle_step_state_step_path(object),
update_url: step_path(object),
create_table_url: step_tables_path(object),
create_text_url: step_texts_path(object),

View file

@ -456,6 +456,7 @@ Rails.application.routes.draw do
resources :texts, controller: 'step_elements/texts', only: %i(create destroy update)
resources :checklists, controller: 'step_elements/checklists', only: %i(create destroy update) do
resources :checklist_items, controller: 'step_elements/checklist_items', only: %i(create update destroy) do
patch :toggle, on: :member
post :reorder, on: :collection
end
end