Class: CallRecordProcessing::VoicemailFollowup

Inherits:
Object
  • Object
show all
Defined in:
app/services/call_record_processing/voicemail_followup.rb

Overview

Creates the CRM follow-up artifacts for a voicemail CallRecord: a lead
Customer for an unmatched caller, and the follow-up Activity for the
(matched or newly-created) caller.

Two callers use this:

  • VoicemailsMailbox — on an inbound voicemail, when the caller already
    matches an existing Customer/Contact it creates an open follow-up
    Activity. Unmatched callers are left alone — no Customer is created up
    front any more.
  • The "Create Customer" button on the call record page — for an unmatched
    caller it creates the placeholder Customer and a completed Activity
    (the employee is acting on the voicemail right then, so there is no open
    task to leave behind).

Constant Summary collapse

FALLBACK_ASSIGNEE_ID =

Fallback assignee when no destination employee is matched (Elodie Pasek).

85

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(call_record) ⇒ VoicemailFollowup

Returns a new instance of VoicemailFollowup.

Parameters:

  • call_record (CallRecord)

    the voicemail record



25
26
27
# File 'app/services/call_record_processing/voicemail_followup.rb', line 25

def initialize(call_record)
  @call_record = call_record
end

Instance Attribute Details

#call_recordObject (readonly)

Returns the value of attribute call_record.



22
23
24
# File 'app/services/call_record_processing/voicemail_followup.rb', line 22

def call_record
  @call_record
end

Instance Method Details

#create_activity(completed: false) ⇒ Activity?

Creates the voicemail follow-up Activity for the caller. No-op unless the
caller resolves to a Customer or Contact.

Parameters:

  • completed (Boolean) (defaults to: false)

    when true the Activity is created already
    completed (result CMP + completion timestamp); otherwise it is left open
    as a follow-up task.

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/services/call_record_processing/voicemail_followup.rb', line 72

def create_activity(completed: false)
  caller = call_record.origin_party
  return unless caller.is_a?(Customer) || caller.is_a?(Contact)

  activity_type = ActivityType.find_by(task_type: 'VOICEMAIL')
  return unless activity_type

  target = working_target_datetime
  attrs = {
    activity_type: activity_type,
    new_note: build_note,
    target_datetime: target,
    original_target_datetime: target,
    assigned_resource: assigned_employee(activity_type, caller),
    call_record: call_record
  }
  if completed
    attrs[:activity_result_type_id] = ActivityResultTypeConstants::CMP
    attrs[:completion_datetime] = Time.current
  end

  activity = caller.activities.create!(attrs)
  Rails.logger.info "[VoicemailFollowup] Created Activity #{activity.id} for CallRecord #{call_record.id}"
  activity
rescue StandardError => e
  Rails.logger.error "[VoicemailFollowup] Failed to create activity for CallRecord #{call_record.id}: #{e.message}"
  nil
end

#create_customer(person_name: nil) ⇒ Customer, ...

Creates a +lead_qualify+ Customer for the caller and links it to the call
record. Returns the already-matched party untouched when one exists.

Parameters:

  • person_name (String, nil) (defaults to: nil)

    optional display name for the customer

Returns:



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
63
# File 'app/services/call_record_processing/voicemail_followup.rb', line 34

def create_customer(person_name: nil)
  return call_record.origin_party if call_record.origin_party
  return nil if call_record.origin_number.blank?

  customer = build_lead_customer(person_name)

  # Lock the call record and re-check inside the transaction so two
  # concurrent "Create Customer" clicks can't both insert a lead and leave
  # an orphan. The insert and the link share the savepoint, so a failed
  # link rolls the Customer back too.
  linked = nil
  created = false
  Customer.transaction(requires_new: true) do
    call_record.with_lock do
      if call_record.origin_party.present?
        linked = call_record.origin_party
      else
        customer.save!
        call_record.update!(origin_party: customer, origin_name: customer.full_name)
        linked = customer
        created = true
      end
    end
  end
  Rails.logger.info "[VoicemailFollowup] Created lead_qualify Customer #{customer.id} for #{call_record.origin_number}" if created
  linked
rescue StandardError => e
  Rails.logger.error "[VoicemailFollowup] Error creating customer for #{call_record.origin_number}: #{e.message}"
  nil
end