Sha256: 1c0703ee3242ee73bc3bc6965391d25418cd812489ad0c3073bca9ca09a82a2c

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

module FatFreeCRM
  module Cloudfuji
    module EventObservers
      class LeadScoringObserver < ::Cloudfuji::EventObserver
        # Fire for all events
        def catch_all
          data  = params['data']
          email = data['email'] || data['recipient']
          # Look up Lead by email address
          if lead = Lead.find_by_email(email)
            event_name = "#{params['category']}_#{params['event']}"

            LeadScoringRule.find_all_by_event(event_name).each do |rule|
              # Find how many times this rule has been applied to this Lead
              count = LeadScoringRuleCount.find_by_lead_id_and_lead_scoring_rule_id(lead, rule) ||
                      LeadScoringRuleCount.new(:lead => lead, :lead_scoring_rule => rule)

              # Don't apply this rule more than once if :once flag is set
              unless rule.once && count.count > 0
                # If :match is present, only apply the rule if data matches string
                if rule.match.blank? || params['data'].inspect.include?(rule.match)
                  lead.without_versioning do
                    lead.update_attribute :score, lead.score + rule.points
                  end
                  # Add history event to lead, to record change of score
                  lead.versions.create! :event => "Rule for '#{event_name}': Score changed by #{rule.points} points. (New total: #{lead.score})"
                  # Increment and save count of rule/lead applications
                  count.count += 1
                  count.save
                end
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ffcrm_cloudfuji-0.2.1 lib/fat_free_crm/cloudfuji/event_observers/lead_scoring_observer.rb
ffcrm_cloudfuji-0.2.0 lib/fat_free_crm/cloudfuji/event_observers/lead_scoring_observer.rb