Skip to content

Commit

Permalink
IN-392 Changes the finder to avoid duplications
Browse files Browse the repository at this point in the history
  • Loading branch information
reinaldob committed Oct 28, 2014
1 parent 4aa8af3 commit 5806cf9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
5 changes: 5 additions & 0 deletions lib/SF_services/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ class Account < Base
def initialize(config)
super("Account", config)
end

def find_account_id_by_email(email)
results = salesforce.query("select Id from Account where PersonEmail = '#{email}'")
results.any? ? results.first['Account__c'] : nil
end
end
end
8 changes: 2 additions & 6 deletions lib/SF_services/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module SFService
class Order < Base
def initialize(config)
super("Opportunity", config)
@account_service = SFService::Account.new(config)
end

def is_present?(id)
Expand All @@ -13,18 +14,13 @@ def find(id)
salesforce.find('Opportunity', id, 'Name')
end

def find_account_id_by_email(email)
results = salesforce.query("select Id from Account where PersonEmail = '#{email}'")
results.any? ? results.first['Account__c'] : nil
end

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

def upsert!(order_attr = {})
account_id = find_account_id_by_email(order_attr.fetch 'AccountId')
account_id = @account_service.find_account_id_by_email(order_attr.fetch 'AccountId')
price_book_id = find_price_book_by_id(order_attr.fetch 'Pricebook2Id')

order_attr = order_attr.merge( { 'AccountId' => account_id } ) if account_id.present?
Expand Down

0 comments on commit 5806cf9

Please sign in to comment.