Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion app/models/fields/custom_field_mailchimp_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def apply_serialization

serialized_attributes = klass.column_names.select{|col| klass.type_for_attribute(col).class == ::ActiveRecord::Type::Serialized }
if !serialized_attributes.include?(self.name)
Rails.logger.debug("#{Time.now.to_s(:db)} FfcrmMailchimp: Serializing #{self.name} as Hash for #{klass}.")
Rails.logger.debug("#{Time.now.to_fs(:db)} FfcrmMailchimp: Serializing #{self.name} as Hash for #{klass}.")
klass.serialize(self.name, Hash)
end
end
Expand Down
24 changes: 12 additions & 12 deletions lib/ffcrm_mailchimp/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,33 @@ def all_lists
def subscribe(list_id, email, body = {}, groupings={})
gibbon = Gibbon::Request.new()
body = body.merge(status_if_new: "subscribed", status: "subscribed", interests: groupings_for_api(list_id, groupings))
FfcrmMailchimp.logger.info("#{Time.now.to_s(:db)} FfcrmMailchimp::Api: subscribing contact #{email} to list #{list_id}. Payload #{body}")
FfcrmMailchimp.logger.info("#{Time.now.to_fs(:db)} FfcrmMailchimp::Api: subscribing contact #{email} to list #{list_id}. Payload #{body}")
begin
gibbon.lists(list_id).members(email_digest(email)).upsert(body: body)
rescue Gibbon::MailChimpError => e
FfcrmMailchimp.logger.error("#{Time.now.to_s(:db)} FfcrmMailchimp::Api: Gibbon::MailchimpError #{e.status_code} #{e.title} #{e.detail} #{e.body} ")
FfcrmMailchimp.logger.error("#{Time.now.to_fs(:db)} FfcrmMailchimp::Api: Gibbon::MailchimpError #{e.status_code} #{e.title} #{e.detail} #{e.body} ")
raise e # throw it again to ensure that the job is tried again
end
end

# Unsubscribe a user from the list entirely (marks them as unsubscribed in Mailchimp)
def unsubscribe(list_id, email)
FfcrmMailchimp.logger.info("#{Time.now.to_s(:db)} FfcrmMailchimp::Api: unsubscribing #{email} from list #{list_id}")
FfcrmMailchimp.logger.info("#{Time.now.to_fs(:db)} FfcrmMailchimp::Api: unsubscribing #{email} from list #{list_id}")
gibbon = Gibbon::Request.new()
begin
gibbon.lists(list_id).members(email_digest(email)).update(body: { status: "unsubscribed" })
rescue Gibbon::MailChimpError => e
if (e.status_code == 404)
FfcrmMailchimp.logger.info("#{Time.now.to_s(:db)} FfcrmMailchimp::Api: user #{email} not found on list #{list_id}. Ignoring.")
FfcrmMailchimp.logger.info("#{Time.now.to_fs(:db)} FfcrmMailchimp::Api: user #{email} not found on list #{list_id}. Ignoring.")
else
FfcrmMailchimp.logger.error("#{Time.now.to_s(:db)} FfcrmMailchimp::Api: Gibbon::MailchimpError #{e.status_code} #{e.title} #{e.detail} #{e.body} ")
FfcrmMailchimp.logger.error("#{Time.now.to_fs(:db)} FfcrmMailchimp::Api: Gibbon::MailchimpError #{e.status_code} #{e.title} #{e.detail} #{e.body} ")
raise e # throw it again to ensure that the job is tried again
end
end
end

