Show error message for protocols templates with same name [SCI-8247] (#5266)

This commit is contained in:
ajugo 2023-04-13 15:34:36 +02:00 committed by GitHub
parent 19c98653a5
commit e85f1f8bbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 53 additions and 34 deletions

View file

@ -1,4 +1,4 @@
/* global HelperModule */ /* global HelperModule I18n */
(function() { (function() {
'use strict'; 'use strict';
@ -47,6 +47,11 @@
HelperModule.flashAlertMsg(data.flash, 'success'); HelperModule.flashAlertMsg(data.flash, 'success');
}); });
$(document).on('ajax:error', 'form#new-user-assignment-form', function(_e, data) {
HelperModule.flashAlertMsg(data.responseJSON.flash
? data.responseJSON.flash : I18n.t('errors.general'), 'danger');
});
$(document).on('ajax:error', 'form.member-item', function(_e, data) { $(document).on('ajax:error', 'form.member-item', function(_e, data) {
HelperModule.flashAlertMsg(data.responseJSON.flash, 'danger'); HelperModule.flashAlertMsg(data.responseJSON.flash, 'danger');
}); });

View file

@ -489,12 +489,8 @@ var ProtocolsIndex = (function() {
$.post(url, { protocol_ids: ids }, (data) => { $.post(url, { protocol_ids: ids }, (data) => {
HelperModule.flashAlertMsg(data.message, 'success'); HelperModule.flashAlertMsg(data.message, 'success');
reloadTable(); reloadTable();
}).error((error) => { }).error((data) => {
if (error.status === 401) { HelperModule.flashAlertMsg(data.responseJSON.message, 'danger');
HelperModule.flashAlertMsg(I18n.t('protocols.index.restore_unauthorized'), 'danger');
} else {
HelperModule.flashAlertMsg(I18n.t('protocols.index.restore_error'), 'danger');
}
}); });
} }

View file

@ -98,11 +98,11 @@ module AccessPermissions
end end
format.json { render :edit } format.json { render :edit }
end end
rescue ActiveRecord::RecordInvalid rescue ActiveRecord::RecordInvalid => e
respond_to do |format| Rails.logger.error e.message
@message = t('access_permissions.create.failure') errors = @project.errors ? @project.errors&.map(&:message)&.join(',') : e.message
format.json { render :new } render json: { flash: errors }, status: :unprocessable_entity
end raise ActiveRecord::Rollback
end end
end end
@ -152,6 +152,10 @@ module AccessPermissions
log_activity(:project_access_changed_all_team_members, log_activity(:project_access_changed_all_team_members,
{ team: @project.team.id, role: @project.default_public_user_role&.name }) { team: @project.team.id, role: @project.default_public_user_role&.name })
end end
rescue ActiveRecord::RecordInvalid => e
Rails.logger.error e.message
render json: { flash: @project.errors&.map(&:message)&.join(',') }, status: :unprocessable_entity
raise ActiveRecord::Rollback
end end
end end

View file

@ -94,11 +94,11 @@ module AccessPermissions
format.json { render :edit } format.json { render :edit }
end end
rescue ActiveRecord::RecordInvalid rescue ActiveRecord::RecordInvalid => e
respond_to do |format| Rails.logger.error e.message
@message = t('access_permissions.create.failure') errors = @protocol.errors ? @protocol.errors&.map(&:message)&.join(',') : e.message
format.json { render :new } render json: { flash: errors }, status: :unprocessable_entity
end raise ActiveRecord::Rollback
end end
end end
@ -132,18 +132,24 @@ module AccessPermissions
end end
def update_default_public_user_role def update_default_public_user_role
current_role = @protocol.default_public_user_role.name ActiveRecord::Base.transaction do
@protocol.update!(permitted_default_public_user_role_params) current_role = @protocol.default_public_user_role.name
@protocol.update!(permitted_default_public_user_role_params)
# revoke all team members access # revoke all team members access
if permitted_default_public_user_role_params[:default_public_user_role_id].blank? if permitted_default_public_user_role_params[:default_public_user_role_id].blank?
log_activity(:protocol_template_access_revoked_all_team_members, log_activity(:protocol_template_access_revoked_all_team_members,
{ team: @protocol.team.id, role: current_role }) { team: @protocol.team.id, role: current_role })
render json: { flash: t('access_permissions.update.revoke_all_team_members') }, status: :ok render json: { flash: t('access_permissions.update.revoke_all_team_members') }, status: :ok
else else
# update all team members access # update all team members access
log_activity(:protocol_template_access_changed_all_team_members, log_activity(:protocol_template_access_changed_all_team_members,
{ team: @protocol.team.id, role: @protocol.default_public_user_role&.name }) { team: @protocol.team.id, role: @protocol.default_public_user_role&.name })
end
rescue ActiveRecord::RecordInvalid => e
Rails.logger.error e.message
render json: { flash: @protocol&.errors&.map(&:message)&.join(',') }, status: :unprocessable_entity
raise ActiveRecord::Rollback
end end
end end

View file

@ -142,7 +142,7 @@ class ProtocolsController < ApplicationController
protocol: @protocol.id, protocol: @protocol.id,
version_number: @protocol.version_number) version_number: @protocol.version_number)
rescue ActiveRecord::RecordInvalid => e rescue ActiveRecord::RecordInvalid => e
flash[:error] = e.message flash[:error] = @protocol.errors&.map(&:message)&.join(',')
Rails.logger.error e.message Rails.logger.error e.message
raise ActiveRecord::Rollback raise ActiveRecord::Rollback
rescue StandardError => e rescue StandardError => e

