Skip to content

Commit

Permalink
Merge pull request #163 from CDLUC3/bug/update-related-works
Browse files Browse the repository at this point in the history
fix for issue with Uc3DmpId gem updater class not saving properly
  • Loading branch information
briri authored Jul 17, 2024
2 parents a732e4f + b62d36a commit a6b7368
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/sam/gems/uc3-dmp-id/lib/uc3-dmp-id/creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def create(provenance:, json:, logger: nil)
annotated['registered'] = annotated['created'] if annotated['registered'].nil?

# Create the item
annotated['dmphub_modifications'] = []
resp = client.put_item(json: annotated, logger:)
raise CreatorError, Helper::MSG_DMP_NO_DMP_ID if resp.nil?

Expand Down
58 changes: 46 additions & 12 deletions src/sam/gems/uc3-dmp-id/lib/uc3-dmp-id/updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,21 @@ def update(provenance:, p_key:, json: {}, logger: nil)
logger&.debug(message: 'Version after process_harvester_mods', details: version)
raise UpdaterError, Helper::MSG_SERVER_ERROR if version.nil?

# Remove the version info any any lingering modification blocks
version.delete('dmphub_versions')
version.delete('dmphub_modifications')

# Set the :modified timestamps
now = Time.now.utc
version['modified'] = now.iso8601
version['dmphub_modification_day'] = now.strftime('%Y-%m-%d')
# Process the incoming payload
payload = _process_modifications(owner:, updater:, version:, payload:, logger:)

# Save the changes
resp = client.put_item(json: version, logger:)
resp = client.put_item(json: _purge_mods(payload:), logger:)
raise UpdaterError, Helper::MSG_DMP_UNABLE_TO_VERSION if resp.nil?

# Send the updates to EZID
_post_process(provenance:, json: version, logger:)
_post_process(provenance:, json: payload, logger:)

# Return the new version record
logger.info(message: "Updated DMP ID: #{p_key}") if logger.respond_to?(:debug)

# Append the :dmphub_versions Array
out = JSON.parse({ dmp: version }.to_json)
out = JSON.parse({ dmp: payload }.to_json)
out = Versioner.append_versions(p_key:, dmp: out, client:, logger:)
Helper.cleanse_dmp_json(json: out)
end
Expand Down Expand Up @@ -106,7 +100,7 @@ def attach_narrative(provenance:, p_key:, url:, logger: nil)

# Save the changes without creating a new version!
logger&.debug(message: "DMP After narrative attachment", details: dmp)
resp = client.put_item(json: dmp, logger:)
resp = client.put_item(json: _purge_mods(payload: dmp), logger:)
raise UpdaterError, Helper::MSG_DMP_UNABLE_TO_VERSION if resp.nil?

logger&.debug(message: "Added DMP ID narrative for PK: #{p_key}, Narrative: #{url}")
Expand All @@ -116,6 +110,12 @@ def attach_narrative(provenance:, p_key:, url:, logger: nil)

private

def _purge_mods(payload:)
# Ensure we've deleted the dmphub_mods!!!!
payload['dmphub_modifications'] = []
payload
end

# Check to make sure the incoming JSON is valid, the DMP ID requested matches the DMP ID in the JSON
# rubocop:disable Metrics/AbcSize
def _updateable?(provenance:, p_key:, latest_version: {}, mods: {})
Expand Down Expand Up @@ -235,6 +235,40 @@ def _process_harvester_mods(client:, p_key:, json:, version:, logger: nil)
logger&.debug(message: 'Returning updated VERSION:', details: version)
version
end

# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def _process_modifications(owner:, updater:, version:, payload:, logger: nil)
return version unless payload.is_a?(Hash) && !updater.nil?
return payload unless version.is_a?(Hash) && !owner.nil?

logger.debug(message: 'Modifications before processing.', details: payload) if logger.respond_to?(:debug)

excluded_keys = %w[modified dmphub_modifications dmphub_versions]

# Always remove the dmphub_versions and dmphub_modifications
excluded_keys.each { |key| payload.delete(key) }

# Always include the PK and SK!
payload['PK'] = version['PK']
payload['SK'] = Helper::DMP_LATEST_VERSION

# Set the :modified timestamps
now = Time.now.utc
version['modified'] = now.iso8601
version['dmphub_modification_day'] = now.strftime('%Y-%m-%d')

# Retain all the other attributes on the original version unless they are
# in the list of exclusions OR the incoming payload already has a value for it
version.keys.each do |key|
next if excluded_keys.include?(key) || !payload[key].nil?

payload[key] = version[key]
end
logger.debug(message: 'Modifications after processing.', details: payload) if logger.respond_to?(:debug)
payload
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

end
end
end
2 changes: 1 addition & 1 deletion src/sam/gems/uc3-dmp-id/lib/uc3-dmp-id/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Uc3DmpId
VERSION = '0.1.78'
VERSION = '0.1.86'
end
1 change: 1 addition & 0 deletions src/sam/gems/uc3-dmp-id/lib/uc3-dmp-id/versioner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def generate_version(client:, latest_version:, owner:, updater:, logger: nil)

# Create the prior version record ()
client = Uc3DmpDynamo::Client.new if client.nil?
prior['dmphub_modifications'] = []
resp = client.put_item(json: prior, logger:)
return nil if resp.nil?

Expand Down
Binary file removed src/sam/gems/uc3-dmp-id/uc3-dmp-id-0.1.78.gem
Binary file not shown.
Binary file added src/sam/gems/uc3-dmp-id/uc3-dmp-id-0.1.86.gem
Binary file not shown.
6 changes: 3 additions & 3 deletions src/sam/layers/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ GEM
racc (1.8.0)
rainbow (3.1.1)
regexp_parser (2.9.2)
rexml (3.3.1)
rexml (3.3.2)
strscan
rspec (3.13.0)
rspec-core (~> 3.13.0)
Expand Down Expand Up @@ -147,15 +147,15 @@ GEM
aws-sdk-ssm (~> 1.150)
httparty (~> 0.21.0)
json (~> 2.6)
uc3-dmp-id (0.1.78)
uc3-dmp-id (0.1.86)
json (~> 2.6)
json-schema (~> 3.0)
text (~> 1.3)
uc3-dmp-citation (~> 0.0)
uc3-dmp-dynamo (~> 0.0)
uc3-dmp-event-bridge (~> 0.0)
uc3-dmp-external-api (~> 0.0)
uc3-dmp-provenance (0.0.13)
uc3-dmp-provenance (0.0.16)
json (~> 2.6)
uc3-dmp-cognito (~> 0.0)
uc3-dmp-dynamo (~> 0.0)
Expand Down

0 comments on commit a6b7368

Please sign in to comment.