Class: Webhookdb::Replicator::EmailOctopusContactV1

Inherits:
Base
  • Object
show all
Includes:
Appydays::Loggable
Defined in:
lib/webhookdb/replicator/email_octopus_contact_v1.rb

Defined Under Namespace

Classes: ContactBackfiller

Constant Summary collapse

CONV_REMOTE_KEY =
Webhookdb::Replicator::Column::IsomorphicProc.new(
  ruby: ->(_, resource:, **_) { "#{resource.fetch('id')}-#{resource.fetch('list_id')}" },
  # Because this is a non-nullable key, we never need this in SQL
  sql: ->(_) { Sequel.lit("'do not use'") },
)

Constants inherited from Base

Base::MAX_INDEX_NAME_LENGTH

Constants included from DBAdapter::ColumnTypes

DBAdapter::ColumnTypes::BIGINT, DBAdapter::ColumnTypes::BIGINT_ARRAY, DBAdapter::ColumnTypes::BOOLEAN, DBAdapter::ColumnTypes::COLUMN_TYPES, DBAdapter::ColumnTypes::DATE, DBAdapter::ColumnTypes::DECIMAL, DBAdapter::ColumnTypes::DOUBLE, DBAdapter::ColumnTypes::FLOAT, DBAdapter::ColumnTypes::INTEGER, DBAdapter::ColumnTypes::INTEGER_ARRAY, DBAdapter::ColumnTypes::OBJECT, DBAdapter::ColumnTypes::TEXT, DBAdapter::ColumnTypes::TEXT_ARRAY, DBAdapter::ColumnTypes::TIMESTAMP, DBAdapter::ColumnTypes::UUID

Instance Attribute Summary

Attributes inherited from Base

#service_integration

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#_any_subscriptions_to_notify?, #_backfill_state_change_fields, #_clear_backfill_information, #_clear_webook_information, #_coalesce_excluded_on_update, #_enqueue_backfill_jobs, #_extra_index_specs, #_fetch_enrichment, #_find_dependency_candidate, #_notify_dependents, #_parallel_backfill, #_prepare_for_insert, #_publish_rowupsert, #_resource_to_data, #_store_enrichment_body?, #_to_json, #_upsert_conflict_target, #_upsert_update_expr, #_upsert_webhook_single_resource, #_verify_backfill_err_msg, #_webhook_state_change_fields, #admin_dataset, #avoid_writes?, #backfill, #backfill_not_supported_message, #calculate_and_backfill_state_machine, #calculate_dependency_state_machine_step, #calculate_preferred_create_state_machine, #calculate_webhook_state_machine, chunked_row_update_bounds, #clear_backfill_information, #clear_webhook_information, #create_table, #create_table_modification, #create_table_partitions, #data_column, #dbadapter_table, #denormalized_columns, #descriptor, #dispatch_request_to, #documentation_url, #enqueue_sync_targets, #enrichment_column, #ensure_all_columns, #ensure_all_columns_modification, #existing_partitions, #find_dependent, #find_dependent!, #indices, #initialize, #on_backfill_error, #partition?, #partitioning, #preferred_create_state_machine_method, #preprocess_headers_for_logging, #primary_key_column, #process_state_change, #process_webhooks_synchronously?, #qualified_table_sequel_identifier, #readonly_dataset, #remote_key_column, #requires_sequence?, #resource_name_plural, #resource_name_singular, #schema_and_table_symbols, #storable_columns, #synchronous_processing_response_body, #timestamp_column, #upsert_has_deps?, #upsert_webhook, #upsert_webhook_body, #verify_backfill_credentials, #webhook_endpoint, #webhook_response, #with_advisory_lock

Constructor Details

This class inherits a constructor from Webhookdb::Replicator::Base

Class Method Details

.descriptorWebhookdb::Replicator::Descriptor



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/webhookdb/replicator/email_octopus_contact_v1.rb', line 9

def self.descriptor
  return Webhookdb::Replicator::Descriptor.new(
    name: "email_octopus_contact_v1",
    ctor: ->(sint) { Webhookdb::Replicator::EmailOctopusContactV1.new(sint) },
    feature_roles: [],
    resource_name_singular: "Email Octopus Contact",
    dependency_descriptor: Webhookdb::Replicator::EmailOctopusListV1.descriptor,
    supports_backfill: true,
    api_docs_url: "https://emailoctopus.com/api-documentation",
  )
end

Instance Method Details

#_backfillersObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/webhookdb/replicator/email_octopus_contact_v1.rb', line 112

