Skip to content

Commit

Permalink
Merge pull request #1063 from datacite/issue_1054
Browse files Browse the repository at this point in the history
Fixes for activities endpoints.
  • Loading branch information
svogt0511 authored Dec 12, 2023
2 parents 4df4d77 + 4afd1dc commit 6d7767e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 42 deletions.
3 changes: 1 addition & 2 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ def show

protected
def set_activity
response = Activity.find_by_id(params[:id])
@activity = response.results.first
@activity = Activity.find_by_request_uuid!(params[:id])
fail ActiveRecord::RecordNotFound if @activity.blank?
end
end
72 changes: 33 additions & 39 deletions app/serializers/activity_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,49 @@ class ActivitySerializer
:version

attribute :changes do |object, params|
changes = object._source.changes
pub = changes.publisher
pub_obj = changes.publisher_obj
# Determine the source and action based on the object's type
if object.is_a? Activity
changes = object.audited_changes
action = object.action
else
changes = object._source.changes
action = object._source.action
end

ret = changes
# Extract publisher and publisher_obj from changes
pub = changes&.dig("publisher")
pub_obj = changes&.dig("publisher_obj")

if object._source.action == "update"
if pub || pub_obj
if params[:publisher] == "true"
if !pub_obj
changes.publisher =
[
{ "name": pub[0] },
{ "name": pub[1] },
]
else
changes.publisher = changes.publisher_obj
end
# Customize publisher information based on params[:publisher]
if pub || pub_obj
if params&.dig(:publisher) == "true"
if pub_obj
changes["publisher"] = pub_obj
else
if !pub
changes.publisher = [ pub_obj[0].name, pub_obj[1].name ]
end
changes["publisher"] =
action == "update" ? [
pub[0] ? { "name": pub[0] } : nil,
pub[1] ? { "name": pub[1] } : nil
] : { "name": pub }
end

ret = changes
end
elsif object._source.action == "create"
if pub || pub_obj
if params[:publisher] == "true"
if !pub_obj
changes.publisher = { "name": pub }
else
changes.publisher = changes.publisher_obj
end
else
if pub_obj
changes["publisher"] =
action == "update" ? [
pub_obj[0] ? pub_obj[0]["name"] : nil,
pub_obj[1] ? pub_obj[1]["name"] : nil
] : pub_obj["name"]
else
if !pub
changes.publisher = pub_obj.name
end
changes["publisher"] = pub
end

ret = changes
end
end

if pub_obj
changes.delete("publisher_obj")
end
# Remove the not needed "publisher_obj" key
changes.delete("publisher_obj")

ret
# Return the modified changes
changes
end

attribute "prov:wasDerivedFrom", &:was_derived_from
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@

resources :heartbeat, only: %i[index]

resources :activities, only: %i[index show]
resources :activities, only: %i[index show], constraints: { id: /.+/ }

resources :clients, constraints: { id: /.+/ } do
resources :prefixes, constraints: { id: /.+/ }
Expand Down
5 changes: 5 additions & 0 deletions lib/tasks/activity.rake
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ namespace :activity do
puts Activity.monitor_reindex
end

desc "Create alias for activities"
task create_alias: :environment do
puts Activity.create_alias(index: ENV["INDEX"], alias: ENV["ALIAS"])
end

desc "Import all activities"
task import: :environment do
from_id = (ENV["FROM_ID"] || 1).to_i
Expand Down
1 change: 1 addition & 0 deletions lib/tasks/elasticsearch.rake
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace :elasticsearch do
Rake::Task["event:create_alias"].invoke

Rake::Task["activity:create_index"].invoke
Rake::Task["activity:create_alias"].invoke

Rake::Task["contact:create_index"].invoke
Rake::Task["contact:create_alias"].invoke
Expand Down

0 comments on commit 6d7767e

Please sign in to comment.