2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-23 21:02:18 +01:00
|
|
|
class DossierOperationLog < ApplicationRecord
|
|
|
|
enum operation: {
|
2020-02-26 10:28:37 +01:00
|
|
|
changer_groupe_instructeur: 'changer_groupe_instructeur',
|
2018-11-23 21:02:18 +01:00
|
|
|
passer_en_instruction: 'passer_en_instruction',
|
|
|
|
repasser_en_construction: 'repasser_en_construction',
|
2023-06-20 16:15:18 +02:00
|
|
|
demander_une_correction: 'demander_une_correction',
|
|
|
|
demander_a_completer: 'demander_a_completer',
|
2019-07-01 17:45:03 +02:00
|
|
|
repasser_en_instruction: 'repasser_en_instruction',
|
2018-11-23 21:02:18 +01:00
|
|
|
accepter: 'accepter',
|
|
|
|
refuser: 'refuser',
|
2019-02-13 16:13:37 +01:00
|
|
|
classer_sans_suite: 'classer_sans_suite',
|
2019-05-02 16:23:47 +02:00
|
|
|
supprimer: 'supprimer',
|
2020-03-26 17:35:50 +01:00
|
|
|
restaurer: 'restaurer',
|
2019-05-02 16:23:47 +02:00
|
|
|
modifier_annotation: 'modifier_annotation',
|
2022-11-30 12:39:47 +01:00
|
|
|
demander_un_avis: 'demander_un_avis'
|
2018-11-23 21:02:18 +01:00
|
|
|
}
|
|
|
|
|
2019-05-02 16:22:16 +02:00
|
|
|
has_one_attached :serialized
|
2020-03-19 18:33:38 +01:00
|
|
|
|
|
|
|
belongs_to :dossier, optional: true
|
2019-06-17 11:01:41 +02:00
|
|
|
belongs_to :bill_signature, optional: true
|
2019-05-02 16:22:16 +02:00
|
|
|
|
2021-10-21 13:29:47 +02:00
|
|
|
scope :not_deletion, -> { where.not(operation: operations.fetch(:supprimer)) }
|
2022-11-18 11:31:23 +01:00
|
|
|
scope :with_data, -> { where.not(data: nil) }
|
2022-03-09 10:27:43 +01:00
|
|
|
scope :brouillon_expired, -> { where(dossier: Dossier.brouillon_expired).not_deletion }
|
|
|
|
scope :en_construction_expired, -> { where(dossier: Dossier.en_construction_expired).not_deletion }
|
|
|
|
scope :termine_expired, -> { where(dossier: Dossier.termine_expired).not_deletion }
|
2021-10-21 13:29:47 +02:00
|
|
|
|
2022-11-18 11:31:23 +01:00
|
|
|
def move_to_cold_storage!
|
|
|
|
if data.present?
|
|
|
|
serialized.attach(
|
|
|
|
io: StringIO.new(data.to_json),
|
|
|
|
filename: "operation-#{digest}.json",
|
|
|
|
content_type: 'application/json',
|
|
|
|
# we don't want to run virus scanner on this file
|
|
|
|
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
|
|
|
)
|
|
|
|
update!(data: nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.purge_discarded
|
|
|
|
not_deletion.destroy_all
|
2023-11-28 16:47:37 +01:00
|
|
|
|
|
|
|
supprimer.map { _1.serialized.purge_later }
|
|
|
|
supprimer.update_all(data: nil)
|
2022-11-18 11:31:23 +01:00
|
|
|
end
|
|
|
|
|
2019-05-02 16:22:16 +02:00
|
|
|
def self.create_and_serialize(params)
|
|
|
|
dossier = params.fetch(:dossier)
|
|
|
|
|
2019-05-02 17:02:47 +02:00
|
|
|
duree_conservation_dossiers = dossier.procedure.duree_conservation_dossiers_dans_ds
|
|
|
|
keep_until = if duree_conservation_dossiers.present?
|
|
|
|
if dossier.en_instruction_at
|
|
|
|
dossier.en_instruction_at + duree_conservation_dossiers.months
|
|
|
|
else
|
|
|
|
dossier.created_at + duree_conservation_dossiers.months
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-02 16:22:16 +02:00
|
|
|
operation_log = new(operation: params.fetch(:operation),
|
|
|
|
dossier_id: dossier.id,
|
2019-05-02 17:02:47 +02:00
|
|
|
keep_until: keep_until,
|
2019-05-02 16:22:16 +02:00
|
|
|
executed_at: Time.zone.now,
|
|
|
|
automatic_operation: !!params[:automatic_operation])
|
|
|
|
|
2022-11-18 11:31:23 +01:00
|
|
|
data = {
|
2019-05-02 16:22:16 +02:00
|
|
|
operation: operation_log.operation,
|
|
|
|
dossier_id: operation_log.dossier_id,
|
|
|
|
author: self.serialize_author(params[:author]),
|
2020-11-17 13:25:35 +01:00
|
|
|
subject: self.serialize_subject(params[:subject], operation_log.operation),
|
2019-05-02 16:22:16 +02:00
|
|
|
automatic_operation: operation_log.automatic_operation?,
|
|
|
|
executed_at: operation_log.executed_at.iso8601
|
2022-11-18 11:31:23 +01:00
|
|
|
}.compact
|
2019-05-02 16:22:16 +02:00
|
|
|
|
2022-11-18 11:31:23 +01:00
|
|
|
operation_log.data = data
|
|
|
|
operation_log.digest = Digest::SHA256.hexdigest(data.to_json)
|
2019-05-02 16:22:16 +02:00
|
|
|
|
|
|
|
operation_log.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.serialize_author(author)
|
|
|
|
if author.nil?
|
|
|
|
nil
|
|
|
|
else
|
2021-01-28 13:53:18 +01:00
|
|
|
{
|
|
|
|
id: serialize_author_id(author),
|
|
|
|
email: author.email
|
|
|
|
}.as_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.serialize_author_id(object)
|
|
|
|
case object
|
|
|
|
when User
|
|
|
|
"Usager##{object.id}"
|
|
|
|
when Instructeur
|
|
|
|
"Instructeur##{object.id}"
|
|
|
|
when Administrateur
|
|
|
|
"Administrateur##{object.id}"
|
|
|
|
when SuperAdmin
|
|
|
|
"Manager##{object.id}"
|
|
|
|
else
|
|
|
|
nil
|
2019-05-02 16:22:16 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-17 13:25:35 +01:00
|
|
|
def self.serialize_subject(subject, operation = nil)
|
2019-05-02 16:22:16 +02:00
|
|
|
if subject.nil?
|
|
|
|
nil
|
2020-11-26 15:13:32 +01:00
|
|
|
elsif operation == operations.fetch(:supprimer)
|
2020-11-17 13:25:35 +01:00
|
|
|
{
|
2021-12-06 15:49:17 +01:00
|
|
|
date_de_depot: subject.depose_at,
|
2020-11-17 13:25:35 +01:00
|
|
|
date_de_mise_en_instruction: subject.en_instruction_at,
|
2021-12-06 15:49:17 +01:00
|
|
|
date_de_decision: subject.processed_at
|
2020-11-17 13:25:35 +01:00
|
|
|
}.as_json
|
2019-05-02 16:22:16 +02:00
|
|
|
else
|
|
|
|
case subject
|
|
|
|
when Dossier
|
2021-01-28 13:53:18 +01:00
|
|
|
SerializerService.dossier(subject)
|
2019-05-02 16:23:47 +02:00
|
|
|
when Champ
|
2021-01-28 13:53:18 +01:00
|
|
|
SerializerService.champ(subject)
|
2019-05-02 16:24:24 +02:00
|
|
|
when Avis
|
2021-01-28 13:53:18 +01:00
|
|
|
SerializerService.avis(subject)
|
2023-06-20 16:15:18 +02:00
|
|
|
when Commentaire
|
|
|
|
SerializerService.message(subject)
|
2019-05-02 16:22:16 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-11-23 21:02:18 +01:00
|
|
|
end
|