Skip to content

Commit

Permalink
Implement lookup by list id
Browse files Browse the repository at this point in the history
  • Loading branch information
citizen428 committed Jan 30, 2020
1 parent ca6d251 commit 158fe52
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
18 changes: 15 additions & 3 deletions app/controllers/webhooks/mailchimp_unsubscribes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
class Webhooks::MailchimpUnsubscribesController < ApplicationController
class InvalidListID < StandardError; end

LIST_MAPPINGS = {
mailchimp_newsletter_id: :email_newsletter,
mailchimp_sustaining_members_id: :email_membership_newsletter,
mailchimp_tag_moderators_id: :email_tag_mod_newsletter,
mailchimp_community_moderators_id: :email_community_mod_newsletter
}.freeze

def create
not_authorized unless valid_secret?
user = User.find_by(email: params.dig(:data, :email))
user = User.find_by!(email: params.dig(:data, :email))
user.update(email_type => false)
end

Expand All @@ -12,7 +21,10 @@ def valid_secret?
end

def email_type
# TODO: map list id to email type
:email_digest_periodic
list_id = params.dig(:data, :list_id)
key = LIST_MAPPINGS.keys.detect { |k| SiteConfig.public_send(k) == list_id }
raise InvalidListID unless key

LIST_MAPPINGS[key]
end
end
7 changes: 5 additions & 2 deletions spec/requests/webhooks/mailchimp/unsubscribe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

describe "POST /webhooks/mailchimp/:secret/unsubscribe" do
let(:secret) { "secret" }
let(:params) { { data: { email: user.email } } }
let(:list_id) { "1234" }
let(:params) { { data: { email: user.email, list_id: list_id } } }

before do
allow(SiteConfig).to receive(:mailchimp_webhook_secret).and_return(secret)
Expand All @@ -18,9 +19,11 @@
end

it "unsubscribes the user if the secret is correct" do
SiteConfig.mailchimp_newsletter_id = list_id

expect do
post "/webhooks/mailchimp/#{secret}/unsubscribe", params: params
end.to change { user.reload.email_digest_periodic }.from(true).to(false)
end.to change { user.reload.email_newsletter }.from(true).to(false)
end
end
end

0 comments on commit 158fe52

Please sign in to comment.