Class: Query::CustomerRelatedCommunications

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

Overview

Builds the seven-way Communication union for a Customer:

  1. recipient_party_id IN (customer + active contact ids)
  2. sender_party_id IN (same scope)
  3. CommunicationRecipient via contact_points belonging to that scope
  4. resource = customer's Quotes
  5. resource = customer's Orders
  6. resource = customer's Opportunities
  7. resource = the Customer record itself

Returns a Communication relation usable in further .where / .order calls.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(customer) ⇒ CustomerRelatedCommunications

Returns a new instance of CustomerRelatedCommunications.



18
19
20
# File 'app/services/query/customer_related_communications.rb', line 18

def initialize(customer)
  @customer = customer
end

Class Method Details

.call(customer) ⇒ Object



14
15
16
# File 'app/services/query/customer_related_communications.rb', line 14

def self.call(customer)
  new(customer).call
end

Instance Method Details

#callObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/query/customer_related_communications.rb', line 22

def call
  party_ids = @customer.self_and_contacts_party_ids_arr
  Communication.union(
    Communication.where(recipient_party_id: party_ids),
    Communication.where(sender_party_id: party_ids),
    via_contact_points(party_ids),
    Communication.where(resource_type: 'Quote', resource_id: @customer.quotes.select(:id)),
    Communication.where(resource_type: 'Order', resource_id: @customer.orders.select(:id)),
    Communication.where(resource_type: 'Opportunity', resource_id: @customer.opportunities.select(:id)),
    Communication.where(resource_type: 'Customer', resource_id: @customer.id)
  )
end