manager: fix SendInBlue emails

This commit is contained in:
Pierre de La Morinerie 2021-04-08 17:13:39 +02:00
parent eb8d5b711f
commit 76e74003ad
5 changed files with 60 additions and 28 deletions

View file

@ -49,14 +49,7 @@ module Manager
def emails def emails
@user = User.find(params[:id]) @user = User.find(params[:id])
@sent_mails = Sendinblue::API.new.sent_mails(@user.email)
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}"
end end
def unblock_user def unblock_user

View file

@ -1,9 +1,8 @@
module EmailHelper module EmailHelper
def event_color_code(email_events) def status_color_code(status)
unique_events = email_events.map(&:event) if status.include?('delivered')
if unique_events.include?('delivered')
return 'email-sent' return 'email-sent'
elsif unique_events.include?('blocked') || unique_events.include?('hardBounces') elsif status.include?('blocked') || status.include?('hardBounces')
return 'email-blocked' return 'email-blocked'
else else
return '' return ''

View file

@ -16,6 +16,7 @@ class Sendinblue::API
end end
def update_contact(email, attributes = {}) 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 = post_api_request('contacts', email: email, attributes: attributes, updateEnabled: true)
req.on_complete do |response| req.on_complete do |response|
if !response.success? if !response.success?
@ -25,6 +26,35 @@ class Sendinblue::API
hydra.queue(req) hydra.queue(req)
end 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 def run
hydra.run hydra.run
@hydra = nil @hydra = nil
@ -70,4 +100,8 @@ class Sendinblue::API
def client_key def client_key
Rails.application.secrets.sendinblue[:api_v3_key] Rails.application.secrets.sendinblue[:api_v3_key]
end end
def parse_date(date)
date.is_a?(String) ? Time.zone.parse(date) : date
end
end end

3
app/lib/sent_mail.rb Normal file
View file

@ -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

View file

@ -18,9 +18,9 @@
<section class="main-content__body"> <section class="main-content__body">
<h2>Historique des email</h2> <h2>Historique des email</h2>
<% if @transactionnal_emails.present? %> <% if @sent_mails.present? %>
<p> <p>
Cet historique contient les 30 derniers jours. Pour un recherche plus fine, il faut <a href="https://app-smtp.sendinblue.com/log">fouiller les logs</a>. Cet historique contient les 30 derniers jours. Pour un recherche plus fine, il faut fouiller les <a href="https://app-smtp.sendinblue.com/log">logs de SendInblue</a> ou de Mailjet.
</p> </p>
<table> <table>
<thead> <thead>
@ -29,41 +29,44 @@
Émetteur Émetteur
</th> </th>
<th class="cell-label cell-label--string cell-label--false" scope="col" role="columnheader" aria-sort="none"> <th class="cell-label cell-label--string cell-label--false" scope="col" role="columnheader" aria-sort="none">
Sujet Objet
</th> </th>
<th class="cell-label cell-label--string cell-label--false" scope="col" role="columnheader" aria-sort="none"> <th class="cell-label cell-label--string cell-label--false" scope="col" role="columnheader" aria-sort="none">
Date Date
</th> </th>
<th class="cell-label cell-label--string cell-label--false" scope="col" role="columnheader" aria-sort="none">
Statut
</th>
<th class="cell-label cell-label--string cell-label--false" scope="col" role="columnheader" aria-sort="none">
Prestataire
</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<% @transactionnal_emails&.transactional_emails&.reverse&.each do |email| %> <% @sent_mails.each do |email| %>
<% matching_events = @events&.events&.select { |e| e.message_id == email.message_id } %> <tr class="<%= status_color_code(email.status) %>">
<tr class="<%= event_color_code(matching_events) %>">
<td class="cell-data cell-data--string" style=""> <td class="cell-data cell-data--string" style="">
<%= email.from %> <%= email.from %>
</td> </td>
<td class="cell-data cell-data--string" style=""> <td class="cell-data cell-data--string" style="">
<%= email.subject %> <%= email.subject %>
</td> </td>
<td class="cell-data cell-data--string" style="text-align: center;"> <td class="cell-data cell-data--string" style="text-align: center;">
<%= 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) %>
</td> </td>
<td class="cell-data cell-data--string" style="text-align: center;"> <td class="cell-data cell-data--string" style="text-align: center;">
<ul> <%= email.status %>
</td>
<% matching_events.each do |event|%> <td class="cell-data cell-data--string" style="">
<li><%= event.event %></li> <%= link_to email.service_name, email.external_url, style: 'text-decoration: underline' %>
<% end %>
</ul>
</td> </td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>
</table> </table>
<% else %> <% else %>
<p>Historique indisponible. Cet email n'existe pas chez Sendinblue, ou nous n'avons pas réussi à échanger. <p>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 <a href="https://app-smtp.sendinblue.com/log">fouiller leurs logs</a>.</p> Vous pouvez éventuellement fouiller les <a href="https://app-smtp.sendinblue.com/log">logs de SendInBlue</a> ou de Mailjet.</p>
<% end %> <% end %>
<h2>Problèmes potentiel</h2> <h2>Problèmes potentiel</h2>