Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update access scopes in DB from app_scopes_update webhook subscription #151

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:)
logger.info("#{self.class} started for shop '#{shop_domain}'")
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

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
Loading