manager: fix SendInBlue emails
This commit is contained in:
parent
eb8d5b711f
commit
76e74003ad
5 changed files with 60 additions and 28 deletions
|
@ -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
|
||||
|
|
|
@ -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 ''
|
||||
|
|
|
@ -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
|
||||
|
|
3
app/lib/sent_mail.rb
Normal file
3
app/lib/sent_mail.rb
Normal 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
|
|
@ -18,9 +18,9 @@
|
|||
|
||||
<section class="main-content__body">
|
||||
<h2>Historique des email</h2>
|
||||
<% if @transactionnal_emails.present? %>
|
||||
<% if @sent_mails.present? %>
|
||||
<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>
|
||||
<table>
|
||||
<thead>
|
||||
|
@ -29,17 +29,22 @@
|
|||
Émetteur
|
||||
</th>
|
||||
<th class="cell-label cell-label--string cell-label--false" scope="col" role="columnheader" aria-sort="none">
|
||||
Sujet
|
||||
Objet
|
||||
</th>
|
||||
<th class="cell-label cell-label--string cell-label--false" scope="col" role="columnheader" aria-sort="none">
|
||||
Date
|
||||
</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>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @transactionnal_emails&.transactional_emails&.reverse&.each do |email| %>
|
||||
<% matching_events = @events&.events&.select { |e| e.message_id == email.message_id } %>
|
||||
<tr class="<%= event_color_code(matching_events) %>">
|
||||
<% @sent_mails.each do |email| %>
|
||||
<tr class="<%= status_color_code(email.status) %>">
|
||||
<td class="cell-data cell-data--string" style="">
|
||||
<%= email.from %>
|
||||
</td>
|
||||
|
@ -47,23 +52,21 @@
|
|||
<%= email.subject %>
|
||||
</td>
|
||||
<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 class="cell-data cell-data--string" style="text-align: center;">
|
||||
<ul>
|
||||
|
||||
<% matching_events.each do |event|%>
|
||||
<li><%= event.event %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<%= email.status %>
|
||||
</td>
|
||||
<td class="cell-data cell-data--string" style="">
|
||||
<%= link_to email.service_name, email.external_url, style: 'text-decoration: underline' %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
<% else %>
|
||||
<p>Historique indisponible. Cet email n'existe pas chez Sendinblue, ou nous n'avons pas réussi à échanger.
|
||||
Vous pouvez éventuellement <a href="https://app-smtp.sendinblue.com/log">fouiller leurs logs</a>.</p>
|
||||
<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 fouiller les <a href="https://app-smtp.sendinblue.com/log">logs de SendInBlue</a> ou de Mailjet.</p>
|
||||
<% end %>
|
||||
|
||||
<h2>Problèmes potentiel</h2>
|
||||
|
|
Loading…
Reference in a new issue