Add activity for importing inventory items

This commit is contained in:
Mojca Lorber 2019-04-18 15:18:10 +02:00
parent be31cc373d
commit b00cf07684
6 changed files with 55 additions and 34 deletions

View file

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

View file

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

View file

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

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}."
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"

View file

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

View file

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