diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index dc9b40eca..a8de372cf 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -152,8 +152,17 @@ class ProtocolsController < ApplicationController @protocol.record_timestamps = false @protocol.assign_attributes(metadata_params) + changes = @protocol.changes.keys + respond_to do |format| if @protocol.save + + changes.each do |key| + if %w(description authors keywords).include?(key) + log_activity("edit_#{key}_in_protocol_repository".to_sym) + end + end + format.json do render json: { updated_at_label: render_to_string( @@ -189,6 +198,8 @@ class ProtocolsController < ApplicationController end if @protocol.update_keywords(params[:keywords]) format.json do + log_activity(:edit_keywords_in_protocol_repository) + render json: { updated_at_label: render_to_string( partial: 'protocols/header/updated_at_label.html.erb' @@ -220,6 +231,8 @@ class ProtocolsController < ApplicationController respond_to do |format| if @protocol.save + log_activity(:create_protocol_in_repository) + format.json do render json: { url: edit_protocol_path( @@ -595,6 +608,15 @@ class ProtocolsController < ApplicationController end else format.json do + Activities::CreateActivityService + .call(activity_type: :import_protocol_in_repository, + owner: current_user, + subject: protocol, + team: current_team, + message_items: { + protocol: protocol.id + }) + render json: { name: p_name, new_name: protocol.name, status: :ok }, @@ -796,6 +818,18 @@ class ProtocolsController < ApplicationController elsif @protocols.length > 1 file_name = 'protocols.eln' end + + @protocols.each do |p| + Activities::CreateActivityService + .call(activity_type: :export_protocol_in_repository, + owner: current_user, + subject: p, + team: current_team, + message_items: { + protocol: p.id + }) + end + send_data(z_output_stream.read, filename: file_name) end end @@ -1189,4 +1223,15 @@ class ProtocolsController < ApplicationController def check_protocolsio_import_permissions render_403 unless can_create_protocols_in_repository?(current_team) end + + def log_activity(type_of) + Activities::CreateActivityService + .call(activity_type: type_of, + owner: current_user, + subject: @protocol, + team: current_team, + message_items: { + protocol: @protocol.id + }) + end end diff --git a/app/models/protocol.rb b/app/models/protocol.rb index 025bbf042..d1615903d 100644 --- a/app/models/protocol.rb +++ b/app/models/protocol.rb @@ -431,6 +431,16 @@ class Protocol < ApplicationRecord self.restored_on = nil self.protocol_type = Protocol.protocol_types[:in_repository_private] save + + Activities::CreateActivityService + .call(activity_type: :move_protocol_in_repository, + owner: user, + subject: self, + team: team, + message_items: { + protocol: id, + action: I18n.t('activities.protocols.team_to_my_message') + }) end # This publish action simply moves the protocol from @@ -448,6 +458,16 @@ class Protocol < ApplicationRecord self.restored_on = nil self.protocol_type = Protocol.protocol_types[:in_repository_public] save + + Activities::CreateActivityService + .call(activity_type: :move_protocol_in_repository, + owner: user, + subject: self, + team: team, + message_items: { + protocol: id, + action: I18n.t('activities.protocols.my_to_team_message') + }) end def archive(user) @@ -476,8 +496,16 @@ class Protocol < ApplicationRecord protocol_type: :unlinked ) end - end + Activities::CreateActivityService + .call(activity_type: :archive_protocol_in_repository, + owner: user, + subject: self, + team: team, + message_items: { + protocol: id + }) + end result end @@ -496,6 +524,15 @@ class Protocol < ApplicationRecord self.protocol_type = Protocol.protocol_types[:in_repository_private] end save + + Activities::CreateActivityService + .call(activity_type: :restore_protocol_in_repository, + owner: user, + subject: self, + team: team, + message_items: { + protocol: id + }) end def update_keywords(keywords) diff --git a/config/locales/en.yml b/config/locales/en.yml index 34eba5fd9..2f5322d16 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1469,6 +1469,9 @@ en: change_users_role_on_team_html: "changed %{user}'s role in team %{team} to %{role}" export_projects_html: "exported project(s) %{projects} to .zip" export_inventory_items_html: "exported inventory item(s) from %{inventory}" + protocols: + my_to_team_message: 'My protocols to Team protocols' + team_to_my_message: 'Team protocols to My protocols' user_my_modules: new: diff --git a/spec/controllers/protocols_controller_spec.rb b/spec/controllers/protocols_controller_spec.rb new file mode 100644 index 000000000..7e9d07d45 --- /dev/null +++ b/spec/controllers/protocols_controller_spec.rb @@ -0,0 +1,119 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe ProtocolsController, type: :controller do + login_user + + let(:user) { subject.current_user } + let(:team) { create :team, created_by: user } + let!(:user_team) { create :user_team, :admin, user: user, team: team } + + describe 'POST create' do + let(:params) do + { + protocol: { + name: 'protocol_name' + } + } + end + + it 'calls create activity for creating inventory column' do + expect(Activities::CreateActivityService) + .to(receive(:call) + .with(hash_including(activity_type: + :create_protocol_in_repository))) + + post :create, params: params, format: :json + end + + it 'adds activity in DB' do + expect { post :create, params: params, format: :json } + .to(change { Activity.count }) + end + end + + describe 'GET export' do + let(:protocol) { create :protocol, :in_public_repository, team: team } + let(:second_protocol) do + create :protocol, :in_public_repository, team: team + end + let(:params) { { protocol_ids: [protocol.id, second_protocol.id] } } + let(:action) { get :export, params: params } + + it 'calls create activity for exporting protocols' do + expect(Activities::CreateActivityService) + .to(receive(:call) + .with(hash_including(activity_type: + :export_protocol_in_repository))).twice + + action + end + + it 'adds activity in DB' do + expect { action } + .to(change { Activity.count }.by(2)) + end + end + + describe 'POST import' do + let(:params) do + { + team_id: team.id, + type: 'public', + # protocol: fixture_file_upload('files/my_test_protocol.eln', + # 'application/json'), + # Not sure where should I attache file? + protocol: { + name: 'my_test_protocol', + description: 'description', + authors: 'authors' + } + } + end + let(:action) { post :import, params: params, format: :json } + + it 'calls create activity for importing protocols' do + expect(Activities::CreateActivityService) + .to(receive(:call) + .with(hash_including(activity_type: + :import_protocol_in_repository))) + + action + end + + it 'adds activity in DB' do + expect { action } + .to(change { Activity.count }) + end + end + + describe 'POST metadata' do + let(:protocol) do + create :protocol, :in_public_repository, team: team, added_by: user + end + let(:params) do + { + id: protocol.id, + protocol: { + description: 'description' + } + } + end + let(:action) { put :update_metadata, params: params, format: :json } + + it 'calls create activity for updating description' do + expect(Activities::CreateActivityService) + .to(receive(:call) + .with(hash_including(activity_type: + :edit_description_in_protocol_repository))) + + action + end + + it 'adds activity in DB' do + expect { action } + .to(change { Activity.count }) + end + end +end diff --git a/spec/factories/protocols.rb b/spec/factories/protocols.rb index 6ebe8763c..a1d5ce56b 100644 --- a/spec/factories/protocols.rb +++ b/spec/factories/protocols.rb @@ -5,5 +5,11 @@ FactoryBot.define do name { Faker::Name.unique.name } team my_module + trait :in_public_repository do + my_module { nil } + protocol_type { :in_repository_public } + added_by { create :user } + published_on { Time.now } + end end end diff --git a/spec/fixtures/files/my_test_protocol.eln b/spec/fixtures/files/my_test_protocol.eln new file mode 100644 index 000000000..a7a2f0bc9 Binary files /dev/null and b/spec/fixtures/files/my_test_protocol.eln differ diff --git a/spec/models/protocol_spec.rb b/spec/models/protocol_spec.rb index 32b84db71..8c1eb2752 100644 --- a/spec/models/protocol_spec.rb +++ b/spec/models/protocol_spec.rb @@ -49,4 +49,75 @@ describe Protocol, type: :model do .is_at_most(Constants::TEXT_MAX_LENGTH) end end + describe '.archive(user)' do + let(:protocol) { create :protocol, :in_public_repository, added_by: user } + let(:user) { create :user } + + it 'calls create activity for archiving protocol' do + expect(Activities::CreateActivityService) + .to(receive(:call) + .with(hash_including(activity_type: + :archive_protocol_in_repository))) + + protocol.archive user + end + + it 'creats one new activity DB' do + expect { protocol.archive(user) }.to change { Activity.count }.by(1) + end + end + + describe '.restore(user)' do + let(:protocol) { create :protocol, :in_public_repository, added_by: user } + let(:user) { create :user } + + it 'calls create activity for restoring protocol' do + expect(Activities::CreateActivityService) + .to(receive(:call) + .with(hash_including(activity_type: + :restore_protocol_in_repository))) + + protocol.restore user + end + + it 'creats one new activity DB' do + expect { protocol.restore(user) }.to change { Activity.count }.by(1) + end + end + + describe '.publish(user)' do + let(:protocol) { create :protocol, :in_public_repository, added_by: user } + let(:user) { create :user } + + it 'calls create activity for restoring protocol' do + expect(Activities::CreateActivityService) + .to(receive(:call) + .with(hash_including(activity_type: + :move_protocol_in_repository))) + + protocol.publish user + end + + it 'creats one new activity DB' do + expect { protocol.publish(user) }.to change { Activity.count }.by(1) + end + end + + describe '.make_private(user)' do + let(:protocol) { create :protocol, :in_public_repository, added_by: user } + let(:user) { create :user } + + it 'calls create activity for restoring protocol' do + expect(Activities::CreateActivityService) + .to(receive(:call) + .with(hash_including(activity_type: + :move_protocol_in_repository))) + + protocol.make_private user + end + + it 'creats one new activity DB' do + expect { protocol.make_private(user) }.to change { Activity.count }.by(1) + end + end end