Skip to content
This repository has been archived by the owner on Apr 17, 2020. It is now read-only.

Commit

Permalink
Rework interfaces and specs for simplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
jkelleyj committed Oct 31, 2016
1 parent 1410b2a commit 8a73855
Show file tree
Hide file tree
Showing 10 changed files with 608 additions and 529 deletions.
62 changes: 62 additions & 0 deletions lib/spree/chimpy/interface/customer_upserter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module Spree::Chimpy
module Interface
class CustomerUpserter
delegate :log, :store_api_call, to: Spree::Chimpy

def initialize(order)
@order = order
end
# CUSTOMER will be pulled first from the MC_EID if present on the order.source
# IF that is not found, customer will be found by our Customer ID
# IF that is not found, customer is created with the order email and our Customer ID
def ensure_customer
# use the one from mail chimp or fall back to the order's email
# happens when this is a new user
customer_id = customer_id_from_eid(@order.source.email_id) if @order.source
customer_id || upsert_customer
end

def self.mailchimp_customer_id(user_id)
"customer_#{user_id}"
end

def customer_id_from_eid(mc_eid)
email = Spree::Chimpy.list.email_for_id(mc_eid)
if email
begin
response = store_api_call
.customers
.retrieve(params: { "fields" => "customers.id", "email_address" => email })

data = response["customers"].first
data["id"] if data
rescue Gibbon::MailChimpError => e
nil
end
end
end

private

def upsert_customer
customer_id = self.class.mailchimp_customer_id(@order.user_id)
begin
response = store_api_call
.customers(customer_id)
.retrieve(params: { "fields" => "id,email_address"})
rescue Gibbon::MailChimpError => e
# Customer Not Found, so create them
response = store_api_call
.customers
.create(body: {
id: customer_id,
email_address: @order.email.downcase,
opt_in_status: Spree::Chimpy::Config.subscribe_to_list || false
})
end
customer_id
end

end
end
end
2 changes: 1 addition & 1 deletion lib/spree/chimpy/interface/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def info(email)
.retrieve(params: { "fields" => "email_address,merge_fields,status"})

response = response.symbolize_keys
response.merge(:email => response[:email_address])
response.merge(email: response[:email_address])
rescue Gibbon::MailChimpError
{}
end
Expand Down
87 changes: 87 additions & 0 deletions lib/spree/chimpy/interface/order_upserter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
module Spree::Chimpy
module Interface
class OrderUpserter
delegate :log, :store_api_call, to: Spree::Chimpy

def initialize(order)
@order = order
end

def upsert
Products.ensure_products(@order)
perform_upsert
end

private

def perform_upsert
data = order_hash
log "Adding order #{@order.number} for #{data[:customer][:id]} with campaign #{data[:campaign_id]}"
begin
find_and_update_order(data)
rescue Gibbon::MailChimpError => e
log "Order #{@order.number} Not Found, creating order"
create_order(data)
end
end

def find_and_update_order(data)
# retrieval is checks if the order exists and raises a Gibbon::MailChimpError when not found
response = store_api_call.orders(@order.number).retrieve(params: { "fields" => "id" })
log "Order #{@order.number} exists, updating data"
store_api_call.orders(@order.number).update(body: data)
end

def create_order(data)
store_api_call
.orders
.create(body: data)
rescue Gibbon::MailChimpError => e
log "Unable to create order #{@order.number}. [#{e.raw_body}]"
end

def order_variant_hash(line_item)
variant = line_item.variant
{
id: "line_item_#{line_item.id}",
product_id: Products.mailchimp_product_id(variant),
product_variant_id: Products.mailchimp_variant_id(variant),
price: variant.price.to_f,
quantity: line_item.quantity
}
end

def order_hash
customer_id = CustomerUpserter.new(@order).ensure_customer
source = @order.source

lines = @order.line_items.map do |line|
# MC can only associate the order with a single category: associate the order with the category right below the root level taxon
order_variant_hash(line)
end

data = {
id: @order.number,
lines: lines,
order_total: @order.total.to_f,
financial_status: @order.payment_state,
fulfillment_status: @order.shipment_state,
currency_code: @order.currency,
processed_at_foreign: @order.completed_at ? @order.completed_at.to_formatted_s(:db) : nil,
updated_at_foreign: @order.updated_at.to_formatted_s(:db),
shipping_total: @order.ship_total.to_f,
tax_total: @order.try(:included_tax_total).to_f + @order.try(:additional_tax_total).to_f,
customer: {
id: customer_id
}
}

if source
data[:campaign_id] = source.campaign_id
end

data
end
end
end
end
Loading

0 comments on commit 8a73855

Please sign in to comment.