Skip to content

Commit

Permalink
fix: move behavior out of rake tasks (#5881)
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticspoon committed Jul 5, 2024
1 parent b010c18 commit 81298e1
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class EmancipationChecklistReminderTask
class EmancipationChecklistReminderService
attr_reader :cases

def initialize
Expand All @@ -11,10 +11,12 @@ def initialize
end

def send_reminders
cases.each do |assignment|
::EmancipationChecklistReminderNotifier
.with(casa_case: assignment.casa_case)
.deliver(assignment.volunteer)
if Time.now.utc.to_date.day == 1
cases.each do |assignment|
::EmancipationChecklistReminderNotifier
.with(casa_case: assignment.casa_case)
.deliver(assignment.volunteer)
end
end
end
end
13 changes: 13 additions & 0 deletions app/services/volunteer_birthday_reminder_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class VolunteerBirthdayReminderService
def send_reminders
if Time.now.utc.to_date.day == 15
Volunteer.active.with_supervisor.birthday_next_month.each do |volunteer|
VolunteerBirthdayNotifier
.with(volunteer:)
.deliver(volunteer.supervisor)
end
else
puts "Volunteer Birthday Reminder Rake task skipped. Today is not the 15th of the month."
end
end
end
11 changes: 1 addition & 10 deletions lib/tasks/volunteer_birthday_reminder.rake
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
desc "Create a notification for supervisors when a volunteer has a birthday coming in the next month, scheduled for the 15th of each month in Heroku Scheduler"
task volunteer_birthday_reminder: :environment do
# Check if the current day of the month is the 15th
if Time.now.utc.to_date.day == 15
Volunteer.active.with_supervisor.birthday_next_month.each do |volunteer|
VolunteerBirthdayNotifier
.with(volunteer: volunteer)
.deliver(volunteer.supervisor)
end
else
puts "Volunteer Birthday Reminder Rake task skipped. Today is not the 15th of the month."
end
VolunteerBirthdayReminder.new.send_reminders
end
56 changes: 0 additions & 56 deletions spec/lib/tasks/emancipation_checklist_reminder_spec.rb

This file was deleted.

28 changes: 0 additions & 28 deletions spec/models/emancipation_checklist_reminder_task_spec.rb

This file was deleted.

68 changes: 68 additions & 0 deletions spec/services/emancipation_checklist_reminder_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require "rails_helper"

RSpec.describe EmancipationChecklistReminderService do
include ActiveJob::TestHelper

let(:send_reminders) { described_class.new.send_reminders }

before do
travel_to Date.new(2022, 10, 1)
end

after do
travel_back
clear_enqueued_jobs
end

context "with only two eligible cases" do
subject(:task) { described_class.new }
let!(:eligible_case1) { create(:case_assignment) }
let!(:eligible_case2) { create(:case_assignment) }
let!(:ineligible_case1) { create(:case_assignment, pre_transition: true) }
let!(:inactive_case) { create(:case_assignment, :inactive) }

it "#initialize correctly captures the eligible cases" do
expect(CasaCase.count).to eq(4)
expect(task.cases).to_not be_empty
expect(task.cases.length).to eq(2)
end
end

context "volunteer with transition age youth case" do
let!(:casa_case) { create(:casa_case, :with_one_case_assignment) }

it "should send notification" do
expect { send_reminders }.to change { casa_case.case_assignments.first.volunteer.notifications.count }.by(1)
end
end

context "volunteer with multiple transition age youth cases" do
let!(:volunteer) { create(:volunteer, :with_casa_cases) }

it "sends notification for each case" do
expect { send_reminders }.to change { volunteer.notifications.count }.by(2)
end
end

context "volunteer without transition age youth case" do
let!(:casa_case) { create(:casa_case, :with_one_case_assignment, birth_month_year_youth: 13.years.ago) }

it "should not send notification" do
expect { send_reminders }.not_to change { casa_case.case_assignments.first.volunteer.notifications.count }
end
end

context "when the case assignment is inactive" do
let!(:case_assignment) { create(:case_assignment, :inactive) }

it "should not send notification" do
expect { send_reminders }.not_to change { case_assignment.volunteer.notifications.count }
end
end

context "when there are no case assignments" do
it "does not raise error" do
expect { send_reminders }.not_to raise_error
end
end
end
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
require "rails_helper"
require "rake"
Rake.application.rake_require "tasks/volunteer_birthday_reminder"

RSpec.describe "lib/tasks/volunteer_birthday_reminder.rake", ci_only: true do
let(:rake_task) { Rake::Task["volunteer_birthday_reminder"].invoke }
RSpec.describe VolunteerBirthdayReminderService do
include ActiveJob::TestHelper

let(:send_reminders) { described_class.new.send_reminders }
let(:now) { Date.new(2022, 10, 15) }
let(:this_month) { now.month }
let(:this_month_15th) { Date.new(now.year, now.month, 15) }
let(:next_month) { Date.new(1988, this_month + 1, 1) }
let(:not_next_month) { Date.new(1998, this_month - 1, 1) }

before do
Rake::Task.clear
Casa::Application.load_tasks

travel_to now
end

after do
travel_back
clear_enqueued_jobs
end

context "there is a volunteer with a birthday next month" do
Expand All @@ -27,18 +25,18 @@
end

it "creates a notification" do
expect { rake_task }.to change { volunteer.supervisor.notifications.count }.by(1)
expect { send_reminders }.to change { volunteer.supervisor.notifications.count }.by(1)
end
end

context "there are many volunteers with birthdays next month" do
volunteer_count = 10
context "there are multiple volunteers with birthdays next month" do
let(:supervisor) { create(:supervisor) }
let!(:volunteer) do
create_list(:volunteer, volunteer_count, :with_assigned_supervisor, date_of_birth: next_month)
create_list(:volunteer, 4, :with_assigned_supervisor, date_of_birth: next_month, supervisor: supervisor)
end

it "creates many notifications" do
expect { rake_task }.to change { Noticed::Notification.count }.by(volunteer_count)
it "creates multiple notifications" do
expect { send_reminders }.to change { Noticed::Notification.count }.by(4)
end
end

Expand All @@ -48,7 +46,7 @@
end

it "does not create a notification" do
expect { rake_task }.to change { Noticed::Notification.count }.by(0)
expect { send_reminders }.to change { Noticed::Notification.count }.by(0)
end
end

Expand All @@ -58,7 +56,7 @@
end

it "does not create a notification" do
expect { rake_task }.to change { volunteer.supervisor.notifications.count }.by(0)
expect { send_reminders }.to change { volunteer.supervisor.notifications.count }.by(0)
end
end

Expand All @@ -68,7 +66,7 @@
end

it "does not create a notification" do
expect { rake_task }.to change { volunteer.supervisor.notifications.count }.by(0)
expect { send_reminders }.to change { volunteer.supervisor.notifications.count }.by(0)
end
end

Expand All @@ -80,15 +78,15 @@
end

it "runs the rake task" do
expect { rake_task }.to change { volunteer.supervisor.notifications.count }.by(1)
expect { send_reminders }.to change { volunteer.supervisor.notifications.count }.by(1)
end
end

context "when today is not the 15th" do
before { travel_to(this_month_15th + 2.days) }

it "skips the rake task" do
expect { rake_task }.to change { Noticed::Notification.count }.by(0)
expect { send_reminders }.to change { Noticed::Notification.count }.by(0)
end
end
end

0 comments on commit 81298e1

Please sign in to comment.