From b08a633560b056a7ee6caa9f8606b5317ae8bdcd Mon Sep 17 00:00:00 2001 From: ikl0 Date: Tue, 8 Aug 2023 18:26:39 +0300 Subject: [PATCH 1/7] add webhook for cancelled --- app/controllers/webhooks_controller.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb index 3cab7b1..c13a1be 100644 --- a/app/controllers/webhooks_controller.rb +++ b/app/controllers/webhooks_controller.rb @@ -1,5 +1,5 @@ class WebhooksController < ApplicationController - skip_before_action :verify_authenticity_token, only: [:product_updated] + skip_before_action :verify_authenticity_token, only: %i[product_updated :order_cancelled] def product_updated data = JSON.parse(request.body.read) @@ -18,6 +18,13 @@ def product_updated end end + def order_cancelled + data = JSON.parse(request.body.read) + order_id = data['id'] + + @order = Order.find_by(shopify_id: order_id) + end + private def assign_product_attributes(data) @@ -52,4 +59,8 @@ def assign_product_attributes(data) @product.product_variants.upsert_all(variant_attributes, unique_by: :id) end + + def assign_order_attributes + + end end From 558ab45a44ceade77c8597cb0ec5b4928ab096d0 Mon Sep 17 00:00:00 2001 From: ikl0 Date: Tue, 8 Aug 2023 18:30:25 +0300 Subject: [PATCH 2/7] add route --- config/routes.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/routes.rb b/config/routes.rb index fee72a2..b38e102 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ get '/auth/callback', to: 'shopify_auth#callback' get '/import_shopify_data', to: 'shopify_import#import' post '/webhooks/shopify/product_updated', to: 'webhooks#product_updated' + post '/webhooks/shopify/order_cancelled', to: 'webhooks#order_cancelled' resources :orders do member do post :cancel From 68f430090cce387f1ab386fb8c62fbdd131b1c3f Mon Sep 17 00:00:00 2001 From: ikl0 Date: Tue, 8 Aug 2023 18:37:38 +0300 Subject: [PATCH 3/7] fix before action --- app/controllers/webhooks_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb index c13a1be..5d58599 100644 --- a/app/controllers/webhooks_controller.rb +++ b/app/controllers/webhooks_controller.rb @@ -1,5 +1,5 @@ class WebhooksController < ApplicationController - skip_before_action :verify_authenticity_token, only: %i[product_updated :order_cancelled] + skip_before_action :verify_authenticity_token, only: %i[product_updated order_cancelled] def product_updated data = JSON.parse(request.body.read) From 66f9a57d646e0cbc9b68a409668475b3f517cdcf Mon Sep 17 00:00:00 2001 From: ikl0 Date: Tue, 8 Aug 2023 19:50:42 +0300 Subject: [PATCH 4/7] add route for update --- config/routes.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/routes.rb b/config/routes.rb index b38e102..142fb53 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,7 @@ get '/import_shopify_data', to: 'shopify_import#import' post '/webhooks/shopify/product_updated', to: 'webhooks#product_updated' post '/webhooks/shopify/order_cancelled', to: 'webhooks#order_cancelled' + post '/webhooks/shopify/order_updated', to: 'webhooks#order_updated' resources :orders do member do post :cancel From df5d893cf15e7599efb0be6305f44ff6777a5b00 Mon Sep 17 00:00:00 2001 From: ikl0 Date: Tue, 8 Aug 2023 19:51:07 +0300 Subject: [PATCH 5/7] add method for update --- app/controllers/webhooks_controller.rb | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb index 5d58599..af11b56 100644 --- a/app/controllers/webhooks_controller.rb +++ b/app/controllers/webhooks_controller.rb @@ -1,5 +1,5 @@ class WebhooksController < ApplicationController - skip_before_action :verify_authenticity_token, only: %i[product_updated order_cancelled] + skip_before_action :verify_authenticity_token, only: %i[product_updated order_cancelled order_updated] def product_updated data = JSON.parse(request.body.read) @@ -18,11 +18,28 @@ def product_updated end end - def order_cancelled + def order_updated data = JSON.parse(request.body.read) order_id = data['id'] + @order = Order.find_by(shopify_id: order_id) + if @order.present? + redirect_to @product, notice: 'Product was successfully updated.' + else + redirect_to products_path, alert: 'Could not find the order in local database.' + end + end + def order_cancelled + data = JSON.parse(request.body.read) + order_id = data['id'] @order = Order.find_by(shopify_id: order_id) + + if @order.present? + CanceledOrder.create(order: @order) + redirect_to orders_path + else + redirect_to @order, alert: 'Error while canceling the order' + end end private @@ -59,8 +76,4 @@ def assign_product_attributes(data) @product.product_variants.upsert_all(variant_attributes, unique_by: :id) end - - def assign_order_attributes - - end end From ff70b42b24db0c0f87cc98ddd3cb5f9ab94308a0 Mon Sep 17 00:00:00 2001 From: ikl0 Date: Wed, 9 Aug 2023 15:49:27 +0300 Subject: [PATCH 6/7] remove route for update --- config/routes.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 142fb53..b38e102 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,7 +5,6 @@ get '/import_shopify_data', to: 'shopify_import#import' post '/webhooks/shopify/product_updated', to: 'webhooks#product_updated' post '/webhooks/shopify/order_cancelled', to: 'webhooks#order_cancelled' - post '/webhooks/shopify/order_updated', to: 'webhooks#order_updated' resources :orders do member do post :cancel From e2d4a4bfa14c0c2c5ba4a70b124635d2e28b43f5 Mon Sep 17 00:00:00 2001 From: ikl0 Date: Wed, 9 Aug 2023 15:49:35 +0300 Subject: [PATCH 7/7] remove action for update --- app/controllers/webhooks_controller.rb | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb index af11b56..caf69a2 100644 --- a/app/controllers/webhooks_controller.rb +++ b/app/controllers/webhooks_controller.rb @@ -1,5 +1,5 @@ class WebhooksController < ApplicationController - skip_before_action :verify_authenticity_token, only: %i[product_updated order_cancelled order_updated] + skip_before_action :verify_authenticity_token, only: %i[product_updated order_cancelled] def product_updated data = JSON.parse(request.body.read) @@ -18,17 +18,6 @@ def product_updated end end - def order_updated - data = JSON.parse(request.body.read) - order_id = data['id'] - @order = Order.find_by(shopify_id: order_id) - - if @order.present? - redirect_to @product, notice: 'Product was successfully updated.' - else - redirect_to products_path, alert: 'Could not find the order in local database.' - end - end def order_cancelled data = JSON.parse(request.body.read) order_id = data['id']