# Enumerate the categories and interest groups that users can subscribe to
# [ {"list_id"=>"4a1df096f3", "id"=>"34b9452245", "title"=>"Interest group 1",
# [ {"list_id"=>"4a1df096f3", "id"=>"34b9452245", "title"=>"Interest group 1",
# "groups"=>[ {"id"=>"70b7107c8a", "name"=>"Option 1"}, {"id"=>"7c1719c788", "name"=>"Option 2"}, {"id"=>"8d856390f6", "name"=>"Option 3"}]
# }
# ]
Expand Down Expand Up @@ -89,7 +89,7 @@ def lookup_member_on_list(list_id, email_address)
begin
result = gibbon.lists(list_id).members(email_digest(email_address)).retrieve
rescue Gibbon::MailChimpError => e
FfcrmMailchimp.logger.error("#{Time.now.to_s(:db)} FfcrmMailchimp::Api: Gibbon::MailchimpError #{e.status_code} #{e.title} #{e.detail} #{e.body} ")
FfcrmMailchimp.logger.error("#{Time.now.to_fs(:db)} FfcrmMailchimp::Api: Gibbon::MailchimpError #{e.status_code} #{e.title} #{e.detail} #{e.body} ")
if (e.status_code == 404)
result = {} # member not found on list
else
Expand All @@ -101,13 +101,13 @@ def lookup_member_on_list(list_id, email_address)

def clear_cache
all_lists.each do |list|
FfcrmMailchimp.logger.info("#{Time.now.to_s(:db)} FfcrmMailchimp::Api: Clearing cache: #{interest_groupings_cache_key(list['id'])}")
FfcrmMailchimp.logger.info("#{Time.now.to_fs(:db)} FfcrmMailchimp::Api: Clearing cache: #{interest_groupings_cache_key(list['id'])}")
Rails.cache.delete( interest_groupings_cache_key(list['id']) )
end
FfcrmMailchimp.logger.info("#{Time.now.to_s(:db)} FfcrmMailchimp::Api: Clearing cache: #{all_lists_cache_key}")
FfcrmMailchimp.logger.info("#{Time.now.to_fs(:db)} FfcrmMailchimp::Api: Clearing cache: #{all_lists_cache_key}")
Rails.cache.delete( all_lists_cache_key )
end

private

def all_lists_cache_key
Expand All @@ -131,7 +131,7 @@ def groupings_for_api(list_id, groupings)
end.flatten.compact.uniq

result = {}
interest_groupings(list_id).each do |interest_category|
interest_groupings(list_id).each do |interest_category|
interest_category['groups'].each do |group|
result[group['id']] = selected_group_names.include?(group['name']) # 'true' or 'false'
end
Expand All @@ -142,4 +142,4 @@ def groupings_for_api(list_id, groupings)
end

end
end
end
10 changes: 5 additions & 5 deletions lib/ffcrm_mailchimp/delayed_outbound_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ def self.subscribe(record)
changes = FfcrmMailchimp::Changes.new(record)
if changes.need_sychronization?
if FfcrmMailchimp.config.sync_enabled?
FfcrmMailchimp.logger.info("#{Time.now.to_s(:db)} FfcrmMailchimp::DelayedOutboundSync Queueing update to Mailchimp for #{record.class}##{record.id}")
FfcrmMailchimp.logger.info("#{Time.now.to_fs(:db)} FfcrmMailchimp::DelayedOutboundSync Queueing update to Mailchimp for #{record.class}##{record.id}")
subscribed_email = changes.old_email || record.email # important to match correct record if email is being changed.
OutboundSyncSubscribeJob.perform_later(record, subscribed_email)
else
FfcrmMailchimp.logger.info("#{Time.now.to_s(:db)} FfcrmMailchimp::DelayedOutboundSync Sync disabled and therefore not queueing update to Mailchimp for #{record.class}##{record.id}")
FfcrmMailchimp.logger.info("#{Time.now.to_fs(:db)} FfcrmMailchimp::DelayedOutboundSync Sync disabled and therefore not queueing update to Mailchimp for #{record.class}##{record.id}")
end
else
FfcrmMailchimp.logger.info("#{Time.now.to_s(:db)} FfcrmMailchimp::DelayedOutboundSync No changes require update to Mailchimp for #{record.class}##{record.id}")
FfcrmMailchimp.logger.info("#{Time.now.to_fs(:db)} FfcrmMailchimp::DelayedOutboundSync No changes require update to Mailchimp for #{record.class}##{record.id}")
end
end

