Class: DailyFocusAnalysisRepWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job, Workers::StatusBroadcastable
Defined in:
app/workers/daily_focus_analysis_rep_worker.rb

Overview

Generates a Daily Focus Analysis for a sales rep (and optional covered reps) via Sunny tools.

Uses Assistant::ChatService + sales_management tools (unified path — no pre-injected CRM dumps).
Enqueued by DailyFocusAnalysisWorker per RunPolicy target, or from CRM generate with optional args.

Constant Summary collapse

MANAGER_ROLES =

Default (sales) manager roles for ownership of sales briefings.

DailyFocus::ConversationSharing::SALES_LEADERSHIP_ROLES

Instance Attribute Summary

Attributes included from Workers::StatusBroadcastable

#broadcast_status_updates

Instance Method Summary collapse

Methods included from Workers::StatusBroadcastable::Overrides

#at, #store, #total

Instance Method Details

#perform(employee_id, conversation_id = nil, covered_employee_ids = [], initiator = 'system') ⇒ void

This method returns an undefined value.

Parameters:

  • employee_id (Integer)

    Primary sales rep receiving the briefing

  • conversation_id (Integer, nil) (defaults to: nil)

    Existing conversation to reuse

  • covered_employee_ids (Array<Integer>) (defaults to: [])

    Reps folded into this run (OOO + backup)

  • initiator (String) (defaults to: 'system')

    'system' for scheduled/reaper runs, 'user' when a
    manager clicked Generate on the review page — recorded on the run's
    AiUsageLog rows so the dashboard can tell scheduled from user activity



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/workers/daily_focus_analysis_rep_worker.rb', line 24

def perform(employee_id, conversation_id = nil, covered_employee_ids = [], initiator = 'system')
  total 3 if jid.present?
  at 1, 'Preparing Daily Focus...' if jid.present?

  employee = Employee.find_by(id: employee_id)
  return unless employee

  covered_ids = Array(covered_employee_ids).flatten.map(&:to_i).uniq - [employee.id]
  covered_employees = Employee.where(id: covered_ids).includes(:employee_record).to_a

  conversation =
    if conversation_id
      AssistantConversation.find_by(id: conversation_id)
    else
      ensure_conversation_for(employee, covered_ids)
    end
  return unless conversation

  at 2, "Generating Daily Focus for #{employee.full_name}..." if jid.present?
  run_analysis(conversation, employee, covered_employees, initiator)
  if jid.present?
    at 3, 'Daily Focus ready for review'
    store(
      redirect_to: Rails.application.routes.url_helpers.assistant_path(conversation),
      info_message: "Daily Focus is ready for #{employee.full_name}."
    )
  end

  Rails.logger.info("[DailyFocusAnalysisRepWorker] Generated daily focus for #{employee.full_name} " \
                    "(conversation #{conversation.id}, pending review)")
rescue StandardError => e
  store(error_message: 'Daily Focus generation failed. Please try again.') if jid.present?
  Rails.logger.error("[DailyFocusAnalysisRepWorker] Failed for employee #{employee_id}: #{e.message}\n#{e.backtrace.first(5).join("\n")}")
  ErrorReporting.error(e, source: :background, context: {
    employee_id: employee_id,
    conversation_id: conversation_id,
    worker: 'DailyFocusAnalysisRepWorker'
  })
end