Class: Customer::SalesRepAssigner

Inherits:
Object
  • Object
show all
Defined in:
app/services/customer/sales_rep_assigner.rb

Constant Summary collapse

SALES_REPS_SELF_ASSIGN_ON_CUSTOMER_CREATE_CATALOG_IDS =
[1, 2].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(customer, options = nil) ⇒ SalesRepAssigner

customer - An instance of Customer
options:
skip_lead: set to true if you do not want a lead activity created
force_assign: set to true if you want to override currently assigned reps
otherwise the current rep will be left untouched.



11
12
13
14
15
16
17
# File 'app/services/customer/sales_rep_assigner.rb', line 11

def initialize(customer, options = nil)
  @customer = customer
  raise 'Customer Store is not set, assigner cannot be used.' if @customer.store.nil?

  @options = options || {}
  @logger = @options[:logger] || Rails.logger
end

Class Method Details

.auto_assign(customer, options = {}) ⇒ Object

class factory convenience method, see initializer for parameters



28
29
30
31
# File 'app/services/customer/sales_rep_assigner.rb', line 28

def self.auto_assign(customer, options = {})
  assigner = new(customer, options)
  assigner.auto_assign
end

.can_self_assign?(customer, current_user) ⇒ Boolean

Condition check to determine if a rep can self assign themselves on an account on creation

Returns:

  • (Boolean)


73
74
75
# File 'app/services/customer/sales_rep_assigner.rb', line 73

def self.can_self_assign?(customer, current_user)
  (current_user.is_sales_rep? && SALES_REPS_SELF_ASSIGN_ON_CUSTOMER_CREATE_CATALOG_IDS.include?(customer.catalog_id) || current_user.is_sales_manager?)
end

.check_for_self_assign(customer, current_user) ⇒ Object

Some catalog allow for rep to self assign themselves to an account on creation (US and Canada) but others are more restrictive, this method
checks for this condition and nilify the rep in the customer if it is not self assignable.
Note: this probably could belong in a step by step wizard on customer creation where the list
of possible reps appear beforehand rather than this trickery done after the fact.



62
63
64
65
66
67
68
69
70
# File 'app/services/customer/sales_rep_assigner.rb', line 62

def self.check_for_self_assign(customer, current_user)
  unless can_self_assign?(customer, current_user)
    # msg = "Reseting customer sales reps since current user #{current_user.id} is a sales rep, but not a sales manager, and the customer catalog is marked to not accept self assignment on customer create"
    customer.primary_sales_rep_id = nil
    customer.secondary_sales_rep_id = nil
    customer.local_sales_rep_id = nil
  end
  customer
end

Instance Method Details

#assign_reps(sales_rep_assignment_options = {}) ⇒ Object



51
52
53
54
55
56
# File 'app/services/customer/sales_rep_assigner.rb', line 51

def assign_reps(sales_rep_assignment_options = {})
  return unless sales_rep_assignment_options.present?

  sales_rep_changer = Customer::SalesRepChanger.new(@customer, skip_activity_creation: @options[:skip_lead])
  sales_rep_changer.assign_reps sales_rep_assignment_options
end

#auto_assignObject

Instance method which will look at all the sales rep entries and find the rep to assign to the
customer. Note that if any reps are present this method will do nothing unless the :force_assign option
is passed on initialization



36
37
38
39
40
41
42
43
44
45
# File 'app/services/customer/sales_rep_assigner.rb', line 36

def auto_assign
  return false if @customer.do_not_auto_assign
  return false unless @customer.sales_reps.empty? || @options[:force_assign]

  sales_rep_assignment_options = determine_sales_rep
  @logger.debug("Customer::SalesRepAssigner#assign",
                customer_id: @customer.id,
                rep_ids: sales_rep_assignment_options&.values&.flatten&.compact)
  assign_reps(sales_rep_assignment_options)
end

#determine_sales_repObject



47
48
49
# File 'app/services/customer/sales_rep_assigner.rb', line 47

def determine_sales_rep
  @customer.store.get_next_sales_rep_assignments_for_customer(@customer)
end

#determine_sales_rep_queue_entryObject



19
20
21
# File 'app/services/customer/sales_rep_assigner.rb', line 19

def determine_sales_rep_queue_entry
  @customer.store&.sales_rep_queue&.first_eligible_sales_queue_entry(@customer)
end

#effective_queue_descriptionObject



23
24
25
# File 'app/services/customer/sales_rep_assigner.rb', line 23

def effective_queue_description
  determine_sales_rep_queue_entry.try(:description) || 'No Sales Rep Queue Entry Found'
end