#
# Always need to sync if contact is deleted.
def self.unsubscribe(record)
if FfcrmMailchimp.config.sync_enabled?
FfcrmMailchimp.logger.info("#{Time.now.to_s(:db)} FfcrmMailchimp::DelayedOutboundSync Scheduled Mailchimp list deletion for deleted contact #{record.class}##{record.id} - #{record.email}")
FfcrmMailchimp.logger.info("#{Time.now.to_fs(:db)} FfcrmMailchimp::DelayedOutboundSync Scheduled Mailchimp list deletion for deleted contact #{record.class}##{record.id} - #{record.email}")
OutboundSyncUnsubscribeJob.perform_later(record.email)
else
FfcrmMailchimp.logger.info("#{Time.now.to_s(:db)} FfcrmMailchimp::DelayedOutboundSync Sync disabled and therefore ignored list deletion for deleted contact #{record.class}##{record.id} - #{record.email}")
FfcrmMailchimp.logger.info("#{Time.now.to_fs(:db)} FfcrmMailchimp::DelayedOutboundSync Sync disabled and therefore ignored list deletion for deleted contact #{record.class}##{record.id} - #{record.email}")
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ffcrm_mailchimp/inbound_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def self.process(params)
def process
return unless list_field_exists?
if config.sync_disabled?
FfcrmMailchimp.logger.info("#{Time.now.to_s(:db)} FfcrmMailchimp::InboundSync Sync disabled. Ignoring incoming #{data.type}")
FfcrmMailchimp.logger.info("#{Time.now.to_fs(:db)} FfcrmMailchimp::InboundSync Sync disabled. Ignoring incoming #{data.type}")
return
end
case data.type
Expand Down
6 changes: 3 additions & 3 deletions lib/ffcrm_mailchimp/outbound_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ def initialize(record, subscribed_email)
# Handles unsubscribes when necessary
def subscribe
if @subscribed_email.blank?
FfcrmMailchimp.logger.info("#{Time.now.to_s(:db)} FfcrmMailchimp::OutboundSync: no email address for #{@record.class}##{@record.id}. Cannot proceed.")
FfcrmMailchimp.logger.info("#{Time.now.to_fs(:db)} FfcrmMailchimp::OutboundSync: no email address for #{@record.class}##{@record.id}. Cannot proceed.")
return
end
mailchimp_list_field_names.each do |column|
subscription = ListSubscription.new( @record.send(column) )
if !subscription.source_is_ffcrm? # Stop if this is a webhook from mailchimp
FfcrmMailchimp.logger.info("#{Time.now.to_s(:db)} FfcrmMailchimp::OutboundSync: ignoring updates to #{@record.class}##{@record.id} (change initiated by webhook or no list subscription data)")
FfcrmMailchimp.logger.info("#{Time.now.to_fs(:db)} FfcrmMailchimp::OutboundSync: ignoring updates to #{@record.class}##{@record.id} (change initiated by webhook or no list subscription data)")
break
end
# Note: it's important to get list_id from the column not the ListSubscription
Expand All @@ -48,7 +48,7 @@ def self.unsubscribe(email)
# When a contact is deleted, remove all mailchimp subscriptions
def unsubscribe(email)
if email.present?
FfcrmMailchimp.logger.info("#{Time.now.to_s(:db)} FfcrmMailchimp::OutboundSync: unsubscribing #{email} from all mailchimp lists.")
FfcrmMailchimp.logger.info("#{Time.now.to_fs(:db)} FfcrmMailchimp::OutboundSync: unsubscribing #{email} from all mailchimp lists.")
ffcrm_list_ids.each do |list_id|
FfcrmMailchimp::Api.unsubscribe(list_id, email)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ffcrm_mailchimp/refresh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def clear_crm_mailchimp_data!
def subscribe_contact(member)
params = FfcrmMailchimp::WebhookParams.new_from_api(member).to_h
params.merge!( type: 'subscribe' )
Rails.logger.info("#{Time.now.to_s(:db)} FfcrmMailchimp: subscribing #{member["email_address"]} to list #{member["list_id"]}")
Rails.logger.info("#{Time.now.to_fs(:db)} FfcrmMailchimp: subscribing #{member["email_address"]} to list #{member["list_id"]}")
InboundSync.process(params)
end

Expand Down