Skip to content

Commit

Permalink
simplify behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Holist committed Oct 8, 2024
1 parent 4150f53 commit 0cad665
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 79 deletions.
4 changes: 2 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def sync_user_with_rdv_solidarites(user)
return if sync.success?

respond_to do |format|
format.turbo_stream { flash.now[:error] = "L'usager n'est plus lié à rdv-solidarités: #{sync.errors}" }
format.json { render json: { errors: sync.errors }, status: :unprocessable_entity }
format.turbo_stream { flash.now[:error] = "L'usager n'est plus lié à rdv-solidarités: #{sync.errors.join(', ')}" }
format.json { render json: { errors: sync.errors.join(", ") }, status: :unprocessable_entity }
end
end
end
2 changes: 1 addition & 1 deletion app/jobs/application_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def call_service!(service_class, **kwargs)
raise(
ApplicationJob::FailedServiceError,
"Calling service #{service_class} failed in #{self.class}:\n" \
"Errors: #{service_result.errors}"
"Errors: #{service_result.errors.join(', ')}"
)
end
end
8 changes: 2 additions & 6 deletions app/services/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,8 @@ def call

private

def add_error(message)
result.errors << ErrorPresenter.new(message)
end

def add_custom_error(message, template_name:, locals: {})
result.errors << TemplatedErrorPresenter.new(message, template_name: template_name, locals: locals)
def add_custom_error(message:, template_name:, locals: {})
result.errors << TemplatedErrorPresenter.new(message: message, template_name: template_name, locals: locals)
end

def call_service!(service_class, **)
Expand Down
11 changes: 0 additions & 11 deletions app/services/error_presenter.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/services/invitations/save_and_send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def send_invitation
def verify_creneaux_are_available
return if retrieve_creneau_availability.creneau_availability

