Skip to content

Commit

Permalink
applys Amines review 👍
Browse files Browse the repository at this point in the history
  • Loading branch information
Holist committed Oct 2, 2024
1 parent 089355e commit 47e7634
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 48 deletions.
7 changes: 5 additions & 2 deletions app/controllers/invitations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ def create # rubocop:disable Metrics/AbcSize
format.json do
render json: {
success: false,
payload: turbo_stream.replace("remote_modal", partial: "invitation_error_modal",
locals: { errors: invite_user.errors })
turbo_stream_html: turbo_stream.replace("remote_modal", partial: "common/custom_errors_modal",
locals: {
errors: invite_user.errors,
custom_errors: invite_user.custom_errors
})
}, status: :unprocessable_entity
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/react/lib/handleUserInvitation.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const handleUserInvitation = async (
motifCategoryId
);
if (!result.success && options.raiseError) {
window.Turbo.renderStreamMessage(result.payload);
window.Turbo.renderStreamMessage(result.turbo_stream_html);
}
return result;
};
Expand Down
13 changes: 9 additions & 4 deletions app/services/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class BaseService
class << self
def call(**)
service = new(**)
service.instance_variable_set(:@result, OpenStruct.new(errors: []))
service.instance_variable_set(:@result, OpenStruct.new(errors: [], custom_errors: []))
output = service.call
format_result(output, service.result)
rescue FailedServiceError => e
Expand All @@ -26,14 +26,15 @@ def format_result(output, result)
def add_status_to(result)
raise UnexpectedResultBehaviourError unless result.is_a? OpenStruct

result[:success?] = result.errors.blank?
result[:failure?] = result.errors.present?
result[:success?] = result.errors.blank? && result.custom_errors.blank?
result[:failure?] = !result[:success?]
result
end

def format_error(result, exception)
errors = result.errors
raise UnexpectedResultBehaviourError unless errors.is_a? Array
custom_errors = result.custom_errors
raise UnexpectedResultBehaviourError unless errors.is_a?(Array) && custom_errors.is_a?(Array)

# we add the exception message only if it is a custom message
errors << exception.message if exception.message != exception.class.to_s
Expand Down Expand Up @@ -70,4 +71,8 @@ def save_record!(record)
def fail!(error_message = nil)
raise FailedServiceError, error_message
end

def add_custom_error(type, locals: {})
result.custom_errors << CustomErrorPresenter.new(type: type, locals: locals)
end
end
12 changes: 12 additions & 0 deletions app/services/custom_error_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CustomErrorPresenter
attr_reader :locals

def initialize(type:, locals: {})
@type = type
@locals = locals
end

def to_partial_path
"common/custom_errors/#{@type}"
end
end
4 changes: 1 addition & 3 deletions app/services/invitations/save_and_send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def send_invitation
def verify_creneaux_are_available
return if retrieve_creneau_availability.creneau_availability

result.errors << {
error_type: "no_creneau_available"
}
add_custom_error("no_creneau_available")
fail!
end

Expand Down
43 changes: 13 additions & 30 deletions app/services/invitations/validate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def validate_invitation_not_already_sent_today
return if invitation.format_postal?
return unless invitation_already_sent_today?

add_error(message: "Une invitation #{invitation.format} a déjà été envoyée aujourd'hui à cet usager")
result.errors << "Une invitation #{invitation.format} a déjà été envoyée aujourd'hui à cet usager"
end

def invitation_already_sent_today?
Expand All @@ -45,25 +45,25 @@ def invitation_already_sent_today?
def validate_user_title_presence
return if user.title?

add_error(message: "La civilité de la personne doit être précisée pour pouvoir envoyer une invitation")
result.errors << "La civilité de la personne doit être précisée pour pouvoir envoyer une invitation"
end

def validate_organisations_are_not_from_different_departments
return if organisations.map(&:department_id).uniq == [department_id]

add_error(message: "Les organisations ne peuvent pas être liés à des départements différents de l'invitation")
result.errors << "Les organisations ne peuvent pas être liés à des départements différents de l'invitation"
end

def validate_no_rdv_pending_taken_today
return if follow_up.participations.none?(&:pending?)

add_error(message: "Cet usager a déjà un rendez-vous à venir pour ce motif")
result.errors << "Cet usager a déjà un rendez-vous à venir pour ce motif"
end

def validate_it_expires_in_more_than_5_days
return if expires_at > 5.days.from_now

