Skip to content

Commit d95ad4e

Browse files
Merge pull request #2279 from NCCE/2897-primary-emails---sending-job
Strapi Email Sender Job
2 parents ebe4958 + 74f16c3 commit d95ad4e

File tree

20 files changed

+261
-20
lines changed

20 files changed

+261
-20
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module Admin
2+
class SentEmailsController < Admin::ApplicationController
3+
# Overwrite any of the RESTful controller actions to implement custom behavior
4+
# For example, you may want to send an email after a foo is updated.
5+
#
6+
# def update
7+
# super
8+
# send_foo_updated_email(requested_resource)
9+
# end
10+
11+
# Override this method to specify custom lookup behavior.
12+
# This will be used to set the resource for the `show`, `edit`, and `update`
13+
# actions.
14+
#
15+
# def find_resource(param)
16+
# Foo.find_by!(slug: param)
17+
# end
18+
19+
# The result of this lookup will be available as `requested_resource`
20+
21+
# Override this if you have certain roles that require a subset
22+
# this will be used to set the records shown on the `index` action.
23+
#
24+
# def scoped_resource
25+
# if current_user.super_admin?
26+
# resource_class
27+
# else
28+
# resource_class.with_less_stuff
29+
# end
30+
# end
31+
32+
# Override `resource_params` if you want to transform the submitted
33+
# data before it's persisted. For example, the following would turn all
34+
# empty values into nil values. It uses other APIs such as `resource_class`
35+
# and `dashboard`:
36+
#
37+
# def resource_params
38+
# params.require(resource_class.model_name.param_key).
39+
# permit(dashboard.permitted_attributes).
40+
# transform_values { |value| value == "" ? nil : value }
41+
# end
42+
43+
# See https://administrate-prototype.herokuapp.com/customizing_controller_actions
44+
# for more information
45+
end
46+
end
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class SentEmailDashboard < BaseDashboard
2+
# ATTRIBUTE_TYPES
3+
# a hash that describes the type of each of the model's fields.
4+
#
5+
# Each different type represents an Administrate::Field object,
6+
# which determines how the attribute is displayed
7+
# on pages throughout the dashboard.
8+
ATTRIBUTE_TYPES = {
9+
id: Field::String,
10+
user: Field::BelongsTo,
11+
subject: Field::String,
12+
mailer_type: Field::String,
13+
created_at: FORMATTED_DATE_TIME
14+
}.freeze
15+
16+
# COLLECTION_ATTRIBUTES
17+
# an array of attributes that will be displayed on the model's index page.
18+
COLLECTION_ATTRIBUTES = %i[
19+
created_at
20+
user
21+
subject
22+
].freeze
23+
24+
# SHOW_PAGE_ATTRIBUTES
25+
# an array of attributes that will be displayed on the model's show page.
26+
SHOW_PAGE_ATTRIBUTES = %i[
27+
user
28+
subject
29+
created_at
30+
].freeze
31+
32+
# FORM_ATTRIBUTES
33+
# an array of attributes that will be displayed
34+
# on the model's form (`new` and `edit`) pages.
35+
FORM_ATTRIBUTES = %i[].freeze
36+
37+
# COLLECTION_FILTERS
38+
# a hash that defines filters that can be used while searching via the search
39+
# field of the dashboard.
40+
#
41+
# For example to add an option to search for open resources by typing "open:"
42+
# in the search field:
43+
#
44+
# COLLECTION_FILTERS = {
45+
# open: ->(resources) { resources.where(open: true) }
46+
# }.freeze
47+
COLLECTION_FILTERS = {}.freeze
48+
49+
# Overwrite this method to customize how support_audits are displayed
50+
# across all pages of the admin dashboard.
51+
#
52+
# def display_resource()
53+
# end
54+
end

app/dashboards/user_dashboard.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class UserDashboard < BaseDashboard
2626
stem_achiever_organisation_no: Field::String,
2727
future_learn_organisation_memberships: Field::Text,
2828
forgotten: Field::Boolean,
29-
audits: Field::HasMany.with_options(sort_by: "created_at", direction: "desc")
29+
audits: Field::HasMany.with_options(sort_by: "created_at", direction: "desc"),
30+
sent_emails: Field::HasMany.with_options(sort_by: "created_at", direction: "desc")
3031
}.freeze
3132

3233
# COLLECTION_ATTRIBUTES
@@ -59,6 +60,7 @@ class UserDashboard < BaseDashboard
5960
achievements
6061
teacher_reference_number
6162
assessment_attempts
63+
sent_emails
6264
].freeze
6365

6466
# FORM_ATTRIBUTES

app/jobs/send_cms_emails_job.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class SendCmsEmailsJob < ApplicationJob
2+
PER_PAGE = 50
3+
def perform
4+
email_templates = Cms::Collections::EmailTemplate.all_records
5+
email_templates.each { process_template(_1) }
6+
end
7+
8+
def process_template(template)
9+
# Do Query
10+
data = template.template
11+
users = Programmes::ProgressQuery.new(data.programme, data.activity_state, data.enrolled, data.completed_programme_activity_groups).call
12+
13+
users.each do |user|
14+
CmsMailer.with(template_slug: data.slug, user_id: user.id).send_template.deliver_later
15+
end
16+
end
17+
end

app/mailers/cms_mailer.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
class CmsMailer < ApplicationMailer
22
def send_template
3-
template_slug = params[:template_slug]
3+
@template_slug = params[:template_slug]
44
@user = User.find(params[:user_id])
5-
@template = Cms::Collections::EmailTemplate.get(template_slug).template
65

7-
@subject = @template.subject(@user)
6+
begin
7+
@template = Cms::Collections::EmailTemplate.get(@template_slug).template
88

9-
mail(to: @user.email, subject: @subject)
9+
@subject = @template.subject(@user)
10+
11+
mail(to: @user.email, subject: @subject)
12+
rescue ActiveRecord::RecordNotFound
13+
Sentry.capture_message("Failed to load the email template #{@template_slug}")
14+
end
1015
end
1116
end

app/services/cms/collections/email_template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Collections
33
class EmailTemplate < Resource
44
def self.is_collection = true
55

6-
def self.collection_attribute_mapping
6+
def self.collection_attribute_mappings
77
[
88
{model: Models::Collections::EmailTemplate, key: nil, param_name: :template}
99
]

app/services/cms/models/collections/email_template.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ module Cms
22
module Models
33
module Collections
44
class EmailTemplate
5-
attr_accessor :slug, :email_content, :programme, :completed_programme_activity_groups, :activity_state
5+
attr_accessor :slug, :email_content, :programme, :completed_programme_activity_groups, :activity_state, :enrolled
66

7-
def initialize(slug:, subject:, email_content:, programme_slug:, completed_programme_activity_group_slugs:, activity_state:)
7+
def initialize(slug:, subject:, email_content:, programme_slug:, completed_programme_activity_group_slugs:, activity_state:, enrolled:)
88
@slug = slug
99
@subject = subject
1010
@email_content = email_content
@@ -17,6 +17,7 @@ def initialize(slug:, subject:, email_content:, programme_slug:, completed_progr
1717
[]
1818
end
1919
@activity_state = activity_state
20+
@enrolled = enrolled
2021
end
2122

2223
def subject(user)

app/services/cms/providers/strapi/factories/model_factory.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ def self.to_email_template(strapi_data, _all_data)
8484
programme_slug: strapi_data[:programme][:data][:attributes][:slug],
8585
email_content: strapi_data[:emailContent].map { ComponentFactory.process_component(_1) }.compact,
8686
completed_programme_activity_group_slugs: strapi_data.dig(:completedGroupings, :data).nil? ? nil : strapi_data[:completedGroupings][:data].collect { _1[:attributes][:slug] },
87-
activity_state: strapi_data[:activityState]
87+
activity_state: strapi_data[:activityState],
88+
enrolled: strapi_data[:enrolled]
8889
}
8990
end
9091

app/services/cms/providers/strapi/mocks/collections/email_template.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class EmailTemplate < StrapiMock
88
attribute(:slug) { Faker::Internet.slug }
99
attribute(:emailContent) { [] }
1010
attribute(:activityState) { "active" }
11+
attribute(:enrolled) { true }
1112
attribute(:programme) {
1213
{data: {attributes: {slug: "primary-certificate"}}}
1314
}

config/environments/development.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@
110110
config.view_component.preview_route = "/rails/components"
111111
config.view_component.generate.sidecar = true
112112

113+
config.action_mailer.default_url_options = {host: "teachcomputing.rpfdev.com"}
114+
routes.default_url_options = {host: "teachcomputing.rpfdev.com"}
115+
113116
config.lograge.enabled = true
114117
config.lograge.ignore_actions = [Healthcheck::CONTROLLER_ACTION]
115118
config.lograge.custom_options = lambda do |event|

0 commit comments

Comments
 (0)