From 6a4d50e44ac94fad8112708650dbdc3e32e0fdd3 Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Wed, 10 Jul 2024 15:46:24 +0200 Subject: [PATCH 1/2] Change import activites and flash messages [SCI-10853] --- app/controllers/repositories_controller.rb | 12 ++++++-- .../repository_import_parser/importer.rb | 29 +++++++++++-------- config/initializers/extends.rb | 4 +-- config/locales/en.yml | 4 +-- config/locales/global_activities/en.yml | 6 ++-- .../repository_import_parser/importer_spec.rb | 2 +- 6 files changed, 36 insertions(+), 21 deletions(-) diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 2c65e7a66..6b4a35750 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -314,10 +314,18 @@ class RepositoriesController < ApplicationController preview: import_params[:preview] ).import! message = t('repositories.import_records.partial_success_flash', - nr: status[:nr_of_added], total_nr: status[:total_nr]) + successful_rows_count: (status[:created_rows_count] + status[:updated_rows_count]), + total_rows_count: status[:total_rows_count]) if status[:status] == :ok - log_activity(:import_inventory_items, num_of_items: status[:nr_of_added]) + unless import_params[:preview] && (status[:created_rows_count] + status[:updated_rows_count]).positive? + log_activity( + :inventory_items_added_or_updated_with_import, + created_rows_count: status[:created_rows_count], + updated_rows_count: status[:updated_rows_count] + ) + end + render json: import_params[:preview] ? status : { message: message }, status: :ok else render json: { message: message }, status: :unprocessable_entity diff --git a/app/utilities/repository_import_parser/importer.rb b/app/utilities/repository_import_parser/importer.rb index 7fd39e506..e7a9a2228 100644 --- a/app/utilities/repository_import_parser/importer.rb +++ b/app/utilities/repository_import_parser/importer.rb @@ -15,12 +15,12 @@ module RepositoryImportParser @columns = [] @name_index = -1 @id_index = nil - @total_new_rows = 0 - @new_rows_added = 0 + @created_rows_count = 0 + @updated_rows_count = 0 @header_skipped = false @repository = repository @sheet = sheet - @rows = SpreadsheetParser.spreadsheet_enumerator(@sheet) + @rows = SpreadsheetParser.spreadsheet_enumerator(@sheet).compact_blank @mappings = mappings @user = user @repository_columns = @repository.repository_columns @@ -60,7 +60,7 @@ module RepositoryImportParser def check_for_duplicate_columns col_compact = @columns.compact if col_compact.map(&:id).uniq.length != col_compact.length - { status: :error, nr_of_added: @new_rows_added, total_nr: @total_new_rows } + { status: :error, total_rows_count: total_rows_count, updated_rows_count: @updated_rows_count, created_rows_count: @created_rows_count } end end @@ -78,8 +78,6 @@ module RepositoryImportParser duplicate_ids = SpreadsheetParser.duplicate_ids(@sheet) @rows.each do |row| - next if row.blank? - unless @header_skipped @header_skipped = true next @@ -88,8 +86,6 @@ module RepositoryImportParser incoming_row = SpreadsheetParser.parse_row(row, @sheet, date_format: @user.settings['date_format']) next if incoming_row.compact.blank? - @total_new_rows += 1 - if @id_index id = incoming_row[@id_index].to_s.gsub(RepositoryRow::ID_PREFIX, '') @@ -129,8 +125,12 @@ module RepositoryImportParser include: [:repository_cells] ).as_json - { status: :ok, nr_of_added: @new_rows_added, total_nr: @total_new_rows, changes: changes, - import_date: I18n.l(Date.today, format: :full_date) } + { status: :ok, + total_rows_count: total_rows_count, + created_rows_count: @created_rows_count, + updated_rows_count: @updated_rows_count, + changes: changes, + import_date: I18n.l(Time.zone.today, format: :full_date) } end def import_row(repository_row, import_row) @@ -195,10 +195,10 @@ module RepositoryImportParser repository_row.import_status = if @errors.present? 'invalid' elsif repository_row.import_status == 'created' - @new_rows_added += 1 + @created_rows_count += 1 'created' elsif @updated - @new_rows_added += 1 + @updated_rows_count += 1 'updated' else 'unchanged' @@ -283,5 +283,10 @@ module RepositoryImportParser value end end + + def total_rows_count + # all rows minus header + @rows.count - 1 + end end end diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 7004c91ee..04d84ac30 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -316,7 +316,7 @@ class Extends user_leave_team: 104, copy_inventory: 105, export_protocol_from_task: 106, - import_inventory_items: 107, + import_inventory_items_legacy: 107, create_tag: 108, delete_tag: 109, edit_image_on_result: 110, @@ -495,7 +495,7 @@ class Extends task_step_asset_renamed: 305, result_asset_renamed: 306, protocol_step_asset_renamed: 307, - item_added_with_import: 308 + inventory_items_added_or_updated_with_import: 308 } ACTIVITY_GROUPS = { diff --git a/config/locales/en.yml b/config/locales/en.yml index e48b383cc..53555a0f8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2347,8 +2347,8 @@ en: import: 'Import' no_header_name: 'No column name' - success_flash: "%{number_of_rows} of %{total_nr} new item(s) successfully imported." - partial_success_flash: "%{nr} of %{total_nr} items successfully imported." + success_flash: "%{successful_rows_count} of %{total_rows_count} new item(s) successfully imported." + partial_success_flash: "%{successful_rows_count} of %{total_rows_count} items successfully imported." error_message: items_limit: "The imported file contains too many rows. Max %{items_size} items allowed to upload at once." importing_duplicates: "Items with duplicates detected: %{duplicate_ids}. These will be ignored on import." diff --git a/config/locales/global_activities/en.yml b/config/locales/global_activities/en.yml index 574990e3e..f6b52f772 100644 --- a/config/locales/global_activities/en.yml +++ b/config/locales/global_activities/en.yml @@ -185,7 +185,8 @@ en: create_tag_html: "%{user} created tag %{tag} in project %{project}." edit_tag_html: "%{user} edited tag %{tag} in project %{project}." delete_tag_html: "%{user} deleted tag %{tag} in project %{project}." - import_inventory_items_html: "%{user} imported %{num_of_items} inventory item(s) to %{repository}." + import_inventory_items_legacy_html: "%{user} imported %{num_of_items} inventory item(s) to %{repository}." + inventory_items_added_or_updated_with_import_html: "%{user} imported %{created_rows_count} new item(s) and updated %{updated_rows_count} existing item(s) by import in %{repository}." edit_image_on_result_html: "%{user} edited image %{asset_name} on result %{result}: %{action}." edit_image_on_step_html: "%{user} edited image %{asset_name} on protocol's step %{step_position} %{step} on task %{my_module}: %{action}." edit_image_on_step_in_repository_html: "%{user} edited image %{asset_name} on protocol %{protocol}'s step %{step_position} %{step} in Protocol repository: %{action}." @@ -460,7 +461,8 @@ en: create_tag: "Tag created" edit_tag: "Tag edited" delete_tag: "Tag deleted" - import_inventory_items: "Inventory items imported" + import_inventory_items_legacy: "Inventory items imported (obsolete)" + inventory_items_added_or_updated_with_import: "Items added or updated with import" item_added_with_import: "Item added with import" edit_image_on_result: "Image on result edited" edit_image_on_step: "Image on task step edited" diff --git a/spec/utilities/repository_import_parser/importer_spec.rb b/spec/utilities/repository_import_parser/importer_spec.rb index 3a80de1a8..6c79a1477 100644 --- a/spec/utilities/repository_import_parser/importer_spec.rb +++ b/spec/utilities/repository_import_parser/importer_spec.rb @@ -43,7 +43,7 @@ describe RepositoryImportParser::Importer do end it 'return a message of imported records' do - expect(subject.run).to eq({ status: :ok, nr_of_added: 5, total_nr: 5 }) + expect(subject.run).to eq({ status: :ok, created_rows_count: 5, updated_rows_count: 0 }) end it 'generate 5 new repository rows' do From 6ebc08a0f4f448850484ab30546a11ec82f44a8b Mon Sep 17 00:00:00 2001 From: Martin Artnik <85488244+artoscinote@users.noreply.github.com> Date: Thu, 11 Jul 2024 14:57:49 +0200 Subject: [PATCH 2/2] Fix count condition for generating activity [SCI-10853] --- app/controllers/repositories_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 6b4a35750..29fa820c3 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -318,7 +318,7 @@ class RepositoriesController < ApplicationController total_rows_count: status[:total_rows_count]) if status[:status] == :ok - unless import_params[:preview] && (status[:created_rows_count] + status[:updated_rows_count]).positive? + unless import_params[:preview] || (status[:created_rows_count] + status[:updated_rows_count]).zero? log_activity( :inventory_items_added_or_updated_with_import, created_rows_count: status[:created_rows_count],