diff --git a/app/controllers/training_completion_controller.rb b/app/controllers/training_completion_controller.rb index faa924c2e8e..076ba33bfe1 100644 --- a/app/controllers/training_completion_controller.rb +++ b/app/controllers/training_completion_controller.rb @@ -15,7 +15,7 @@ def create user = current_user current_user.cancel_participation_from_regular_events current_user.delete_and_assign_new_organizer - Newspaper.publish(:training_completion_create, { user: }) + ActiveSupport::Notifications.instrument('training_completion.create', user:) user.clear_github_data notify_to_user(user) notify_to_admins(user) diff --git a/app/models/retirement.rb b/app/models/retirement.rb index cdf794db87a..32aab615db7 100644 --- a/app/models/retirement.rb +++ b/app/models/retirement.rb @@ -33,7 +33,7 @@ def execute remove_as_event_organizer clear_github_info destroy_cards - publish_to_newspaper + publish notify true rescue ActiveRecord::RecordInvalid => e @@ -82,8 +82,8 @@ def remove_as_event_organizer @user.delete_and_assign_new_organizer end - def publish_to_newspaper - Newspaper.publish(:retirement_create, { user: @user }) + def publish + ActiveSupport::Notifications.instrument('retirement.create', user: @user) end def notify diff --git a/app/models/times_channel_destroyer.rb b/app/models/times_channel_destroyer.rb index 4a295bd9dde..d00cb5c5d40 100644 --- a/app/models/times_channel_destroyer.rb +++ b/app/models/times_channel_destroyer.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class TimesChannelDestroyer - def call(payload) + def call(_name, _started, _finished, _id, payload) user = payload[:user] return unless user.discord_profile.times_id diff --git a/app/models/unfinished_data_destroyer.rb b/app/models/unfinished_data_destroyer.rb index 6af72f7484e..38ae6d0240d 100644 --- a/app/models/unfinished_data_destroyer.rb +++ b/app/models/unfinished_data_destroyer.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class UnfinishedDataDestroyer - def call(payload) + def call(_name, _started, _finished, _id, payload) user = payload[:user] Product.where(user:).unchecked.destroy_all Report.where(user:).wip.destroy_all diff --git a/config/initializers/active_support_notifications.rb b/config/initializers/active_support_notifications.rb index cb8d2eea85a..58924ff9cc8 100644 --- a/config/initializers/active_support_notifications.rb +++ b/config/initializers/active_support_notifications.rb @@ -60,4 +60,12 @@ question_notifier = QuestionNotifier.new ActiveSupport::Notifications.subscribe('question.create', question_notifier) ActiveSupport::Notifications.subscribe('question.update', question_notifier) + + unfinished_data_destroyer = UnfinishedDataDestroyer.new + ActiveSupport::Notifications.subscribe('retirement.create', unfinished_data_destroyer) + ActiveSupport::Notifications.subscribe('training_completion.create', unfinished_data_destroyer) + + times_channel_destroyer = TimesChannelDestroyer.new + ActiveSupport::Notifications.subscribe('retirement.create', times_channel_destroyer) + ActiveSupport::Notifications.subscribe('training_completion.create', times_channel_destroyer) end diff --git a/config/initializers/newspaper.rb b/config/initializers/newspaper.rb index 08993429d2e..83906530ec1 100644 --- a/config/initializers/newspaper.rb +++ b/config/initializers/newspaper.rb @@ -1,13 +1,5 @@ # frozen_string_literal: true Rails.configuration.after_initialize do - unfinished_data_destroyer = UnfinishedDataDestroyer.new - Newspaper.subscribe(:retirement_create, unfinished_data_destroyer) - Newspaper.subscribe(:training_completion_create, unfinished_data_destroyer) - - times_channel_destroyer = TimesChannelDestroyer.new - Newspaper.subscribe(:retirement_create, times_channel_destroyer) - Newspaper.subscribe(:training_completion_create, times_channel_destroyer) - Newspaper.subscribe(:came_comment_in_talk, CommentNotifierForAdmin.new) end diff --git a/test/models/retirement_test.rb b/test/models/retirement_test.rb index fe7794c250e..de83cecac95 100644 --- a/test/models/retirement_test.rb +++ b/test/models/retirement_test.rb @@ -20,7 +20,7 @@ class RetirementTest < ActiveSupport::TestCase remove_as_event_organizer clear_github_info destroy_cards - publish_to_newspaper + publish notify ] diff --git a/test/models/times_channel_destroyer_test.rb b/test/models/times_channel_destroyer_test.rb index f22c25da96c..66d9df58ae0 100644 --- a/test/models/times_channel_destroyer_test.rb +++ b/test/models/times_channel_destroyer_test.rb @@ -12,7 +12,7 @@ class TimesChannelDestroyerTest < ActiveSupport::TestCase Rails.logger.stub(:warn, ->(message) { logs << message }) do Discord::Server.stub(:delete_text_channel, true) do - TimesChannelDestroyer.new.call({ user: }) + TimesChannelDestroyer.new.call(nil, nil, nil, nil, { user: }) end assert_nil user.discord_profile.times_id assert_nil user.discord_profile.times_url @@ -26,7 +26,7 @@ class TimesChannelDestroyerTest < ActiveSupport::TestCase user.discord_profile.update!(times_id: '987654321987654321') Rails.logger.stub(:warn, ->(message) { logs << message }) do Discord::Server.stub(:delete_text_channel, nil) do - TimesChannelDestroyer.new.call({ user: }) + TimesChannelDestroyer.new.call(nil, nil, nil, nil, { user: }) end assert_equal '987654321987654321', user.discord_profile.times_id assert_equal "[Discord API] #{user.login_name}の分報チャンネルが削除できませんでした。", logs.last diff --git a/test/models/unfinished_data_destroyer_test.rb b/test/models/unfinished_data_destroyer_test.rb new file mode 100644 index 00000000000..36d8d5acd3c --- /dev/null +++ b/test/models/unfinished_data_destroyer_test.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +require 'test_helper' + +class UnfinishedDataDestroyerTest < ActiveSupport::TestCase + setup do + @payload = { user: users(:kimura) } + end + + test '#call deletes user wip reports and unchecked products and resets career_path' do + user = @payload[:user] + user.update!(career_path: 1) + + 3.times do |i| + Report.create!( + user:, + title: "wipの日報#{i}", + description: 'テスト日報', + wip: true, + reported_on: Date.current - i + ) + end + + practices = Practice.where.not(id: user.products.pluck(:practice_id)).take(3) + practices.each do |practice| + Product.create!( + user:, + practice:, + body: '提出物', + wip: false + ) + end + + UnfinishedDataDestroyer.new.call(nil, nil, nil, nil, @payload) + + assert_equal 0, Product.unchecked.where(user:).count + assert_equal 0, Report.wip.where(user:).count + assert_equal 'unset', user.reload.career_path + end + + test 'does not delete other users wip report and unchecked product' do + other_user_unchecked_product = products(:product1) + other_user_wip_report = reports(:report9) + + UnfinishedDataDestroyer.new.call(nil, nil, nil, nil, @payload) + + assert Product.exists?(other_user_unchecked_product.id) + assert Report.exists?(other_user_wip_report.id) + end + + test 'does not delete checked product or non-wip report' do + checked_product = products(:product2) + non_wip_report = reports(:report24) + + UnfinishedDataDestroyer.new.call(nil, nil, nil, nil, @payload) + + assert Product.exists?(checked_product.id) + assert Report.exists?(non_wip_report.id) + end +end