Skip to content
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
39 changes: 39 additions & 0 deletions app/controllers/webhook_customers_controller.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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