diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index 0610d5e36..075c7eb79 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -68,16 +68,13 @@ class ReportsController < ApplicationController if continue && @report.save_with_contents(report_contents) # record an activity - Activity.create( - type_of: :create_report, - project: @report.project, - user: current_user, - message: I18n.t( - 'activities.create_report', - user: current_user.full_name, - report: @report.name - ) - ) + Activities::CreateActivityService + .call(activity_type: :create_report, + owner: current_user, + subject: @report, + team: @report.team, + project: @report.project, + message_items: { report: @report.id }) respond_to do |format| format.json do render json: { url: reports_path }, status: :ok @@ -113,16 +110,13 @@ class ReportsController < ApplicationController if continue && @report.save_with_contents(report_contents) # record an activity - Activity.create( - type_of: :edit_report, - project: @report.project, - user: current_user, - message: I18n.t( - 'activities.edit_report', - user: current_user.full_name, - report: @report.name - ) - ) + Activities::CreateActivityService + .call(activity_type: :edit_report, + owner: current_user, + subject: @report, + team: @report.team, + project: @report.project, + message_items: { report: @report.id }) respond_to do |format| format.json do render json: { url: reports_path }, status: :ok @@ -149,16 +143,13 @@ class ReportsController < ApplicationController report = Report.find_by_id(report_id) next unless report.present? && can_manage_reports?(current_team) # record an activity - Activity.create( - type_of: :delete_report, - project: report.project, - user: current_user, - message: I18n.t( - 'activities.delete_report', - user: current_user.full_name, - report: report.name - ) - ) + Activities::CreateActivityService + .call(activity_type: :delete_report, + owner: current_user, + subject: report, + team: report.team, + project: report.project, + message_items: { report: report.id }) report.destroy end diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 322a7582d..b6cab4435 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -85,11 +85,11 @@ class Extends FILE_FA_ICON_MAPPINGS = {} ACTIVITY_SUBJECT_TYPES = %w( - Team Repository Project Experiment MyModule Result Protocol Step + Team Repository Project Experiment MyModule Result Protocol Step Report ).freeze SEARCHABLE_ACTIVITY_SUBJECT_TYPES = %w( - Repository Project Experiment MyModule Result Protocol Step + Repository Project Experiment MyModule Result Protocol Step Report ).freeze ACTIVITY_MESSAGE_ITEMS_TYPES = diff --git a/spec/controllers/reports_controller_spec.rb b/spec/controllers/reports_controller_spec.rb new file mode 100644 index 000000000..515e78faa --- /dev/null +++ b/spec/controllers/reports_controller_spec.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe ReportsController, type: :controller do + login_user + + let(:user) { User.first } + let!(:team) { create :team, created_by: user } + let!(:user_team) { create :user_team, team: team, user: user } + let(:user_project) { create :user_project, :owner, user: user } + let(:project) do + create :project, team: team, user_projects: [user_project] + end + let(:report) do + create :report, user: user, project: project, team: team, + name: 'test repot A1', description: 'test description A1' + end + + describe '#create' do + context 'in JSON format' do + let(:params) do + { project_id: project.id, + report: { name: 'test report created', + description: 'test description created' }, + report_contents: '[{"type_of":"project_header","id":{"project_id":' + + project.id.to_s + '},"sort_order":null,"children":[]}]' } + end + + it 'calls create activity service' do + expect(Activities::CreateActivityService).to receive(:call) + .with(hash_including(activity_type: :create_report)) + + post :create, params: params, format: :json + end + end + end + + describe '#update' do + context 'in JSON format' do + let(:params) do + { project_id: project.id, + id: report.id, + report: { name: 'test report update', + description: 'test description update' }, + report_contents: '[{"type_of":"project_header","id":{"project_id":' + + project.id.to_s + '},"sort_order":null,"children":[]}]' } + end + it 'calls create activity service' do + expect(Activities::CreateActivityService).to receive(:call) + .with(hash_including(activity_type: :edit_report)) + + put :update, params: params, format: :json + end + end + end + + describe '#destroy' do + let(:params) { { report_ids: "[#{report.id}]" } } + + it 'calls create activity service' do + expect(Activities::CreateActivityService).to receive(:call) + .with(hash_including(activity_type: :delete_report)) + + delete :destroy, params: params + end + end +end diff --git a/spec/factories/reports.rb b/spec/factories/reports.rb index 05ca33de5..e8b55923c 100644 --- a/spec/factories/reports.rb +++ b/spec/factories/reports.rb @@ -6,5 +6,6 @@ FactoryBot.define do project team name { Faker::Name.unique.name } + description { Faker::Lorem.sentence } end end