Class: SpreeKlaviyo::FetchProfile

Inherits:
Base
  • Object
show all
Includes:
Spree::ServiceModule::Base
Defined in:
app/services/spree_klaviyo/fetch_profile.rb

Instance Method Summary collapse

Instance Method Details

#call(klaviyo_integration:, user:) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'app/services/spree_klaviyo/fetch_profile.rb', line 5

def call(klaviyo_integration:, user:)
  return failure(false, ::Spree.t('admin.integrations.klaviyo.not_found')) unless klaviyo_integration

  return success(user.klaviyo_id) if user.klaviyo_id.present?

  klaviyo_integration.fetch_profile(email: user.email).tap do |result|
    user.update!(klaviyo_id: JSON.parse(result.value)['data'].first['id']) if result.success?
  end
end