Skip to content

Commit

Permalink
2 and 5 month EY progress email update
Browse files Browse the repository at this point in the history
  • Loading branch information
asmega committed Nov 15, 2024
1 parent 4cc10e1 commit 437bdbd
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 1 deletion.
34 changes: 34 additions & 0 deletions app/jobs/early_years_progress_emails_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class EarlyYearsProgressEmailsJob < CronJob
self.cron_expression = "0 8 * * *" # Every day at 08:00

def perform
periods_after_submission = [2.months, 5.months]
today = Date.today

periods_after_submission.each do |period_after_submission|
previous_date = today - period_after_submission

# handle when current month has more days then previous month
if today.last_day_of_the_month? && (today.day > previous_date.end_of_month.day)
next
end

window_open = previous_date.beginning_of_day

# handle when previous month has more days that current month
window_close = if today.last_day_of_the_month? && (previous_date.end_of_month.day > today.day)
previous_date.end_of_month.end_of_day
else # normal 1:1
previous_date.end_of_day
end

Claim
.by_policy(Policies::EarlyYearsPayments)
.awaiting_decision
.where(submitted_at: window_open..window_close)
.find_each do |claim|
EarlyYearsPaymentsMailer.with(claim:).progress_update.deliver_later
end
end
end
end
18 changes: 18 additions & 0 deletions app/mailers/early_years_payments_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ def submitted(claim)
end
end

def progress_update
claim = params[:claim]

personalisation = {
first_name: claim.first_name,
application_date: claim.submitted_at.to_date.to_fs(:long_date),
ref_number: claim.reference
}

template_mail(
"1d20d638-61e4-4768-beba-c447cfa8c5a7",
to: claim.email_address,
subject: nil,
reply_to_id: claim.policy.notify_reply_to_id,
personalisation:
)
end

private

def submitted_by_provider_and_send_to_provider(claim)
Expand Down
2 changes: 1 addition & 1 deletion app/models/claim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class Claim < ApplicationRecord
end

scope :require_in_progress_update_emails, -> {
by_policies(Policies.all.select{|p| p.require_in_progress_update_emails? })
by_policies(Policies.all.select { |p| p.require_in_progress_update_emails? })
}

def onelogin_idv_full_name
Expand Down
1 change: 1 addition & 0 deletions config/initializers/core_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "core_ext/date"
9 changes: 9 additions & 0 deletions lib/core_ext/date.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module CoreExt
module Date
def last_day_of_the_month?
day == end_of_month.day
end
end
end

Date.include CoreExt::Date
105 changes: 105 additions & 0 deletions spec/jobs/early_years_progress_emails_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
require "rails_helper"

RSpec.describe EarlyYearsProgressEmailsJob do
let(:policy) { Policies::EarlyYearsPayments }

describe "#perform" do
context "when no claims" do
it "does not enqueue any jobs" do
expect { subject.perform }.not_to have_enqueued_job
end
end

context "when normal day of the month" do
it "enqueues jobs normally" do
travel_to(Date.new(2024, 11, 12)) do
create(:claim, :submitted, policy:, submitted_at: 2.months.ago - 1.day)
claim_2 = create(:claim, :submitted, policy:, submitted_at: 2.months.ago)
create(:claim, :submitted, policy:, submitted_at: 2.months.ago + 1.day)

create(:claim, :submitted, policy:, submitted_at: 5.months.ago - 1.day)
claim_5 = create(:claim, :submitted, policy:, submitted_at: 5.months.ago)
create(:claim, :submitted, policy:, submitted_at: 5.months.ago + 1.day)

expect { subject.perform }.to have_enqueued_job.twice

expect(GlobalID::Locator.locate(queue_adapter.enqueued_jobs[0].dig(:args, 3, "params", "claim", "_aj_globalid"))).to eql(claim_2)
expect(GlobalID::Locator.locate(queue_adapter.enqueued_jobs[1].dig(:args, 3, "params", "claim", "_aj_globalid"))).to eql(claim_5)
end
end
end

