diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ccef194ec..894af0f6f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -25,6 +25,7 @@ class ApplicationController < ActionController::Base before_action do Current.request_id = request.uuid + Current.user = current_user end def staging_authenticate diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 9baa5928f..3c897622c 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -125,6 +125,8 @@ class Dossier < ApplicationRecord belongs_to :transfer, class_name: 'DossierTransfer', foreign_key: 'dossier_transfer_id', optional: true, inverse_of: :dossiers has_many :transfer_logs, class_name: 'DossierTransferLog', dependent: :destroy + after_destroy_commit :log_destroy + accepts_nested_attributes_for :champs accepts_nested_attributes_for :champs_public accepts_nested_attributes_for :champs_private @@ -1395,4 +1397,22 @@ class Dossier < ApplicationRecord avis.each { |a| ExpertMailer.send_dossier_decision_v2(a).deliver_later } end + + def log_destroy + app_traces = caller.reject { _1.match?(%r{/ruby/.+/gems/}) }.map { _1.sub(Rails.root.to_s, "") } + + payload = { + message: "Dossier destroyed", + dossier_id: id, + procedure_id: procedure.id, + request_id: Current.request_id, + user_id: Current.user&.id, + controller: app_traces.find { _1.match?(%r{/controllers/|/jobs/}) }, + caller: app_traces.second # first is the callback definition + } + + logger = Lograge.logger || Rails.logger + + logger.info payload.to_json + end end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 0e8c92bd5..b16097168 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1934,6 +1934,17 @@ describe Dossier, type: :model do expect { dossier.procedure.reset! }.not_to raise_error expect { dossier.reload }.to raise_error(ActiveRecord::RecordNotFound) end + + it "call logger with context" do + json_message = nil + + allow(Rails.logger).to receive(:info) { json_message ||= _1 } + dossier.destroy + + expect(JSON.parse(json_message)).to a_hash_including( + { message: "Dossier destroyed", dossier_id: dossier.id, procedure_id: procedure.id }.stringify_keys + ) + end end describe "#spreadsheet_columns" do