-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
app/models/state_file/automated_message/post_deadline_reminder.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module StateFile::AutomatedMessage | ||
class PostDeadlineReminder < BaseAutomatedMessage | ||
|
||
def self.name | ||
'messages.state_file.post_deadline_reminder'.freeze | ||
end | ||
|
||
def self.after_transition_notification? | ||
false | ||
end | ||
|
||
def self.send_only_once? | ||
true | ||
end | ||
|
||
def sms_body(**args) | ||
I18n.t("messages.state_file.post_deadline_reminder.sms", **args) | ||
end | ||
|
||
def email_subject(**args) | ||
I18n.t("messages.state_file.post_deadline_reminder.email.subject", **args) | ||
end | ||
|
||
def email_body(**args) | ||
I18n.t("messages.state_file.post_deadline_reminder.email.body", **args) | ||
end | ||
end | ||
end |
38 changes: 38 additions & 0 deletions
38
app/services/state_file/send_post_deadline_reminder_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module StateFile | ||
class SendPostDeadlineReminderService | ||
BATCH_SIZE = 10 | ||
HOURS_AGO = 24 | ||
|
||
def self.run | ||
cutoff_time_ago = HOURS_AGO.hours.ago | ||
intakes_to_notify = [] | ||
|
||
ApplicationRecord::STATE_INTAKE_CLASS_NAMES.each do |base_class| | ||
class_object = base_class.constantize | ||
intakes_to_notify += class_object.left_joins(:efile_submissions) | ||
.where(efile_submissions: { id: nil }) | ||
.where.not(email_address: nil) | ||
.where.not(email_address_verified_at: nil) | ||
.where(unsubscribed_from_email: false) | ||
.where("#{base_class.underscore.pluralize}.message_tracker #> '{messages.state_file.post_deadline_reminder}' IS NULL") | ||
.select do |intake| | ||
if intake.message_tracker.present? && intake.message_tracker["messages.state_file.finish_return"] | ||
finish_return_msg_sent_time = Date.parse(intake.message_tracker["messages.state_file.finish_return"]) | ||
finish_return_msg_sent_time < cutoff_time_ago | ||
else | ||
true | ||
end | ||
end | ||
end | ||
|
||
intakes_to_notify.each_slice(BATCH_SIZE) do |batch| | ||
batch.each do |intake| | ||
StateFile::MessagingService.new( | ||
message: StateFile::AutomatedMessage::PostDeadlineReminder, | ||
intake: intake | ||
).send_message | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
require 'rails_helper' | ||
|
||
describe 'state_file:post_deadline_reminder' do | ||
before(:context) do | ||
Rails.application.load_tasks | ||
end | ||
|
||
context 'Sends the notification to all state-filing' do | ||
let!(:az_intake) { create :state_file_az_intake, email_address: 'test@example.com', email_address_verified_at: 1.minute.ago, created_at: 25.hours.ago } | ||
let!(:ny_intake) { create :state_file_ny_intake, email_address: 'test+01@example.com', email_address_verified_at: 1.minute.ago, created_at: 25.hours.ago } | ||
let!(:submitted_intake) { create :state_file_ny_intake, email_address: 'test+01@example.com', email_address_verified_at: 1.minute.ago } | ||
let!(:efile_submission) { create :efile_submission, :for_state, data_source: submitted_intake } | ||
|
||
it 'intakes without submissions & without reminders' do | ||
messaging_service = spy('StateFile::MessagingService') | ||
allow(StateFile::MessagingService).to receive(:new).and_return(messaging_service) | ||
|
||
Rake::Task['state_file:post_deadline_reminder'].execute | ||
|
||
expect(StateFile::MessagingService).to have_received(:new).exactly(2).times | ||
end | ||
end | ||
|
||
context 'Sends the notification to intakes that have' do | ||
let!(:intake_with_reminder) { | ||
create :state_file_az_intake, email_address: "test@example.com", | ||
email_address_verified_at: 1.hour.ago, created_at: 25.hours.ago | ||
} | ||
|
||
before do | ||
allow_any_instance_of(StateFileNyIntake).to receive(:message_tracker).and_return((Time.now - 25.hours).utc.to_s) | ||
end | ||
|
||
it 'the reminder_notification sent more than 24 hours ago' do | ||
messaging_service = spy('StateFile::MessagingService') | ||
allow(StateFile::MessagingService).to receive(:new).and_return(messaging_service) | ||
|
||
Rake::Task['state_file:post_deadline_reminder'].execute | ||
|
||
expect(StateFile::MessagingService).to have_received(:new).exactly(1).times | ||
end | ||
end | ||
|
||
context 'Does NOT send the notification to' do | ||
let!(:intake_with_reminder) { | ||
create :state_file_az_intake, email_address: "test@example.com", | ||
email_address_verified_at: 1.hour.ago, created_at: 25.hours.ago | ||
} | ||
|
||
before do | ||
allow_any_instance_of(StateFileAzIntake).to receive(:message_tracker).and_return( | ||
{ "messages.state_file.finish_return" => (Time.now - 2.hours).utc.to_s } | ||
) | ||
end | ||
|
||
it 'intakes that have the reminder_notification sent less than 24 hours ago' do | ||
messaging_service = spy('StateFile::MessagingService') | ||
allow(StateFile::MessagingService).to receive(:new).and_return(messaging_service) | ||
|
||
Rake::Task['state_file:post_deadline_reminder'].execute | ||
|
||
expect(StateFile::MessagingService).to have_received(:new).exactly(0).times | ||
end | ||
end | ||
end |