forked from JoshvaR88/salesforce_integration
-
Notifications
You must be signed in to change notification settings - Fork 1
/
salesforce_endpoint.rb
77 lines (64 loc) · 2.47 KB
/
salesforce_endpoint.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require 'sinatra'
require 'endpoint_base'
require File.expand_path(File.dirname(__FILE__) + '/lib/salesforce_integration')
class SalesforceEndpoint < EndpointBase::Sinatra::Base
enable :logging
post '/send_order' do
Integration::Order.new(@config, @payload).upsert!
result 200, "Opportunity # #{@payload["order"]["id"]} sent to Salesforce"
end
post '/send_return' do
Integration::Return.new(@config, @payload).upsert!
result 200, "Return # #{@payload[:return][:id]} updated as Note in Salesforce"
end
post '/send_customer' do
Integration::ContactAccount.new(@config, @payload[:customer]).upsert!
result 200, "Contact for #{@payload[:customer][:email]} updated in Salesforce"
end
post '/send_product' do
Integration::Product.new(@config, @payload[:product]).upsert!
set_summary "Product #{@payload["product"]["id"]} updated (or created) in Salesforce"
result 200
end
post '/send_shipment' do
begin
Integration::Shipment.new(@config, @payload).upsert!
result 200, "Shipment #{@payload[:shipment][:id]} updated as Note in Salesforce"
rescue Faraday::Error::ResourceNotFound
result 500, "Could not find Opportunity # #{@payload[:shipment][:order_id]}"
end
end
post "/get_products" do
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
end
end
post "/get_customers" do
contact_integration = Integration::ContactAccount.new(@config, @payload[:customer])
contacts = contact_integration.fetch_updates
add_value "customers", contacts
if (count = contacts.count) > 0
add_parameter "salesforce_contacts_since", contact_integration.latest_timestamp_update
result 200, "Received #{count} #{"customer".pluralize count} from Salesforce"
else
result 200
end
end
post "/get_orders" do
integration = Integration::Order.new(@config)
orders = integration.fetch_updates
if (count = orders.count) > 0
add_value "orders", orders
add_parameter "salesforce_orders_since", integration.latest_timestamp_update
result 200, "Received #{count} #{"order".pluralize count} from Salesforce"
else
result 200
end
end
end