From 07f9cc50b0b9f581bc488d62fe7b059f2752e3b8 Mon Sep 17 00:00:00 2001 From: ShalaevPavel Date: Mon, 7 Aug 2023 09:52:34 +0300 Subject: [PATCH] added webhook to customers and route --- .../webhook_customers_controller.rb | 39 +++++++++++++++++++ config/routes.rb | 1 + 2 files changed, 40 insertions(+) create mode 100644 app/controllers/webhook_customers_controller.rb diff --git a/app/controllers/webhook_customers_controller.rb b/app/controllers/webhook_customers_controller.rb new file mode 100644 index 0000000..a7e5efb --- /dev/null +++ b/app/controllers/webhook_customers_controller.rb @@ -0,0 +1,39 @@ +class WebhooksCustomerController < ApplicationController + skip_before_action :verify_authenticity_token, only: [:customer_updated] + + def customer_updated + data = JSON.parse(request.body.read) + customer_id = data['id'] + + @customer = Customer.find_by(shopify_id: customer_id) + if @customer.present? + assign_customer_attributes(data) + if @customer.save! + redirect_to @customer, notice: 'updated.' + else + redirect_to @customer, notice: 'Could not update' + end + else + redirect_to customers_path, alert: 'error. Not found' + end + end + + private + + def assign_customer_attributes(data) + @customer.assign_attributes( + email: data['email'], + first_name: data['first_name'], + last_name: data['last_name'], + accepts_marketing: data['accepts_marketing'], + created_at: data['created_at'], + verified_email: data['verified_email'], + status: data['status'], + tags: data['tags'] + ) + + + end + + +end diff --git a/config/routes.rb b/config/routes.rb index 9fa7917..c21a0d7 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/customer_updated', to: 'webhooks_customer#customer_updated' resources :orders, only: [:index, :show] resources :products end