Skip to content

Commit

Permalink
Merge branch 'main' into release
Browse files Browse the repository at this point in the history
Co-authored-by: Tim O'Farrell <tofarrell@codeforamerica.org>
  • Loading branch information
tahsinaislam and tofarr committed Apr 17, 2024
2 parents c3755d9 + e7fcbe4 commit 2d36543
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 21 deletions.
4 changes: 2 additions & 2 deletions app/services/state_file/after_transition_messaging_service.rb
Expand Up @@ -22,7 +22,7 @@ def send_efile_submission_accepted_message
intake: @intake,
submission: @submission,
message: message,
body_args: body_args).send_message
body_args: body_args).send_message(require_verification: false)

schedule_survey_notification_job
end
Expand All @@ -41,7 +41,7 @@ def send_efile_submission_rejected_message
submission: @submission,
message: message,
body_args: body_args
).send_message
).send_message(require_verification: false)
end

def send_efile_submission_still_processing_message
Expand Down
29 changes: 25 additions & 4 deletions app/services/state_file/messaging_service.rb
Expand Up @@ -20,10 +20,10 @@ def message_tracker
@message_tracker ||= MessageTracker.new(data_source: data_source, message: message)
end

def send_message
def send_message(require_verification: true)
return nil if message_tracker.already_sent? && message.send_only_once?

send_email if @do_email && !intake.unsubscribed_from_email?
send_email(require_verification: require_verification) if @do_email && !intake.unsubscribed_from_email?
if intake.unsubscribed_from_email?
DatadogApi.increment("mailgun.state_file_notification_emails.not_sent_because_unsubscribed")
end
Expand All @@ -36,8 +36,10 @@ def send_message

private

def send_email
return unless intake.email_address.present? && intake.email_address_verified_at.present?
def send_email(require_verification: true)
email_verified = intake.email_address_verified_at.present? || matching_intakes_has_email_verified_at?(intake)
return if intake.email_address.nil?
return if require_verification && !email_verified
return if intake.unsubscribed_from_email?

if @message_instance.email_body.present?
Expand All @@ -51,6 +53,25 @@ def send_email
end
end

def matching_intakes_has_email_verified_at?(intake)
return if intake.email_address.nil? || intake.hashed_ssn.nil?
matching_intakes = case intake.state_code
when "az"
StateFileAzIntake
.where(email_address: intake.email_address, hashed_ssn: intake.hashed_ssn)
.where.not(email_address_verified_at: nil)
when "ny"
StateFileNyIntake
.where(email_address: intake.email_address, hashed_ssn: intake.hashed_ssn)
.where.not(email_address_verified_at: nil)
else
return false
end

matching_intakes.present?
end


# def send_sms
# return unless Flipper.enabled?(:sms_notifications)
# end
Expand Down
44 changes: 31 additions & 13 deletions app/services/state_file/send_post_deadline_reminder_service.rb
Expand Up @@ -5,32 +5,50 @@ class SendPostDeadlineReminderService

def self.run
cutoff_time_ago = HOURS_AGO.hours.ago
intakes_to_notify = []

ApplicationRecord::STATE_INTAKE_CLASS_NAMES.each do |base_class|
intakes_to_notify = ApplicationRecord::STATE_INTAKE_CLASS_NAMES.map 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|

# First we get all intake ids by email address
intake_ids_by_email = class_object.select(:id, :email_address).where.not(email_address: nil).each_with_object({}) do |intake, result|
ids = result[intake.email_address]
ids = [] unless ids.present?
ids.append(intake.id)
end

# Next we get all intake ids by hashed SSN
intake_ids_by_hashed_ssn = class_object.select(:id, :hashed_ssn).where.not(hashed_ssn: nil).each_with_object({}) do |intake, result|
ids = result[intake.hashed_ssn]
ids = [] unless ids.present?
ids.append(intake.id)
end

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 = Time.parse(intake.message_tracker["messages.state_file.finish_return"])
finish_return_msg_sent_time < cutoff_time_ago
else
true
end
end.select do |intake|
# New criteria - gonna see if any associated intakes have submissions
intake_ids = (intake_ids_by_email[intake.email_address] || []) + (intake_ids_by_hashed_ssn[intake.hashed_ssn] || [])
intake_ids = intake_ids.to_set
(intake_ids.length == 1) || EfileSubmission.where(data_source_id: intake_ids, data_source_type: base_class).none?
end
end
end.flatten

intakes_to_notify.each_slice(BATCH_SIZE) do |batch|
batch.each do |intake|
StateFile::MessagingService.new(
message: StateFile::AutomatedMessage::PostDeadlineReminder,
intake: intake
).send_message
).send_message(require_verification: false)
end
end
end
Expand Down
Expand Up @@ -17,7 +17,7 @@ def self.run
.where("#{base_class.underscore.pluralize}.message_tracker #> '{messages.state_file.pre_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 = Time.parse(intake.message_tracker["messages.state_file.finish_return"])
finish_return_msg_sent_time < cutoff_time_ago
else
true
Expand Down
@@ -0,0 +1,9 @@
class AddEmailAddressIndexToIntake < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def change
add_index :state_file_az_intakes, :email_address, algorithm: :concurrently
add_index :state_file_ny_intakes, :email_address, algorithm: :concurrently
end

end
2 changes: 1 addition & 1 deletion db/schema.rb
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_04_03_214006) do
ActiveRecord::Schema[7.1].define(version: 2024_04_16_180539) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
enable_extension "plpgsql"
Expand Down

0 comments on commit 2d36543

Please sign in to comment.