Skip to content

Commit

Permalink
Fix: enhance agents errors messages (#725)
Browse files Browse the repository at this point in the history
* accept different orcid forms in agents creation/update form

* humanize empty values errors in creating and updating agents

* accept ror identifier in all forms and handle it in the UI

* humanize identifier already used when updating agent

* put agent form errors in local lang files

* extract generate agents errors function

* remove ror/orcid ui validators

* enhance invalid agents identifiers url error message
  • Loading branch information
Bilelkihal authored and syphax-bouazzouni committed Aug 8, 2024
1 parent 173cbd0 commit 3e9de4b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
71 changes: 69 additions & 2 deletions app/controllers/agents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def create
alert_id = agent_id_alert_container_id(params[:id], parent_id)
deletable = params[:deletable]&.eql?('true')
if new_agent.errors
render_turbo_stream alert_error(id: alert_id) { JSON.pretty_generate(response_errors(new_agent)) }
errors = generate_errors(response_errors(new_agent))
render_turbo_stream alert_error(id: alert_id) { errors.join(', ') }
else
success_message = t('agents.add_agent')
streams = [alert_success(id: alert_id) { success_message }]
Expand Down Expand Up @@ -95,7 +96,8 @@ def update
alert_id = agent_alert_container_id(agent, parent_id)
deletable = params[:deletable]&.eql?('true')
if response_error?(agent_update)
render_turbo_stream(alert_error(id: alert_id) { JSON.pretty_generate(response_errors(agent_update)) })
errors = generate_errors(response_errors(agent_update).values.first)
render_turbo_stream alert_error(id: alert_id) { errors.join(', ') }
else
success_message = t('agents.update_agent')
table_line_id = agent_table_line_id(agent_id(agent))
Expand Down Expand Up @@ -276,7 +278,13 @@ def agent_params
identifiers_schemaAgency = params[:agentType].eql?('person') ? 'ORCID' : 'ROR'
p[:identifiers]&.each_value do |identifier|
identifier[:schemaAgency] = identifiers_schemaAgency
if identifier[:schemaAgency].downcase.eql?('orcid')
identifier[:notation] = normalize_orcid(identifier[:notation])
else
identifier[:notation] = normalize_ror(identifier[:notation])
end
end

p[:identifiers] = (p[:identifiers] || {}).values
p[:affiliations] = (p[:affiliations] || {}).values
p[:affiliations].each do |affiliation|
Expand All @@ -291,4 +299,63 @@ def find_agent_display_all(id = params[:id])
obj.id.to_s.eql?("#{rest_url}/Agents/#{id}")
end.first
end

def normalize_orcid(orcid)
case orcid
when /\A\d{16}\z/
# Case 1: 16 digits, add dashes
orcid = orcid.scan(/.{1,4}/).join('-')

when /\A\d{4}-\d{4}-\d{4}-\d{4}\z/
orcid = orcid

when /\Ahttps:\/\/(www\.)?orcid\.org\/\d{4}-\d{4}-\d{4}-\d{4}\z/
# Case 3: ORCID URL (with or without "www."), extract the numbers with dashes
orcid = orcid.split('/').last

when /\Aorcid\.org\/\d{4}-\d{4}-\d{4}-\d{4}\z/
# Case 4: ORCID without scheme (http/https)
orcid = orcid.split('/').last

when /\Awww\.orcid\.org\/\d{4}-\d{4}-\d{4}-\d{4}\z/
# Case 5: ORCID with "www." without scheme
orcid = orcid.split('/').last
end

return orcid
end

def normalize_ror(ror)
case ror
when /\A0\w{6}\d{2}\z/
# Case 1: 9 characters, starting with '0', 6 alphanumeric, ending with 2 digits
ror = ror

when /\Ahttps:\/\/ror\.org\/(0\w{6}\d{2})\z/
# Case 2: Full URL with 'https://ror.org/', extract the ROR ID
ror = ror.split('/').last

when /\Aror\.org\/(0\w{6}\d{2})\z/
# Case 3: ROR without scheme (http/https), extract the ROR ID
ror = ror.split('/').last
end

return ror
end

def generate_errors(response_errors)
errors = []
response_errors.values.each_with_index do |v, i|
if v[:existence]
errors << "#{response_errors.keys[i].capitalize} #{t('agents.errors.required')}"
elsif v[:unique_identifiers]
errors << t('agents.errors.used_identifier')
elsif v[:no_url]
errors << t('agents.errors.invalid_url')
else
errors << JSON.pretty_generate(response_errors)
end
end
return errors
end
end
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,10 @@ en:
actions: Actions
see_other_properties: See other properties
agents:
errors:
required: is required and cannot be empty
used_identifier: This identifier is already used by another agent
invalid_url: The identifier must be a valid ORCID/ROR URL
not_found_agent: Agent with id %{id}
add_agent: New Agent added successfully
update_agent: Agent successfully updated
Expand Down
4 changes: 4 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,10 @@ fr:


agents:
errors:
required: est requis et ne peut pas être vide
used_identifier: Cet identifiant est déjà utilisé par un autre agent
invalid_url: L'identifiant doit être une URL ORCID/ROR valide
not_found_agent: Agent avec id %{id}
add_agent: Nouvel agent ajouté avec succès
update_agent: Agent mis à jour avec succès
Expand Down

0 comments on commit 3e9de4b

Please sign in to comment.