From d5c08247238f3de354eaedc243cc036fa5c64875 Mon Sep 17 00:00:00 2001 From: nicolas-entourage <75681929+nicolas-entourage@users.noreply.github.com> Date: Wed, 4 Dec 2024 16:55:48 +0100 Subject: [PATCH 01/38] EN-7825 add variables to user_message_broadcasts, welcome chat_message --- app/helpers/users_helper.rb | 4 - app/models/chat_message.rb | 18 ++++ app/models/concerns/availabilable.rb | 9 ++ app/models/conversation_message_broadcast.rb | 7 +- .../onboarding/chat_messages_service.rb | 5 +- .../admin/moderation_areas/_form.html.erb | 8 +- .../user_message_broadcasts/_form.html.erb | 2 +- .../admin/users/_onboarding_fields.html.erb | 2 +- .../_interpolation_specifications.html.erb | 13 +++ spec/models/chat_message_spec.rb | 99 +++++++++++++++++++ 10 files changed, 147 insertions(+), 20 deletions(-) create mode 100644 app/views/common/_interpolation_specifications.html.erb diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 6f79f4f92..b02ef407d 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -1,8 +1,4 @@ module UsersHelper - def day_name(day_number) - I18n.t("date.day_names")[day_number.to_i % 7] - end - def users_for_select users users.map do |user| ["#{user.full_name} (#{user.email})", user.id] diff --git a/app/models/chat_message.rb b/app/models/chat_message.rb index cc3f9234d..468e8dbef 100644 --- a/app/models/chat_message.rb +++ b/app/models/chat_message.rb @@ -83,6 +83,24 @@ def path key end end + class << self + def interpolate message:, user:, author: nil + first_name = UserPresenter.format_first_name(user.first_name) + + message + .gsub(/\{\{\s*first_name\s*\}\}/, first_name.to_s) + .gsub(/\{\{\s*email\s*\}\}/, user.email.to_s) + .gsub(/\{\{\s*phone\s*\}\}/, user.phone.to_s) + .gsub(/\{\{\s*city\s*\}\}/, user.city.to_s) + .gsub(/\{\{\s*uuid\s*\}\}/, user.uuid.to_s) + .gsub(/\{\{\s*default_neighborhood\s*\}\}/, user.default_neighborhood&.name.to_s) + .gsub(/\{\{\s*interests\s*\}\}/, user.interest_i18n.join(', ')) + .gsub(/\{\{\s*involvements\s*\}\}/, user.involvement_i18n.join(', ')) + .gsub(/\{\{\s*availability\s*\}\}/, user.availability_formatted.to_s) + .gsub(/\{\{\s*interlocutor\s*\}\}/, author&.first_name.to_s) + end + end + def active? status.to_sym == :active end diff --git a/app/models/concerns/availabilable.rb b/app/models/concerns/availabilable.rb index 436d0636c..b6142cf16 100644 --- a/app/models/concerns/availabilable.rb +++ b/app/models/concerns/availabilable.rb @@ -40,4 +40,13 @@ def validate_availability_format end end + def availability_formatted + availability.map do |day_number, hours| + "#{Availabilable.day_name(day_number)} : #{hours.join(', ')}" + end.join("\n") + end + + def self.day_name day_number + I18n.t("date.day_names")[day_number.to_i % 7] + end end diff --git a/app/models/conversation_message_broadcast.rb b/app/models/conversation_message_broadcast.rb index 46b6e7c85..eca88bdc1 100644 --- a/app/models/conversation_message_broadcast.rb +++ b/app/models/conversation_message_broadcast.rb @@ -17,12 +17,7 @@ def find_with_cast id end def content_for_user user - content - .gsub("{{first_name}}", user.first_name.to_s) - .gsub("{{email}}", user.email.to_s) - .gsub("{{phone}}", user.phone.to_s) - .gsub("{{city}}", user.city.to_s) - .gsub("{{uuid}}", user.uuid.to_s) + ChatMessage.interpolate(message: content, user: user) end def recipients diff --git a/app/services/onboarding/chat_messages_service.rb b/app/services/onboarding/chat_messages_service.rb index 5dc830e28..139b4a76f 100644 --- a/app/services/onboarding/chat_messages_service.rb +++ b/app/services/onboarding/chat_messages_service.rb @@ -38,11 +38,8 @@ def self.deliver_welcome_message next if messages.empty? - first_name = UserPresenter.format_first_name user.first_name - messages.each do |message| - message = message.gsub(/\{\{\s*first_name\s*\}\}/, first_name) - message = message.gsub(/\{\{\s*interlocutor\s*\}\}/, author.first_name) if author.present? + message = ChatMessage.interpolate(message: message, user: user, author: author) builder = ChatServices::ChatMessageBuilder.new( user: author, diff --git a/app/views/admin/moderation_areas/_form.html.erb b/app/views/admin/moderation_areas/_form.html.erb index acefe9d79..6ed2ef7da 100644 --- a/app/views/admin/moderation_areas/_form.html.erb +++ b/app/views/admin/moderation_areas/_form.html.erb @@ -65,7 +65,7 @@ pour le profil <%= t 'community.entourage.goals_compact.offer_help' %> <% end %> <%= f.text_area :welcome_message_1_offer_help, rows: 10, class: "form-control" %> - Utilisez {{first_name}} pour insérer le prénom du destinataire. + <%= render partial: 'common/interpolation_specifications' %> @@ -76,7 +76,7 @@ si <%= t 'community.entourage.goals_compact.ask_for_help' %> <% end %> <%= f.text_area :welcome_message_1_ask_for_help, rows: 10, class: "form-control" %> - Utilisez {{first_name}} pour insérer le prénom du destinataire. + <%= render partial: 'common/interpolation_specifications' %> @@ -87,7 +87,7 @@ pour le profil <%= t 'community.entourage.goals_compact.organization' %> <% end %> <%= f.text_area :welcome_message_1_organization, rows: 10, class: "form-control" %> - Utilisez {{first_name}} pour insérer le prénom du destinataire. + <%= render partial: 'common/interpolation_specifications' %> @@ -98,7 +98,7 @@ pour le profil <%= t 'community.entourage.goals_compact.goal_not_known' %> <% end %> <%= f.text_area :welcome_message_1_goal_not_known, rows: 10, class: "form-control" %> - Utilisez {{first_name}} pour insérer le prénom du destinataire. + <%= render partial: 'common/interpolation_specifications' %> diff --git a/app/views/admin/user_message_broadcasts/_form.html.erb b/app/views/admin/user_message_broadcasts/_form.html.erb index 2b8c6cfc9..32884c863 100644 --- a/app/views/admin/user_message_broadcasts/_form.html.erb +++ b/app/views/admin/user_message_broadcasts/_form.html.erb @@ -137,8 +137,8 @@
<%= f.label :content %> -

Vous pouvez utiliser les variables : {{first_name}}, {{email}}, {{phone}}, {{city}}, {{uuid}}

<%= f.text_area :content, class: "form-control", rows: 5, required: true %> +

<%= render partial: 'common/interpolation_specifications' %>

<% if @user_message_broadcast.draft? %> diff --git a/app/views/admin/users/_onboarding_fields.html.erb b/app/views/admin/users/_onboarding_fields.html.erb index 6c7dbf682..ed93d21ea 100644 --- a/app/views/admin/users/_onboarding_fields.html.erb +++ b/app/views/admin/users/_onboarding_fields.html.erb @@ -5,7 +5,7 @@