Skip to content

Commit

Permalink
Add /get_products webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
huoxito committed Oct 24, 2014
1 parent 5eec38d commit 32ad8ab
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/SF_services/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ def find_id_by_code(product_code)
results.any? ? results.first['Id'] : nil
end

def latest_updates(time = Time.now.utc.iso8601)
since = time ? Time.parse(time).utc.iso8601 : Time.now.utc.iso8601

fields = "Id, Name, ProductCode, Description"
products = salesforce.query("select #{fields} from Product2 where LastModifiedDate > #{since}")
end

def find_prices_by_product_ids(product_ids)
fields = "Product2Id, UnitPrice"
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)

Expand Down
24 changes: 23 additions & 1 deletion lib/integrations/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ def upsert!(item = nil)
product_service.upsert!(item['ProductCode'], item)
end

def fetch_updates
products = product_service.latest_updates config[:salesforce_products_since]
product_ids = products.map { |p| "'#{p["Id"]}'" }.join(", ")

prices = product_service.find_prices_by_product_ids product_ids

products.map do |product|
{
id: product["ProductCode"],
name: product["Name"],
description: product["Description"],
price: price_from_product_id(prices, product["Id"]),
salesforce_id: product["Id"]
}
end
end

def import_from_order!
look_up('line_items').each { |item| upsert!(order_product_params(with_currency(item))) }
end
Expand All @@ -21,6 +38,12 @@ def look_up(what)
object[what]
end

def price_from_product_id(prices, product_id)
if price = prices.find { |p| p["Product2Id"] == product_id }
price["UnitPrice"]
end
end

private

def with_currency(item)
Expand All @@ -38,6 +61,5 @@ def order_product_params(payload_item)
def look_up(what)
object['order'][what]
end

end
end
11 changes: 11 additions & 0 deletions salesforce_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,15 @@ class SalesforceEndpoint < EndpointBase::Sinatra::Base
end
end
end

post "/get_products" do
products = Integration::Product.new(@config, @payload).fetch_updates
products.each { |p| add_object "product", p }

if (count = products.count) > 0
result 200, "Received #{count} #{"product".pluralize count} from Salesforce"
else
result 200
end
end
end
14 changes: 14 additions & 0 deletions spec/salesforce_endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,18 @@
expect(json_response["summary"]).to match "Product #{id}"
end
end

it "get products" do
payload = {
parameters: config.merge(salesforce_products_since: "2014-10-17T16:14:57-03:00")
}

VCR.use_cassette "requests/get_products" do
post "/get_products", payload.to_json, auth

expect(json_response["summary"]).to match "Received"
expect(last_response.status).to eq 200
expect(json_response["products"]).to be_a Array
end
end
end

0 comments on commit 32ad8ab

Please sign in to comment.