Skip to content

Commit

Permalink
Return salesforce_products_since parameter to Wombat
Browse files Browse the repository at this point in the history
  • Loading branch information
huoxito committed Oct 24, 2014
1 parent 7c4150a commit 08deed2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/SF_services/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def find_id_by_code(product_code)
def latest_updates(time = Time.now.utc.iso8601)
since = time ? Time.parse(time).utc.iso8601 : Time.now.utc.iso8601

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

Expand Down
19 changes: 15 additions & 4 deletions lib/integrations/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ def upsert!(item = nil)
end

def fetch_updates
products = product_service.latest_updates config[:salesforce_products_since]
return [] if products.to_a.empty?
return [] if latest_products.to_a.empty?

product_ids = products.map { |p| "'#{p["Id"]}'" }.join(", ")
product_ids = latest_products.map { |p| "'#{p["Id"]}'" }.join(", ")
prices = product_service.find_prices_by_product_ids product_ids

products.map do |product|
latest_products.map do |product|
{
id: product["ProductCode"],
name: product["Name"],
Expand All @@ -39,6 +38,18 @@ def look_up(what)
object[what]
end

def latest_products
@latest_products ||= product_service.latest_updates config[:salesforce_products_since]
end

def latest_timestamp_update(products = nil)
if product = (products || latest_products).sort_by { |p| p["LastModifiedDate"] }.last
Time.parse(product["LastModifiedDate"]).utc.iso8601
else
Time.now.utc.iso8601
end
end

def price_from_product_id(prices, product_id)
if price = prices.find { |p| p["Product2Id"] == product_id }
price["UnitPrice"]
Expand Down
4 changes: 3 additions & 1 deletion salesforce_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ class SalesforceEndpoint < EndpointBase::Sinatra::Base
end

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

if (count = products.count) > 0
add_parameter "salesforce_products_since", product_service.latest_timestamp_update
result 200, "Received #{count} #{"product".pluralize count} from Salesforce"
else
result 200
Expand Down
11 changes: 11 additions & 0 deletions spec/lib/integrations/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,16 @@
expect(subject.send(:product_service)).to receive(:upsert!).once
subject.upsert!
end

it "grabs last timestamp and return in iso8601" do
products = [
{ "LastModifiedDate" => "2014-09-21 T21:44:32.000+0000" },
{ "LastModifiedDate" => "2014-10-21 T21:44:32.000+0000" },
{ "LastModifiedDate" => "2014-10-21 T01:43:32.000+0000" },
]

expect(subject.latest_timestamp_update products).to eq "2014-10-21T21:44:32Z"
expect(subject.latest_timestamp_update []).to be_present
end
end
end
2 changes: 2 additions & 0 deletions spec/salesforce_endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@

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

expect(json_response["parameters"]).to have_key "salesforce_products_since"
expect(json_response["products"]).to be_a Array
end
end
Expand Down

0 comments on commit 08deed2

Please sign in to comment.