From 29080aae6dda22541a5b869a92e07f1938cd6ee9 Mon Sep 17 00:00:00 2001 From: mbianco-stripe <45374579+mbianco-stripe@users.noreply.github.com> Date: Thu, 16 Jun 2022 16:51:42 -0700 Subject: [PATCH] Refactor order poller, log masked SF key (#507) * Refactoring order poller to be more modular Makes debugging in prod easier * Log masked api key for easier debugging --- config/application.rb | 9 ++++++++- lib/stripe-force/constants.rb | 1 + lib/stripe-force/jobs/order_poller.rb | 27 +++++++++++++++------------ lib/stripe-force/jobs/poller_base.rb | 4 ---- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/config/application.rb b/config/application.rb index 74e72297b7..57b44f0cd7 100644 --- a/config/application.rb +++ b/config/application.rb @@ -52,6 +52,7 @@ class Application < Rails::Application SALESFORCE_ACCOUNT_ID_HEADER, SALESFORCE_PACKAGE_NAMESPACE_HEADER, SALESFORCE_INSTANCE_TYPE_HEADER, + SALESFORCE_PACKAGE_ID_HEADER, ] config.lograge.enabled = true @@ -63,7 +64,13 @@ class Application < Rails::Application SALESFORCE_HEADERS.each {|h| custom_params[h] = event.payload[:request].headers[h] } # include a boolean to indicate if the key was specified - custom_params[SALESFORCE_KEY_HEADER] = event.payload[:request].headers[SALESFORCE_KEY_HEADER].present? + sf_key = event.payload[:request].headers[SALESFORCE_KEY_HEADER] + custom_params[SALESFORCE_KEY_HEADER] = if sf_key.present? + # https://stackoverflow.com/questions/42938422/how-to-mask-all-but-last-four-characters-in-a-string + sf_key.gsub(/.(?=.{4})/, '*') + else + 'false' + end custom_params.reject {|_, v| v.blank? } end diff --git a/lib/stripe-force/constants.rb b/lib/stripe-force/constants.rb index 48f8f0c04b..25829f2670 100644 --- a/lib/stripe-force/constants.rb +++ b/lib/stripe-force/constants.rb @@ -111,6 +111,7 @@ class OrderStatusOptions < T::Enum SALESFORCE_KEY_HEADER = 'Salesforce-Key' SALESFORCE_PACKAGE_NAMESPACE_HEADER = "Salesforce-Package-Namespace" SALESFORCE_INSTANCE_TYPE_HEADER = 'Salesforce-Type' + SALESFORCE_PACKAGE_ID_HEADER = 'Salesforce-Package-Id' CONNECTOR_SETTING_SALESFORCE_NAMESPACE = "salesforce_namespace" CONNECTOR_SETTING_SALESFORCE_INSTANCE_TYPE = 'salesforce_instance_type' diff --git a/lib/stripe-force/jobs/order_poller.rb b/lib/stripe-force/jobs/order_poller.rb index 73d53594de..1f5b87b7cc 100644 --- a/lib/stripe-force/jobs/order_poller.rb +++ b/lib/stripe-force/jobs/order_poller.rb @@ -5,7 +5,7 @@ class StripeForce::OrderPoller < StripeForce::PollerBase def perform - # TODO lock on job in a separate wrapper job + # TODO lock on job in a separate wrapper job in the future locker.lock_on_poll_job(self.class) execution_time = Time.now.utc @@ -18,17 +18,7 @@ def perform log.info 'initiating poll', from: poll_record.last_polled_at, to: execution_time # note that SF fields never contain empty strings, they are reported null instead - updated_orders = sf.query( - <<~EOL - SELECT Id - FROM #{SF_ORDER} - WHERE - #{prefixed_stripe_field(GENERIC_STRIPE_ID)} = null AND - LastModifiedDate >= #{poll_record.last_polled_at.iso8601} AND - LastModifiedDate < #{execution_time.iso8601} - #{user_specified_where_clause_for_object} - EOL - ) + updated_orders = @user.sf_client.query(generate_soql(poll_record.last_polled_at, execution_time)) # important to use `size` here and not count because of the paging issue described below log.info 'order query complete', size: updated_orders.size @@ -60,6 +50,19 @@ def perform updated_order_ids end + private def generate_soql(time_start, time_end) + # LastModifiedDate has second, but not millisecond, granularity + <<~EOL + SELECT Id + FROM #{poll_type} + WHERE + #{prefixed_stripe_field(GENERIC_STRIPE_ID)} = null AND + LastModifiedDate >= #{time_start.iso8601} AND + LastModifiedDate < #{time_end.iso8601} + #{user_specified_where_clause_for_object} + EOL + end + private def user_specified_where_clause_for_object custom_query = @user.connector_settings.dig("filters", poll_type) diff --git a/lib/stripe-force/jobs/poller_base.rb b/lib/stripe-force/jobs/poller_base.rb index be8db1903d..2cdfe790c9 100644 --- a/lib/stripe-force/jobs/poller_base.rb +++ b/lib/stripe-force/jobs/poller_base.rb @@ -53,10 +53,6 @@ def poll_timestamp ) end - def sf - @user.sf_client - end - sig { abstract.returns(String) } def poll_type; end