Sha256: fba15fc50e60cf585536266718df62d82735e02ad041a61660b4b20d1609baf7

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

module FatFreeCRM
  module Cloudfuji
    module EventObservers
      class CustomerObserver < ::Cloudfuji::EventObserver
        # "customer_created"
        # :account_balance => 0
        # :object          => "customer"
        # :email           => "s+cfdemo@cloudfuji.com"
        # :created         => 1332269951
        # :id              => "cus_cpkg4h0KfLD3lp"
        # :livemode        => true
        # :human           => "Customer CREATED (cus_cpkg4h0KfLD3lp), s+cfdemo@cloudfuji.com"}
        def customer_created
          note_customer_activity("#{data['email']} created as a customer with external id #{data['id']}") if data['livemode']
        end

        def customer_signed_up
          note_customer_activity("#{data['first_name']} #{data['last_name']} (#{data['email']}) signed up as a customer")
        end

        private

        def note_customer_activity(message)
          subject = find_or_create_activity_subject!
          puts "Found subject: #{subject.inspect}"

          subject.versions.create! :event => message
        end

        def data
          params['data']
        end

        def find_or_create_activity_subject!
          lookups = [Account, Lead, Contact]
          lookups.each do |model|
            puts "#{model}.find_by_email( #{data['email']} )"
            result = model.find_by_email(data['email'])
            return result if result
          end

          lead = Lead.find_by_email(data['email'])
          lead ||= Lead.new

          lead.email        = data['email']
          lead.first_name ||= data['first_name'] || recipient.split("@").first if lead.first_name.blank?
          lead.last_name  ||= data['last_name']  || recipient.split("@").last  if lead.last_name.blank?

          lead.save

          lead
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ffcrm_cloudfuji-0.1.5 lib/fat_free_crm/cloudfuji/event_observers/customer_observer.rb
ffcrm_cloudfuji-0.1.4 lib/fat_free_crm/cloudfuji/event_observers/customer_observer.rb
ffcrm_cloudfuji-0.1.3 lib/fat_free_crm/cloudfuji/event_observers/customer_observer.rb