Skip to content

Commit

Permalink
Handle Shopify Partner API errors
Browse files Browse the repository at this point in the history
  • Loading branch information
forsbergplustwo committed Sep 14, 2023
1 parent a42caa2 commit 8ad2674
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 90 deletions.
11 changes: 10 additions & 1 deletion app/controllers/partner_api_credentials_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def new
end

def edit
add_status_messsage_error if @partner_api_credential.invalid_status?
end

def create
Expand Down Expand Up @@ -46,8 +47,16 @@ def set_partner_api_credential
@partner_api_credential = current_user.partner_api_credential
end

def add_status_messsage_error
@partner_api_credential.errors.add(:base, @partner_api_credential.status_message)
end

# Only allow a list of trusted parameters through.
def partner_api_credential_params
params.require(:partner_api_credential).permit(:access_token, :organization_id)
params.require(:partner_api_credential).permit(
:access_token,
:organization_id,
user_attributes: [:count_usage_charges_as_recurring]
)
end
end
4 changes: 4 additions & 0 deletions app/helpers/partner_api_credentials_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ def partner_api_credential_path_for(current_user)
new_partner_api_credential_path
end
end

def partner_api_credential_badge_for(current_user)
current_user.partner_api_credential&.status_message.present? ? "!" : nil
end
end
6 changes: 5 additions & 1 deletion app/models/import/adaptor/shopify_payments_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def fetch_from_api
variables: {createdAtMin: @created_at_min, cursor: @cursor, first: batch_size},
context: @context
)
raise StandardError.new(results.errors.messages.map { |k, v| "#{k}=#{v}" }.join("&")) if results.errors.any?
handle_error(results.errors) if results.errors.any?
results
end

Expand Down Expand Up @@ -140,4 +140,8 @@ def throttle(start_time)
sleep wait_time if wait_time > 0.0
Time.zone.now
end

def handle_error(api_error)
@import.partner_api_credential.invalidate_with_message!(results.errors.messages.map { |k, v| "#{k}=#{v}" }.join("&"))
end
end
5 changes: 1 addition & 4 deletions app/models/import/payments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ def initialize(import:)
@user = @import.user
@created_at_min = @user.calculate_from_date
@source_adaptor = @import.source_adaptor.new(import: @import, created_at_min: @created_at_min)
@batch_of_payments = []
end

def import!
Expand All @@ -28,10 +27,8 @@ def import_new_payments
new_payment(transaction)
end.compact

@batch_of_payments.concat(payments)
Payment.import!(@batch_of_payments, validate: false, no_returning: true)
Payment.import!(payments, validate: false, no_returning: true)
@import.touch
@batch_of_payments.clear
end
end

Expand Down
6 changes: 6 additions & 0 deletions app/models/partner_api_credential.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ def context
}
end

def invalidate_with_message!(message)
update!(status: :invalid, status_message: message)
end

private

def credentials_have_access
errors.add(:access_token, "is required") if access_token.blank?
errors.add(:organization_id, "is required") if organization_id.blank?

return if !will_save_change_to_access_token? && !will_save_change_to_organization_id?

if errors.empty?
response = test_api_credentials
if response.success?
Expand Down
73 changes: 0 additions & 73 deletions app/views/layouts/_import_modal.html.erb

This file was deleted.

1 change: 1 addition & 0 deletions app/views/layouts/frame/_navigation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
label: "Partner API Credentials",
icon: "InsertDynamicSourceMajor",
selected: nav_item_selected?(partner_api_credential_path_for(current_user)),
badge: partner_api_credential_badge_for(current_user),
link_arguments: {}
) %>
<% end %>
Expand Down
6 changes: 1 addition & 5 deletions app/views/partner_api_credentials/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
<%= render "shared/status", resource: @partner_api_credential %>
<% end %>

<%= polaris_vertical_stack(gap: "5") do %>

<%= render "form", partner_api_credential: @partner_api_credential %>

<% end %>
<%= render "form", partner_api_credential: @partner_api_credential %>

<% end %>
7 changes: 3 additions & 4 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ en:
organization_id_help_text: "Find your organization ID in the Shopify Partners Dashboard."
access_token: "Shopify access token"
organization_id_help_text: "Generate an access token in the Shopify Partners Dashboard."
instructions_banner:
title: "Instructions"
message: "View the wiki article for instructions on how to set up your Shopify Partner API credentials."
button_text: "View wiki article"
status_message_banner:
title: "Shopify API import error"
message: "We couldn't import your data from the Shopify API, due to the following error:"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
desc "Creates the inital import for all users, so all historical payments and metrics have an import"
task create_initial_import: :environment do
task create_initial_imports: :environment do
User.find_each do |user|
import = user.imports.create!(
source: Import.sources[:shopify_payments_api]
Expand Down
13 changes: 13 additions & 0 deletions lib/tasks/create_partner_api_credentials.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
desc "Creates the inital import for all users, so all historical payments and metrics have an import"
task create_partner_api_credentials: :environment do
User.find_each do |user|
if user.partner_api_access_token.present? && user.partner_api_organization_id.present? && !user.partner_api_errors&.include?("Unauthorized")
if user.partner_api_credential.blank?
user.create_partner_api_credential!(
organization_id: user.partner_api_organization_id,
access_token: user.partner_api_access_token
)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/tasks/import_all_from_partner_api.rake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
desc "Import transactions for existing users, with partner api credentials"
task import_all_from_partner_api: :environment do
User.find_each do |user|
if user.has_partner_api_credentials? && !user.partner_api_errors.include?("Unauthorized")
if user.partner_api_credential&.valid_status?
user.imports.create(source: Import.sources[:shopify_payments_api])
end
end
Expand Down

0 comments on commit 8ad2674

Please sign in to comment.