diff --git a/app/services/submission/plant_line_parser.rb b/app/services/submission/plant_line_parser.rb index 766c9809b..3012511b0 100644 --- a/app/services/submission/plant_line_parser.rb +++ b/app/services/submission/plant_line_parser.rb @@ -18,16 +18,7 @@ def call @upload.log "Detected wrong file format - please make sure the file is not encoded (e.g. you are uploading an xls, instead of a text file)." end - if @upload.errors.empty? - @upload.submission.content.append(:step03, - plant_line_list: @plant_line_names, - new_plant_lines: @new_plant_lines, - new_plant_varieties: @new_plant_varieties, - new_plant_accessions: @new_plant_accessions) - @upload.submission.content.update(:step03, uploaded_plant_lines: @plant_line_names) - @upload.submission.save! - @upload.submission.content.save! - end + update_submission_content if @upload.errors.empty? end private @@ -79,6 +70,36 @@ def parse_plant_lines @upload.log "Detected #{@new_plant_varieties.size} plant line(s) of new (i.e. not existing in BIP) plant variety(ies)." end + def update_submission_content + remove_overriden_content + append_uploaded_content + + @upload.submission.save! + end + + def remove_overriden_content + current_new_plant_lines = @upload.submission.content.new_plant_lines || [] + current_new_plant_varieties = @upload.submission.content.new_plant_varieties || {} + current_new_plant_accessions = @upload.submission.content.new_plant_accessions || {} + + overriden_new_plant_lines = + current_new_plant_lines.select { |npl| @plant_line_names.include?(npl.fetch("plant_line_name")) } + + @upload.submission.content.update(:step03, + new_plant_lines: current_new_plant_lines - overriden_new_plant_lines, + new_plant_varieties: current_new_plant_varieties.except(*@plant_line_names), + new_plant_accessions: current_new_plant_accessions.except(*@plant_line_names)) + end + + def append_uploaded_content + @upload.submission.content.append(:step03, + plant_line_list: @plant_line_names, + new_plant_lines: @new_plant_lines, + new_plant_varieties: @new_plant_varieties, + new_plant_accessions: @new_plant_accessions) + @upload.submission.content.update(:step03, uploaded_plant_lines: @plant_line_names) + end + def csv @csv ||= CSV.new(input) end @@ -97,8 +118,6 @@ def correct_input?(row) if reused_plant_line?(plant_line_name) @upload.log "Ignored row for #{plant_line_name} since a plant line with that name is already defined in the uploaded file." - elsif current_new_plant_lines.include?(plant_line_name) - @upload.log "Ignored row for #{plant_line_name} since a plant line with that name is already defined. Please clear the 'Plant line list' field before re-uploading a CSV file." elsif existing_plant_line?(plant_line_name) && !existing_plant_line_match?(pl_attrs, pv_attrs) @upload.log "Ignored row for #{plant_line_name} since a plant line with that name is already present in BIP "\ "but uploaded data does not match existing record." @@ -192,10 +211,6 @@ def current_plant_lines @current_plant_lines ||= @upload.submission.content.plant_line_list || [] end - def current_new_plant_lines - @current_new_plant_lines ||= (@upload.submission.content.new_plant_lines || []).map { |pl| pl["plant_line_name"] } - end - def header_columns [ "Species", diff --git a/spec/services/submission/plant_line_parser_spec.rb b/spec/services/submission/plant_line_parser_spec.rb index fc845a961..2627da467 100644 --- a/spec/services/submission/plant_line_parser_spec.rb +++ b/spec/services/submission/plant_line_parser_spec.rb @@ -140,52 +140,59 @@ end context "override of previously defined content" do + before { allow(subject).to receive(:parse_header) } + it 'allows override of existing plant line with existing plant line' do pl = create(:plant_line) - submission.content.update(:step03, plant_line_list: pl.plant_line_name) + submission.content.update(:step03, plant_line_list: [pl.plant_line_name]) submission.save! input_is "#{pl.taxonomy_term.name},,,#{pl.plant_line_name}" - subject.send(:parse_plant_lines) - expect(subject.plant_line_names).to eq [pl.plant_line_name] - expect(subject.new_plant_lines).to eq [] + subject.call + expect(submission.reload.content.plant_line_list).to eq [pl.plant_line_name] + expect(submission.content.new_plant_varieties).to be_blank + expect(submission.content.new_plant_accessions).to be_blank end - it 'blocks override of new plant line with new plant line' do - submission.content.update(:step03, plant_line_list: ["new-pl"], - new_plant_lines: [{ plant_line_name: "new-pl", - plant_variety_name: "new-pv", - taxonomy_term: "Brassica napus" }]) + it 'allows override of new plant line with new plant line' do + submission.content.update(:step03, + plant_line_list: ["new-pl"], + new_plant_lines: [{ plant_line_name: "new-pl", plant_variety_name: "new-pv", taxonomy_term: "Brassica napus" }], + new_plant_varieties: { "new-pl" => { "plant_variety_name" => "new-pv" } }, + new_plant_accessions: { "new-pl" => { "plant_accession" => "new-pa" } } + ) submission.save! - input_is "Brassica napus,,,new-pl" - subject.send(:parse_plant_lines) + input_is "Brassica napus,new-pv-2,,new-pl" + subject.call + expect(upload.errors).to be_blank expect(submission.reload.content.plant_line_list).to eq ["new-pl"] expect(submission.content.new_plant_lines). - to eq [{ "plant_line_name" => "new-pl", "plant_variety_name" => "new-pv", "taxonomy_term" => "Brassica napus" }] + to eq [{ "plant_line_name" => "new-pl", "plant_variety_name" => "new-pv-2", "taxonomy_term" => "Brassica napus" }] - expect(upload.logs). - to include "Ignored row for new-pl since a plant line with that name is already defined. "\ - "Please clear the 'Plant line list' field before re-uploading a CSV file." + expect(submission.content.new_plant_varieties).to eq("new-pl" => { "plant_variety_name" => "new-pv-2" }) + expect(submission.content.new_plant_accessions).to be_blank end - it 'blocks override of new plant line by existing plant line' do + it 'allows override of new plant line by existing plant line' do pl = create(:plant_line) - submission.content.update(:step03, plant_line_list: [pl.plant_line_name], - new_plant_lines: [{ plant_line_name: pl.plant_line_name, - taxonomy_term: "Brassica napus" }]) + submission.content.update(:step03, + plant_line_list: [pl.plant_line_name], + new_plant_lines: [{ plant_line_name: pl.plant_line_name, plant_variety_name: "new-pv", taxonomy_term: "Brassica napus" }], + new_plant_varieties: { pl.plant_line_name => { "plant_variety_name" => "new-pv" } }, + new_plant_accessions: { pl.plant_line_name => { "plant_accession" => "new-pa" } } + ) submission.save! input_is "#{pl.taxonomy_term.name},,,#{pl.plant_line_name}" - subject.send(:parse_plant_lines) + subject.call + expect(upload.errors).to be_blank expect(submission.reload.content.plant_line_list).to eq [pl.plant_line_name] - expect(submission.content.new_plant_lines).to eq [{ "plant_line_name" => pl.plant_line_name, - "taxonomy_term" => "Brassica napus" }] - expect(upload.logs). - to include "Ignored row for #{pl.plant_line_name} since a plant line with that name is already defined. "\ - "Please clear the 'Plant line list' field before re-uploading a CSV file." + expect(submission.content.new_plant_lines).to be_blank + expect(submission.content.new_plant_varieties).to be_blank + expect(submission.content.new_plant_accessions).to be_blank end end