diff --git a/app/dashboards/sent_email_dashboard.rb b/app/dashboards/sent_email_dashboard.rb index fadd135cb0..9d16f51da4 100644 --- a/app/dashboards/sent_email_dashboard.rb +++ b/app/dashboards/sent_email_dashboard.rb @@ -18,6 +18,7 @@ class SentEmailDashboard < BaseDashboard COLLECTION_ATTRIBUTES = %i[ created_at user + mailer_type subject ].freeze @@ -25,6 +26,7 @@ class SentEmailDashboard < BaseDashboard # 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 diff --git a/app/jobs/send_cms_emails_job.rb b/app/jobs/send_cms_emails_job.rb index fe409403dc..890010300e 100644 --- a/app/jobs/send_cms_emails_job.rb +++ b/app/jobs/send_cms_emails_job.rb @@ -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 diff --git a/app/mailers/cms_mailer.rb b/app/mailers/cms_mailer.rb index ac68acd86b..cf749f5df8 100644 --- a/app/mailers/cms_mailer.rb +++ b/app/mailers/cms_mailer.rb @@ -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 diff --git a/app/services/cms/models/collections/email_template.rb b/app/services/cms/models/collections/email_template.rb index 96d3de98b8..8d6bf48887 100644 --- a/app/services/cms/models/collections/email_template.rb +++ b/app/services/cms/models/collections/email_template.rb @@ -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 diff --git a/app/views/cms_mailer/send_template.text.erb b/app/views/cms_mailer/send_template.text.erb index 88dcd5e7f0..872dae583b 100644 --- a/app/views/cms_mailer/send_template.text.erb +++ b/app/views/cms_mailer/send_template.text.erb @@ -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 %> diff --git a/lib/tasks/send_cms_emails.rake b/lib/tasks/send_cms_emails.rake new file mode 100644 index 0000000000..d86f4d392a --- /dev/null +++ b/lib/tasks/send_cms_emails.rake @@ -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 diff --git a/previews/mailers/cms_mailer_preview.rb b/previews/mailers/cms_mailer_preview.rb index 0698bc1c2b..99007da50d 100644 --- a/previews/mailers/cms_mailer_preview.rb +++ b/previews/mailers/cms_mailer_preview.rb @@ -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 diff --git a/spec/mailers/cms_mailer_spec.rb b/spec/mailers/cms_mailer_spec.rb index 7c094c357c..8f5ba2cd8c 100644 --- a/spec/mailers/cms_mailer_spec.rb +++ b/spec/mailers/cms_mailer_spec.rb @@ -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