Skip to content

Commit

Permalink
Make product ids available inside Integration Order
Browse files Browse the repository at this point in the history
  • Loading branch information
huoxito committed Dec 16, 2014
1 parent 2ae5bae commit 7ade76d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
6 changes: 3 additions & 3 deletions lib/SF_services/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def find_prices_by_product_ids(product_ids)
salesforce.query("select #{fields} from PricebookEntry where Product2Id in (#{product_ids})")
end

def upsert!(product_code, attributes = {})
product_id = find_id_by_code(product_code)
def upsert!(attributes = {})
product_id = attributes["Id"]

pricebookentry = SFService::Base.new "PricebookEntry", config

Expand All @@ -42,7 +42,7 @@ def upsert!(product_code, attributes = {})
update!(attributes.merge({ Id: product_id }))
result = salesforce.query "select Id from PricebookEntry where Pricebook2Id = '#{standard["Id"]}' and Product2Id = '#{product_id}'"

# NOTE Create if can find pricebook entry?
# NOTE Create if cant find pricebook entry?
if result.first && pricebookentry_id = result.first["Id"]
pricebookentry.update!(Id: pricebookentry_id, UnitPrice: price)
end
Expand Down
15 changes: 14 additions & 1 deletion lib/integrations/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,26 @@ def upsert!
params = order_params.merge "AccountId" => account_id
opportunity_id = order_service.upsert! params

product_integration = Product.new(config, object[:order])
line_item_integration = LineItem.new(config, object[:order])

object[:order][:line_items].each do |line_item|

unless product_id = product_integration.find_id_by_code(line_item[:product_id])
params = product_integration.order_product_params
params.merge Currency: object[:order][:currency]

product_id = product_integration.create! params
end
end

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

private

def order_params
Builder::Order.new(object['order']).build
Builder::Order.new(object[:order]).build
end
end
end
12 changes: 7 additions & 5 deletions lib/integrations/product.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Integration
class Product < Base

delegate :find_id_by_code, :create!, to: :product_service
attr_reader :object

def initialize(config, object)
Expand All @@ -10,7 +11,8 @@ def initialize(config, object)

def upsert!(item = nil)
item ||= product_params
product_service.upsert!(item['ProductCode'], item)
item.merge Id: product_id if product_id = find_id_by_code(product_code)
product_service.upsert! item
end

def fetch_updates
Expand Down Expand Up @@ -57,6 +59,10 @@ def price_from_product_id(prices, product_id)
end
end

def order_product_params(payload_item)
Integration::Builder::OrderProduct.new(payload_item).build
end

private

def with_currency(item)
Expand All @@ -67,10 +73,6 @@ def product_params
Integration::Builder::Product.new(object).build
end

def order_product_params(payload_item)
Integration::Builder::OrderProduct.new(payload_item).build
end

def look_up(what)
object['order'][what]
end
Expand Down
2 changes: 0 additions & 2 deletions salesforce_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ class SalesforceEndpoint < EndpointBase::Sinatra::Base
['/add_order', '/update_order'].each do |path|
post path do
Integration::Order.new(@config, @payload).upsert!

# 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 7ade76d

Please sign in to comment.