Skip to content

Commit

Permalink
Update access scopes in DB from new webhook subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
zzooeeyy committed Oct 30, 2024
1 parent 2940958 commit acdb96b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion shopify.app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
scopes = "write_products"

[webhooks]
api_version = "2024-07"
api_version = "2024-10"

[[webhooks.subscriptions]]
topics = [ "app/scopes_update" ]
uri = "/api/webhooks/app_scopes_update"

[[webhooks.subscriptions]]
uri = "/api/webhooks/app_uninstalled"
Expand Down
13 changes: 13 additions & 0 deletions web/app/controllers/webhooks/app_scopes_update_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Webhooks
class AppScopesUpdateController < ApplicationController
include ShopifyApp::WebhookVerification

def receive
webhook_request = ShopifyAPI::Webhooks::Request.new(raw_body: request.raw_post, headers: request.headers.to_h)
AppScopesUpdateJob.perform_later(shop_domain: webhook_request.shop, webhook: webhook_request.parsed_body)
head(:no_content)
end
end
end
16 changes: 16 additions & 0 deletions web/app/jobs/app_scopes_update_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class AppScopesUpdateJob < ActiveJob::Base
def perform(shop_domain:, webhook:)
shop = Shop.find_by(shopify_domain: shop_domain)

if shop.nil?
logger.error("#{self.class} failed: cannot find shop with domain '#{shop_domain}'")
return
end

logger.info("#{self.class} started for shop '#{shop_domain}'")
shop.access_scopes = webhook["current"].join(",")
shop.save!
end
end
1 change: 1 addition & 0 deletions web/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
end
namespace :webhooks do
post "/app_uninstalled", to: "app_uninstalled#receive"
post "/app_scopes_update", to: "app_scopes_update#receive"
post "/customers_data_request", to: "customers_data_request#receive"
post "/customers_redact", to: "customers_redact#receive"
post "/shop_redact", to: "shop_redact#receive"
Expand Down

0 comments on commit acdb96b

Please sign in to comment.