def _backfillers
  list_sint = self.service_integration.depends_on
  api_key = list_sint.replicator.backfill_key!
  backfillers = list_sint.replicator.admin_dataset(timeout: :fast) do |list_ds|
    list_ds.select(:email_octopus_id).map do |list|
      ContactBackfiller.new(
        contact_svc: self,
        list_id: list[:email_octopus_id],
        api_key:,
      )
    end
  end
  return backfillers
end

#_denormalized_columnsObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/webhookdb/replicator/email_octopus_contact_v1.rb', line 37

def _denormalized_columns
  return [
    Webhookdb::Replicator::Column.new(:email_octopus_id, TEXT, data_key: "id"),
    Webhookdb::Replicator::Column.new(:email_octopus_list_id, TEXT, data_key: "list_id"),
    Webhookdb::Replicator::Column.new(:email_address, TEXT),
    Webhookdb::Replicator::Column.new(:status, TEXT),
    Webhookdb::Replicator::Column.new(:created_at, TIMESTAMP, index: true, converter: :time, skip_nil: true),
    Webhookdb::Replicator::Column.new(:deleted_at, TIMESTAMP, converter: :time, optional: true),
    Webhookdb::Replicator::Column.new(:row_updated_at, TIMESTAMP, defaulter: :now, optional: true),
  ]
end

#_remote_key_columnObject



27
28
29
30
31
32
33
34
35
# File 'lib/webhookdb/replicator/email_octopus_contact_v1.rb', line 27

def _remote_key_column
  return Webhookdb::Replicator::Column.new(
    :compound_identity,
    TEXT,
    data_key: "<compound key, see converter>",
    optional: true,
    converter: CONV_REMOTE_KEY,
  )
end

#_resource_and_event(request) ⇒ Object



80
81
82
# File 'lib/webhookdb/replicator/email_octopus_contact_v1.rb', line 80

def _resource_and_event(request)
  return request.body, nil
end

#_timestamp_column_nameObject



88
89
90
# File 'lib/webhookdb/replicator/email_octopus_contact_v1.rb', line 88

def _timestamp_column_name
  return :row_updated_at
end

#_update_where_exprObject



84
85
86
# File 'lib/webhookdb/replicator/email_octopus_contact_v1.rb', line 84

def _update_where_expr
  return self.qualified_table_sequel_identifier[:data] !~ Sequel[:excluded][:data]
end

#_upsert_webhook(request, upsert: true) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/webhookdb/replicator/email_octopus_contact_v1.rb', line 49

def _upsert_webhook(request, upsert: true)
  return super unless request.body.is_a?(Array)

  # If the body is an array, it means we are upserting data from webhooks, which has been passed to this function by
  # the event replicator, and we have to transform the data in order to be able to upsert.
  new_bodies = request.body.
    # Events older than 30 days may not have occurred_at on free plans
    select { |wh| wh.key?("occurred_at") }.
    map do |wh|
      event_type = wh.fetch("type")
      {
        "id" => wh.fetch("contact_id"),
        "list_id" => wh.fetch("list_id"),
        "email_address" => wh.fetch("contact_email_address"),
        "status" => wh.fetch("contact_status"),
        "row_updated_at" => wh.fetch("occurred_at"),
        "created_at" => event_type == "contact.created" ? wh.fetch("occurred_at") : nil,
        "deleted_at" => event_type == "contact.deleted" ? wh.fetch("occurred_at") : nil,
        # These fields do not get denormalized but we still want the info
        # to be present in the "data" field of the row.
        "fields" => wh["contact_fields"],
        "tags" => wh["contact_tags"],
      }
    end
  new_bodies.each do |b|
    new_request = request.dup
    new_request.body = b
    super(new_request, upsert:)
  end
end

#_webhook_response(_request) ⇒ Object



92
93
94
# File 'lib/webhookdb/replicator/email_octopus_contact_v1.rb', line 92

def _webhook_response(_request)
  return Webhookdb::WebhookResponse.ok
end

#calculate_backfill_state_machineObject



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/webhookdb/replicator/email_octopus_contact_v1.rb', line 96

def calculate_backfill_state_machine
  if (step = self.calculate_dependency_state_machine_step(dependency_help: ""))
    return step
  end
  step = Webhookdb::Replicator::StateMachineStep.new
  # We're using the API Key from the dependency, we don't need to ask for it here
  step.output = %(Great! We are going to start replicating your #{self.resource_name_plural}.
#{self._query_help_output}
  )
  return step.completed
end

#on_dependency_webhook_upsert(_replicator, _payload) ⇒ Object



108
109
110
# File 'lib/webhookdb/replicator/email_octopus_contact_v1.rb', line 108

def on_dependency_webhook_upsert(_replicator, _payload, *)
  return
end