2020-06-26 11:37:28 +02:00
|
|
|
class ProcedureRevision < ApplicationRecord
|
2020-07-22 11:56:19 +02:00
|
|
|
self.implicit_order_column = :created_at
|
2020-08-27 19:55:10 +02:00
|
|
|
belongs_to :procedure, -> { with_discarded }, inverse_of: :revisions, optional: false
|
2022-02-04 14:40:16 +01:00
|
|
|
belongs_to :dossier_submitted_message, inverse_of: :revisions, optional: true, dependent: :destroy
|
2020-06-26 11:37:28 +02:00
|
|
|
|
2020-09-17 11:15:21 +02:00
|
|
|
has_many :dossiers, inverse_of: :revision, foreign_key: :revision_id
|
|
|
|
|
2022-05-18 15:47:52 +02:00
|
|
|
has_many :revision_types_de_champ, -> { order(:position, :id) }, class_name: 'ProcedureRevisionTypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision
|
2022-04-28 14:25:49 +02:00
|
|
|
has_many :revision_types_de_champ_public, -> { root.public_only.ordered }, class_name: 'ProcedureRevisionTypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision
|
2021-12-06 21:04:01 +01:00
|
|
|
has_many :revision_types_de_champ_private, -> { root.private_only.ordered }, class_name: 'ProcedureRevisionTypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision
|
2024-03-03 10:27:48 +01:00
|
|
|
has_many :revision_types_de_champ_private_and_public, -> { root.ordered }, class_name: 'ProcedureRevisionTypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision
|
2022-05-02 10:36:16 +02:00
|
|
|
has_many :types_de_champ, through: :revision_types_de_champ, source: :type_de_champ
|
2022-04-28 14:25:49 +02:00
|
|
|
has_many :types_de_champ_public, through: :revision_types_de_champ_public, source: :type_de_champ
|
2020-06-26 11:37:28 +02:00
|
|
|
has_many :types_de_champ_private, through: :revision_types_de_champ_private, source: :type_de_champ
|
|
|
|
|
2021-05-27 12:17:10 +02:00
|
|
|
has_one :draft_procedure, -> { with_discarded }, class_name: 'Procedure', foreign_key: :draft_revision_id, dependent: :nullify, inverse_of: :draft_revision
|
|
|
|
has_one :published_procedure, -> { with_discarded }, class_name: 'Procedure', foreign_key: :published_revision_id, dependent: :nullify, inverse_of: :published_revision
|
2021-04-13 20:24:12 +02:00
|
|
|
|
2021-12-06 21:04:01 +01:00
|
|
|
scope :ordered, -> { order(:created_at) }
|
2024-01-26 13:43:10 +01:00
|
|
|
|
2022-05-31 11:21:57 +02:00
|
|
|
validate :conditions_are_valid?
|
2023-03-24 10:31:01 +01:00
|
|
|
validate :header_sections_are_valid?
|
2023-09-28 15:56:32 +02:00
|
|
|
validate :expressions_regulieres_are_valid?
|
2022-05-31 11:21:57 +02:00
|
|
|
|
2023-02-21 16:00:58 +01:00
|
|
|
delegate :path, to: :procedure, prefix: true
|
|
|
|
|
2022-11-10 22:21:14 +01:00
|
|
|
def build_champs_public
|
2022-06-01 11:23:23 +02:00
|
|
|
# reload: it can be out of sync in test if some tdcs are added wihtout using add_tdc
|
2023-02-02 09:59:03 +01:00
|
|
|
types_de_champ_public.reload.map(&:build_champ)
|
2020-08-27 19:55:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_champs_private
|
2022-06-01 11:23:23 +02:00
|
|
|
# reload: it can be out of sync in test if some tdcs are added wihtout using add_tdc
|
2023-02-02 09:59:03 +01:00
|
|
|
types_de_champ_private.reload.map(&:build_champ)
|
2020-08-27 19:55:10 +02:00
|
|
|
end
|
|
|
|
|
2020-06-26 11:42:24 +02:00
|
|
|
def add_type_de_champ(params)
|
2022-07-02 16:20:42 +02:00
|
|
|
parent_stable_id = params.delete(:parent_stable_id)
|
2022-07-08 09:52:23 +02:00
|
|
|
parent_coordinate, _ = coordinate_and_tdc(parent_stable_id)
|
|
|
|
parent_id = parent_coordinate&.id
|
2022-05-12 14:18:28 +02:00
|
|
|
|
2022-07-08 09:52:23 +02:00
|
|
|
after_stable_id = params.delete(:after_stable_id)
|
|
|
|
after_coordinate, _ = coordinate_and_tdc(after_stable_id)
|
2022-05-12 14:18:28 +02:00
|
|
|
|
2024-01-22 11:27:09 +01:00
|
|
|
siblings = siblings_for(parent_coordinate:, private_tdc: params[:private])
|
2022-05-12 14:18:28 +02:00
|
|
|
|
|
|
|
tdc = TypeDeChamp.new(params)
|
|
|
|
if tdc.save
|
2024-01-22 11:27:09 +01:00
|
|
|
# moving all the impacted tdc down
|
2024-01-17 16:12:25 +01:00
|
|
|
position = next_position_for(after_coordinate:, siblings:)
|
|
|
|
siblings.where("position >= ?", position).update_all("position = position + 1")
|
2024-01-22 11:27:09 +01:00
|
|
|
|
|
|
|
# insertion of the new tdc
|
|
|
|
h = { type_de_champ: tdc, parent_id: parent_id, position: position }
|
2024-01-17 16:12:25 +01:00
|
|
|
revision_types_de_champ.create!(h)
|
2022-05-12 14:18:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
tdc
|
|
|
|
rescue => e
|
|
|
|
TypeDeChamp.new.tap { |tdc| tdc.errors.add(:base, e.message) }
|
2020-06-26 11:42:24 +02:00
|
|
|
end
|
|
|
|
|
2022-05-25 09:40:02 +02:00
|
|
|
def find_and_ensure_exclusive_use(stable_id)
|
2022-05-18 19:29:26 +02:00
|
|
|
coordinate, tdc = coordinate_and_tdc(stable_id)
|
2020-06-26 11:42:24 +02:00
|
|
|
|
2022-05-18 19:29:26 +02:00
|
|
|
if tdc.only_present_on_draft?
|
|
|
|
tdc
|
2020-06-26 11:42:24 +02:00
|
|
|
else
|
2022-05-24 15:57:31 +02:00
|
|
|
replace_type_de_champ_by_clone(coordinate)
|
2020-06-26 11:42:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-06 10:54:21 +02:00
|
|
|
def move_type_de_champ(stable_id, position)
|
2022-05-17 22:04:53 +02:00
|
|
|
coordinate, _ = coordinate_and_tdc(stable_id)
|
2024-01-17 16:12:25 +01:00
|
|
|
siblings = coordinate.siblings
|
2020-06-26 11:42:24 +02:00
|
|
|
|
2024-01-17 15:51:06 +01:00
|
|
|
if position > coordinate.position
|
2024-01-17 16:12:25 +01:00
|
|
|
siblings.where(position: coordinate.position..position).update_all("position = position - 1")
|
2024-01-17 15:51:06 +01:00
|
|
|
else
|
2024-01-17 16:12:25 +01:00
|
|
|
siblings.where(position: position..coordinate.position).update_all("position = position + 1")
|
2024-01-17 15:51:06 +01:00
|
|
|
end
|
|
|
|
coordinate.update_column(:position, position)
|
2022-06-01 15:23:18 +02:00
|
|
|
coordinate
|
2020-06-26 11:42:24 +02:00
|
|
|
end
|
|
|
|
|
2024-03-10 07:44:13 +01:00
|
|
|
def move_type_de_champ_after(stable_id, position)
|
|
|
|
coordinate, _ = coordinate_and_tdc(stable_id)
|
|
|
|
siblings = coordinate.siblings
|
|
|
|
|
|
|
|
if position > coordinate.position
|
|
|
|
siblings.where(position: coordinate.position..position).update_all("position = position - 1")
|
|
|
|
coordinate.update_column(:position, position)
|
|
|
|
else
|
|
|
|
siblings.where(position: (position + 1)...coordinate.position).update_all("position = position + 1")
|
|
|
|
coordinate.update_column(:position, position + 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
coordinate.reload
|
|
|
|
|
|
|
|
coordinate
|
|
|
|
end
|
|
|
|
|
2022-05-06 10:54:21 +02:00
|
|
|
def remove_type_de_champ(stable_id)
|
2022-05-17 22:04:53 +02:00
|
|
|
coordinate, tdc = coordinate_and_tdc(stable_id)
|
2022-05-11 20:22:52 +02:00
|
|
|
|
2022-10-11 16:27:49 +02:00
|
|
|
# in case of replay
|
|
|
|
return nil if coordinate.nil?
|
|
|
|
|
2022-05-24 15:57:31 +02:00
|
|
|
children = children_of(tdc).to_a
|
2022-05-11 20:22:52 +02:00
|
|
|
coordinate.destroy
|
|
|
|
|
2022-05-24 15:57:31 +02:00
|
|
|
children.each(&:destroy_if_orphan)
|
|
|
|
tdc.destroy_if_orphan
|
2022-05-11 20:22:52 +02:00
|
|
|
|
2024-01-17 16:12:25 +01:00
|
|
|
coordinate.siblings.where("position >= ?", coordinate.position).update_all("position = position - 1")
|
|
|
|
|
2022-06-01 15:23:18 +02:00
|
|
|
coordinate
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_up_type_de_champ(stable_id)
|
|
|
|
coordinate, _ = coordinate_and_tdc(stable_id)
|
|
|
|
|
|
|
|
if coordinate.position > 0
|
|
|
|
move_type_de_champ(stable_id, coordinate.position - 1)
|
|
|
|
else
|
|
|
|
coordinate
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_down_type_de_champ(stable_id)
|
|
|
|
coordinate, _ = coordinate_and_tdc(stable_id)
|
|
|
|
|
|
|
|
move_type_de_champ(stable_id, coordinate.position + 1)
|
2020-06-26 11:42:24 +02:00
|
|
|
end
|
|
|
|
|
2020-06-26 11:37:28 +02:00
|
|
|
def draft?
|
2023-02-09 14:39:33 +01:00
|
|
|
procedure.draft_revision_id == id
|
2020-06-26 11:37:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def locked?
|
|
|
|
!draft?
|
|
|
|
end
|
2020-06-26 11:42:24 +02:00
|
|
|
|
2022-01-12 17:38:57 +01:00
|
|
|
def different_from?(revision)
|
2022-11-09 12:10:54 +01:00
|
|
|
revision_types_de_champ != revision.revision_types_de_champ
|
2021-01-20 13:21:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def compare(revision)
|
|
|
|
changes = []
|
2022-05-18 15:47:52 +02:00
|
|
|
changes += compare_revision_types_de_champ(revision_types_de_champ, revision.revision_types_de_champ)
|
2021-01-20 13:21:23 +01:00
|
|
|
changes
|
|
|
|
end
|
|
|
|
|
2022-04-07 11:40:53 +02:00
|
|
|
def dossier_for_preview(user)
|
|
|
|
dossier = Dossier
|
2023-07-18 11:29:44 +02:00
|
|
|
.create_with(autorisation_donnees: true)
|
2022-04-07 11:40:53 +02:00
|
|
|
.find_or_initialize_by(revision: self, user: user, for_procedure_preview: true, state: Dossier.states.fetch(:brouillon))
|
|
|
|
|
|
|
|
if dossier.new_record?
|
|
|
|
dossier.build_default_individual
|
|
|
|
dossier.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
dossier
|
|
|
|
end
|
|
|
|
|
2024-02-08 18:28:15 +01:00
|
|
|
def types_de_champ_for(scope: nil, root: false)
|
2023-11-28 17:22:17 +01:00
|
|
|
# We return an unordered collection
|
|
|
|
return types_de_champ if !root && scope.nil?
|
|
|
|
return types_de_champ.filter { scope == :public ? _1.public? : _1.private? } if !root
|
|
|
|
|
|
|
|
# We return an ordered collection
|
|
|
|
case scope
|
|
|
|
when :public
|
|
|
|
types_de_champ_public
|
|
|
|
when :private
|
|
|
|
types_de_champ_private
|
|
|
|
else
|
|
|
|
types_de_champ_public + types_de_champ_private
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-06 15:47:57 +02:00
|
|
|
def children_of(tdc)
|
2023-02-09 14:39:33 +01:00
|
|
|
if revision_types_de_champ.loaded?
|
|
|
|
parent_coordinate_id = revision_types_de_champ
|
|
|
|
.filter { _1.type_de_champ_id == tdc.id }
|
|
|
|
.map(&:id)
|
|
|
|
|
|
|
|
revision_types_de_champ
|
|
|
|
.filter { _1.parent_id.in?(parent_coordinate_id) }
|
|
|
|
.sort_by(&:position)
|
|
|
|
.map(&:type_de_champ)
|
|
|
|
else
|
|
|
|
parent_coordinate_id = revision_types_de_champ.where(type_de_champ: tdc).select(:id)
|
2022-05-06 15:47:57 +02:00
|
|
|
|
2023-02-09 14:39:33 +01:00
|
|
|
types_de_champ
|
|
|
|
.where(procedure_revision_types_de_champ: { parent_id: parent_coordinate_id })
|
|
|
|
.order("procedure_revision_types_de_champ.position")
|
|
|
|
end
|
2022-05-06 15:47:57 +02:00
|
|
|
end
|
|
|
|
|
2024-03-14 13:34:20 +01:00
|
|
|
def parent_of(tdc)
|
|
|
|
revision_types_de_champ
|
|
|
|
.find { _1.type_de_champ_id == tdc.id }.parent&.type_de_champ
|
|
|
|
end
|
|
|
|
|
|
|
|
def child?(tdc)
|
|
|
|
revision_types_de_champ
|
|
|
|
.find { _1.type_de_champ_id == tdc.id }.child?
|
|
|
|
end
|
|
|
|
|
2022-05-24 15:57:31 +02:00
|
|
|
def remove_children_of(tdc)
|
|
|
|
children_of(tdc).each do |child|
|
|
|
|
remove_type_de_champ(child.stable_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-16 12:19:49 +01:00
|
|
|
def dependent_conditions(tdc)
|
|
|
|
stable_id = tdc.stable_id
|
|
|
|
|
|
|
|
(tdc.public? ? types_de_champ_public : types_de_champ_private).filter do |other_tdc|
|
|
|
|
next if !other_tdc.condition?
|
|
|
|
|
|
|
|
other_tdc.condition.sources.include?(stable_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-24 14:17:10 +02:00
|
|
|
# Estimated duration to fill the form, in seconds.
|
|
|
|
#
|
|
|
|
# If the revision is locked (i.e. published), the result is cached (because type de champs can no longer be mutated).
|
2022-05-24 11:39:03 +02:00
|
|
|
def estimated_fill_duration
|
2022-05-24 14:17:10 +02:00
|
|
|
Rails.cache.fetch("#{cache_key_with_version}/estimated_fill_duration", expires_in: 12.hours, force: !locked?) do
|
|
|
|
compute_estimated_fill_duration
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-01 15:23:18 +02:00
|
|
|
def coordinate_for(tdc)
|
|
|
|
revision_types_de_champ.find_by!(type_de_champ: tdc)
|
|
|
|
end
|
|
|
|
|
2022-11-16 11:50:19 +01:00
|
|
|
def carte?
|
|
|
|
types_de_champ_public.any?(&:carte?)
|
|
|
|
end
|
|
|
|
|
2023-04-05 18:07:32 +02:00
|
|
|
def coordinate_and_tdc(stable_id)
|
|
|
|
return [nil, nil] if stable_id.blank?
|
|
|
|
|
|
|
|
coordinate = revision_types_de_champ
|
|
|
|
.joins(:type_de_champ)
|
|
|
|
.find_by(type_de_champ: { stable_id: stable_id })
|
|
|
|
|
|
|
|
[coordinate, coordinate&.type_de_champ]
|
|
|
|
end
|
|
|
|
|
2023-05-09 14:36:20 +02:00
|
|
|
def routable_types_de_champ
|
2023-09-15 09:50:29 +02:00
|
|
|
types_de_champ_public.filter(&:routable?)
|
2023-05-09 14:36:20 +02:00
|
|
|
end
|
|
|
|
|
2022-05-24 14:17:10 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def compute_estimated_fill_duration
|
2022-10-29 12:43:37 +02:00
|
|
|
types_de_champ_public.sum do |tdc|
|
|
|
|
next tdc.estimated_read_duration unless tdc.fillable?
|
|
|
|
|
|
|
|
duration = tdc.estimated_read_duration + tdc.estimated_fill_duration(self)
|
|
|
|
duration /= 2 unless tdc.mandatory?
|
|
|
|
|
|
|
|
duration
|
2022-05-24 11:39:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-20 12:27:26 +02:00
|
|
|
def children_types_de_champ_as_json(tdcs_as_json, parent_tdcs)
|
|
|
|
parent_tdcs.each do |parent_tdc|
|
|
|
|
tdc_as_json = tdcs_as_json.find { |json| json["id"] == parent_tdc.stable_id }
|
|
|
|
tdc_as_json&.merge!(types_de_champ: children_of(parent_tdc).includes(piece_justificative_template_attachment: :blob).map(&:as_json_for_editor))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-17 16:12:25 +01:00
|
|
|
def siblings_for(parent_coordinate: nil, private_tdc: false)
|
|
|
|
if parent_coordinate
|
|
|
|
parent_coordinate.revision_types_de_champ
|
|
|
|
elsif private_tdc
|
|
|
|
revision_types_de_champ_private
|
|
|
|
else
|
|
|
|
revision_types_de_champ_public
|
|
|
|
end
|
2022-05-11 20:22:52 +02:00
|
|
|
end
|
|
|
|
|
2024-01-17 16:12:25 +01:00
|
|
|
def next_position_for(siblings:, after_coordinate: nil)
|
2024-01-23 09:52:20 +01:00
|
|
|
# either we are at the beginning of the list or after another item
|
|
|
|
if after_coordinate.nil? # first element of the list, starts at 0
|
2024-01-17 16:12:25 +01:00
|
|
|
0
|
2024-01-23 09:52:20 +01:00
|
|
|
else # after another item
|
2024-01-17 16:12:25 +01:00
|
|
|
after_coordinate.position + 1
|
|
|
|
end
|
2024-01-17 16:01:30 +01:00
|
|
|
end
|
|
|
|
|
2022-05-18 15:47:52 +02:00
|
|
|
def compare_revision_types_de_champ(from_coordinates, to_coordinates)
|
|
|
|
if from_coordinates == to_coordinates
|
2021-01-20 13:21:23 +01:00
|
|
|
[]
|
|
|
|
else
|
2022-05-18 15:47:52 +02:00
|
|
|
from_h = from_coordinates.index_by(&:stable_id)
|
|
|
|
to_h = to_coordinates.index_by(&:stable_id)
|
2021-01-20 13:21:23 +01:00
|
|
|
|
|
|
|
from_sids = from_h.keys
|
|
|
|
to_sids = to_h.keys
|
|
|
|
|
2022-12-23 13:01:18 +01:00
|
|
|
removed = (from_sids - to_sids).map { ProcedureRevisionChange::RemoveChamp.new(from_h[_1]) }
|
|
|
|
added = (to_sids - from_sids).map { ProcedureRevisionChange::AddChamp.new(to_h[_1]) }
|
2021-01-20 13:21:23 +01:00
|
|
|
|
|
|
|
kept = from_sids.intersection(to_sids)
|
|
|
|
|
|
|
|
moved = kept
|
2022-12-23 13:01:18 +01:00
|
|
|
.map { [from_h[_1], to_h[_1]] }
|
|
|
|
.filter { |from, to| from.position != to.position }
|
|
|
|
.map { |from, to| ProcedureRevisionChange::MoveChamp.new(from, from.position, to.position) }
|
2021-01-20 13:21:23 +01:00
|
|
|
|
|
|
|
changed = kept
|
2022-12-23 13:01:18 +01:00
|
|
|
.map { [from_h[_1], to_h[_1]] }
|
|
|
|
.flat_map { |from, to| compare_type_de_champ(from.type_de_champ, to.type_de_champ, from_coordinates, to_coordinates) }
|
2021-01-20 13:21:23 +01:00
|
|
|
|
2023-02-07 10:37:03 +01:00
|
|
|
(removed + added + moved + changed).sort_by { _1.op == :remove ? from_sids.index(_1.stable_id) : to_sids.index(_1.stable_id) }
|
2021-01-20 13:21:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-26 21:21:55 +02:00
|
|
|
def compare_type_de_champ(from_type_de_champ, to_type_de_champ, from_coordinates, to_coordinates)
|
2021-01-20 13:21:23 +01:00
|
|
|
changes = []
|
|
|
|
if from_type_de_champ.type_champ != to_type_de_champ.type_champ
|
2022-12-23 13:01:18 +01:00
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
|
|
|
:type_champ,
|
|
|
|
from_type_de_champ.type_champ,
|
|
|
|
to_type_de_champ.type_champ)
|
2021-01-20 13:21:23 +01:00
|
|
|
end
|
|
|
|
if from_type_de_champ.libelle != to_type_de_champ.libelle
|
2022-12-23 13:01:18 +01:00
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
|
|
|
:libelle,
|
|
|
|
from_type_de_champ.libelle,
|
|
|
|
to_type_de_champ.libelle)
|
2021-01-20 13:21:23 +01:00
|
|
|
end
|
2022-11-08 09:26:34 +01:00
|
|
|
if from_type_de_champ.collapsible_explanation_enabled? != to_type_de_champ.collapsible_explanation_enabled?
|
2022-12-23 13:01:18 +01:00
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
|
|
|
:collapsible_explanation_enabled,
|
|
|
|
from_type_de_champ.collapsible_explanation_enabled?,
|
|
|
|
to_type_de_champ.collapsible_explanation_enabled?)
|
2022-11-08 09:26:34 +01:00
|
|
|
end
|
|
|
|
if from_type_de_champ.collapsible_explanation_text != to_type_de_champ.collapsible_explanation_text
|
2022-12-23 13:01:18 +01:00
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
|
|
|
:collapsible_explanation_text,
|
|
|
|
from_type_de_champ.collapsible_explanation_text,
|
|
|
|
to_type_de_champ.collapsible_explanation_text)
|
2022-11-08 09:26:34 +01:00
|
|
|
end
|
2021-01-20 13:21:23 +01:00
|
|
|
if from_type_de_champ.description != to_type_de_champ.description
|
2022-12-23 13:01:18 +01:00
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
|
|
|
:description,
|
|
|
|
from_type_de_champ.description,
|
|
|
|
to_type_de_champ.description)
|
2021-01-20 13:21:23 +01:00
|
|
|
end
|
|
|
|
if from_type_de_champ.mandatory? != to_type_de_champ.mandatory?
|
2022-12-23 13:01:18 +01:00
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
|
|
|
:mandatory,
|
|
|
|
from_type_de_champ.mandatory?,
|
|
|
|
to_type_de_champ.mandatory?)
|
2021-01-20 13:21:23 +01:00
|
|
|
end
|
2022-07-13 12:35:11 +02:00
|
|
|
|
2022-07-13 15:32:25 +02:00
|
|
|
if from_type_de_champ.condition != to_type_de_champ.condition
|
2022-12-23 13:01:18 +01:00
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
|
|
|
:condition,
|
|
|
|
from_type_de_champ.condition&.to_s(from_coordinates.map(&:type_de_champ)),
|
|
|
|
to_type_de_champ.condition&.to_s(to_coordinates.map(&:type_de_champ)))
|
2022-07-13 12:35:11 +02:00
|
|
|
end
|
|
|
|
|
2021-01-20 13:21:23 +01:00
|
|
|
if to_type_de_champ.drop_down_list?
|
|
|
|
if from_type_de_champ.drop_down_list_options != to_type_de_champ.drop_down_list_options
|
2022-12-23 13:01:18 +01:00
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
|
|
|
:drop_down_options,
|
|
|
|
from_type_de_champ.drop_down_list_options,
|
|
|
|
to_type_de_champ.drop_down_list_options)
|
2021-06-23 15:54:12 +02:00
|
|
|
end
|
2021-10-19 18:48:45 +02:00
|
|
|
if to_type_de_champ.linked_drop_down_list?
|
|
|
|
if from_type_de_champ.drop_down_secondary_libelle != to_type_de_champ.drop_down_secondary_libelle
|
2022-12-23 13:01:18 +01:00
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
|
|
|
:drop_down_secondary_libelle,
|
|
|
|
from_type_de_champ.drop_down_secondary_libelle,
|
|
|
|
to_type_de_champ.drop_down_secondary_libelle)
|
2021-10-19 18:48:45 +02:00
|
|
|
end
|
|
|
|
if from_type_de_champ.drop_down_secondary_description != to_type_de_champ.drop_down_secondary_description
|
2022-12-23 13:01:18 +01:00
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
|
|
|
:drop_down_secondary_description,
|
|
|
|
from_type_de_champ.drop_down_secondary_description,
|
|
|
|
to_type_de_champ.drop_down_secondary_description)
|
2021-10-19 18:48:45 +02:00
|
|
|
end
|
|
|
|
end
|
2022-08-02 12:57:41 +02:00
|
|
|
if from_type_de_champ.drop_down_other? != to_type_de_champ.drop_down_other?
|
2022-12-23 13:01:18 +01:00
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
|
|
|
:drop_down_other,
|
|
|
|
from_type_de_champ.drop_down_other?,
|
|
|
|
to_type_de_champ.drop_down_other?)
|
2021-10-22 20:44:46 +02:00
|
|
|
end
|
2021-06-23 15:54:12 +02:00
|
|
|
elsif to_type_de_champ.carte?
|
|
|
|
if from_type_de_champ.carte_optional_layers != to_type_de_champ.carte_optional_layers
|
2022-12-23 13:01:18 +01:00
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
|
|
|
:carte_layers,
|
|
|
|
from_type_de_champ.carte_optional_layers,
|
|
|
|
to_type_de_champ.carte_optional_layers)
|
2021-01-20 13:21:23 +01:00
|
|
|
end
|
|
|
|
elsif to_type_de_champ.piece_justificative?
|
2023-12-19 10:44:51 +01:00
|
|
|
if from_type_de_champ.checksum_for_attachment(:piece_justificative_template) != to_type_de_champ.checksum_for_attachment(:piece_justificative_template)
|
2022-12-23 13:01:18 +01:00
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
|
|
|
:piece_justificative_template,
|
2023-12-19 10:44:51 +01:00
|
|
|
from_type_de_champ.filename_for_attachement(:piece_justificative_template),
|
|
|
|
to_type_de_champ.filename_for_attachement(:piece_justificative_template))
|
|
|
|
end
|
|
|
|
elsif to_type_de_champ.explication?
|
|
|
|
if from_type_de_champ.checksum_for_attachment(:notice_explicative) != to_type_de_champ.checksum_for_attachment(:notice_explicative)
|
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
|
|
|
:notice_explicative,
|
|
|
|
from_type_de_champ.filename_for_attachement(:notice_explicative),
|
|
|
|
to_type_de_champ.filename_for_attachement(:notice_explicative))
|
2021-01-20 13:21:23 +01:00
|
|
|
end
|
2023-04-28 18:46:44 +02:00
|
|
|
elsif to_type_de_champ.textarea?
|
2023-04-28 23:28:13 +02:00
|
|
|
if from_type_de_champ.character_limit != to_type_de_champ.character_limit
|
2023-04-28 18:46:44 +02:00
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
2023-04-28 23:28:13 +02:00
|
|
|
:character_limit,
|
|
|
|
from_type_de_champ.character_limit,
|
|
|
|
to_type_de_champ.character_limit)
|
2023-04-28 18:46:44 +02:00
|
|
|
end
|
2023-10-12 17:09:32 +02:00
|
|
|
elsif to_type_de_champ.expression_reguliere?
|
|
|
|
if from_type_de_champ.expression_reguliere != to_type_de_champ.expression_reguliere
|
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
|
|
|
:expression_reguliere,
|
|
|
|
from_type_de_champ.expression_reguliere,
|
|
|
|
to_type_de_champ.expression_reguliere)
|
|
|
|
end
|
|
|
|
if from_type_de_champ.expression_reguliere_exemple_text != to_type_de_champ.expression_reguliere_exemple_text
|
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
|
|
|
:expression_reguliere_exemple_text,
|
|
|
|
from_type_de_champ.expression_reguliere_exemple_text,
|
|
|
|
to_type_de_champ.expression_reguliere_exemple_text)
|
|
|
|
end
|
|
|
|
if from_type_de_champ.expression_reguliere_error_message != to_type_de_champ.expression_reguliere_error_message
|
|
|
|
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
|
|
|
|
:expression_reguliere_error_message,
|
|
|
|
from_type_de_champ.expression_reguliere_error_message,
|
|
|
|
to_type_de_champ.expression_reguliere_error_message)
|
|
|
|
end
|
2021-01-20 13:21:23 +01:00
|
|
|
end
|
|
|
|
changes
|
|
|
|
end
|
|
|
|
|
2022-05-24 15:57:31 +02:00
|
|
|
def replace_type_de_champ_by_clone(coordinate)
|
|
|
|
cloned_type_de_champ = coordinate.type_de_champ.deep_clone do |original, kopy|
|
2024-02-07 17:29:47 +01:00
|
|
|
ClonePiecesJustificativesService.clone_attachments(original, kopy)
|
2021-07-01 13:33:22 +02:00
|
|
|
end
|
2022-05-18 19:29:26 +02:00
|
|
|
coordinate.update!(type_de_champ: cloned_type_de_champ)
|
2020-09-08 15:53:07 +02:00
|
|
|
cloned_type_de_champ
|
|
|
|
end
|
2022-06-01 15:23:18 +02:00
|
|
|
|
2022-05-31 11:21:57 +02:00
|
|
|
def conditions_are_valid?
|
2022-09-26 21:11:43 +02:00
|
|
|
public_tdcs = types_de_champ_public.to_a
|
2023-04-04 10:24:11 +02:00
|
|
|
.flat_map { _1.repetition? ? children_of(_1) : _1 }
|
2022-05-31 11:21:57 +02:00
|
|
|
|
2022-09-26 21:11:43 +02:00
|
|
|
public_tdcs
|
2022-05-31 11:21:57 +02:00
|
|
|
.map.with_index
|
2023-02-09 14:39:33 +01:00
|
|
|
.filter_map { |tdc, i| tdc.condition? ? [tdc, i] : nil }
|
2023-04-04 10:24:11 +02:00
|
|
|
.map do |tdc, i|
|
|
|
|
[tdc, tdc.condition.errors(public_tdcs.take(i))]
|
|
|
|
end
|
2022-07-11 21:18:08 +02:00
|
|
|
.filter { |_tdc, errors| errors.present? }
|
|
|
|
.each { |tdc, message| errors.add(:condition, message, type_de_champ: tdc) }
|
2022-05-31 11:21:57 +02:00
|
|
|
end
|
2023-03-24 10:31:01 +01:00
|
|
|
|
|
|
|
def header_sections_are_valid?
|
|
|
|
public_tdcs = types_de_champ_public.to_a
|
|
|
|
|
|
|
|
root_tdcs_errors = errors_for_header_sections_order(public_tdcs)
|
|
|
|
repetition_tdcs_errors = public_tdcs
|
|
|
|
.filter_map { _1.repetition? ? children_of(_1) : nil }
|
|
|
|
.map { errors_for_header_sections_order(_1) }
|
|
|
|
|
|
|
|
repetition_tdcs_errors + root_tdcs_errors
|
|
|
|
end
|
|
|
|
|
2023-09-28 15:56:32 +02:00
|
|
|
def expressions_regulieres_are_valid?
|
|
|
|
types_de_champ_public.to_a
|
|
|
|
.flat_map { _1.repetition? ? children_of(_1) : _1 }
|
2023-09-29 23:09:41 +02:00
|
|
|
.each do |tdc|
|
|
|
|
if tdc.expression_reguliere? && tdc.invalid_regexp?
|
|
|
|
errors.add(:expression_reguliere, type_de_champ: tdc)
|
|
|
|
end
|
|
|
|
end
|
2023-09-28 15:56:32 +02:00
|
|
|
end
|
|
|
|
|
2023-03-24 10:31:01 +01:00
|
|
|
def errors_for_header_sections_order(tdcs)
|
|
|
|
tdcs
|
|
|
|
.map.with_index
|
|
|
|
.filter_map { |tdc, i| tdc.header_section? ? [tdc, i] : nil }
|
2023-03-24 11:08:31 +01:00
|
|
|
.map { |tdc, i| [tdc, tdc.check_coherent_header_level(tdcs.take(i))] }
|
2023-03-24 10:31:01 +01:00
|
|
|
.filter { |_tdc, errors| errors.present? }
|
|
|
|
.each { |tdc, message| errors.add(:header_section, message, type_de_champ: tdc) }
|
|
|
|
end
|
2020-06-26 11:37:28 +02:00
|
|
|
end
|