uc3-dmp-id 0.1.79 → 0.1.81

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d05302ee8b8190c77e7efc0ad602ad0de2a74739b1653672d707b1fda95eec16
4
- data.tar.gz: 3cca6911d51d58ff974308f8647bab075fb9bf717092bce45f3cbe2a151c7712
3
+ metadata.gz: 3de2c7edd72200592a63d6bad11a19ed289c9f3dfa35272d82ff98ec666b89ab
4
+ data.tar.gz: 7b887212a1babb8f7928ba8bc3062c4c6a282d29af346c36d5e0f61083e67356
5
5
  SHA512:
6
- metadata.gz: e19a514c8233bdf9abc32c375b636611181738f6f64818431d0b3e26b1fd40c3817b5dcf1cfc8e1a270e3c521ad67636dd50baaec7638af9a162ebb864b26975
7
- data.tar.gz: 3b09fdc606348c0c3dddcc7536563920c2971d475981c1b11a89a7147157db712d416d52eafec3689ec0e004be3d845acdf83cbb2113b2df08aca58936421915
6
+ metadata.gz: 8685a865bb59f270cced7d0ffab66cd12a87a03a2d078accb2676a4d28ba73f13cf3d728b829e7e207aecddaa4cc10a53257414cb53c8e13f8cd0559b34e1210
7
+ data.tar.gz: 13886474742a157b539d70b7e77961ab2a5272818ab0bd6df519ea11b7a6d18720b1f4268ec43ee532f3e4493bd92b9a5b31485d0530b38e6450972a821ef429
@@ -37,7 +37,9 @@ module Uc3DmpId
37
37
  funder = args['funder']
38
38
 
39
39
  owner_pks = owner.nil? ? [] : _by_owner(owner: owner, client:, logger:)
40
- org_pks = org.nil? ? [] : _by_org(org: org, client:, logger:)
40
+ # There may be multiple Org ids, so query them all
41
+ org_pks = org.nil? ? [] : org.split('|').map { |o| _by_org(org: o, client:, logger:) }
42
+ org_pks = org_pks.flatten.uniq
41
43
  funder_pks = funder.nil? ? [] : _by_funder(funder: funder, client:, logger:)
42
44
  # pks = [owner_pks, org_pks, funder_pks].reject(&:empty?)
43
45
  logger&.debug(
@@ -172,6 +174,7 @@ module Uc3DmpId
172
174
  def _by_org(org:, client: nil, logger: nil)
173
175
  regex = /^[a-zA-Z0-9]+$/
174
176
  id = "#{ROR_DOMAIN}#{org.strip}" unless (org.to_s =~ regex).nil?
177
+ return [] if id.nil?
175
178
 
176
179
  resp = client.get_item(key: { PK: 'AFFILIATION_INDEX', SK: id }, logger:)
177
180
  return [] unless resp.is_a?(Hash)
@@ -184,7 +187,8 @@ module Uc3DmpId
184
187
  def _by_funder(funder:, client: nil, logger: nil)
185
188
  regex = /^[a-zA-Z0-9]+$/
186
189
  id = "#{ROR_DOMAIN}/#{funder.strip}" unless (funder.to_s =~ regex).nil?
187
- id = "#{DOI_DOMAIN}#{org.strip}" if id.nil? && !(org.to_s =~ Helper::DOI_REGEX).nil?
190
+ id = "#{DOI_DOMAIN}#{funder.strip}" if id.nil? && !(funder.to_s =~ Helper::DOI_REGEX).nil?
191
+ return [] if id.nil?
188
192
 
189
193
  resp = client.get_item(key: { PK: 'FUNDER_INDEX', SK: id }, logger:)
190
194
  return [] unless resp.is_a?(Hash)
@@ -16,9 +16,9 @@ module Uc3DmpId
16
16
  def update(provenance:, p_key:, json: {}, logger: nil)
17
17
  raise UpdaterError, Helper::MSG_DMP_INVALID_DMP_ID unless p_key.is_a?(String) && !p_key.strip.empty?
18
18
 
19
- mods = Helper.parse_json(json:).fetch('dmp', {})
19
+ payload = Helper.parse_json(json:).fetch('dmp', {})
20
20
  p_key = Helper.append_pk_prefix(p_key:)
21
- logger.debug(message: "Incoming modifications for PK #{p_key}", details: mods) if logger.respond_to?(:debug)
21
+ logger.debug(message: "Incoming modifications for PK #{p_key}", details: payload) if logger.respond_to?(:debug)
22
22
 
23
23
  # Fetch the latest version of the DMP ID
24
24
  client = Uc3DmpDynamo::Client.new
@@ -28,29 +28,31 @@ module Uc3DmpId
28
28
 
29
29
  # Verify that the DMP ID is updateable with the info passed in
30
30
  errs = _updateable?(provenance:, p_key:, latest_version: latest_version['dmp'],
31
- mods: mods['dmp'])
31
+ mods: payload['dmp'])
32
32
  logger.error(message: errs.join(', ')) if logger.respond_to?(:error) && errs.is_a?(Array) && errs.any?
