Merge pull request #1686 from mlorb/ml-sci-3324

Add activity for importing inventory items [SCI-3324]
This commit is contained in:
mlorb 2019-04-26 15:20:40 +02:00 committed by GitHub
commit 12564bd070
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 34 deletions

View file

@ -1,8 +1,8 @@
class RepositoriesController < ApplicationController class RepositoriesController < ApplicationController
before_action :load_vars, 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: 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_team, only: %i(parse_sheet import_records)
before_action :check_view_all_permissions, only: :index before_action :check_view_all_permissions, only: :index
before_action :check_view_permissions, only: %i(export_repository show) before_action :check_view_permissions, only: %i(export_repository show)
@ -243,6 +243,9 @@ class RepositoriesController < ApplicationController
status = import_records.import! status = import_records.import!
if status[:status] == :ok if status[:status] == :ok
log_activity(:import_inventory_items,
num_of_items: status[:nr_of_added])
flash[:success] = t('repositories.import_records.success_flash', flash[:success] = t('repositories.import_records.success_flash',
number_of_rows: status[:nr_of_added], number_of_rows: status[:nr_of_added],
total_nr: status[:total_nr]) total_nr: status[:total_nr])
@ -272,15 +275,7 @@ class RepositoriesController < ApplicationController
def export_repository def export_repository
if params[:row_ids] && params[:header_ids] if params[:row_ids] && params[:header_ids]
RepositoryZipExport.generate_zip(params, @repository, current_user) RepositoryZipExport.generate_zip(params, @repository, current_user)
log_activity(:export_inventory_items)
Activities::CreateActivityService
.call(activity_type: :export_inventory_items,
owner: current_user,
subject: current_team,
team: current_team,
message_items: {
repository: @repository.id
})
else else
flash[:alert] = t('zip_export.export_error') flash[:alert] = t('zip_export.export_error')
end end
@ -355,12 +350,14 @@ class RepositoriesController < ApplicationController
end end
end end
def log_activity(type_of) def log_activity(type_of, message_items = {})
message_items = { repository: @repository.id }.merge(message_items)
Activities::CreateActivityService Activities::CreateActivityService
.call(activity_type: type_of, .call(activity_type: type_of,
owner: current_user, owner: current_user,
subject: @repository, subject: @repository,
team: @team, team: @team,
message_items: { repository: @repository.id }) message_items: message_items)
end end
end end

View file

@ -108,8 +108,6 @@ module RepositoryImportParser
raise ActiveRecord::Rollback raise ActiveRecord::Rollback
end end
# Disable per row activity logging
# log_activity(record_row)
@new_rows_added += 1 @new_rows_added += 1
end end
end end
@ -158,18 +156,5 @@ module RepositoryImportParser
end end
current_column current_column
end 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
end end

View file

@ -208,7 +208,8 @@ class Extends
copy_protocol_in_repository: 103, copy_protocol_in_repository: 103,
user_leave_team: 104, user_leave_team: 104,
copy_inventory: 105, copy_inventory: 105,
export_protocol_from_task: 106 export_protocol_from_task: 106,
import_inventory_items: 107
} }
ACTIVITY_GROUPS = { ACTIVITY_GROUPS = {
@ -219,7 +220,7 @@ class Extends
task_inventory: [55, 56], task_inventory: [55, 56],
experiment: [*27..31, 57], experiment: [*27..31, 57],
reports: [48, 50, 49], 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], protocol_repository: [80, 103, 89, 87, 79, 90, 91, 88, 85, 86, 84, 81, 82, 83, 101],
team: [92, 94, 93, 97, 104] team: [92, 94, 93, 97, 104]
}.freeze }.freeze

View file

@ -127,6 +127,7 @@ en:
change_users_role_on_team_html: "%{user} changed user %{user_changed}'s role in team %{team} to %{role}." 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_projects_html: "%{user} exported project(s) %{projects} to .zip."
export_inventory_items_html: "%{user} exported inventory item(s) from %{repository}." 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: activity_name:
create_project: "Project created" create_project: "Project created"
rename_project: "Project renamed" rename_project: "Project renamed"
@ -225,6 +226,7 @@ en:
user_leave_team: "User left team" user_leave_team: "User left team"
export_projects: "Projects exported" export_projects: "Projects exported"
export_inventory_items: "Inventory items exported" export_inventory_items: "Inventory items exported"
import_inventory_items: "Inventory items imported"
activity_group: activity_group:
projects: "Projects" projects: "Projects"
task_results: "Task results" task_results: "Task results"

View file

@ -13,7 +13,7 @@ describe RepositoriesController, type: :controller do
describe 'POST create' do describe 'POST create' do
let(:params) { { repository: { name: 'My Repository' } } } 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) expect(Activities::CreateActivityService)
.to(receive(:call) .to(receive(:call)
.with(hash_including(activity_type: :create_inventory))) .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(:params) { { id: repository.id, team_id: team.id } }
let(:action) { delete :destroy, params: params } 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) expect(Activities::CreateActivityService)
.to(receive(:call) .to(receive(:call)
.with(hash_including(activity_type: :delete_inventory))) .with(hash_including(activity_type: :delete_inventory)))
@ -53,7 +53,7 @@ describe RepositoriesController, type: :controller do
end end
let(:action) { put :update, params: params, format: :json } 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) expect(Activities::CreateActivityService)
.to(receive(:call) .to(receive(:call)
.with(hash_including(activity_type: :rename_inventory))) .with(hash_including(activity_type: :rename_inventory)))
@ -87,7 +87,7 @@ describe RepositoriesController, type: :controller do
end end
let(:action) { post :export_repository, params: params, format: :json } 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) expect(Activities::CreateActivityService)
.to(receive(:call) .to(receive(:call)
.with(hash_including(activity_type: :export_inventory_items))) .with(hash_including(activity_type: :export_inventory_items)))
@ -100,4 +100,37 @@ describe RepositoriesController, type: :controller do
.to(change { Activity.count }) .to(change { Activity.count })
end end
end end
describe 'POST import_records' do
let(:repository) { create :repository, team: team }
let(:mappings) do
{ '0': '-1', '1': '', '2': '', '3': '', '4': '', '5': '' }
end
let(:params) do
{ id: repository.id,
team_id: team.id,
file_id: 'file_id',
mappings: mappings }
end
let(:action) { post :import_records, params: params, format: :json }
it 'calls create activity for importing inventory items' do
allow_any_instance_of(ImportRepository::ImportRecords)
.to receive(:import!).and_return(status: :ok)
expect(Activities::CreateActivityService)
.to(receive(:call)
.with(hash_including(activity_type: :import_inventory_items)))
action
end
it 'adds activity in DB' do
ImportRepository::ImportRecords.any_instance.stub(:import!)
.and_return(status: :ok)
expect { action }
.to(change { Activity.count })
end
end
end end