app/models/kits/mailchimp_kit.rb in artfully_ose-1.1.0 vs app/models/kits/mailchimp_kit.rb in artfully_ose-1.2.0.alpha.1

- old
+ new

@@ -5,10 +5,11 @@ acts_as_kit do self.configurable = true state_machine do state :cancelled, :enter => :kit_cancelled + event(:submit_for_approval) { transitions :from => :fresh, :to => :pending } end when_active do end end @@ -49,47 +50,53 @@ false end end def lists - gibbon.lists["data"].map do |list| + gibbon.lists({:start => 0, :limit=> 100})["data"].map do |list| [list["name"], list["id"]] end end def list_attached?(list_id) attached_lists.any? { |list| list[:list_id] == list_id } end def change_lists(new_list_ids) + added_list_names = [] + added_list_ids = [] + removed_list_names = [] lists.each do |list| list_id = list[1] if !list_attached?(list_id) && new_list_ids.include?(list_id) add_list(list_id) + added_list_ids << list_id + added_list_names << find_list_name(list_id) elsif list_attached?(list_id) && !new_list_ids.include?(list_id) remove_list(list_id) + removed_list_names << find_list_name(list_id) end end + send_updated_lists_email(added_list_ids, added_list_names, removed_list_names) end def add_list(list_id) - list_name = lists.find { |list| list[1] == list_id }[0] - self.attached_lists = attached_lists.reject { |list| list.empty? } attached_lists << { :list_id => list_id, - :list_name => list_name + :list_name => find_list_name(list_id) } - save - Delayed::Job.enqueue MailchimpSyncJob.new(self, :type => :initial_sync, :list_id => list_id), :queue => QUEUE end + def send_updated_lists_email(added_list_ids, added_list_names, removed_list_names) + Delayed::Job.enqueue MailchimpSyncJob.new(self, :type => :initial_sync, :list_ids => added_list_ids, :added_list_names => added_list_names, :removed_list_names => removed_list_names), :queue => QUEUE + end + def remove_list(list_id) self.attached_lists = attached_lists.reject { |list| list[:list_id] == list_id } save - Delayed::Job.enqueue MailchimpSyncJob.new(self, :type => :list_removal, :list_id => list_id), :queue => QUEUE end def create_webhooks(list_id) gibbon.list_webhook_add({ :id => list_id, @@ -123,11 +130,13 @@ :first_name => member["First Name"], :last_name => member["Last Name"], :email => member["Email Address"], :skip_sync_to_mailchimp => true, :subscribed_lists => [list_id] - }) + }) do |person| + person.skip_commit = true + end note = person.notes.build({ :text => "Imported from MailChimp", :occurred_at => Time.now }) note.organization_id = organization_id @@ -245,10 +254,20 @@ person.skip_sync_to_mailchimp = true person.save person end + def sync_mailchimp_webhook_cleaned(list_id, data) + person = organization.people.find_by_email(data["email"]) + return if person.nil? + + reason = (data["reason"] == "hard" ? "a hard bounce" : "abuse") + person.subscribed_lists.delete(list_id) + person.new_note("MailChimp cleaned #{person.email} from #{list_name(list_id)} because of #{reason}.", Time.now, nil, organization_id) + person.save + end + def sync_mailchimp_webhook_update_person_email(list_id, data) person = organization.people.find_by_email(data["old_email"]) return if person.nil? || person.do_not_email? person.update_attributes(:email => data["new_email"], :skip_sync_to_mailchimp => true) @@ -269,31 +288,35 @@ end def sync_mailchimp_webhook_member_unsubscribe(list_id, data) person = organization.people.find_by_email(data["email"]) return unless person - note = person.notes.build({ - :text => "Unsubscribed in MailChimp from #{list_name(list_id)}", - :occurred_at => Time.now - }) - note.organization_id = organization_id - note.save + person.new_note("Unsubscribed in MailChimp from #{list_name(list_id)}", Time.now, nil, organization_id) person.subscribed_lists.delete(list_id) person.save end def sync_mailchimp_webhook_campaign_sent(list_id, data) occurred_at = Time.now + + emails = [] + + begin + response = gibbon.campaign_members(:cid => data["id"]) + response["data"].each do |user| + emails << user["email"] + end + end while response["total"] > emails.count + organization.people.each do |person| next if !person.subscribed_lists.include?(list_id) - hear_action = HearAction.new - hear_action.set_params({ - :details => %{"#{data["subject"].truncate(25)}" delivered to #{list_name(list_id)} MailChimp list.}, - :occurred_at => occurred_at, - :subtype => "Email (Sent)" - }, person) - hear_action.organization_id = organization_id + next if !emails.include?(person.email) + hear_action = HearAction.for_organization(organization) + hear_action.details = %{"#{data["subject"].truncate(25)}" delivered to #{list_name(list_id)} MailChimp list.} + hear_action.occurred_at = occurred_at + hear_action.subtype = "Mailchimp (Sent)" + hear_action.person = person hear_action.save end end def sync_artfully_person_update(person_id, person_changes) @@ -351,11 +374,11 @@ @mailchimp_list_members ||= members.collect do |member| member_hash = {} mailchimp_attributes.inject({}) do |member_hash, attribute| - member_hash[attribute] = member[headers.index(attribute)] + member_hash[attribute] = member[headers.index(attribute)] unless headers.index(attribute).nil? member_hash end end end @@ -442,7 +465,11 @@ response = gibbon.list_subscribe({ :id => list_id, :email_address => email, :merge_vars => { "FNAME" => first_name, "LNAME" => last_name } }) + end + + def find_list_name(list_id) + lists.find { |list| list[1] == list_id }[0] end end