33
33
  raise UpdaterError, errs if errs.is_a?(Array) && errs.any?
34
34
  # Don't continue if nothing has changed!
35
- raise UpdaterError, Helper::MSG_NO_CHANGE if Helper.eql?(dmp_a: latest_version, dmp_b: mods)
35
+ raise UpdaterError, Helper::MSG_NO_CHANGE if Helper.eql?(dmp_a: latest_version, dmp_b: payload)
36
36
 
37
37
  # Version the DMP ID record (if applicable).
38
38
  owner = latest_version['dmphub_provenance_id']
39
39
  updater = provenance['PK']
40
40
  version = Versioner.generate_version(client:, latest_version:, owner:,
41
41
  updater:, logger:)
42
+ logger&.debug(message: 'New Version', details: version)
42
43
  raise UpdaterError, Helper::MSG_DMP_UNABLE_TO_VERSION if version.nil?
43
44
  # Bail if the system trying to make the update is not the creator of the DMP ID
44
45
  raise UpdaterError, Helper::MSG_DMP_FORBIDDEN if owner != updater
45
46
 
46
47
  # Handle any changes to the dmphub_modifications section
47
- version = _process_harvester_mods(client:, p_key:, json: version, logger:)
48
+ version = _process_harvester_mods(client:, p_key:, json: payload, version:, logger:)
49
+ logger&.debug(message: 'Version after process_harvester_mods', details: version)
50
+ raise UpdaterError, Helper::MSG_SERVER_ERROR if version.nil?
48
51
 
49
- # Remove the version info because we don't want to save it on the record
52
+ # Remove the version info any any lingering modification blocks
50
53
  version.delete('dmphub_versions')
54
+ version.delete('dmphub_modifications')
51
55
 
52
- # Splice the assertions
53
- version = _process_modifications(owner:, updater:, version:, mods:, logger:)
54
56
  # Set the :modified timestamps
55
57
  now = Time.now.utc
56
58
  version['modified'] = now.iso8601
@@ -67,9 +69,9 @@ module Uc3DmpId
67
69
  logger.info(message: "Updated DMP ID: #{p_key}") if logger.respond_to?(:debug)
68
70
 
69
71
  # Append the :dmphub_versions Array
70
- json = JSON.parse({ dmp: version }.to_json)
71
- json = Versioner.append_versions(p_key:, dmp: json, client:, logger:)
72
- Helper.cleanse_dmp_json(json:)
72
+ out = JSON.parse({ dmp: version }.to_json)
73
+ out = Versioner.append_versions(p_key:, dmp: out, client:, logger:)
74
+ Helper.cleanse_dmp_json(json: out)
73
75
  end
74
76
  # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
75
77
  # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
@@ -130,24 +132,6 @@ module Uc3DmpId
130
132
  end
131
133
  # rubocop:enable Metrics/AbcSize
132
134
 
133
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
134
- def _process_modifications(owner:, updater:, version:, mods:, logger: nil)
135
- return version unless mods.is_a?(Hash) && !updater.nil?
136
- return mods unless version.is_a?(Hash) && !owner.nil?
137
-
138
- logger.debug(message: 'Modifications before merge.', details: mods) if logger.respond_to?(:debug)
139
- keys_to_retain = version.keys.select do |key|
140
- (key.start_with?('dmphub_') && !%w[dmphub_modifications dmphub_versions].include?(key)) ||
141
- key.start_with?('PK') || key.start_with?('SK') || key.start_with?('dmproadmap_related_identifiers')
142
- end
143
- keys_to_retain.each do |key|
144
- mods[key] = version[key]
145
- end
146
- logger.debug(message: 'Modifications after merge.', details: mods) if logger.respond_to?(:debug)
147
- mods
148
- end
149
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
150
-
151
135
  # Once the DMP has been updated, we need to update it's DOI metadata
