2020-08-06 16:35:45 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: deleted_dossiers
|
|
|
|
#
|
2020-11-12 10:38:10 +01:00
|
|
|
# id :bigint not null, primary key
|
|
|
|
# deleted_at :datetime
|
|
|
|
# reason :string
|
|
|
|
# state :string
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# dossier_id :bigint
|
|
|
|
# groupe_instructeur_id :bigint
|
|
|
|
# procedure_id :bigint
|
|
|
|
# revision_id :bigint
|
|
|
|
# user_id :bigint
|
2020-08-06 16:35:45 +02:00
|
|
|
#
|
2018-05-30 11:36:48 +02:00
|
|
|
class DeletedDossier < ApplicationRecord
|
2020-07-20 16:35:44 +02:00
|
|
|
belongs_to :procedure, -> { with_discarded }, inverse_of: :deleted_dossiers, optional: false
|
2022-11-04 10:07:56 +01:00
|
|
|
belongs_to :groupe_instructeur, inverse_of: :deleted_dossiers, optional: true
|
2019-02-13 16:13:37 +01:00
|
|
|
|
2020-11-17 13:25:35 +01:00
|
|
|
scope :order_by_updated_at, -> (order = :desc) { order(created_at: order) }
|
2021-05-26 13:50:02 +02:00
|
|
|
scope :deleted_since, -> (since) { where('deleted_dossiers.deleted_at >= ?', since) }
|
2020-11-17 13:25:35 +01:00
|
|
|
|
2020-03-19 11:51:47 +01:00
|
|
|
enum reason: {
|
2020-03-26 09:08:52 +01:00
|
|
|
user_request: 'user_request',
|
|
|
|
manager_request: 'manager_request',
|
|
|
|
user_removed: 'user_removed',
|
|
|
|
procedure_removed: 'procedure_removed',
|
2020-11-17 13:25:35 +01:00
|
|
|
expired: 'expired',
|
|
|
|
instructeur_request: 'instructeur_request'
|
2020-03-19 11:51:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
def self.create_from_dossier(dossier, reason)
|
2022-03-09 10:27:43 +01:00
|
|
|
return if !dossier.log_operations?
|
|
|
|
|
2021-10-27 10:02:56 +02:00
|
|
|
# We have some bad data because of partially deleted dossiers in the past.
|
|
|
|
# For now use find_or_create_by! to avoid errors.
|
|
|
|
create_with(
|
2020-03-19 11:51:47 +01:00
|
|
|
reason: reasons.fetch(reason),
|
2020-11-12 10:38:10 +01:00
|
|
|
groupe_instructeur_id: dossier.groupe_instructeur_id,
|
|
|
|
revision_id: dossier.revision_id,
|
|
|
|
user_id: dossier.user_id,
|
2020-03-19 11:51:47 +01:00
|
|
|
procedure: dossier.procedure,
|
|
|
|
state: dossier.state,
|
|
|
|
deleted_at: Time.zone.now
|
2021-10-27 10:02:56 +02:00
|
|
|
).create_or_find_by!(dossier_id: dossier.id)
|
2019-02-13 16:13:37 +01:00
|
|
|
end
|
2020-03-26 17:35:50 +01:00
|
|
|
|
|
|
|
def procedure_removed?
|
|
|
|
reason == self.class.reasons.fetch(:procedure_removed)
|
|
|
|
end
|
2021-09-01 18:17:26 +02:00
|
|
|
|
|
|
|
def user_locale
|
|
|
|
User.find_by(id: user_id)&.locale || I18n.default_locale
|
|
|
|
end
|
2018-05-30 11:36:48 +02:00
|
|
|
end
|