Class: Query::ContactRelatedCommunications

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

Overview

Builds the two-source Communication union for a Contact:

  • direct: recipient_party_id within the contact's customer scope (or self
    when the contact has no customer)
  • via recipients: CommunicationRecipient -> contact_point belonging to the
    same scope

Differs from Query::CustomerRelatedCommunications (no senders, no quote/order
resource joins). Returns a Communication relation usable in further .where
/ .order calls; uses a SQL UNION so callers can paginate over a single
select_from() relation.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contact) ⇒ ContactRelatedCommunications

Returns a new instance of ContactRelatedCommunications.



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

def initialize(contact)
  @contact = contact
end

Class Method Details

.call(contact) ⇒ Object



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

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

Instance Method Details

#callObject



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

def call
  party_ids = scope_party_ids
  direct = Communication.where(recipient_party_id: party_ids)
  via_recipients = Communication.where(
    id: CommunicationRecipient.joins(:contact_point)
                              .where(contact_points: { party_id: party_ids })
                              .select(:communication_id)
  )

  Communication.from(
    "(#{direct.to_sql} UNION #{via_recipients.to_sql}) AS communications"
  )
end