Skip to content

Commit

Permalink
Make failed api imports retryable
Browse files Browse the repository at this point in the history
  • Loading branch information
forsbergplustwo committed Oct 12, 2023
1 parent 79ca9fe commit f710143
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 11 deletions.
10 changes: 10 additions & 0 deletions app/controllers/imports/retry_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Imports::RetryController < ApplicationController
def create
@import = current_user.imports.find(params[:import_id])
if @import.retriable? && @import.retry
redirect_to @import, notice: "Import being retried."
else
redirect_to @import, alert: "Import failed to retry."
end
end
end
22 changes: 22 additions & 0 deletions app/helpers/imports_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
module ImportsHelper
def secondary_import_actions(import)
actions = []
if import.retriable?
actions << {
content: t("actions.retry", resource: resource_name_for(Import)),
url: import_retry_path(import),
data: {
turbo_method: "post"
}
}
end
actions << {
content: t("actions.delete", resource: resource_name_for(Import)),
destructive: true,
data: {
controller: "polaris",
target: "#destroy-modal",
action: "polaris#openModal"
}
}
end

def metrics_date_range_text(import)
if import.metrics.empty?
t("imports.no_metrics")
Expand Down
9 changes: 9 additions & 0 deletions app/models/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class Import < ApplicationRecord

scope :in_progress, -> { where(status: %i[scheduled importing calculating]) }

def retriable?
shopify_payments_api_source? && failed? && user.imports.in_progress.empty?
end

def retry
update(status: :draft)
schedule
end

def schedule
return unless draft?
scheduled!
Expand Down
12 changes: 1 addition & 11 deletions app/views/imports/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,7 @@
subtitle: @import.created_at.to_fs(:long),
id: dom_id(@import),
back_url: imports_path,
secondary_actions: [
{
content: t("actions.delete", resource: resource_name_for(Import)),
destructive: true,
data: {
controller: "polaris",
target: "#destroy-modal",
action: "polaris#openModal"
}
}
],
secondary_actions: secondary_import_actions(@import),
) do |page| %>

<% page.with_title_metadata do %>
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ en:
delete_all: "Delete all %{resource}"
more_actions: "More actions"
add: "Add %{resource}"
retry: "Retry %{resource}"
statuses:
draft: "Draft"
scheduled: "Scheduled"
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

resources :imports, only: [:index, :show, :new, :create, :destroy] do
resource :globe, only: [:show], controller: "imports/globes"
resource :retry, only: [:create], controller: "imports/retry"
collection do
delete :destroy_all, to: "imports/destroy_all#destroy"
end
Expand Down
16 changes: 16 additions & 0 deletions test/controllers/imports/retry_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "test_helper"

class Imports::RetryControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in users(:regular)
@import = imports(:failed_shopify_payments_api)
end

test "should retry shopify api imports" do
post import_retry_url(@import)

assert @import.reload.draft? || @import.reload.scheduled?
assert_redirected_to import_url(@import)
assert flash[:notice], "Import being retried."
end
end
5 changes: 5 additions & 0 deletions test/fixtures/imports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ failed:
source: <%= Import.sources[:csv_file] %>
status: failed
user: regular

failed_shopify_payments_api:
source: <%= Import.sources[:shopify_payments_api] %>
status: failed
user: regular

0 comments on commit f710143

Please sign in to comment.