Log newsletter emails sent

With this commit we are logging which emails have already received the
newsletter

This could be important if something goes wrong sending the newsletter,
to be able to identify which users have already received the newsletter
and be able to skip them

We’ve had to add a new action to the Activity model (email) and add
paranoia features to be able to deal gracefully with the default
`with_hidden` scope in Activities[1]

[1]
https://github.com/AyuntamientoMadrid/consul/blob/master/app/models/acti
vity.rb#L2
This commit is contained in:
rgarcia
2018-05-17 16:53:47 +02:00
committed by decabeza
parent 8960928a76
commit 9264763ec5
6 changed files with 34 additions and 2 deletions

View File

@@ -2,7 +2,7 @@ class Activity < ActiveRecord::Base
belongs_to :actionable, -> { with_hidden }, polymorphic: true
belongs_to :user, -> { with_hidden }
VALID_ACTIONS = %w(hide block restore valuate)
VALID_ACTIONS = %w(hide block restore valuate email)
validates :action, inclusion: {in: VALID_ACTIONS}

View File

@@ -8,6 +8,9 @@ class Newsletter < ActiveRecord::Base
validates_format_of :from, :with => /@/
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
def list_of_recipient_emails
UserSegments.user_segment_emails(segment_recipient) if valid_segment_recipient?
end
@@ -24,8 +27,9 @@ class Newsletter < ActiveRecord::Base
run_at = first_batch_run_at
list_of_recipient_emails_in_batches.each do |recipient_emails|
recipient_emails.each do |recipient_email|
Mailer.delay(run_at: run_at).newsletter(self, recipient_email)
if valid_email?(recipient_email)
Mailer.delay(run_at: run_at).newsletter(self, recipient_email)
log_delivery(recipient_email)
end
end
run_at += batch_interval
@@ -57,4 +61,9 @@ class Newsletter < ActiveRecord::Base
def valid_email?(email)
email.match(/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i)
end
def log_delivery(recipient_email)
user = User.where(email: recipient_email).first
Activity.log(user, :email, self)
end
end