From d1a46b3fc2721427747fe517a5317132ca92da3d Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Tue, 9 Aug 2022 14:15:49 +0200 Subject: [PATCH] Fix protocol linked protocol changes tracking [SCI-6986] --- app/controllers/api/service/protocols_controller.rb | 1 + app/controllers/api/service/steps_controller.rb | 1 + app/controllers/step_elements/checklist_items_controller.rb | 3 ++- app/controllers/step_orderable_elements_controller.rb | 3 ++- app/controllers/steps_controller.rb | 3 ++- app/javascript/vue/protocol/container.vue | 5 ++--- app/javascript/vue/protocol/step.vue | 3 ++- app/javascript/vue/protocol/step_elements/checklist.vue | 3 ++- app/models/checklist_item.rb | 3 ++- app/models/comment.rb | 3 ++- app/views/shared/comments/_comments.html.erb | 2 +- app/views/shared/comments/_comments_list.html.erb | 2 +- 12 files changed, 20 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/service/protocols_controller.rb b/app/controllers/api/service/protocols_controller.rb index 9fd6c463f..c1ba0f351 100644 --- a/app/controllers/api/service/protocols_controller.rb +++ b/app/controllers/api/service/protocols_controller.rb @@ -15,6 +15,7 @@ module Api @protocol.steps.find(order['id']).update_column(:position, order['position']) # rubocop:enable Rails/SkipsModelValidations end + @protocol.touch rescue StandardError head :bad_request end diff --git a/app/controllers/api/service/steps_controller.rb b/app/controllers/api/service/steps_controller.rb index da99ab12e..c23e9efee 100644 --- a/app/controllers/api/service/steps_controller.rb +++ b/app/controllers/api/service/steps_controller.rb @@ -14,6 +14,7 @@ module Api @step.step_orderable_elements.find(order['id']).update_column(:position, order['position']) # rubocop:enable Rails/SkipsModelValidations end + @step.touch rescue StandardError head :bad_request end diff --git a/app/controllers/step_elements/checklist_items_controller.rb b/app/controllers/step_elements/checklist_items_controller.rb index e2ae5c171..7a39e3a52 100644 --- a/app/controllers/step_elements/checklist_items_controller.rb +++ b/app/controllers/step_elements/checklist_items_controller.rb @@ -93,10 +93,11 @@ module StepElements end def reorder - ActiveRecord::Base.transaction do + @checklist.with_lock do params[:checklist_item_positions].each do |id, position| @checklist.checklist_items.find(id).update_column(:position, position) end + @checklist.touch end render json: params[:checklist_item_positions], status: :ok diff --git a/app/controllers/step_orderable_elements_controller.rb b/app/controllers/step_orderable_elements_controller.rb index 8d2996830..70e7509be 100644 --- a/app/controllers/step_orderable_elements_controller.rb +++ b/app/controllers/step_orderable_elements_controller.rb @@ -5,7 +5,7 @@ class StepOrderableElementsController < ApplicationController before_action :check_manage_permissions def reorder - ActiveRecord::Base.transaction do + @step.with_lock do params[:step_orderable_element_positions].each do |id, position| @step.step_orderable_elements.find(id).update_column(:position, position) end @@ -15,6 +15,7 @@ class StepOrderableElementsController < ApplicationController else log_activity(:protocol_step_content_rearranged, nil, protocol: @protocol.id) end + @step.touch end render json: params[:step_orderable_element_positions], status: :ok diff --git a/app/controllers/steps_controller.rb b/app/controllers/steps_controller.rb index f0780f1eb..1ec85c45f 100644 --- a/app/controllers/steps_controller.rb +++ b/app/controllers/steps_controller.rb @@ -458,7 +458,7 @@ class StepsController < ApplicationController end def reorder - ActiveRecord::Base.transaction do + @protocol.with_lock do params[:step_positions].each do |id, position| @protocol.steps.find(id).update_column(:position, position) end @@ -468,6 +468,7 @@ class StepsController < ApplicationController else log_activity(:protocol_steps_rearranged, nil, protocol: @protocol.id) end + @protocol.touch end render json: { diff --git a/app/javascript/vue/protocol/container.vue b/app/javascript/vue/protocol/container.vue index 749a8286b..8140ad8c2 100644 --- a/app/javascript/vue/protocol/container.vue +++ b/app/javascript/vue/protocol/container.vue @@ -267,10 +267,9 @@ data: JSON.stringify(stepPositions), contentType: "application/json", dataType: "json", - error: (() => HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger')) + error: (() => HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger')), + success: (() => this.reorderSteps(this.steps)) }); - - this.reorderSteps(this.steps); }, startStepReorder() { this.reordering = true; diff --git a/app/javascript/vue/protocol/step.vue b/app/javascript/vue/protocol/step.vue index 38d6dbe41..6479a4f0d 100644 --- a/app/javascript/vue/protocol/step.vue +++ b/app/javascript/vue/protocol/step.vue @@ -369,7 +369,8 @@ data: JSON.stringify(elementPositions), contentType: "application/json", dataType: "json", - error: (() => HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger')) + error: (() => HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger')), + success: (() => this.$emit('stepUpdated')) }); this.reorderElements(this.elements); diff --git a/app/javascript/vue/protocol/step_elements/checklist.vue b/app/javascript/vue/protocol/step_elements/checklist.vue index 66581e770..cc8644c8d 100644 --- a/app/javascript/vue/protocol/step_elements/checklist.vue +++ b/app/javascript/vue/protocol/step_elements/checklist.vue @@ -231,7 +231,8 @@ data: JSON.stringify(checklistItemPositions), contentType: "application/json", dataType: "json", - error: (() => HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger')) + error: (() => HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger')), + success: (() => this.update()) }); }, handleMultilinePaste(data) { diff --git a/app/models/checklist_item.rb b/app/models/checklist_item.rb index 9617a60fa..db055e221 100644 --- a/app/models/checklist_item.rb +++ b/app/models/checklist_item.rb @@ -8,7 +8,8 @@ class ChecklistItem < ApplicationRecord validates :position, uniqueness: { scope: :checklist }, unless: -> { position.nil? } belongs_to :checklist, - inverse_of: :checklist_items + inverse_of: :checklist_items, + touch: true belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User', diff --git a/app/models/comment.rb b/app/models/comment.rb index 6aad56787..7f145e946 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -49,9 +49,10 @@ class Comment < ApplicationRecord end end - def self.mark_as_seen_by(user) + def self.mark_as_seen_by(user, commentable) # rubocop:disable Rails/SkipsModelValidations all.where('? = ANY (unseen_by)', user.id).update_all("unseen_by = array_remove(unseen_by, #{user.id.to_i}::bigint)") + commentable.touch # rubocop:enable Rails/SkipsModelValidations end end diff --git a/app/views/shared/comments/_comments.html.erb b/app/views/shared/comments/_comments.html.erb index 89d8ef276..8c7214c12 100644 --- a/app/views/shared/comments/_comments.html.erb +++ b/app/views/shared/comments/_comments.html.erb @@ -12,7 +12,7 @@ <% end %>
<%= render partial: 'shared/comments/list.html.erb', locals: { comments: comments} %> - <% comments.mark_as_seen_by(current_user) %> + <% comments.mark_as_seen_by(current_user, object) %>
<% if can_create_comments %> diff --git a/app/views/shared/comments/_comments_list.html.erb b/app/views/shared/comments/_comments_list.html.erb index 66cf4ee14..5fcefb5a5 100644 --- a/app/views/shared/comments/_comments_list.html.erb +++ b/app/views/shared/comments/_comments_list.html.erb @@ -4,4 +4,4 @@ locals: { comment: comment, skip_header: user == comment.user } %> <% user = comment.user %> <% end %> -<% comments.mark_as_seen_by(current_user) && @commentable.touch %> +<% comments.mark_as_seen_by(current_user, @commentable) %>