Skip to content

Commit 31fb8d9

Browse files
committed
Move org processing code to HashToOrgService
1 parent 7ca17b2 commit 31fb8d9

File tree

2 files changed

+40
-41
lines changed

2 files changed

+40
-41
lines changed

app/controllers/contributors_controller.rb

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,7 @@ def translate_roles(hash:)
114114
def process_org(hash:)
115115
return hash unless hash.present? && hash[:org_id].present?
116116

117-
# Check if org already exists in the DB
118-
existing_org = existing_org_from_hash(hash)
119-
120-
return finalize_org_hash(hash, existing_org, false) if existing_org.present?
121-
122-
# Check if entered org has a valid ROR
123-
# If so, allow creation
124-
allow = validate_ror(hash)
117+
hash, allow = OrgSelection::HashToOrgService.process_org(hash: hash)
125118

126119
# If org ROR cannot be validated, then contributor org is invalid
127120
# Only trigger flash during a real request (tests calling this method directly have no request object)
@@ -137,25 +130,6 @@ def process_org(hash:)
137130
finalize_org_hash(hash, org, allow)
138131
end
139132

140-
def parse_org_json(hash)
141-
JSON.parse(hash[:org_id]) rescue nil # rubocop:disable Style/RescueModifier
142-
end
143-
144-
def existing_org_from_hash(hash)
145-
# hash[:org_id] is a JSON string like:
146-
# "{\"id\":1200,\"name\":\"Some Org\",\"ror\":\"https://ror.org/123\"}"
147-
# So it must be parsed into a Ruby hash for HashToOrgService
148-
parsed = parse_org_json(hash)
149-
150-
return nil unless parsed.present?
151-
152-
# Returns org in DB if it exists
153-
OrgSelection::HashToOrgService.to_org(
154-
hash: parsed.to_h,
155-
allow_create: false
156-
)
157-
end
158-
159133
def finalize_org_hash(hash, org, allow)
160134
hash = remove_org_selection_params(params_in: hash)
161135

@@ -166,20 +140,6 @@ def finalize_org_hash(hash, org, allow)
166140
hash
167141
end
168142

169-
def validate_ror(hash)
170-
# Make external API call to ROR to find org with entered name
171-
ror_result = ExternalApis::RorService.search(term: hash[:org_name]).first
172-
173-
# Find ROR value in org hash
174-
parsed_ror = parse_org_json(hash)['ror']
175-
176-
# If org hash ROR and external ROR values are the same
177-
# The org is valid and has the correct ROR
178-
return ror_result[:ror] == parsed_ror if ror_result.present? && parsed_ror.present?
179-
180-
false
181-
end
182-
183143
# When creating, just remove the ORCID if it was left blank
184144
def process_orcid_for_create(hash:)
185145
return hash unless hash[:identifiers_attributes].present?

app/services/org_selection/hash_to_org_service.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,47 @@ def to_identifiers(hash:)
6161
end
6262
# rubocop:enable Metrics/AbcSize
6363

64+
def process_org(hash:)
65+
# Check if org already exists in the DB
66+
parsed = parse_org_json(hash)
67+
68+
return nil unless parsed.present?
69+
70+
# Returns org in DB if it exists
71+
existing_org = to_org(
72+
hash: parsed.to_h,
73+
allow_create: false
74+
)
75+
76+
return [hash, false] if existing_org.present?
77+
78+
# Check if entered org has a valid ROR
79+
# If so, allow creation
80+
allow = validate_ror(hash)
81+
82+
[hash, allow]
83+
end
84+
6485
private
6586

87+
def validate_ror(hash)
88+
# Make external API call to ROR to find org with entered name
89+
ror_result = ExternalApis::RorService.search(term: hash[:org_name]).first
90+
91+
# Find ROR value in org hash
92+
parsed_ror = parse_org_json(hash)['ror']
93+
94+
# If org hash ROR and external ROR values are the same
95+
# The org is valid and has the correct ROR
96+
return ror_result[:ror] == parsed_ror if ror_result.present? && parsed_ror.present?
97+
98+
false
99+
end
100+
101+
def parse_org_json(hash)
102+
JSON.parse(hash[:org_id]) rescue nil # rubocop:disable Style/RescueModifier
103+
end
104+
66105
# Lookup the Org by it's :id and return if the name matches the search
67106
def lookup_org_by_id(hash:)
68107
org = Org.where(id: hash[:id]).first if hash[:id].present?

0 commit comments

Comments
 (0)