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) }
|
2023-02-23 17:14:25 +01:00
|
|
|
scope :state_termine, -> { where(state: [states.fetch(:accepte), states.fetch(:refuse), states.fetch(:sans_suite)]) }
|
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
|
|
|
}
|
|
|
|
|
2023-02-23 17:14:25 +01:00
|
|
|
enum state: {
|
|
|
|
en_construction: 'en_construction',
|
|
|
|
en_instruction: 'en_instruction',
|
|
|
|
accepte: 'accepte',
|
|
|
|
refuse: 'refuse',
|
|
|
|
sans_suite: 'sans_suite'
|
|
|
|
}
|
|
|
|
|
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
|