View file

@ -85,6 +85,10 @@ class StepsController < ApplicationController
) )
@step = @protocol.insert_step(@step, params[:position]) @step = @protocol.insert_step(@step, params[:position])
if @protocol.in_repository? && @protocol.errors
return render json: { error: @protocol.errors }, status: :unprocessable_entity
end
# Generate activity # Generate activity
if @protocol.in_module? if @protocol.in_module?
log_activity(:create_step, @my_module.experiment.project, { my_module: @my_module.id }.merge(step_message_items)) log_activity(:create_step, @my_module.experiment.project, { my_module: @my_module.id }.merge(step_message_items))

View file

@ -274,6 +274,8 @@
this.$nextTick(() => this.scrollToBottom()); this.$nextTick(() => this.scrollToBottom());
} }
this.refreshProtocolStatus(); this.refreshProtocolStatus();
}).error((data) => {
HelperModule.flashAlertMsg(data.responseJSON.error ? Object.values(data.responseJSON.error).join(', ') : I18n.t('errors.general'), 'danger');
}) })
}, },
updateStepsPosition(step, action = 'add') { updateStepsPosition(step, action = 'add') {

View file

@ -112,6 +112,9 @@
data: { protocol: { authors: authors } }, data: { protocol: { authors: authors } },
success: (result) => { success: (result) => {
this.$emit('update', result.data.attributes) this.$emit('update', result.data.attributes)
},
error: (data) => {
HelperModule.flashAlertMsg(data.responseJSON ? Object.values(data.responseJSON).join(', ') : I18n.t('errors.general'), 'danger');
} }
}); });
}, },

View file

@ -76,7 +76,7 @@ class Protocol < ApplicationRecord
validate :ensure_single_draft validate :ensure_single_draft
validate :versions_same_name_constraint validate :versions_same_name_constraint
end end
with_options if: -> { in_repository? && !parent } do |protocol| with_options if: -> { in_repository? && !parent && !archived_changed?(from: false) } do |protocol|
# Active protocol must have unique name inside its team # Active protocol must have unique name inside its team
protocol protocol
.validates_uniqueness_of :name, case_sensitive: false, .validates_uniqueness_of :name, case_sensitive: false,
@ -230,6 +230,9 @@ class Protocol < ApplicationRecord
step.position = position step.position = position
step.protocol = self step.protocol = self
step.save! step.save!
rescue ActiveRecord::RecordInvalid => e
Rails.logger.error e.message
raise ActiveRecord::Rollback
end end
step step
end end

View file

@ -2807,10 +2807,6 @@ en:
make_private_error: "Error occurred while moving selected protocols to My protocols." make_private_error: "Error occurred while moving selected protocols to My protocols."
publish_unauthorized: "You do not have permission to move selected protocols to Team protocols." publish_unauthorized: "You do not have permission to move selected protocols to Team protocols."
publish_error: "Error occurred while moving selected protocols to Team protocols." publish_error: "Error occurred while moving selected protocols to Team protocols."
archive_unauthorized: "You do not have permission to archive selected protocols."
archive_error: "Error occurred while archiving selected protocols."
restore_unauthorized: "You do not have permission to restore selected protocols."
restore_error: "Error occurred while restoring selected protocols."
row_renamed_html: "%{old_name}<i> to </i>%{new_name}" row_renamed_html: "%{old_name}<i> to </i>%{new_name}"
no_protocol_name: "(no name)" no_protocol_name: "(no name)"
create: create: