2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-03-03 10:11:26 +01:00
|
|
|
class ExportTemplateValidator < ActiveModel::Validator
|
2024-07-15 23:23:14 +02:00
|
|
|
def validate(export_template)
|
|
|
|
validate_all_templates(export_template)
|
|
|
|
|
|
|
|
return if export_template.errors.any? # no need to continue if the templates are invalid
|
|
|
|
|
|
|
|
validate_dossier_folder(export_template)
|
|
|
|
validate_export_pdf(export_template)
|
|
|
|
validate_pjs(export_template)
|
|
|
|
|
|
|
|
validate_different_templates(export_template)
|
2024-03-03 10:11:26 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-07-15 23:23:14 +02:00
|
|
|
def validate_all_templates(export_template)
|
|
|
|
[export_template.dossier_folder, export_template.export_pdf, *export_template.pjs].each(&:template_string)
|
|
|
|
|
|
|
|
rescue StandardError
|
|
|
|
export_template.errors.add(:base, :invalid_template)
|
2024-03-03 10:11:26 +01:00
|
|
|
end
|
|
|
|
|
2024-07-15 23:23:14 +02:00
|
|
|
def validate_dossier_folder(export_template)
|
|
|
|
if !mentions(export_template.dossier_folder.template).include?('dossier_number')
|
|
|
|
export_template.errors.add(:dossier_folder, :dossier_number_required)
|
2024-03-03 10:11:26 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-15 23:23:14 +02:00
|
|
|
def mentions(template)
|
|
|
|
TiptapService.used_tags_and_libelle_for(template).map(&:first)
|
2024-03-03 10:11:26 +01:00
|
|
|
end
|
|
|
|
|
2024-07-15 23:23:14 +02:00
|
|
|
def validate_export_pdf(export_template)
|
|
|
|
return if !export_template.export_pdf.enabled?
|
2024-03-03 10:11:26 +01:00
|
|
|
|
2024-07-15 23:23:14 +02:00
|
|
|
if export_template.export_pdf.template_string.empty?
|
|
|
|
export_template.errors.add(:export_pdf, :blank)
|
2024-03-03 10:11:26 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-15 23:23:14 +02:00
|
|
|
def validate_pjs(export_template)
|
|
|
|
libelle_by_stable_ids = pj_libelle_by_stable_id(export_template)
|
|
|
|
|
|
|
|
export_template.pjs.filter(&:enabled?).each do |pj|
|
|
|
|
if pj.template_string.empty?
|
|
|
|
libelle = libelle_by_stable_ids[pj.stable_id]
|
|
|
|
export_template.errors.add(libelle, I18n.t(:blank, scope: 'errors.messages'))
|
|
|
|
end
|
2024-03-03 10:11:26 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-15 23:23:14 +02:00
|
|
|
def validate_different_templates(export_template)
|
|
|
|
templates = [export_template.export_pdf, *export_template.pjs]
|
|
|
|
.filter(&:enabled?)
|
|
|
|
.map(&:template_string)
|
|
|
|
|
|
|
|
return if templates.uniq.size == templates.size
|
|
|
|
|
|
|
|
export_template.errors.add(:base, :different_templates)
|
|
|
|
end
|
|
|
|
|
|
|
|
def pj_libelle_by_stable_id(export_template)
|
|
|
|
export_template.procedure.exportables_pieces_jointes
|
|
|
|
.pluck(:stable_id, :libelle).to_h
|
2024-03-03 10:11:26 +01:00
|
|
|
end
|
|
|
|
end
|