Skip to content

Commit

Permalink
ER-891 New module mailer update (#986)
Browse files Browse the repository at this point in the history
* Update scope to all users

* Test `NewModuleMailJob.enqueue` with empty `ModuleRelease` table

* - create task for future tests
- permit running without seeds
  • Loading branch information
peterdavidhamilton authored Dec 4, 2023
1 parent 506fe38 commit 90bb76b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 35 deletions.
11 changes: 5 additions & 6 deletions app/jobs/new_module_mail_job.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# @see HookController#release
#
# Notify users of the latest published module and record the release timestamp
class NewModuleMailJob < MailJob
# @param release_id [Integer]
def run(release_id)
Expand All @@ -8,9 +11,7 @@ def run(release_id)
recipient.send_new_module_notification(latest_module)
end

newest_release = Release.find(release_id)

record_module_release(latest_module, newest_release)
record_module_release latest_module, Release.find(release_id)
end
end

Expand All @@ -23,9 +24,7 @@ def latest_module

# @return [Boolean]
def new_module_published?
return false unless ModuleRelease.exists?

ModuleRelease.ordered.last.module_position < latest_module.position
ModuleRelease.ordered.last&.module_position.to_i < latest_module.position
end

# @param mod [Training::Module]
Expand Down
6 changes: 3 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ def self.dashboard_headers
scope :last_visit_4_weeks_ago, -> { where(id: month_old_visits).where.not(id: visits_within_month) }

# emails
scope :training_email_recipients, -> { where(training_emails: [true, nil]) }
scope :early_years_email_recipients, -> { where(early_years_emails: true) }
scope :training_email_recipients, -> { order(:id).where(training_emails: [true, nil]) }
scope :early_years_email_recipients, -> { order(:id).where(early_years_emails: true) }
scope :start_training_mail_job_recipients, -> { order(:id).training_email_recipients.month_old_confirmation.registration_complete.not_started_training }
scope :complete_registration_mail_job_recipients, -> { order(:id).training_email_recipients.month_old_confirmation.registration_incomplete }
scope :continue_training_mail_job_recipients, -> { order(:id).training_email_recipients.last_visit_4_weeks_ago.distinct(&:course_in_progress?) }
scope :new_module_mail_job_recipients, -> { order(:id).training_email_recipients.completed_available_modules.to_a }
scope :new_module_mail_job_recipients, -> { order(:id).training_email_recipients }

# data
scope :dashboard, -> { not_closed }
Expand Down
22 changes: 22 additions & 0 deletions lib/tasks/cms.rake
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ namespace :eyfs do
SeedSnippets.new.call
end

# ./bin/docker-rails 'eyfs:cms:seed_releases'
desc 'Seed content releases'
task seed_releases: :environment do
Training::Module.live.each do |mod|
next if ModuleRelease.find_by(module_position: mod.position)

release =
Release.create!(
name: Random.hex,
properties: {},
time: Time.zone.now,
)

ModuleRelease.create!(
release_id: release.id,
module_position: mod.position,
name: mod.name,
first_published_at: release.time,
)
end
end

# @see .env
# CONTENTFUL_ENVIRONMENT=demo
#
Expand Down
13 changes: 5 additions & 8 deletions spec/jobs/new_module_mail_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@
let(:included) { [user] }

let(:excluded) do
create_list :user, 2, :registered, confirmed_at: 4.weeks.ago, module_time_to_completion: { alpha: 1 }
create_list :user, 2, :registered,
confirmed_at: 4.weeks.ago,
module_time_to_completion: { alpha: 1 }
end

# Recipients must have completed all available modules - in this case, alpha and bravo
# Create records for the previously released modules completed by the recipients
# Each `module_release` must have a corresponding `release` record
before do
# Create records for the previously released modules completed by the recipients
# Each `module_release` must have a corresponding `release` record
create(:release, id: 1)
create(:module_release, release_id: 1, module_position: 1, name: 'alpha')
create(:release, id: 2)
create(:module_release, release_id: 1, module_position: 2, name: 'bravo')

# This will complete the alpha and bravo modules for `user`
complete_module(alpha, 1.minute)
complete_module(bravo, 1.minute)
end

it_behaves_like 'an email prompt', 2, Training::Module.by_name(:charlie)
Expand Down
41 changes: 23 additions & 18 deletions spec/models/data_analysis/user_overview_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,40 +57,45 @@
complete_registration_mail_recipients: 1,
start_training_mail_recipients: 1,
continue_training_mail_recipients: 0,
new_module_mail_recipients: 1,
new_module_mail_recipients: 5,
},
]
end

let(:user_1) do
create :user, :registered, module_time_to_completion: { alpha: 1, bravo: 1, charlie: 0 }
let(:user) do
create :user, :registered
end

let(:user_2) do
create :user, :registered, module_time_to_completion: { alpha: 2, bravo: 0, charlie: 1 }
create :user, :registered,
module_time_to_completion: { alpha: 1, bravo: 1, charlie: 0 }
end

let(:release_1) { create(:release) }
let(:user_3) do
create :user, :registered,
module_time_to_completion: { alpha: 2, bravo: 0, charlie: 1 }
end

let(:release) { create(:release) }

before do
# create records for the previously released modules completed by the `new_module_mail_recipients`
create(:module_release, release_id: release_1.id, module_position: 1, name: 'alpha')
create(:module_release, release_id: release_1.id, module_position: 2, name: 'bravo')
create(:module_release, release_id: release_1.id, module_position: 3, name: 'charlie')
create(:module_release, release_id: release.id, module_position: 1, name: 'alpha')
create(:module_release, release_id: release.id, module_position: 2, name: 'bravo')
create(:module_release, release_id: release.id, module_position: 3, name: 'charlie')

# user#1
complete_module(alpha, 1.minute)
complete_module(bravo, 1.minute)
complete_module(charlie, 1.minute)

# create notes for the `with_notes` and `without_notes` users
create(:note, user: user_1)
# user#2 user#3 with notes
create(:note, user: user_2)
create(:note, user: user_3)

# A user who confirmed their email 4 weeks ago will receive the complete registration mail
# user#4 complete registration notification
create(:user, :confirmed, confirmed_at: 4.weeks.ago)
# A registered user who will receive the start training mail
# user#5 start training notification
create(:user, :registered, confirmed_at: 4.weeks.ago)

# This will complete the alpha, bravo and charlie modules for `user`
complete_module(alpha, 1.minute)
complete_module(bravo, 1.minute)
complete_module(charlie, 1.minute)
end

it_behaves_like 'a data export model'
Expand Down

0 comments on commit 90bb76b

Please sign in to comment.