Class: Webhookdb::Replicator::IntercomContactV1

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

Constant Summary

Constants included from IntercomV1Mixin

Webhookdb::Replicator::IntercomV1Mixin::QUESTIONABLE_TIMESTAMP

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 included from IntercomV1Mixin

#_fetch_backfill_page, #_timestamp_column_name, #_update_where_expr, #_webhook_response, #auth_credentials?, #calculate_backfill_state_machine, #find_auth_integration, #intercom_auth_headers, #on_dependency_webhook_upsert

Methods inherited from Base

#_any_subscriptions_to_notify?, #_backfill_state_change_fields, #_backfillers, #_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?, #_timestamp_column_name, #_to_json, #_update_where_expr, #_upsert_webhook, #_verify_backfill_err_msg, #_webhook_response, #_webhook_state_change_fields, #admin_dataset, #backfill, #backfill_not_supported_message, #calculate_and_backfill_state_machine, #calculate_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, #data_column, #dbadapter_table, #denormalized_columns, #descriptor, #dispatch_request_to, #documentation_url, #enqueue_sync_targets, #enrichment_column, #ensure_all_columns, #ensure_all_columns_modification, #find_dependent, #find_dependent!, #indices, #initialize, #on_dependency_webhook_upsert, #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

Constructor Details

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

Class Method Details

.descriptorObject



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

def self.descriptor
  return Webhookdb::Replicator::Descriptor.new(
    name: "intercom_contact_v1",
    ctor: self,
    feature_roles: ["intercom"],
    resource_name_singular: "Intercom Contact",
    dependency_descriptor: Webhookdb::Replicator::IntercomMarketplaceRootV1.descriptor,
    supports_backfill: true,
    api_docs_url: "https://developers.intercom.com/docs/references/rest-api/api.intercom.io/Contacts/",
  )
end

Instance Method Details

#_denormalized_columnsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/webhookdb/replicator/intercom_contact_v1.rb', line 26

def _denormalized_columns
  return [
    # All of these fields are missing on delete.
    # We merge the deleted info into an existing one when handling the upsert.
    Webhookdb::Replicator::Column.new(:external_id, TEXT, optional: true, index: true),
    Webhookdb::Replicator::Column.new(:email, TEXT, optional: true, index: true),
    Webhookdb::Replicator::Column.new(
      :created_at, TIMESTAMP, converter: QUESTIONABLE_TIMESTAMP, optional: true, index: true,
    ),
    Webhookdb::Replicator::Column.new(
      :updated_at, TIMESTAMP, converter: QUESTIONABLE_TIMESTAMP, optional: true, index: true,
    ),
    # This is set in the contact.deleted webhook
    Webhookdb::Replicator::Column.new(:deleted_at, TIMESTAMP, optional: true),
    # This is set in the contact.archived webhook
    Webhookdb::Replicator::Column.new(:archived_at, TIMESTAMP, optional: true),
  ]
end

#_mixin_backfill_urlObject



45
# File 'lib/webhookdb/replicator/intercom_contact_v1.rb', line 45

def _mixin_backfill_url = "https://api.intercom.io/contacts"

#_remote_key_columnObject



22
23
24
# File 'lib/webhookdb/replicator/intercom_contact_v1.rb', line 22

def _remote_key_column
  return Webhookdb::Replicator::Column.new(:intercom_id, TEXT, data_key: "id")
end

#_resource_and_event(request) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/webhookdb/replicator/intercom_contact_v1.rb', line 47

def _resource_and_event(request)
  resource, event = super
  return resource, nil if event.nil?
  # noinspection RubyCaseWithoutElseBlockInspection
  case event.fetch("topic")
      when "contact.deleted"
        resource["updated_at"] = Time.now
        resource["deleted_at"] = Time.now
      when "contact.archived"
        resource["updated_at"] = Time.now
        resource["archived_at"] = Time.now
      when "contact.unsubscribed"
        resource = resource.fetch("contact")
    end
  return resource, event
end

#_upsert_update_expr(inserting, enrichment: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/webhookdb/replicator/intercom_contact_v1.rb', line 64

def _upsert_update_expr(inserting, enrichment: nil)
  full_update = super
  # In the case of a delete or archive, update the deleted_at/archived_at field,
  # and merge 'deleted' or 'archived' into the :data field.
  if inserting[:deleted_at]
    status_key = :deleted_at
    status_field = "deleted"
  elsif inserting[:archived_at]
    status_key = :archived_at
    status_field = "archived"
  else
    return full_update
  end
  result = {updated_at: full_update.fetch(:updated_at)}
  result[status_key] = full_update.fetch(status_key)
  data_col = Sequel[self.service_integration.table_name.to_sym][:data]
  result[:data] = Sequel.join([data_col, Sequel.lit("'{\"#{status_field}\":true}'::jsonb")])
  return result
end