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

Suppress sending emails to deactivated email addresses #145

Merged
Prev Previous commit
Next Next commit
Handle subscription changes for purchase email addresses
floehopper committed Jan 10, 2024
commit 5d8fe6b11e3a15716ab9aa3bf6c2ed4ed0d399f2
7 changes: 6 additions & 1 deletion app/controllers/email_subscription_changes_controller.rb
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ def create

update_sending_suppressed_for(user)
update_sending_suppressed_for(interest)
update_sending_suppressed_for(purchase)
render json: { user: { id: user&.id }, interest: { id: interest&.id } }, status: :created
end

@@ -43,13 +44,17 @@ def interest
@interest ||= Interest.find_by(email: params[:Recipient])
end

def purchase
@purchase ||= Purchase.find_by(customer_email: params[:Recipient])
end

def authenticate
authenticate_or_request_with_http_token do |token|
ActiveSupport::SecurityUtils.secure_compare(token, TOKEN)
end
end

def ensure_record_exists
raise ActiveRecord::RecordNotFound if user.blank? && interest.blank?
raise ActiveRecord::RecordNotFound if user.blank? && interest.blank? && purchase.blank?
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddSendingSuppressedAtToPurchases < ActiveRecord::Migration[7.1]
def change
add_column :purchases, :sending_suppressed_at, :datetime
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions test/controllers/email_subscription_changes_controller_test.rb
Original file line number Diff line number Diff line change
@@ -29,6 +29,17 @@ class EmailSubscriptionChangesControllerTest < ActionDispatch::IntegrationTest
assert_equal Time.zone.iso8601(params['ChangedAt']), interest.sending_suppressed_at
end

test 'suppresses sending for purchase' do
purchase = create_purchase(email: params['Recipient'])

post(email_subscription_changes_path, headers:, params:)

assert_response :created
purchase.reload
assert purchase.suppress_sending?
assert_equal Time.zone.iso8601(params['ChangedAt']), purchase.sending_suppressed_at
end

test 'does not suppress sending for user if bearer token is not valid' do
post(email_subscription_changes_path, headers: headers(token: 'invalid'), params:)

@@ -64,6 +75,10 @@ def create_interest(email:)
create(:interest, email:)
end

def create_purchase(email:)
create(:purchase, customer_email: email)
end

def headers(token: EmailSubscriptionChangesController::TOKEN)
{ 'Authorization' => "Bearer #{token}" }
end