diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index f81ee1385..0daa1a255 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -287,14 +287,14 @@ def wizard_mode? def update_work @wizard_mode = wizard_mode? - + upload_service = WorkUploadsEditService.new(@work, current_user) if @work.approved? upload_keys = work_params[:deleted_uploads] || [] - deleted_uploads = WorkUploadsEditService.find_post_curation_uploads(work: @work, upload_keys: upload_keys) + deleted_uploads = upload_service.find_post_curation_uploads(upload_keys: upload_keys) return head(:forbidden) unless deleted_uploads.empty? else - @work = WorkUploadsEditService.update_precurated_file_list(@work, work_params) + @work = upload_service.update_precurated_file_list(work_params) end process_updates diff --git a/app/models/work.rb b/app/models/work.rb index 838ed0a93..627a37bb0 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -362,6 +362,11 @@ def log_changes(resource_compare, current_user) WorkActivity.add_system_activity(id, resource_compare.differences.to_json, current_user.id, activity_type: "CHANGES") end + def log_file_changes(changes, current_user) + return if changes.count == 0 + WorkActivity.add_system_activity(id, changes.to_json, current_user.id, activity_type: "FILE-CHANGES") + end + def activities WorkActivity.where(work_id: id).sort_by(&:updated_at).reverse end diff --git a/app/models/work_activity.rb b/app/models/work_activity.rb index 037cb5c81..e9f7dd224 100644 --- a/app/models/work_activity.rb +++ b/app/models/work_activity.rb @@ -42,6 +42,8 @@ def created_by_user def message_html if activity_type == "CHANGES" changes_html + elsif activity_type == "FILE-CHANGES" + file_changes_html else comment_html end @@ -62,7 +64,16 @@ def comment_html Kramdown::Document.new(text).to_html end - # Returns the message formatted to display changes that were logged as an activity + # Returns the message formatted to display _file_ changes that were logged as an activity + def file_changes_html + changes = JSON.parse(message) + changes_html = changes.map do |change| + "#{change['action']} #{change['filename']}
" + end + "

Files: #{changes_html.join}

" + end + + # Returns the message formatted to display _metadata_ changes that were logged as an activity def changes_html text = "" changes = JSON.parse(message) diff --git a/app/services/work_uploads_edit_service.rb b/app/services/work_uploads_edit_service.rb index 67b6b5371..30348cb11 100644 --- a/app/services/work_uploads_edit_service.rb +++ b/app/services/work_uploads_edit_service.rb @@ -1,50 +1,76 @@ # frozen_string_literal: true class WorkUploadsEditService - class << self - def update_precurated_file_list(work, work_params) - if work_params.key?(:deleted_uploads) || work_params.key?(:pre_curation_uploads) || work_params.key?(:replaced_uploads) - if work_params.key?(:deleted_uploads) - delete_pre_curation_uploads(work.pre_curation_uploads, work_params[:deleted_uploads]) - elsif work_params.key?(:pre_curation_uploads) - work.pre_curation_uploads.each(&:purge) - work.reload # reload the work to pick up the changes in the attachments - Array(work_params[:pre_curation_uploads]).each { |new_upload| work.pre_curation_uploads.attach(new_upload) } - elsif work_params.key?(:replaced_uploads) - replace_uploads(work, work_params[:replaced_uploads]) + def initialize(work, current_user) + @work = work + @current_user = current_user + @changes = [] + end + + def work + @work + end + + def update_precurated_file_list(work_params) + if work_params.key?(:deleted_uploads) || work_params.key?(:pre_curation_uploads) || work_params.key?(:replaced_uploads) + if work_params.key?(:deleted_uploads) + # delete the files indicated in the parameters + delete_pre_curation_uploads(work_params[:deleted_uploads]) + elsif work_params.key?(:pre_curation_uploads) + # delete all existing uploads and then and then add the ones indicated in the parameters + work.pre_curation_uploads.each do |existing_upload| + track_change(:deleted, existing_upload.filename.to_s) + existing_upload.purge end work.reload # reload the work to pick up the changes in the attachments - - else # no changes in the parameters, just return the original work - work + Array(work_params[:pre_curation_uploads]).each do |new_upload| + track_change(:added, new_upload.original_filename) + work.pre_curation_uploads.attach(new_upload) + end + elsif work_params.key?(:replaced_uploads) + # replace the files indicated in the parameters + replace_uploads(work_params[:replaced_uploads]) end + + work.log_file_changes(@changes, @current_user) + work.reload # reload the work to pick up the changes in the attachments + else # no changes in the parameters, just return the original work + work end + end - def find_post_curation_uploads(work:, upload_keys: []) - return [] unless work.approved? && !upload_keys.empty? + def find_post_curation_uploads(upload_keys: []) + return [] unless work.approved? && !upload_keys.empty? + work.post_curation_uploads.select { |upload| upload_keys.include?(upload.key) } + end - work.post_curation_uploads.select { |upload| upload_keys.include?(upload.key) } + private + + def replace_uploads(replaced_uploads_params) + new_uploads = [] + work.pre_curation_uploads.each_with_index do |existing, i| + key = i.to_s + next unless replaced_uploads_params.key?(key) + new_uploads << replaced_uploads_params[key] + track_change(:deleted, existing.filename.to_s) + existing.purge + end + work.reload + new_uploads.each do |new_upload| + track_change(:added, new_upload.original_filename) + work.pre_curation_uploads.attach(new_upload) + end end - private - - def replace_uploads(work, replaced_uploads_params) - new_uploads = [] - work.pre_curation_uploads.each_with_index do |existing, i| - key = i.to_s - next unless replaced_uploads_params.key?(key) - new_uploads << replaced_uploads_params[key] - existing.purge - end - work.reload - new_uploads.each { |new_upload| work.pre_curation_uploads.attach(new_upload) } + def delete_pre_curation_uploads(deleted_uploads_params) + work.pre_curation_uploads.each do |existing| + if deleted_uploads_params.key?(existing.key) && deleted_uploads_params[existing.key] == "1" + track_change(:deleted, existing.filename.to_s) + existing.purge end + end + end - def delete_pre_curation_uploads(persisted_pre_curation_uploads, deleted_uploads_params) - persisted_pre_curation_uploads.each do |existing| - if deleted_uploads_params.key?(existing.key) && deleted_uploads_params[existing.key] == "1" - existing.purge - end - end - end - end + def track_change(action, filename) + @changes << { action: action, filename: filename } + end end