Skip to content

CMS Mailer #2432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/dashboards/sent_email_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ class SentEmailDashboard < BaseDashboard
COLLECTION_ATTRIBUTES = %i[
created_at
user
mailer_type
subject
].freeze

# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = %i[
user
mailer_type
subject
created_at
].freeze
Expand Down
11 changes: 9 additions & 2 deletions app/jobs/send_cms_emails_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ def process_template(template)
data = template.template
users = Programmes::ProgressQuery.new(data.programme, data.activity_state, data.enrolled, data.completed_programme_activity_groups).call

users.each do |user|
CmsMailer.with(template_slug: data.slug, user_id: user.id).send_template.deliver_later
user_ids = users.pluck(:id)

users_to_email = user_ids - User.joins(:sent_emails)
.where(sent_emails: {mailer_type: data.slug})
.where(id: user_ids)
.pluck(:id)

users_to_email.each do |user|
CmsMailer.with(template_slug: data.slug, user_id: user).send_template.deliver_later
end
end
end
3 changes: 1 addition & 2 deletions app/mailers/cms_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ def send_template

begin
@template = Cms::Collections::EmailTemplate.get(@template_slug).template

@subject = @template.subject(@user)

mail(to: @user.email, subject: @subject)
mail(to: @user, subject: @subject, record_sent_mail: true, mailer_type: @template_slug)
rescue ActiveRecord::RecordNotFound
Sentry.capture_message("Failed to load the email template #{@template_slug}")
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/cms/models/collections/email_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(slug:, subject:, email_content:, programme_slug:, completed_progr
@programme = Programme.find_by(slug: @programme_slug)
@completed_programme_activity_group_slugs = completed_programme_activity_group_slugs
@completed_programme_activity_groups = if completed_programme_activity_group_slugs
completed_programme_activity_group_slugs.each { ProgrammeActivityGrouping.find_by(cms_slug: _1) }
completed_programme_activity_group_slugs.map { ProgrammeActivityGrouping.find_by(cms_slug: _1) }
else
[]
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/cms_mailer/send_template.text.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<% @template.email_content.each do |comp| %>
<%= render comp.render_text(@template, @user) if comp.render?(@template, @user) %>
<%= render comp.render_text(@template, @user) if comp.render?(@template, @user) %>
<% end %>
6 changes: 6 additions & 0 deletions lib/tasks/send_cms_emails.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace :cms_emails do
desc "Iterates through Strapi email templates and sends to relevant users"
task send: :environment do
SendCmsEmailsJob.perform_now
end
end
2 changes: 1 addition & 1 deletion previews/mailers/cms_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class CmsMailerPreview < ActionMailer::Preview
def send_template
template_slug = params[:template_slug].presence || "primary-cert-1a.2"
template_slug = params[:template_slug].presence || "primary-cert-1a.1"
user_id = params[:user_id].presence || User.first.id
CmsMailer.with(user_id:, template_slug:, preview: true).send_template
end
Expand Down
29 changes: 29 additions & 0 deletions spec/mailers/cms_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,33 @@
.with("Failed to load the email template #{missing_slug}")
end
end

describe "with user who already received the email" do
let(:sent_email) { create(:sent_email, user:, mailer_type: slug, subject:) }

before do
sent_email
end

it "does not send the email" do
@mail = described_class.with(user_id: user.id, template_slug: slug).send_template

expect {
@mail.deliver_now
}.not_to change { ActionMailer::Base.deliveries.count }
end
end

describe "sent email tracking" do
before do
described_class.with(user_id: user.id, template_slug: slug).send_template.deliver_now
end

it "records the correct details in sent email" do
sent_email = SentEmail.last
expect(sent_email.user).to eq(user)
expect(sent_email.mailer_type).to eq(slug)
expect(sent_email.subject).to eq(subject)
end
end
end