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 fee72a2..3c7edfa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,10 +4,14 @@ 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 do member do post :cancel end end + resources :products end