add_error(message: "La durée de validité de l'invitation pour un courrier doit être supérieure à 5 jours")
result.errors << "La durée de validité de l'invitation pour un courrier doit être supérieure à 5 jours"
end

def validate_user_belongs_to_an_org_linked_to_motif_category
Expand All @@ -78,27 +78,24 @@ def validate_user_belongs_to_an_org_linked_to_motif_category
def validate_motif_of_this_category_is_defined_in_organisations
return if organisations_motifs.map(&:motif_category).include?(motif_category)

add_error(message: "Aucun motif de la catégorie #{motif_category_name} n'est défini sur RDV-Solidarités")
result.errors << "Aucun motif de la catégorie #{motif_category_name} n'est défini sur RDV-Solidarités"
end

def validate_referents_are_assigned
return if user.referent_ids.any?

add_error(message: "Un référent doit être assigné au bénéficiaire pour les rdvs avec référents")
result.errors << "Un référent doit être assigné au bénéficiaire pour les rdvs avec référents"
end

def validate_follow_up_motifs_are_defined
return if organisations_motifs.any? do |motif|
motif.follow_up? && motif.motif_category == motif_category
end

add_error(
error_type: "no_follow_up_category",
attributes: {
organisation_id: organisations.first.id,
motif_category_name: motif_category_name
}
)
add_custom_error("no_follow_up_category", locals: {
organisation_id: organisations.first.id,
motif_category_name: motif_category_name
})
end

def organisations_motifs
Expand All @@ -113,24 +110,10 @@ def validate_help_phone_number_presence

organisation_names = organisations_without_phone_number.map(&:name).to_sentence(last_word_connector: " et ")
if organisations_without_phone_number.size > 1
add_error(message: "Les téléphones de contact des organisations (#{organisation_names}) doivent être indiqués.")
result.errors << "Les téléphones de contact des organisations (#{organisation_names}) doivent être indiqués."
elsif organisations_without_phone_number.size == 1
add_error(message: "Le téléphone de contact de l'organisation #{organisation_names} doit être indiqué.")
end
end

def add_error(error_type: "generic", message: nil, attributes: {})
# Attributes are used to build the error message in the view
# Error type is used to determine which custom message will be displayed in the view
if error_type == "generic" && message.blank?
raise ArgumentError, "Le message est requis pour les erreurs génériques"
result.errors << "Le téléphone de contact de l'organisation #{organisation_names} doit être indiqué."
end

result.errors << {
error_type: error_type,
message: message,
attributes: attributes
}
end
end
end
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<%= render "common/remote_modal" do %>
<div>
<div class="text-center mb-3">
<%= image_tag "illustrations/error-line.svg", alt: "Illustration d'un message d'avertissement", class: "mb-3" %>
<%= image_tag "illustrations/error-line.svg", alt: "Illustration d'un message d'erreur", class: "mb-3" %>
</div>
<h4 class="text-center"><b>Impossible d'inviter l'usager</b></h4>
<div class="p-3">
<% errors.each_with_index do |error, index| %>
<% if error[:error_type] != "generic" %>
<%= render "invitations/custom_errors/#{error[:error_type]}", error: error %>
<% else %>
<p><%= error[:message] %></p>
<p><%= error %></p>
<% if index < errors.size - 1 || custom_errors.any? %>
<hr class="mx-auto w-50">
<% end %>
<% if index < errors.size - 1 %>
<% end %>
<% custom_errors.each_with_index do |custom_error, index| %>
<%= render custom_error, **custom_error.locals %>
<% if index < custom_errors.size - 1 %>
<hr class="mx-auto w-50">
<% end %>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<p>
La catégorie <%= error[:attributes][:motif_category_name] %> a été configurée en « RDV de suivi (avec référent) » sur rdv-insertion.
La catégorie <%= motif_category_name %> a été configurée en « RDV de suivi (avec référent) » sur rdv-insertion.
</p>
<p>
Pour inviter des usagers à ces rendez-vous, créez un motif de suivi dans les <%= link_to("paramètres de votre organisation sur RDV-Solidarités",
"https://www.rdv-solidarites.fr/admin/organisations/#{error[:attributes][:organisation_id]}/motifs",
"https://www.rdv-solidarites.fr/admin/organisations/#{organisation_id}/motifs",
target: "_blank", class: "text-decoration-underline") %>.
</p>
<p>
Expand Down

0 comments on commit 47e7634

Please sign in to comment.