152
136
  # -------------------------------------------------------------------------
153
137
  # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
@@ -191,32 +175,65 @@ module Uc3DmpId
191
175
  # rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
192
176
 
193
177
  # Fetch any Harvester modifications to the JSON
194
- def _process_harvester_mods(client:, p_key:, json:, logger: nil)
195
- return json if json.fetch('dmphub_modifications', []).empty?
178
+ def _process_harvester_mods(client:, p_key:, json:, version:, logger: nil)
179
+ logger&.debug(message: 'Incoming modifications', details: json)
180
+ return version if json.fetch('dmphub_modifications', []).empty?
196
181
 
197
182
  # Fetch the `"SK": "HARVESTER_MODS"` record
198
183
  client = Uc3DmpDynamo::Client.new if client.nil?
199
184
  resp = client.get_item(
200
185
  key: { PK: Helper.append_pk_prefix(p_key:), SK: Helper::SK_HARVESTER_MODS }, logger:
201
186
  )
202
- return json unless resp.is_a?(Hash) && resp['related_works'].is_a?(Hash)
187
+ return version unless resp.is_a?(Hash) && resp['related_works'].is_a?(Hash)
203
188
 
189
+ logger&.debug(message: 'Original HARVESTER_MODS record', details: resp)
204
190
  # The `dmphub_modifications` array will ONLY ever have things the harvester mods know about
205
191
  # so just find them and update the status accordingly
206
- mods = resp.dup
192
+ original = resp.dup
193
+ json['dmproadmap_related_identifiers'] = [] if json['dmproadmap_related_identifiers'].nil?
194
+
207
195
  json['dmphub_modifications'].each do |entry|
208
- next if entry.fetch('dmproadmap_related_identifiers', []).empty?
196
+ next if entry.is_a?(Hash) && entry.fetch('dmproadmap_related_identifiers', []).empty?
209
197
 
210
198
  entry['dmproadmap_related_identifiers'].each do |related|
211
- next if mods['related_works'][related.identifier].nil?
212
-
213
- mods['related_works'][related.identifier]['status'] = related['status']
199
+ # Detrmine if the HARVESTER_MODS record even knows about the mod
200
+ related_id = related.respond_to?(:identifier) ? related.identifier : related['identifier']
201
+ related_domain = related.respond_to?(:domain) ? related.domain : related['domain']
202
+ key = "#{related_domain.end_with?('/') ? related_domain : "#{related_domain}/"}#{related_id}"
203
+ key_found = original['related_works'].has_key?(key)
204
+ logger&.debug(message: "No matching HARVEST_MOD found for #{key}") unless key_found
205
+ next unless key_found
206
+
207
+ # Update the status in the HARVESTER_MODS record
208
+ logger&.debug(message: "Updating status for #{key} from #{original['related_works'][key]['status']} to #{related['status']}")
209
+ original['related_works'][key]['status'] = related['status']
210
+
211
+ existing = version['dmproadmap_related_identifiers'].select do |ri|
212
+ ri['identifier'] == key
213
+ end
214
+
215
+ # Add it if it was approved and doesn't exist in dmproadmap_related_identifiers
216
+ if related['status'] == 'approved' && existing.empty?
217
+ version['dmproadmap_related_identifiers'] << JSON.parse({
218
+ identifier: key,
219
+ work_type: related['work_type'],
220
+ type: related['type'],
221
+ descriptor: related['descriptor'],
222
+ citation: related['citation']
223
+ }.to_json)
224
+ elsif related['status'] == 'rejected' && existing.any?
225
+ # otherwise remove it
226
+ version['dmproadmap_related_identifiers'] = version['dmproadmap_related_identifiers'].reject { |ri| ri == existing.first }
227
+ end
214
228
  end
215
229
  end
216
230
 
217
- client.put_item(json: mods, logger:)
218
- json.delete('dmphub_modifications')
219
- json
231
+ logger&.debug(message: 'Updating HARVESTER_MODS with:', details: original)
232
+ resp = client.put_item(json: original, logger:)
233
+ logger&.error(message: 'Unable to update HARVESTER_MODS', details: original) if resp.nil?
234
+
235
+ logger&.debug(message: 'Returning updated VERSION:', details: version)
236
+ version
220
237
  end
221
238
  end
222
239
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Uc3DmpId
4
- VERSION = '0.1.79'
4
+ VERSION = '0.1.81'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uc3-dmp-id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.79
4
+ version: 0.1.81
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Riley