From b00cf07684591db98828da02cc44398153f855e0 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Thu, 18 Apr 2019 15:18:10 +0200 Subject: [PATCH] Add activity for importing inventory items --- app/controllers/repositories_controller.rb | 23 ++++++------ .../repository_import_parser/importer.rb | 15 -------- config/initializers/extends.rb | 5 +-- config/locales/global_activities/en.yml | 2 ++ .../repositories_controller_spec.rb | 36 ++++++++++++++++--- spec/factories/temp_files.rb | 8 +++++ 6 files changed, 55 insertions(+), 34 deletions(-) create mode 100644 spec/factories/temp_files.rb diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 47df4d5ab..dd5f41949 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -1,8 +1,8 @@ class RepositoriesController < ApplicationController before_action :load_vars, - except: %i(index create create_modal parse_sheet import_records) + except: %i(index create create_modal parse_sheet) before_action :load_parent_vars, except: - %i(repository_table_index export_repository parse_sheet import_records) + %i(repository_table_index parse_sheet) before_action :check_team, only: %i(parse_sheet import_records) before_action :check_view_all_permissions, only: :index before_action :check_view_permissions, only: %i(export_repository show) @@ -242,6 +242,9 @@ class RepositoriesController < ApplicationController import_records = repostiory_import_actions status = import_records.import! + log_activity(:import_inventory_items, + num_of_items: status[:nr_of_added]) + if status[:status] == :ok flash[:success] = t('repositories.import_records.success_flash', number_of_rows: status[:nr_of_added], @@ -272,15 +275,7 @@ class RepositoriesController < ApplicationController def export_repository if params[:row_ids] && params[:header_ids] RepositoryZipExport.generate_zip(params, @repository, current_user) - - Activities::CreateActivityService - .call(activity_type: :export_inventory_items, - owner: current_user, - subject: current_team, - team: current_team, - message_items: { - repository: @repository.id - }) + log_activity(:export_inventory_items) else flash[:alert] = t('zip_export.export_error') end @@ -355,12 +350,14 @@ class RepositoriesController < ApplicationController end end - def log_activity(type_of) + def log_activity(type_of, message_items = {}) + message_items = { repository: @repository.id }.merge(message_items) + Activities::CreateActivityService .call(activity_type: type_of, owner: current_user, subject: @repository, team: @team, - message_items: { repository: @repository.id }) + message_items: message_items) end end diff --git a/app/utilities/repository_import_parser/importer.rb b/app/utilities/repository_import_parser/importer.rb index c526e2768..f82bbde65 100644 --- a/app/utilities/repository_import_parser/importer.rb +++ b/app/utilities/repository_import_parser/importer.rb @@ -108,8 +108,6 @@ module RepositoryImportParser raise ActiveRecord::Rollback end - # Disable per row activity logging - # log_activity(record_row) @new_rows_added += 1 end end @@ -158,18 +156,5 @@ module RepositoryImportParser end current_column end - - def log_activity(repository_row) - Activities::CreateActivityService.call( - activity_type: :create_item_inventory, - owner: @user, - subject: @repository, - team: @repository.team, - message_items: { - repository_row: repository_row.id, - repository: @repository.id - } - ) - end end end diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 1555a45c5..1a6c36e05 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -208,7 +208,8 @@ class Extends copy_protocol_in_repository: 103, user_leave_team: 104, copy_inventory: 105, - export_protocol_from_task: 106 + export_protocol_from_task: 106, + import_inventory_items: 107 } ACTIVITY_GROUPS = { @@ -219,7 +220,7 @@ class Extends task_inventory: [55, 56], experiment: [*27..31, 57], reports: [48, 50, 49], - inventories: [70, 71, 105, 72, 73, 74, 102, 75, 76, 77, 78, 96], + inventories: [70, 71, 105, 72, 73, 74, 102, 75, 76, 77, 78, 96, 107], protocol_repository: [80, 103, 89, 87, 79, 90, 91, 88, 85, 86, 84, 81, 82, 83, 101], team: [92, 94, 93, 97, 104] }.freeze diff --git a/config/locales/global_activities/en.yml b/config/locales/global_activities/en.yml index b61850158..769826162 100644 --- a/config/locales/global_activities/en.yml +++ b/config/locales/global_activities/en.yml @@ -127,6 +127,7 @@ en: change_users_role_on_team_html: "%{user} changed user %{user_changed}'s role in team %{team} to %{role}." export_projects_html: "%{user} exported project(s) %{projects} to .zip." export_inventory_items_html: "%{user} exported inventory item(s) from %{repository}." + import_inventory_items_html: "%{user} imported %{num_of_items} inventory item(s) to %{repository}." activity_name: create_project: "Project created" rename_project: "Project renamed" @@ -225,6 +226,7 @@ en: user_leave_team: "User left team" export_projects: "Projects exported" export_inventory_items: "Inventory items exported" + import_inventory_items: "Inventory items imported" activity_group: projects: "Projects" task_results: "Task results" diff --git a/spec/controllers/repositories_controller_spec.rb b/spec/controllers/repositories_controller_spec.rb index 40a112023..740d84c15 100644 --- a/spec/controllers/repositories_controller_spec.rb +++ b/spec/controllers/repositories_controller_spec.rb @@ -13,7 +13,7 @@ describe RepositoriesController, type: :controller do describe 'POST create' do let(:params) { { repository: { name: 'My Repository' } } } - it 'calls create activity for unarchiving experiment' do + it 'calls create activity for creating inventory' do expect(Activities::CreateActivityService) .to(receive(:call) .with(hash_including(activity_type: :create_inventory))) @@ -32,7 +32,7 @@ describe RepositoriesController, type: :controller do let(:params) { { id: repository.id, team_id: team.id } } let(:action) { delete :destroy, params: params } - it 'calls create activity for unarchiving experiment' do + it 'calls create activity for deleting inventory' do expect(Activities::CreateActivityService) .to(receive(:call) .with(hash_including(activity_type: :delete_inventory))) @@ -53,7 +53,7 @@ describe RepositoriesController, type: :controller do end let(:action) { put :update, params: params, format: :json } - it 'calls create activity for unarchiving experiment' do + it 'calls create activity for renaming inventory' do expect(Activities::CreateActivityService) .to(receive(:call) .with(hash_including(activity_type: :rename_inventory))) @@ -87,7 +87,7 @@ describe RepositoriesController, type: :controller do end let(:action) { post :export_repository, params: params, format: :json } - it 'calls create activity for unarchiving experiment' do + it 'calls create activity for exporting inventory items' do expect(Activities::CreateActivityService) .to(receive(:call) .with(hash_including(activity_type: :export_inventory_items))) @@ -100,4 +100,32 @@ describe RepositoriesController, type: :controller do .to(change { Activity.count }) end end + + describe 'POST import_records' do + let(:repository) { create :repository, team: team } + let(:temp_file) { create :temp_file, session_id: session.id } + let(:mappings) do + { '0': '-1', '1': '', '2': '', '3': '', '4': '', '5': '' } + end + let(:params) do + { id: repository.id, + team_id: team.id, + file_id: temp_file.id, + mappings: mappings } + end + let(:action) { post :import_records, params: params, format: :json } + + it 'calls create activity for importing inventory items' do + expect(Activities::CreateActivityService) + .to(receive(:call) + .with(hash_including(activity_type: :import_inventory_items))) + + action + end + + it 'adds activity in DB' do + expect { action } + .to(change { Activity.count }) + end + end end diff --git a/spec/factories/temp_files.rb b/spec/factories/temp_files.rb new file mode 100644 index 000000000..b208c7257 --- /dev/null +++ b/spec/factories/temp_files.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :temp_file do + session_id 'ba5c0a9ed3b3814b4c16595cc33462e9' + file_file_name 'export.csv' + end +end