Skip to content

Commit

Permalink
Grab account_id within Integration::Order
Browse files Browse the repository at this point in the history
  • Loading branch information
huoxito committed Dec 16, 2014
1 parent eda4fae commit 7650218
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 18 deletions.
2 changes: 0 additions & 2 deletions lib/SF_services/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ def find_price_book_by_id(price_book_id)
end

def upsert!(order_attr = {})
account_id = @account_service.find_account_id_by_email(order_attr.fetch 'AccountId')
price_book_id = find_price_book_by_id(order_attr.fetch 'Pricebook2Id')

order_attr = order_attr.merge( { 'AccountId' => account_id } ) if account_id.present?
order_attr = order_attr.merge( { 'Pricebook2Id' => price_book_id } ) if price_book_id.present?

order_id = is_present?(order_attr.fetch 'Name')
Expand Down
2 changes: 1 addition & 1 deletion lib/integrations/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Base
attr_reader :config

def initialize(config)
@config = config.with_indifferent_access
@config = config
end

private
Expand Down
1 change: 0 additions & 1 deletion lib/integrations/builders/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def initialize(object)

def build
params = {
'AccountId' => object['email'],
'Amount' => object['totals']['order'],
'CloseDate' => object['placed_on'],
# 'HasOpportunityLineItem' => true,
Expand Down
17 changes: 7 additions & 10 deletions lib/integrations/contact_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ def initialize(config, object)
super(config)
end

def upsert!
contact_service.upsert!(customer_params['Email'], customer_params_with_account)
def upsert!(account_id = {})
params = customer_params.merge account_id
contact_service.upsert!(customer_params['Email'], params)
end

def fetch_updates
Expand Down Expand Up @@ -49,25 +50,21 @@ def build_address(account, kind = "Billing")
}
end

def account_id
contact_service.find_account_id_by_email(customer_params['Email']) || account_service.create!(account_params)
end

private
def latest_contacts
@latest_contacts ||= contact_service.latest_updates config[:salesforce_contacts_since]
end

def account_id
contact_service.find_account_id_by_email(customer_params['Email']) || account_service.create!(account_params)
end

def account_params
Integration::Builder::Account.new(object).build
end

def customer_params
Integration::Builder::Contact.new(object).build
end

def customer_params_with_account
Builder::Contact.new(object).build.merge({ AccountId: account_id })
end
end
end
9 changes: 6 additions & 3 deletions lib/integrations/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ class Order < Base
attr_reader :object

def initialize(config, object)
@object = object.with_indifferent_access
@object = object
super(config)
end

def upsert!
order_service.upsert!(order_params)
contact_account = ContactAccount.new(config, object[:order])
account_id = contact_account.account_id
contact_account.upsert! AccountId: account_id

order_service.upsert!(order_params.merge "AccountId" => account_id)
end

private

def order_params
Integration::Builder::Order.new(object['order']).build
end

end
end
1 change: 0 additions & 1 deletion salesforce_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class SalesforceEndpoint < EndpointBase::Sinatra::Base

['/add_order', '/update_order'].each do |path|
post path do
Integration::ContactAccount.new(@config, @payload[:order]).upsert!
Integration::Order.new(@config, @payload).upsert!

Integration::Product.new(@config, @payload).import_from_order!
Expand Down

0 comments on commit 7650218

Please sign in to comment.