Skip to content

Commit

Permalink
Merge pull request #1708 from datadryad/salesforce
Browse files Browse the repository at this point in the history
Do not replace existing Salesforce case owner
  • Loading branch information
ryscher authored Jun 18, 2024
2 parents 3d7092a + 1369d1b commit ee2aa2a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/models/stash_engine/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ def changed_related(other_related)
def update_salesforce_metadata
sf_cases = Stash::Salesforce.find_cases_by_doi(identifier&.identifier)
sf_cases&.each do |c|
Stash::Salesforce.update_case_metadata(case_id: c.id, resource: self, update_timestamp: true)
Stash::Salesforce.update_case_metadata(case_id: c.id, resource: self, update_timestamp: true, update_owner: c.owner)
end
end

Expand Down
23 changes: 13 additions & 10 deletions lib/stash/salesforce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def self.case_view_url(case_id: nil, case_num: nil)
end

def self.find_cases_by_doi(doi)
result = db_query("SELECT Id, Status, Case_Reasons__c, Reason, Case_Reason_Other__c FROM Case Where Subject like '%#{doi}%' " \
result = db_query("SELECT Id, Status, OwnerId, Case_Reasons__c, Reason, Case_Reason_Other__c FROM Case Where Subject like '%#{doi}%' " \
"or DOI__c like '%#{doi}%' ")
return unless result && result.size > 0

Expand All @@ -71,7 +71,8 @@ def self.find_cases_by_doi(doi)
id: found.Id,
path: case_view_url(case_num: found.CaseNumber),
status: found.Status,
reason: reason }.to_ostruct
reason: reason,
owner: found.OwnerId.present? }.to_ostruct
end
cases_found
end
Expand Down Expand Up @@ -101,7 +102,7 @@ def self.create_case(identifier:, owner:)

# Update the metadata in a case based on the metadata in a resource
# Updates each field separately, so a failure of one field doesn't impact the others
def self.update_case_metadata(case_id:, resource:, update_timestamp: false)
def self.update_case_metadata(case_id:, resource:, update_timestamp: false, update_owner: false)
return unless case_id.present? && resource.present?

if resource.title.present?
Expand All @@ -115,13 +116,6 @@ def self.update_case_metadata(case_id:, resource:, update_timestamp: false)
kv_hash: { Dataset_Status__c: readable_status })
end

current_editor = StashEngine::User.find(resource.current_editor_id)&.orcid
owner_id = find_user_by_orcid(current_editor)
if owner_id.present?
update(obj_type: 'Case', obj_id: case_id,
kv_hash: { OwnerId: owner_id })
end

if resource.identifier.journal.present?
update(obj_type: 'Case', obj_id: case_id,
kv_hash: { Journal__c: find_account_by_name(resource.identifier.journal&.title) })
Expand All @@ -133,6 +127,15 @@ def self.update_case_metadata(case_id:, resource:, update_timestamp: false)
kv_hash: { Institutional_Affiliation__c: find_account_by_tenant(tenant) })
end

if update_owner
current_editor = StashEngine::User.find(resource.current_editor_id)&.orcid
owner_id = find_user_by_orcid(current_editor)
if owner_id.present?
update(obj_type: 'Case', obj_id: case_id,
kv_hash: { OwnerId: owner_id })
end
end

return unless update_timestamp

update(obj_type: 'Case', obj_id: case_id,
Expand Down

0 comments on commit ee2aa2a

Please sign in to comment.