Merge pull request #7696 from artoscinote/ma_SCI_10853

Change import activites and flash messages [SCI-10853]
This commit is contained in:
Martin Artnik 2024-07-11 15:04:55 +02:00 committed by GitHub
commit 34e9c9f4e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 36 additions and 21 deletions

View file

@ -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]).zero?
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

View file

@ -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
@ -69,8 +69,6 @@ module RepositoryImportParser
duplicate_ids = SpreadsheetParser.duplicate_ids(@sheet)
@rows.each do |row|
next if row.blank?
unless @header_skipped
@header_skipped = true
next
@ -79,8 +77,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, '')
@ -120,8 +116,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)
@ -192,10 +192,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'
@ -280,5 +280,10 @@ module RepositoryImportParser
value
end
end
def total_rows_count
# all rows minus header
@rows.count - 1
end
end
end

View file

@ -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 = {

View file

@ -2349,8 +2349,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."

View file

@ -185,7 +185,8 @@ en:
create_tag_html: "%{user} created tag <strong>%{tag}</strong> in project %{project}."
edit_tag_html: "%{user} edited tag <strong>%{tag}</strong> in project %{project}."
delete_tag_html: "%{user} deleted tag <strong>%{tag}</strong> 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"

View file

@ -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