diff --git a/lib/SF_services/payment.rb b/lib/SF_services/payment.rb index 2ed96ae..1de9081 100644 --- a/lib/SF_services/payment.rb +++ b/lib/SF_services/payment.rb @@ -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 diff --git a/lib/integrations/order.rb b/lib/integrations/order.rb index 4dfb5e5..d705999 100644 --- a/lib/integrations/order.rb +++ b/lib/integrations/order.rb @@ -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 @@ -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 diff --git a/lib/integrations/payment.rb b/lib/integrations/payment.rb deleted file mode 100644 index 81c281b..0000000 --- a/lib/integrations/payment.rb +++ /dev/null @@ -1,36 +0,0 @@ -module Integration - class Payment < Base - - attr_reader :object - - def initialize(config, object) - @object = object.with_indifferent_access - super(config) - end - - def upsert!(payment) - payment_service.upsert!(payment_params(payment), order_id, email) - end - - def import! - look_up('payments').each { |p| upsert!(p) } - end - - def order_id - look_up('id') - end - - def email - look_up('email') - end - - def look_up(what) - object[what] - end - - def payment_params(payment) - Integration::Builder::Payment.new(payment).build - end - - end -end diff --git a/lib/salesforce_integration.rb b/lib/salesforce_integration.rb index 1bf67ff..5fe9ea5 100644 --- a/lib/salesforce_integration.rb +++ b/lib/salesforce_integration.rb @@ -9,7 +9,6 @@ require 'integrations/contact_account' require 'integrations/product' require 'integrations/order' -require 'integrations/payment' require 'integrations/return' require 'integrations/builders/account' diff --git a/salesforce_endpoint.rb b/salesforce_endpoint.rb index 2c40a27..a43879f 100644 --- a/salesforce_endpoint.rb +++ b/salesforce_endpoint.rb @@ -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