2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-04-29 17:29:47 +02:00
|
|
|
# == Schema Information
|
|
|
|
class Archive < ApplicationRecord
|
2022-07-07 18:15:48 +02:00
|
|
|
include TransientModelsWithPurgeableJobConcern
|
2021-04-29 17:29:47 +02:00
|
|
|
|
2022-02-28 13:16:27 +01:00
|
|
|
RETENTION_DURATION = 4.days
|
2023-03-09 15:37:13 +01:00
|
|
|
MAX_DUREE_GENERATION = 16.hours
|
2022-03-18 16:04:31 +01:00
|
|
|
MAX_SIZE = 100.gigabytes
|
2021-04-29 17:29:47 +02:00
|
|
|
|
|
|
|
has_and_belongs_to_many :groupe_instructeurs
|
|
|
|
|
|
|
|
has_one_attached :file
|
2024-02-07 17:19:50 +01:00
|
|
|
belongs_to :user_profile, polymorphic: true, optional: true
|
2021-04-29 17:29:47 +02:00
|
|
|
|
|
|
|
scope :for_groupe_instructeur, -> (groupe_instructeur) {
|
|
|
|
joins(:archives_groupe_instructeurs)
|
|
|
|
.where(
|
|
|
|
archives_groupe_instructeurs: { groupe_instructeur: groupe_instructeur }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
enum time_span_type: {
|
|
|
|
everything: 'everything',
|
|
|
|
monthly: 'monthly'
|
|
|
|
}
|
|
|
|
|
|
|
|
def filename(procedure)
|
|
|
|
if time_span_type == 'everything'
|
|
|
|
"procedure-#{procedure.id}.zip"
|
|
|
|
else
|
|
|
|
"procedure-#{procedure.id}-mois-#{I18n.l(month, format: '%Y-%m')}.zip"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-07 17:19:50 +01:00
|
|
|
def self.find_or_create_archive(time_span_type, month, groupe_instructeurs, user_profile)
|
2021-04-29 17:29:47 +02:00
|
|
|
create_with(groupe_instructeurs: groupe_instructeurs)
|
2024-02-07 17:19:50 +01:00
|
|
|
.create_or_find_by(time_span_type:, month:, user_profile:, key: generate_cache_key(groupe_instructeurs))
|
2021-04-29 17:29:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.generate_cache_key(groupe_instructeurs)
|
|
|
|
groupe_instructeurs.map(&:id).sort.join('-')
|
|
|
|
end
|
|
|
|
end
|