add_custom_error("Il n'y a plus de créneaux disponibles pour inviter cet usager",
add_custom_error(message: "Il n'y a plus de créneaux disponibles pour inviter cet usager",
template_name: "no_creneau_available")
fail!
end
Expand Down
26 changes: 12 additions & 14 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("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,54 +45,52 @@ def invitation_already_sent_today?
def validate_user_title_presence
return if user.title?

add_error("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("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("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("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
return if user.unarchived_organisations.flat_map(&:motif_categories).include?(motif_category)

add_error(
"L'usager n'appartient pas ou n'est pas actif" \
" dans une organisation qui gère la catégorie #{motif_category_name}"
)
result.errors << "L'usager n'appartient pas ou n'est pas actif dans une organisation qui gère la catégorie " \
"#{motif_category_name}"
end

def validate_motif_of_this_category_is_defined_in_organisations
return if organisations_motifs.map(&:motif_category).include?(motif_category)

add_error("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("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_custom_error("Aucun motif de suivi n'a été défini pour la catégorie #{motif_category_name}",
add_custom_error(message: "Aucun motif de suivi n'a été défini pour la catégorie #{motif_category_name}",
template_name: "no_follow_up_category",
locals: {
organisation_id: organisations.first.id,
Expand All @@ -112,9 +110,9 @@ 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("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("Le téléphone de contact de l'organisation #{organisation_names} doit être indiqué.")
result.errors << "Le téléphone de contact de l'organisation #{organisation_names} doit être indiqué."
end
end
end
Expand Down
12 changes: 8 additions & 4 deletions app/services/templated_error_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
class TemplatedErrorPresenter < ErrorPresenter
attr_reader :template_name, :locals
class TemplatedErrorPresenter
attr_reader :message, :template_name, :locals

def initialize(message, template_name:, locals: {})
super(message)
def initialize(message:, template_name:, locals: {})
@message = message
@template_name = template_name
@locals = locals
end

def to_s
message
end

def to_partial_path
"common/custom_errors/#{@template_name}"
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/common/_custom_errors_modal.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<% if error.is_a?(TemplatedErrorPresenter) %>
<%= render error, **error.locals %>
<% else %>
<p><%= error.to_s %></p>
<p><%= error %></p>
<% end %>
<% if index < errors.size - 1 %>
<hr class="mx-auto w-50">
Expand Down
5 changes: 4 additions & 1 deletion spec/services/invitations/save_and_send_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@

it "stores an error message" do
expect(subject.errors).to include(
an_object_having_attributes(template_name: "no_creneau_available")
an_object_having_attributes(
message: "Il n'y a plus de créneaux disponibles pour inviter cet usager",
template_name: "no_creneau_available"
)
)
expect(subject.errors.first).to be_a(TemplatedErrorPresenter)
end
Expand Down
55 changes: 17 additions & 38 deletions spec/services/invitations/validate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@

it "stores an error message" do
expect(subject.errors).to include(
an_object_having_attributes(
message: "Le téléphone de contact de l'organisation #{organisation.name} doit être indiqué."
)
"Le téléphone de contact de l'organisation #{organisation.name} doit être indiqué."
)
end
end
Expand All @@ -71,9 +69,7 @@

it "stores an error message" do
expect(subject.errors).to include(
an_object_having_attributes(
message: "Les organisations ne peuvent pas être liés à des départements différents de l'invitation"
)
"Les organisations ne peuvent pas être liés à des départements différents de l'invitation"
)
end
end
Expand All @@ -89,9 +85,7 @@

it "stores an error message" do
expect(subject.errors).to include(
an_object_having_attributes(
message: "Cet usager a déjà un rendez-vous à venir pour ce motif"
)
"Cet usager a déjà un rendez-vous à venir pour ce motif"
)
end
end
Expand All @@ -103,9 +97,7 @@

it "stores an error message" do
expect(subject.errors).to include(
an_object_having_attributes(
message: "La civilité de la personne doit être précisée pour pouvoir envoyer une invitation"
)
"La civilité de la personne doit être précisée pour pouvoir envoyer une invitation"
)
end
end
Expand All @@ -117,9 +109,7 @@

it "stores an error message" do
expect(subject.errors).to include(
an_object_having_attributes(
message: "La durée de validité de l'invitation pour un courrier doit être supérieure à 5 jours"
)
"La durée de validité de l'invitation pour un courrier doit être supérieure à 5 jours"
)
end
end
Expand All @@ -133,10 +123,7 @@

it "stores an error message" do
expect(subject.errors).to include(
an_object_having_attributes(
message: "L'usager n'appartient pas ou n'est pas actif dans" \
" une organisation qui gère la catégorie RSA orientation"
)
"L'usager n'appartient pas ou n'est pas actif dans une organisation qui gère la catégorie RSA orientation"
)
end

Expand All @@ -147,10 +134,7 @@

it "stores an error message" do
expect(subject.errors).to include(
an_object_having_attributes(
message: "L'usager n'appartient pas ou n'est pas actif dans" \
" une organisation qui gère la catégorie RSA orientation"
)
"L'usager n'appartient pas ou n'est pas actif dans une organisation qui gère la catégorie RSA orientation"
)
end
end
Expand All @@ -163,9 +147,7 @@

it "stores an error message" do
expect(subject.errors).to include(
an_object_having_attributes(
message: "Aucun motif de la catégorie RSA orientation n'est défini sur RDV-Solidarités"
)
"Aucun motif de la catégorie RSA orientation n'est défini sur RDV-Solidarités"
)
end
end
Expand All @@ -178,11 +160,7 @@
end

it "stores the error" do
expect(subject.errors).to include(
an_object_having_attributes(
message: "Une invitation sms a déjà été envoyée aujourd'hui à cet usager"
)
)
expect(subject.errors).to include("Une invitation sms a déjà été envoyée aujourd'hui à cet usager")
end

context "when the format is postal" do
Expand Down Expand Up @@ -213,9 +191,7 @@

it "stores an error message" do
expect(subject.errors).to include(
an_object_having_attributes(
message: "Un référent doit être assigné au bénéficiaire pour les rdvs avec référents"
)
"Un référent doit être assigné au bénéficiaire pour les rdvs avec référents"
)
end
end
Expand All @@ -225,11 +201,14 @@

it("is a failure") { is_a_failure }

it "stores an error message" do
it "stores a templated error message" do
expect(subject.errors).to include(
an_object_having_attributes(template_name: "no_follow_up_category",
locals: { motif_category_name: "RSA orientation",
organisation_id: organisation.id })
an_object_having_attributes(
message: "Aucun motif de suivi n'a été défini pour la catégorie #{category_orientation.name}",
template_name: "no_follow_up_category",
locals: { motif_category_name: "RSA orientation",
organisation_id: organisation.id }
)
)
end
end
Expand Down

0 comments on commit 0cad665

Please sign in to comment.