Class: PartyActivityCreator

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

Overview

Creates an Activity for a Party (Customer or Contact) for a given task type.

Behavior preserved from the legacy Party#create_activity helper:

  • returns nil if the party has no customer (e.g. orphaned contact)
  • returns nil if the task type is unknown
  • by default, dedupes by skipping when an open activity of the same type
    already exists on the customer (override with skip_if_exists: false)
  • resolves the assigned resource via the supplied option, the activity
    type's own assignment logic, or the customer's sales manager
  • rolls the target date to the next working day for the assignee

Flat name (not Activity::Creator) because Activity is an AR model and
Zeitwerk would otherwise create a top-level Activity module.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(party, task_type, **options) ⇒ PartyActivityCreator

Returns a new instance of PartyActivityCreator.

Parameters:

  • party (Party)

    the customer/prospect the activity is for

  • task_type (String)

    the activity task type code

  • options (Hash)

    Options hash

Options Hash (**options):

  • :skip_if_exists (Boolean)

    skip creation when a matching open activity already exists (default true)

  • :description (String)

    activity description (defaults to the activity type's)

  • :notes (String)

    activity notes

  • :resource (ApplicationRecord)

    the resource the activity is attached to

  • :assigned_resource (Resource)

    explicit assignee (overrides activity type logic)



29
30
31
32
33
# File 'app/services/party_activity_creator.rb', line 29

def initialize(party, task_type, **options)
  @party = party
  @task_type = task_type
  @options = options
end

Class Method Details

.call(party, task_type) ⇒ Object



17
18
19
# File 'app/services/party_activity_creator.rb', line 17

def self.call(party, task_type, **)
  new(party, task_type, **).call
end

Instance Method Details

#callObject



35
36
37
38
39
40
41
42
43
# File 'app/services/party_activity_creator.rb', line 35

def call
  return unless customer_present?

  activity_type = ActivityType.find_by(task_type: @task_type)
  return unless activity_type
  return if duplicate_open_activity?(activity_type)

  persist_activity!(activity_type)
end