Skip to content

Commit

Permalink
Pass opportunity_id to Integration::LineItem
Browse files Browse the repository at this point in the history
So it doesnt have to query again for it
  • Loading branch information
huoxito committed Dec 16, 2014
1 parent 7650218 commit 896588a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 41 deletions.
13 changes: 3 additions & 10 deletions lib/SF_services/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ def initialize(config)
super("OpportunityLineItem", config)
end

def find_order(order_code)
results = salesforce.query("select Id from Opportunity where Name = '#{order_code}'")
results.any? ? results.first['Id'] : nil
end

def find_pricebook_entry(standard_id, product_id)
filter = "where Pricebook2Id = '#{standard_id}' and Product2Id = '#{product_id}'"
results = salesforce.query "select Id, IsActive from PricebookEntry #{filter}"
Expand All @@ -21,9 +16,7 @@ def find_line_item(order_id, product_id)
results.any? ? results.first['Id'] : nil
end

def upsert!(line_item_attr = {}, order_code, product_code)
order_id = find_order(order_code)

def upsert!(line_item_attr = {}, opportunity_id, product_code)
# FIXME product ids are already fetched before on the stack
results = salesforce.query("select Id from Product2 where ProductCode = '#{product_code}'")
raise SalesfoceIntegrationError, "Product #{product_code} not found" unless results.first
Expand Down Expand Up @@ -58,8 +51,8 @@ def upsert!(line_item_attr = {}, order_code, product_code)
)
end

if line_item_id = find_line_item(order_id, pricebook_entry_id)
line_item_attr = line_item_attr.merge OpportunityId: order_id , PricebookEntryId: pricebook_entry_id
if line_item_id = find_line_item(opportunity_id, pricebook_entry_id)
line_item_attr = line_item_attr.merge OpportunityId: opportunity_id, PricebookEntryId: pricebook_entry_id
create!(line_item_attr)
else
update!(line_item_attr.merge Id: line_item_id )
Expand Down
12 changes: 8 additions & 4 deletions lib/SF_services/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def initialize(config)
@account_service = SFService::Account.new(config)
end

def is_present?(id)
results = salesforce.query("select Id from Opportunity where Name = '#{id}'")
def find_opportunity_id_by_name(name)
results = salesforce.query("SELECT Id FROM Opportunity WHERE Name = '#{name}' LIMIT 1")
results.any? ? results.first.fetch('Id') : nil
end

Expand All @@ -24,8 +24,12 @@ def upsert!(order_attr = {})

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

order_id = is_present?(order_attr.fetch 'Name')
order_id.present? ? update!(order_attr.merge({ Id: order_id })) : create!(order_attr)
if opportunity_id = find_opportunity_id_by_name(order_attr.fetch 'Name')
update! order_attr.merge(Id: opportunity_id)
opportunity_id
else
create!(order_attr)
end
end
end
end
6 changes: 2 additions & 4 deletions lib/integrations/builders/line_item.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
module Integration
module Builder
class LineItem

attr_reader :object

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

def build
params = {
'Quantity' => object['quantity'],
'UnitPrice' => object['price'],
# 'CurrencyIsoCode' => object['currency'],
'UnitPrice' => object['price']
}
end
end
Expand Down
29 changes: 10 additions & 19 deletions lib/integrations/line_item.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
module Integration
class LineItem < Base

attr_reader :object
attr_reader :order, :items

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

def upsert!(item)
line_item_service.upsert!(item_params(item), look_up('id'), item.fetch('product_id'))
end
def initialize(config, order)
@order = order
@items = order[:line_items]

def import!
look_up('line_items').each { |li| upsert!(with_currency(li)) }
end

def with_currency(item)
item.merge({ 'Currency' => look_up('currency') })
super(config)
end

def look_up(what)
object[what]
def upsert!(opportunity_id)
items.each do |item|
line_item_service.upsert!(item_params(item), opportunity_id, item.fetch('product_id'))
end
end

def item_params(item)
Integration::Builder::LineItem.new(item).build
Integration::Builder::LineItem.new(item).build.merge 'Currency' => order['currency']
end

end
end
5 changes: 4 additions & 1 deletion lib/integrations/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def upsert!
account_id = contact_account.account_id
contact_account.upsert! AccountId: account_id

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

Integration::LineItem.new(config, object[:order]).upsert! opportunity_id
end

private
Expand Down
4 changes: 1 addition & 3 deletions salesforce_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ class SalesforceEndpoint < EndpointBase::Sinatra::Base
post path do
Integration::Order.new(@config, @payload).upsert!

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

Integration::LineItem.new(@config, @payload[:order]).import!
# Integration::Product.new(@config, @payload).import_from_order!
Integration::Payment.new(@config, @payload[:order]).import!

result 200, "Opportunity # #{@payload["order"]["id"]} sent to Salesforce"
Expand Down

0 comments on commit 896588a

Please sign in to comment.