context "when current month same number of days as previous month" do
it "enqueues jobs normally" do
travel_to(Date.new(2024, 12, 31)) do
create(:claim, :submitted, policy:, submitted_at: 2.months.ago - 1.day)
claim_2 = create(:claim, :submitted, policy:, submitted_at: 2.months.ago)
create(:claim, :submitted, policy:, submitted_at: 2.months.ago + 1.day)

expect { subject.perform }.to have_enqueued_job.once

expect(GlobalID::Locator.locate(queue_adapter.enqueued_jobs[0].dig(:args, 3, "params", "claim", "_aj_globalid"))).to eql(claim_2)
end
end
end

context "when current month has more days than previous month" do
it "enqueues no jobs on last day" do
claim_1 = create(:claim, :submitted, policy:, submitted_at: Date.new(2024, 2, 28))
claim_2 = create(:claim, :submitted, policy:, submitted_at: Date.new(2024, 2, 29))
claim_3 = create(:claim, :submitted, policy:, submitted_at: Date.new(2024, 3, 1))

travel_to(Date.new(2024, 4, 28)) do
expect { subject.perform }.to have_enqueued_job.once
expect(GlobalID::Locator.locate(queue_adapter.enqueued_jobs[0].dig(:args, 3, "params", "claim", "_aj_globalid"))).to eql(claim_1)
end

travel_to(Date.new(2024, 4, 29)) do
expect { subject.perform }.to have_enqueued_job.once
expect(GlobalID::Locator.locate(queue_adapter.enqueued_jobs[1].dig(:args, 3, "params", "claim", "_aj_globalid"))).to eql(claim_2)
end

travel_to(Date.new(2024, 4, 30)) do
expect { subject.perform }.not_to have_enqueued_job
end

travel_to(Date.new(2024, 5, 1)) do
expect { subject.perform }.to have_enqueued_job.once
expect(GlobalID::Locator.locate(queue_adapter.enqueued_jobs[2].dig(:args, 3, "params", "claim", "_aj_globalid"))).to eql(claim_3)
end
end
end

context "when previous month has more days than current month" do
it "enqueues extra jobs on last day" do
claim_1 = create(:claim, :submitted, policy:, submitted_at: Date.new(2024, 7, 29))
claim_2 = create(:claim, :submitted, policy:, submitted_at: Date.new(2024, 7, 30))
claim_3 = create(:claim, :submitted, policy:, submitted_at: Date.new(2024, 7, 31))
claim_4 = create(:claim, :submitted, policy:, submitted_at: Date.new(2024, 8, 1))

travel_to(Date.new(2024, 9, 29)) do
expect { subject.perform }.to have_enqueued_job.once
expect(GlobalID::Locator.locate(queue_adapter.enqueued_jobs[0].dig(:args, 3, "params", "claim", "_aj_globalid"))).to eql(claim_1)
end

travel_to(Date.new(2024, 9, 30)) do
expect { subject.perform }.to have_enqueued_job.twice

expect do
claims = queue_adapter.enqueued_jobs[1..2].map do |job|
GlobalID::Locator.locate(job.dig(:args, 3, "params", "claim", "_aj_globalid"))
end

expect(claims).to include(claim_2)
expect(claims).to include(claim_3)
end
end

travel_to(Date.new(2024, 10, 1)) do
expect { subject.perform }.to have_enqueued_job.once
expect(GlobalID::Locator.locate(queue_adapter.enqueued_jobs[3].dig(:args, 3, "params", "claim", "_aj_globalid"))).to eql(claim_4)
end
end
end
end
end
16 changes: 16 additions & 0 deletions spec/mailers/early_years_payments_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "rails_helper"

RSpec.describe EarlyYearsPaymentsMailer, type: :mailer do
let(:claim) { build(:claim, :submitted, policy: Policies::EarlyYearsPayments) }

describe "#progress_update" do
it "forms correct email" do
mail = described_class.with(claim:).progress_update

expect(mail.to).to eql([claim.email_address])
expect(mail.personalisation[:first_name]).to eql(claim.first_name)
expect(mail.personalisation[:application_date]).to eql(claim.submitted_at.to_date.to_fs(:long_date))
expect(mail.personalisation[:ref_number]).to eql(claim.reference)
end
end
end

0 comments on commit 437bdbd

Please sign in to comment.