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

Commit

Permalink
Merge pull request DynamoMTL#80 from jkelleyj/ensure-customer
Browse files Browse the repository at this point in the history
Do not submit orders created via guest checkout
  • Loading branch information
braidn authored Nov 30, 2016
2 parents dd5f057 + 7ed2846 commit a1b0d43
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/spree/chimpy/interface/customer_upserter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def customer_id_from_eid(mc_eid)
private

def upsert_customer
return unless @order.user_id

customer_id = self.class.mailchimp_customer_id(@order.user_id)
begin
response = store_api_call
Expand Down
8 changes: 7 additions & 1 deletion lib/spree/chimpy/interface/order_upserter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ def initialize(order)
@order = order
end

def customer_id
@customer_id ||= CustomerUpserter.new(@order).ensure_customer
end

def upsert
return unless customer_id

Products.ensure_products(@order)

perform_upsert
end

Expand Down Expand Up @@ -52,7 +59,6 @@ def order_variant_hash(line_item)
end

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

lines = @order.line_items.map do |line|
Expand Down
21 changes: 15 additions & 6 deletions spec/lib/customers_interface_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,23 @@
expect(interface.ensure_customer).to eq "customer_999"
end

it "upserts the customer when not found by order source" do
allow(interface).to receive(:customer_id_from_eid)
.with('id-abcd')
.and_return(nil)
context "when no customer from order source" do
before(:each) do
allow(interface).to receive(:customer_id_from_eid)
.with('id-abcd')
.and_return(nil)
end

allow(interface).to receive(:upsert_customer) { "customer_998" }
it "upserts the customer" do
allow(interface).to receive(:upsert_customer) { "customer_998" }

expect(interface.ensure_customer).to eq "customer_998"
expect(interface.ensure_customer).to eq "customer_998"
end

it "returns nil if guest checkout" do
order.user_id = nil
expect(interface.ensure_customer).to be_nil
end
end
end

Expand Down
9 changes: 9 additions & 0 deletions spec/lib/order_upserter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ def check_hash(h, expected_customer_id)
interface.upsert
end

it "does not perform the order upsert if no customer_id exists" do
expect(customer_upserter).to receive(:ensure_customer)
.and_return(nil)

expect(interface).to_not receive(:perform_upsert)

interface.upsert
end

context "when order already exists" do
before(:each) do
allow(order_api).to receive(:retrieve)
Expand Down

0 comments on commit a1b0d43

Please sign in to comment.