Skip to content

Commit

Permalink
Move Payment logic into Integration Order as well
Browse files Browse the repository at this point in the history
  • Loading branch information
huoxito committed Dec 17, 2014
1 parent 59435d8 commit f0bb30e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 52 deletions.
19 changes: 6 additions & 13 deletions lib/SF_services/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,16 @@ def initialize(config)
super("Note", config)
end

def is_present?(number)
results = salesforce.query("select Id from Note where Title = 'Payment:#{number}'")
def find_note_by_title(title)
results = salesforce.query("SELECT Id FROM Note WHERE Title = 'Payment:#{title}' LIMIT 1")
results.any? ? results.first.fetch('Id') : nil
end

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

def upsert!(payment_attr = {}, order_code, email)
order_id = find_order(order_code)

payment_attr = payment_attr.merge( { 'ParentId' => order_id } ) if order_id.present?
def upsert!(payment_attr = {}, opportunity_id)
payment_attr = payment_attr.merge ParentId: opportunity_id

payment_id = is_present?(payment_attr.fetch 'Title')
payment_id.present? ? update!(payment_attr.merge({ Id: payment_id })) : create!(payment_attr)
note_id = find_note_by_title(payment_attr.fetch 'Title')
note_id.present? ? update!(payment_attr.merge(Id: note_id)) : create!(payment_attr)
end
end
end
13 changes: 13 additions & 0 deletions lib/integrations/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def upsert!
# Create or Update Opportunity line
line_item_integration.upsert! line_item, opportunity_id, pricebook_entry_id
end

payment_integration = Payment.new(config)

object[:order][:payments].each do |payment|
payment_integration.upsert! payment, opportunity_id
end
end

private
Expand All @@ -54,4 +60,11 @@ def upsert!(item, opportunity_id, pricebook_entry_id)
line_item_service.upsert!(params, opportunity_id, pricebook_entry_id)
end
end

class Payment < Base
def upsert!(payment, opportunity_id)
attributes = Integration::Builder::Payment.new(payment).build
payment_service.upsert! attributes, opportunity_id
end
end
end
36 changes: 0 additions & 36 deletions lib/integrations/payment.rb

This file was deleted.

1 change: 0 additions & 1 deletion lib/salesforce_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
require 'integrations/contact_account'
require 'integrations/product'
require 'integrations/order'
require 'integrations/payment'
require 'integrations/return'

require 'integrations/builders/account'
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::Payment.new(@config, @payload[:order]).import!

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

0 comments on commit f0bb30e

Please sign in to comment.