Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add before_action in IncomingEmail webhook controller #4443

Merged
merged 7 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/controllers/mailgun_webhooks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class MailgunWebhooksController < ActionController::Base
skip_before_action :verify_authenticity_token
before_action :authenticate_mailgun_request
before_action :re_optin_when_client_replies, only: :create_incoming_email

REGEX_FROM_ENVELOPE = /.*\<(?<address>(.*))>/.freeze

Expand Down Expand Up @@ -158,6 +159,16 @@ def update_outgoing_email_status

private

def re_optin_when_client_replies
sender_email_address = parse_valid_email_address(from: params["from"], sender: params["sender"])
opted_out_state_intakes = StateFileBaseIntake.opted_out_state_file_intakes(sender_email_address)
unless opted_out_state_intakes.empty?
opted_out_state_intakes.each do |intake|
Copy link
Contributor Author

@rickreyhsig rickreyhsig Apr 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure there's better logic here for updating only one intake, but since we don't have a unique validation constraint on the intake, there could be multiple intakes with the same email. I kept the pattern of creating multiple incoming_emails established in the MailgunWebhooksController. Grabbing the last intake is also not a guarantee to grabbing the right one, so I am open to feedback here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think updating all of them rather than just the last one is absolutely the right call

intake.update(unsubscribed_from_email: false)
end
end
end

def parse_valid_email_address(from:, sender:)
if REGEX_FROM_ENVELOPE.match?(from)
provided_address = REGEX_FROM_ENVELOPE.match(from).named_captures["address"]
Expand Down
1 change: 1 addition & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true

STATE_INTAKE_CLASS_NAMES = %w[StateFileAzIntake StateFileNyIntake].map(&:to_s).freeze
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this here to make it available to all our models instead of just EfileSubmission

# Allow counting up to a max number; see https://alexcastano.com/the-hidden-cost-of-the-invisible-queries-in-rails/#how-far-do-you-plan-to-count
scope :count_greater_than?, ->(n) { limit(n + 1).count > n }

Expand Down
2 changes: 0 additions & 2 deletions app/models/efile_submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ class EfileSubmission < ApplicationRecord
has_one_attached :submission_bundle
validates :irs_submission_id, format: { with: /\A[0-9]{6}[0-9]{7}[0-9a-z]{7}\z/ }, presence: true, uniqueness: true, allow_nil: true

STATE_INTAKE_CLASS_NAMES = [StateFileAzIntake, StateFileNyIntake].map(&:to_s).freeze

include Statesman::Adapters::ActiveRecordQueries[
transition_class: EfileSubmissionTransition,
initial_state: EfileSubmissionStateMachine.initial_state,
Expand Down
7 changes: 0 additions & 7 deletions app/models/incoming_email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class IncomingEmail < ApplicationRecord
has_many :documents, as: :contact_record

after_create { InteractionTrackingService.record_incoming_interaction(client) }
after_create { enable_email_opt_in }

def body
stripped_text.present? ? [stripped_text, stripped_signature].map(&:presence).compact.join("\n") : body_plain
Expand All @@ -43,10 +42,4 @@ def body
def datetime
received_at
end

def enable_email_opt_in
if to == 'hello@getyourrefund.org' || to == 'support@getyourrefund.org'
self.client.intake.update(email_notification_opt_in: "yes")
end
end
end
9 changes: 9 additions & 0 deletions app/models/state_file_base_intake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,13 @@ def controller_for_current_step
end
end
end

def self.opted_out_state_file_intakes(email)
state_intakes = []
STATE_INTAKE_CLASS_NAMES.each do |state|
class_object = state.constantize
state_intakes += class_object.where(email_address: email).where(unsubscribed_from_email: true)
end
state_intakes
end
end
13 changes: 13 additions & 0 deletions spec/models/efile_submission_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -706,4 +706,17 @@
end
end
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this spec since I moved STATE_INTAKE_CLASS_NAMES form the efile_submission model, and I wanted to make sure I didn't break anything.

describe '#is_for_state_filing?' do
let(:state_efile_submission) { create :efile_submission, :for_state }
let(:non_state_efile_submission) { create :efile_submission }

it 'returns true for state submission' do
expect(state_efile_submission.is_for_state_filing?).to eq true
end

it 'returns false for non-state submission' do
expect(non_state_efile_submission.is_for_state_filing?).to eq false
end
end
end
20 changes: 0 additions & 20 deletions spec/models/incoming_email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,4 @@
let(:subject) { build(:incoming_email) }
end
end

context "enable_email_opt_in" do
let(:intake) { create :intake, email_notification_opt_in: "no" }
let(:client) { intake.client }
let(:subject) { build(
:incoming_email,
stripped_text: nil,
stripped_signature: nil,
body_plain: "I love sending emails to websites",
client_id: client.id,
to: 'hello@getyourrefund.org',
)
}

it 'updates email_notification_opt_in to yes with correct incoming_email' do
expect(intake.email_notification_opt_in).to eq('no')
subject.save
expect(intake.reload.email_notification_opt_in).to eq('yes')
end
end
end
17 changes: 17 additions & 0 deletions spec/models/state_file_base_intake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,21 @@
end
end
end

describe '.opted_out_state_file_intakes' do
let!(:email) { 'email@test.com' }
let!(:intake) { create :state_file_az_intake, email_address: email, unsubscribed_from_email: true }

context 'when is a FYST email' do
it 'returns the intakes' do
expect(StateFileBaseIntake.opted_out_state_file_intakes(email).length).to eq 1
end
end

context 'when is NOT a FYST email' do
it 'returns empty' do
expect(StateFileBaseIntake.opted_out_state_file_intakes('another_email@test.com')).to be_empty
end
end
end
end
Loading