Class: SesDashboard::WebhookEventPersistor

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

Overview

Persists a processed SNS event (from WebhookProcessor::Result) to the database.

Separating parsing (WebhookProcessor, in lib/) from persistence (here) keeps the parser free of Rails/AR dependencies and fully unit-testable.

Instance Method Summary collapse

Constructor Details

#initialize(project, result) ⇒ WebhookEventPersistor

Returns a new instance of WebhookEventPersistor.



8
9
10
11
# File 'app/services/ses_dashboard/webhook_event_persistor.rb', line 8

def initialize(project, result)
  @project = project
  @result  = result
end

Instance Method Details

#persistObject



13
14
15
16
17
18
19
20
21
22
23
# File 'app/services/ses_dashboard/webhook_event_persistor.rb', line 13

def persist
  return if @result.message_id.blank?

  ActiveRecord::Base.transaction do
    email = find_or_create_email
    create_event(email)
    update_email_state(email)
  end
rescue ActiveRecord::RecordInvalid => e
  log_error("Failed to persist webhook event: #{e.message}")
end