Class: WebhookProcessors::SendgridProcessor

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

Overview

Processor for SendGrid Event Webhook callbacks
Handles email delivery events (delivered, bounced, opened, clicked, etc.)

Reference: https://www.twilio.com/docs/sendgrid/for-developers/tracking-events/event

This processor delegates to the existing Communication::EventParser
which handles EmailPreference updates, CommunicationRecipient state transitions,
and WebhookEvent creation.

Examples:

WebhookProcessors::SendgridProcessor.call(webhook_log)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(webhook_log) ⇒ SendgridProcessor

Returns a new instance of SendgridProcessor.



21
22
23
24
# File 'app/services/webhook_processors/sendgrid_processor.rb', line 21

def initialize(webhook_log)
  @webhook_log = webhook_log
  @data = webhook_log.data.deep_symbolize_keys
end

Class Method Details

.call(webhook_log) ⇒ Object



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

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

Instance Method Details

#callObject



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
# File 'app/services/webhook_processors/sendgrid_processor.rb', line 26

def call
  event_type = @data[:event]&.to_s&.downcase
  email = @data[:email]
  unique_id = @data[:unique_id] || @data[:sg_message_id]

  Rails.logger.info "[SendgridProcessor] Processing #{event_type} event for #{email}"

  # Delegate to existing EventParser for actual processing
  result = Communication::EventParser.new.process(@data.stringify_keys)

  # Build response data
  response = {
    event_type: event_type,
    email: email,
    unique_id: unique_id,
    processed: result != false
  }

  # Try to resolve the CommunicationRecipient for linking
  if unique_id.present? && email.present?
    cr = find_communication_recipient(email, unique_id)
    if cr
      # Update webhook_log with resource reference
      @webhook_log.update_columns(
        resource_type: 'CommunicationRecipient',
        resource_id: cr.id
      )
      response[:communication_recipient_id] = cr.id
      response[:communication_id] = cr.communication_id
    end
  end

  response
end