Module: Assistant::SalesManagement::Helpers

Defined in:
app/services/assistant/sales_management/helpers.rb

Overview

Shared lookup + formatting helpers used by more than one sub-builder.
apply_role_scope and resolve_employee are referenced by the
employee-domain tools and by the rep-* pipeline tools, so they live
here rather than being duplicated.

Class Method Summary collapse

Class Method Details

.apply_role_scope(scope, role) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'app/services/assistant/sales_management/helpers.rb', line 12

def apply_role_scope(scope, role)
  case role.to_s
  when 'sales_rep'             then scope.sales_reps
  when 'primary_sales_rep'     then scope.primary_sales_reps
  when 'secondary_sales_rep'   then scope.secondary_sales_reps
  when 'local_sales_rep'       then scope.local_sales_reps
  when 'engineer'              then scope.engineers
  when 'technical_support_rep' then scope.technical_support_reps
  else scope.by_role(role.to_s)
  end
end

.format_duration(seconds) ⇒ Object



45
46
47
48
49
50
51
# File 'app/services/assistant/sales_management/helpers.rb', line 45

def format_duration(seconds)
  return '0s' unless seconds&.positive?

  mins = seconds / 60
  secs = seconds % 60
  mins.positive? ? "#{mins}m #{secs}s" : "#{secs}s"
end

.resolve_employee(rep_id:, rep_name:) ⇒ Object

Returns [employee_or_nil, error_json_or_nil].



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/services/assistant/sales_management/helpers.rb', line 25

def resolve_employee(rep_id:, rep_name:)
  if rep_id.present?
    [Employee.find_by(id: rep_id), nil]
  elsif rep_name.present?
    matches = Employee.active_employees
                      .where('parties.full_name ILIKE ?', "%#{rep_name}%")
                      .limit(5).to_a
    if matches.size > 1
      names = matches.map { |match| "#{match.full_name} (ID: #{match.id})" }
      error = { error: "Multiple employees match '#{rep_name}': #{names.join(', ')}. " \
                       'Use find_employee or provide the rep_id to disambiguate.' }.to_json
      [nil, error]
    else
      [matches.first, nil]
    end
  else
    [nil, nil]
  end
end

.truncate_json(json_string) ⇒ Object



53
54
55
# File 'app/services/assistant/sales_management/helpers.rb', line 53

def truncate_json(json_string)
  Assistant::ChatToolBuilder.truncate_result(json_string)
end