diff --git a/app/controllers/api/checks_controller.rb b/app/controllers/api/checks_controller.rb index 08450480392..8620b6efebd 100644 --- a/app/controllers/api/checks_controller.rb +++ b/app/controllers/api/checks_controller.rb @@ -11,22 +11,30 @@ def index def create if checkable.checks.empty? - @check = Check.create!( - user: current_user, - checkable: - ) - ActiveSupport::Notifications.instrument('check.create', check: @check) - head :created + begin + Check.transaction do + @check = Check.create!(user: current_user, checkable:) + ActiveSupport::Notifications.instrument('check.create', check: @check) + end + head :created + rescue StandardError => e + Rails.logger.error("[API::ChecksController#create] チェック作成でエラー: #{e.message}") + render json: { message: 'エラーが発生しました。' }, status: :internal_server_error + end else render json: { message: "この#{checkable.class.model_name.human}は確認済です。" }, status: :unprocessable_entity end end def destroy - @check = Check.find(params[:id]).destroy - ActiveSupport::Notifications.instrument('check.cancel', check: @check) - + Check.transaction do + @check = Check.find(params[:id]).destroy! + ActiveSupport::Notifications.instrument('check.cancel', check: @check) + end head :no_content + rescue StandardError => e + Rails.logger.error("[API::ChecksController#destroy] チェック削除でエラー: #{e.message}") + render json: { message: 'エラーが発生しました。' }, status: :internal_server_error end private diff --git a/app/controllers/checks_controller.rb b/app/controllers/checks_controller.rb index d290d19d507..425706e42ad 100644 --- a/app/controllers/checks_controller.rb +++ b/app/controllers/checks_controller.rb @@ -14,15 +14,15 @@ def create @check = @checkable.checks.build(user: current_user) - if @check.save - Newspaper.publish(:check_create, { check: @check }) - if @checkable.is_a?(Product) - @checkable.change_learning_status(:complete) - redirect_back(fallback_location: @checkable, notice: '提出物を合格にしました。') - else - redirect_back(fallback_location: @checkable, notice: '日報を確認済みにしました。') + begin + Check.transaction do + @check.save! + ActiveSupport::Notifications.instrument('check.create', check: @check) end - else + notice = @checkable.is_a?(Product) ? '提出物を合格にしました。' : '日報を確認済みにしました。' + redirect_back(fallback_location: @checkable, notice:) + rescue StandardError => e + Rails.logger.error("[ChecksController#create] チェック作成でエラー: #{e.message}") redirect_back(fallback_location: @checkable, alert: 'エラーが発生しました。') end end @@ -31,10 +31,16 @@ def destroy @check = Check.find(params[:id]) @checkable = @check.checkable - @check.destroy - Newspaper.publish(:check_cancel, { check: @check }) - @checkable.change_learning_status(:submitted) if @checkable.is_a?(Product) - redirect_back(fallback_location: @checkable) + begin + Check.transaction do + @check.destroy! + ActiveSupport::Notifications.instrument('check.cancel', check: @check) + end + redirect_back(fallback_location: @checkable) + rescue StandardError => e + Rails.logger.error("[ChecksController#destroy] チェック削除でエラー: #{e.message}") + redirect_back(fallback_location: @checkable, alert: 'エラーが発生しました。') + end end private diff --git a/app/models/check.rb b/app/models/check.rb index 006cda209e7..97452f79aaa 100644 --- a/app/models/check.rb +++ b/app/models/check.rb @@ -3,8 +3,8 @@ class Check < ApplicationRecord belongs_to :user belongs_to :checkable, polymorphic: true - after_create CheckCallbacks.new - after_destroy CheckCallbacks.new + after_create_commit -> { CheckCallbacks.new.after_create(self) } + after_destroy_commit -> { CheckCallbacks.new.after_destroy(self) } alias sender user validates :user_id, uniqueness: { scope: %i[checkable_id checkable_type] } diff --git a/app/models/product.rb b/app/models/product.rb index 8512b24c529..c409768026d 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -96,7 +96,7 @@ def change_learning_status(status) user_id: user.id, practice_id: practice.id ) - learning.update(status:) + learning.update!(status:) end # nilの場合あり