From 76e74003ad54af4c421a34ef1bdb9bbe030b58fe Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Thu, 8 Apr 2021 17:13:39 +0200 Subject: [PATCH] manager: fix SendInBlue emails --- app/controllers/manager/users_controller.rb | 9 +----- app/helpers/email_helper.rb | 7 ++--- app/lib/sendinblue/api.rb | 34 ++++++++++++++++++++ app/lib/sent_mail.rb | 3 ++ app/views/manager/users/emails.html.erb | 35 +++++++++++---------- 5 files changed, 60 insertions(+), 28 deletions(-) create mode 100644 app/lib/sent_mail.rb diff --git a/app/controllers/manager/users_controller.rb b/app/controllers/manager/users_controller.rb index 0fcdc27e8..604ba99d7 100644 --- a/app/controllers/manager/users_controller.rb +++ b/app/controllers/manager/users_controller.rb @@ -49,14 +49,7 @@ module Manager def emails @user = User.find(params[:id]) - - transactionnal_api = ::SibApiV3Sdk::TransactionalEmailsApi.new - - @transactionnal_emails = transactionnal_api.get_transac_emails_list(email: @user.email) - @events = transactionnal_api.get_email_event_report(email: @user.email, days: 30) - - rescue ::SibApiV3Sdk::ApiError => e - flash.alert = "Impossible de récupérer les emails de cet utilisateur chez Sendinblue : #{e.message}" + @sent_mails = Sendinblue::API.new.sent_mails(@user.email) end def unblock_user diff --git a/app/helpers/email_helper.rb b/app/helpers/email_helper.rb index 9bd74dc65..60cfb1fde 100644 --- a/app/helpers/email_helper.rb +++ b/app/helpers/email_helper.rb @@ -1,9 +1,8 @@ module EmailHelper - def event_color_code(email_events) - unique_events = email_events.map(&:event) - if unique_events.include?('delivered') + def status_color_code(status) + if status.include?('delivered') return 'email-sent' - elsif unique_events.include?('blocked') || unique_events.include?('hardBounces') + elsif status.include?('blocked') || status.include?('hardBounces') return 'email-blocked' else return '' diff --git a/app/lib/sendinblue/api.rb b/app/lib/sendinblue/api.rb index 7cfe78ae9..2e287999e 100644 --- a/app/lib/sendinblue/api.rb +++ b/app/lib/sendinblue/api.rb @@ -16,6 +16,7 @@ class Sendinblue::API end def update_contact(email, attributes = {}) + # TODO: refactor this to use the official SiB SDK (by using contact create + attributes) req = post_api_request('contacts', email: email, attributes: attributes, updateEnabled: true) req.on_complete do |response| if !response.success? @@ -25,6 +26,35 @@ class Sendinblue::API hydra.queue(req) end + # Get messages sent to a user through SendInBlue. + # + # Returns an array of SentMail objects. + def sent_mails(email_address) + client = ::SibApiV3Sdk::TransactionalEmailsApi.new + @events = client.get_email_event_report(email: email_address, days: 30).events + + if @events.blank? + Rails.logger.info "SendInBlue::API: no messages found for email address '#{email_address}'" + return [] + end + + @events.group_by(&:message_id).values.map do |message_events| + latest_event = message_events.first + SentMail.new( + from: latest_event.from, + to: latest_event.email, + subject: latest_event.subject, + delivered_at: parse_date(latest_event.date), + status: latest_event.event, + service_name: 'SendInBlue', + external_url: 'https://app-smtp.sendinblue.com/log' + ) + end + rescue ::SibApiV3Sdk::ApiError => e + Rails.logger.error e.message + [] + end + def run hydra.run @hydra = nil @@ -70,4 +100,8 @@ class Sendinblue::API def client_key Rails.application.secrets.sendinblue[:api_v3_key] end + + def parse_date(date) + date.is_a?(String) ? Time.zone.parse(date) : date + end end diff --git a/app/lib/sent_mail.rb b/app/lib/sent_mail.rb new file mode 100644 index 000000000..828dd62e1 --- /dev/null +++ b/app/lib/sent_mail.rb @@ -0,0 +1,3 @@ +# Represent an email sent using an external API +class SentMail < Struct.new(:from, :to, :subject, :delivered_at, :status, :service_name, :external_url, keyword_init: true) +end diff --git a/app/views/manager/users/emails.html.erb b/app/views/manager/users/emails.html.erb index 6af6416d3..b0b4ccfd8 100644 --- a/app/views/manager/users/emails.html.erb +++ b/app/views/manager/users/emails.html.erb @@ -18,9 +18,9 @@

Historique des email

-<% if @transactionnal_emails.present? %> +<% if @sent_mails.present? %>

- Cet historique contient les 30 derniers jours. Pour un recherche plus fine, il faut fouiller les logs. + Cet historique contient les 30 derniers jours. Pour un recherche plus fine, il faut fouiller les logs de SendInblue ou de Mailjet.

@@ -29,41 +29,44 @@ Émetteur + + - <% @transactionnal_emails&.transactional_emails&.reverse&.each do |email| %> - <% matching_events = @events&.events&.select { |e| e.message_id == email.message_id } %> - + <% @sent_mails.each do |email| %> + + <% end %>
- Sujet + Objet Date + Statut + + Prestataire +
<%= email.from %> - <%= email.subject %> + <%= email.subject %> - <%= l(email.date.is_a?(String) ? Time.zone.parse(email.date) : email.date, format: '%d/%m/%y à %H:%M') %> + <%= l(email.delivered_at, format: :long) %> -
    - - <% matching_events.each do |event|%> -
  • <%= event.event %>
  • - <% end %> -
+ <%= email.status %> +
+ <%= link_to email.service_name, email.external_url, style: 'text-decoration: underline' %>
<% else %> -

Historique indisponible. Cet email n'existe pas chez Sendinblue, ou nous n'avons pas réussi à échanger. - Vous pouvez éventuellement fouiller leurs logs.

+

Historique indisponible. Cette adresse email n'existe pas chez nos prestataires d'envoi, ou nous n'avons pas réussi à en charger des données. + Vous pouvez éventuellement fouiller les logs de SendInBlue ou de Mailjet.

<% end %>

Problèmes potentiel