diff --git a/app/helpers/external/mailbluster_helper.rb b/app/helpers/external/mailbluster_helper.rb index 5c39bf2f..b1dc93b4 100644 --- a/app/helpers/external/mailbluster_helper.rb +++ b/app/helpers/external/mailbluster_helper.rb @@ -39,8 +39,6 @@ module External body: { 'email' => user.email, 'firstName' => user.name, - 'overrideExisting' => true, - 'subscribed' => true, 'addTags' => add_tags, 'removeTags' => remove_tags }.to_json diff --git a/app/models/user.rb b/app/models/user.rb index f36a25d1..fbf19a67 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -81,6 +81,7 @@ class User < ApplicationRecord after_save :touch_events after_commit :mailbluster_create_lead, on: :create + after_commit :mailbluster_delete_lead, on: :destroy # add scope scope :comment_notifiable, ->(conference) {joins(:roles).where('roles.name IN (?)', [:organizer, :cfp]).where('roles.resource_type = ? AND roles.resource_id = ?', 'Conference', conference.id)} @@ -378,6 +379,10 @@ class User < ApplicationRecord ApplicationController.helpers.create_lead(self) end + def mailbluster_delete_lead + ApplicationController.helpers.delete_lead(self) + end + def touch_events event_users.each(&:touch) end diff --git a/spec/helpers/external/mailbluster_helper_spec.rb b/spec/helpers/external/mailbluster_helper_spec.rb index d06fff0c..8e4933a4 100644 --- a/spec/helpers/external/mailbluster_helper_spec.rb +++ b/spec/helpers/external/mailbluster_helper_spec.rb @@ -44,7 +44,66 @@ describe External::MailblusterHelper, type: :helper do end describe 'edit_lead' do - pending + it 'makes a put request to Mailbluster\'s API to change the email and gets the correct response' do + response_body = "{ + \"message\": \"Lead updated\", + \"lead\": { + \"id\": 329395, + \"firstName\": \"#{user.name}\", + \"lastName\": \"\", + \"fullName\": \"#{user.name}\", + \"email\": \"#{user.email}\", + \"subscribed\": true, + \"tags\": [ + #{ENV['OSEM_NAME'] || 'snapcon'} + ], + } + }" + stub_request(:put, url) + .to_return(body: response_body, status: 200) + add_tags = ['2021'] + old_email = user.email + user.email = "new@new.org" + user.save + response = edit_lead(user, old_email: old_email) + + expect(WebMock).to have_requested(:put, url + Digest::MD5.hexdigest(old_email)).with(body: { + 'email': user.email, + 'firstName': user.name, + 'addTags': add_tags, + 'removeTags': [] + }.to_json) + expect(response).to eq(response_body) + end + + it 'makes a put request to Mailbluster\'s API to add a tag and gets the correct response' do + response_body = "{ + \"message\": \"Lead updated\", + \"lead\": { + \"id\": 329395, + \"firstName\": \"#{user.name}\", + \"lastName\": \"\", + \"fullName\": \"#{user.name}\", + \"email\": \"#{user.email}\", + \"subscribed\": true, + \"tags\": [ + #{ENV['OSEM_NAME'] || 'snapcon'}, '2021' + ], + } + }" + stub_request(:put, url + Digest::MD5.hexdigest(user.email)) + .to_return(body: response_body, status: 200) + add_tags = ['2021'] + response = edit_lead(user, add_tags: add_tags) + + expect(WebMock).to have_requested(:put, url).with(body: { + 'email': user.email, + 'firstName': user.name, + 'addTags': add_tags, + 'removeTags': [] + }.to_json) + expect(response).to eq(response_body) + end end describe 'delete_lead' do