Skip to content

Commit

Permalink
WIP to use offline tokens instead of online
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomarg committed Jun 23, 2022
1 parent a7ac51e commit 6c2d9a1
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 208 deletions.
19 changes: 0 additions & 19 deletions web/app/controllers/graphql_controller.rb

This file was deleted.

16 changes: 16 additions & 0 deletions web/app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,20 @@ class ProductsController < AuthenticatedController
def count
render(json: ShopifyAPI::Product.count.body)
end

def create
ProductCreator.call(count: 5)

success = true
error = nil
status_code = 200
rescue => e
success = false
error = e.message
status_code = e.is_a?(ShopifyAPI::Errors::HttpResponseError) ? e.code : 500

logger.info("Failed to create products: #{error}")
ensure
render(json: {success: success, error: error}, status: status_code)
end
end
2 changes: 0 additions & 2 deletions web/app/jobs/app_uninstalled_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ def perform(topic:, shop_domain:, webhook:)
end

logger.info("#{self.class} started for shop '#{shop_domain}'")
users = User.where(shopify_domain: shop_domain)
users.each(&:destroy)
shop.destroy
end
end
9 changes: 0 additions & 9 deletions web/app/models/user.rb

This file was deleted.

5 changes: 5 additions & 0 deletions web/app/services/application_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ApplicationService
def self.call(*args, **kwargs, &block)
new(*args, **kwargs, &block).call
end
end
115 changes: 115 additions & 0 deletions web/app/services/product_creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
class ProductCreator < ApplicationService
attr_reader :count

CREATE_PRODUCTS_MUTATION = <<~'QUERY'
mutation populateProduct($input: ProductInput!) {
productCreate(input: $input) {
product {
id
}
}
}
QUERY

def initialize(count:, session: ShopifyAPI::Context.active_session)
@count = count
@session = session
end

def call
client = ShopifyAPI::Clients::Graphql::Admin.new(session: @session)

for i in 1..count
client.query(
query: CREATE_PRODUCTS_MUTATION,
variables: {
input: {
title: random_title,
variants: [{ price: random_price }],
}
}
)
end
end

private

def random_title
adjective = ADJECTIVES[rand(ADJECTIVES.size)]
noun = NOUNS[rand(NOUNS.size)]

"#{adjective} #{noun}"
end

def random_price
(100.0 + rand(1000)) / 100
end

ADJECTIVES = [
"autumn",
"hidden",
"bitter",
"misty",
"silent",
"empty",
"dry",
"dark",
"summer",
"icy",
"delicate",
"quiet",
"white",
"cool",
"spring",
"winter",
"patient",
"twilight",
"dawn",
"crimson",
"wispy",
"weathered",
"blue",
"billowing",
"broken",
"cold",
"damp",
"falling",
"frosty",
"green",
"long",
]

NOUNS = [
"waterfall",
"river",
"breeze",
"moon",
"rain",
"wind",
"sea",
"morning",
"snow",
"lake",
"sunset",
"pine",
"shadow",
"leaf",
"dawn",
"glitter",
"forest",
"hill",
"cloud",
"meadow",
"sun",
"glade",
"bird",
"brook",
"butterfly",
"bush",
"dew",
"dust",
"field",
"fire",
"flower",
]
end
1 change: 0 additions & 1 deletion web/config/initializers/shopify_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
config.after_authenticate_job = false
config.api_version = ShopifyAPI::AdminVersions::LATEST_SUPPORTED_ADMIN_VERSION
config.shop_session_repository = "Shop"
config.user_session_repository = "User"

config.reauth_on_access_scope_changes = true

Expand Down
5 changes: 2 additions & 3 deletions web/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

get "/api/products-count", to: "products#count"

post "/api/graphql", to: "graphql#proxy"
get "/api/products/count", to: "products#count"
get "/api/products/create", to: "products#create"

# Any other routes will just render the react app
match "*path" => "home#index", via: [:get, :post]
Expand Down
16 changes: 0 additions & 16 deletions web/db/migrate/20220609125828_create_users.rb

This file was deleted.

This file was deleted.

12 changes: 1 addition & 11 deletions web/db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

113 changes: 0 additions & 113 deletions web/test/controllers/graphql_controller_test.rb

This file was deleted.

9 changes: 0 additions & 9 deletions web/test/controllers/home_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ class HomeControllerTestTest < ActionDispatch::IntegrationTest
assert_redirected_to(%r{/api/auth\?shop=#{unknown_shop}})
end

test "index does not redirect if shop is installed but there is no online session" do
User.delete_all

get "/?shop=#{KNOWN_SHOP}"

assert_response :success
assert_match "text/html", @response.headers["Content-Type"]
end

test "index returns a 200 with HTML if a session exists" do
get "/?shop=#{KNOWN_SHOP}"

Expand Down
Loading

0 comments on commit 6c2d9a1

Please sign in to comment.