Skip to content

Commit

Permalink
refactor investigation controller, and handle external id mismatch th…
Browse files Browse the repository at this point in the history
…rough an exception #1981
  • Loading branch information
stuzart committed Sep 24, 2024
1 parent da8e381 commit e54eb13
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
25 changes: 10 additions & 15 deletions app/controllers/investigations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,18 @@ def new_object_based_on_existing_one

def submit_fairdata_station
path = params[:datastation_data].path
datastation_inv = Seek::FairDataStation::Reader.new.parse_graph(path).first
error = false
if datastation_inv.external_id != @investigation.external_identifier
flash[:error] = "This #{t('investigation')} does not match the identifier provided in the FAIR Data Station metadata"
error = true
else
begin
Investigation.transaction do
@investigation = Seek::FairDataStation::Writer.new.update_isa(@investigation, datastation_inv, current_person, @investigation.projects, @investigation.policy)
@investigation.save!
end
rescue ActiveRecord::RecordInvalid => e
flash[:error] = e.message
error = true
data_station_inv = Seek::FairDataStation::Reader.new.parse_graph(path).first

begin
Investigation.transaction do
@investigation = Seek::FairDataStation::Writer.new.update_isa(@investigation, data_station_inv, current_person, @investigation.projects, @investigation.policy)
@investigation.save!
end
rescue ActiveRecord::RecordInvalid, Seek::FairDataStation::ExternalIdMismatchException => e
flash[:error] = e.message
end
if error

if flash[:error].present?
respond_to do |format|
format.html { render action: :update_from_fairdata_station, status: :unprocessable_entity }
end
Expand Down
5 changes: 5 additions & 0 deletions lib/seek/fair_data_station/writer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Seek
module FairDataStation
class ExternalIdMismatchException < RuntimeError; end;
class Writer
def construct_isa(datastation_inv, contributor, projects, policy)
reset_data_file_cache
Expand All @@ -23,6 +24,10 @@ def construct_isa(datastation_inv, contributor, projects, policy)
end

def update_isa(investigation, datastation_inv, contributor, projects, policy)
unless investigation.external_identifier == datastation_inv.external_id
raise ExternalIdMismatchException.new('Investigation external identifiers do not match')
end

preload_data_file_cache(investigation.related_data_files)
update_entity(investigation, datastation_inv, contributor, projects, policy)
datastation_inv.studies.each do |datastation_study|
Expand Down
4 changes: 2 additions & 2 deletions test/functional/investigations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1378,8 +1378,8 @@ def test_title
assert_no_difference('Assay.count') do
post :submit_fairdata_station, params: {id: investigation, datastation_data: ttl_file }
assert_response :unprocessable_entity
assert_match /This Investigation does not match the identifier provided in the FAIR Data Station metadata/, flash[:error]
assert_select 'div#error_flash', text: /This Investigation does not match the identifier provided in the FAIR Data Station metadata/
assert_match /Investigation external identifiers do not match/, flash[:error]
assert_select 'div#error_flash', text: /Investigation external identifiers do not match/
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions test/unit/fair_data_station/fair_data_station_writer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,24 @@ class FairDataStationWriterTest < ActiveSupport::TestCase
assert_equal 'seek-test-study-1', assay.study.external_identifier
end

test 'update_isa id mis match' do
investigation = setup_test_case_investigation
policy = investigation.policy
projects = investigation.projects
contributor = investigation.contributor

path = "#{Rails.root}/test/fixtures/files/fairdatastation/demo.ttl"
inv = Seek::FairDataStation::Reader.new.parse_graph(path).first

refute_equal investigation.external_identifier, inv.external_id

assert_raises(Seek::FairDataStation::ExternalIdMismatchException) do
investigation = Seek::FairDataStation::Writer.new.update_isa(investigation, inv, contributor,
projects, policy)
end

end

private

def setup_test_case_investigation
Expand Down

0 comments on commit e54eb13

Please sign in to comment.