2021-10-13 14:41:05 +02:00
|
|
|
module DossierRebaseConcern
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
def rebase!
|
2022-02-11 18:23:46 +01:00
|
|
|
if can_rebase?
|
2021-10-13 14:41:05 +02:00
|
|
|
transaction do
|
|
|
|
rebase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-11 18:23:46 +01:00
|
|
|
def can_rebase?
|
|
|
|
revision != procedure.published_revision &&
|
|
|
|
(brouillon? || accepted_en_construction_changes? || accepted_en_instruction_changes?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def pending_changes
|
|
|
|
revision.compare(procedure.published_revision)
|
|
|
|
end
|
|
|
|
|
2021-10-13 14:41:05 +02:00
|
|
|
private
|
|
|
|
|
2022-02-11 18:23:46 +01:00
|
|
|
def accepted_en_construction_changes?
|
|
|
|
en_construction? && pending_changes.all? { |change| accepted_en_construction_change?(change) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def accepted_en_instruction_changes?
|
|
|
|
en_instruction? && pending_changes.all? { |change| accepted_en_instruction_change?(change) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def accepted_en_construction_change?(change)
|
|
|
|
if change[:model] == :attestation_template || change[:op] == :move || change[:op] == :remove
|
|
|
|
true
|
|
|
|
elsif change[:op] == :update
|
|
|
|
case change[:attribute]
|
|
|
|
when :carte_layers
|
|
|
|
true
|
|
|
|
when :mandatory
|
|
|
|
change[:from] && !change[:to]
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def accepted_en_instruction_change?(change)
|
|
|
|
change[:model] == :attestation_template
|
|
|
|
end
|
|
|
|
|
2021-10-13 14:41:05 +02:00
|
|
|
def rebase
|
2022-05-18 15:47:52 +02:00
|
|
|
# revision we are rebasing to
|
|
|
|
target_revision = procedure.published_revision
|
|
|
|
|
2022-05-20 11:56:09 +02:00
|
|
|
# index published types de champ coordinates by stable_id
|
2022-05-20 14:20:26 +02:00
|
|
|
target_coordinates_by_stable_id = target_revision
|
2022-05-18 15:47:52 +02:00
|
|
|
.revision_types_de_champ
|
|
|
|
.includes(:type_de_champ, :parent)
|
|
|
|
.index_by(&:stable_id)
|
|
|
|
|
2022-05-23 11:23:14 +02:00
|
|
|
changes_by_op = pending_changes
|
|
|
|
.filter { |change| change[:model] == :type_de_champ }
|
|
|
|
.group_by { |change| change[:op] }
|
|
|
|
.tap { |h| h.default = [] }
|
2021-10-13 14:41:05 +02:00
|
|
|
|
2022-05-23 11:23:14 +02:00
|
|
|
# add champ
|
|
|
|
changes_by_op[:add]
|
|
|
|
.map { |change| change[:stable_id] }
|
|
|
|
.map { |stable_id| target_coordinates_by_stable_id[stable_id] }
|
|
|
|
.each { |coordinate| add_new_champs_for_revision(coordinate) }
|
2022-05-18 15:47:52 +02:00
|
|
|
|
2022-05-23 11:23:14 +02:00
|
|
|
# remove champ
|
|
|
|
changes_by_op[:remove]
|
|
|
|
.each { |change| delete_champs_for_revision(change[:stable_id]) }
|
2022-05-18 15:47:52 +02:00
|
|
|
|
2022-05-23 11:23:14 +02:00
|
|
|
changes_by_op[:update]
|
|
|
|
.map { |change| [change, Champ.joins(:type_de_champ).where(dossier: self, type_de_champ: { stable_id: change[:stable_id] })] }
|
|
|
|
.each { |change, champs| apply(change, champs) }
|
|
|
|
|
|
|
|
# due to repetition tdc clone on update or erase
|
|
|
|
# we must reassign tdc to the latest version
|
|
|
|
Champ
|
|
|
|
.includes(:type_de_champ)
|
|
|
|
.where(dossier: self)
|
|
|
|
.map { |c| [c, target_coordinates_by_stable_id[c.stable_id].type_de_champ] }
|
|
|
|
.each { |c, target_tdc| c.update_columns(type_de_champ_id: target_tdc.id, rebased_at: Time.zone.now) }
|
2021-10-13 14:41:05 +02:00
|
|
|
|
2022-05-18 15:47:52 +02:00
|
|
|
# update dossier revision
|
|
|
|
self.update_column(:revision_id, target_revision.id)
|
2022-05-23 11:23:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def apply(change, champs)
|
|
|
|
case change[:attribute]
|
|
|
|
when :type_champ
|
2022-05-24 15:14:14 +02:00
|
|
|
champs.each { |champ| champ.piece_justificative_file.purge_later } # FIX ME: change updated_at
|
2022-05-23 11:23:14 +02:00
|
|
|
GeoArea.where(champ: champs).destroy_all
|
|
|
|
|
|
|
|
{
|
|
|
|
type: "Champs::#{change[:to].classify}Champ",
|
|
|
|
value: nil,
|
|
|
|
external_id: nil,
|
|
|
|
data: nil
|
|
|
|
}
|
|
|
|
when :drop_down_options
|
|
|
|
{ value: nil }
|
|
|
|
when :carte_layers
|
|
|
|
# if we are removing cadastres layer, we need to remove cadastre geo areas
|
|
|
|
if change[:from].include?(:cadastres) && !change[:to].include?(:cadastres)
|
|
|
|
champs.each { |champ| champ.cadastres.each(&:destroy) }
|
|
|
|
end
|
2022-05-18 15:47:52 +02:00
|
|
|
|
2022-05-23 11:23:14 +02:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
&.then { |update_params| champs.update_all(update_params) }
|
2021-10-13 14:41:05 +02:00
|
|
|
end
|
|
|
|
|
2022-05-23 11:23:14 +02:00
|
|
|
def add_new_champs_for_revision(target_coordinate)
|
|
|
|
if target_coordinate.child?
|
2022-05-18 15:47:52 +02:00
|
|
|
# If this type de champ is a child, we create a new champ for each row of the parent
|
2022-05-23 11:23:14 +02:00
|
|
|
parent_stable_id = target_coordinate.parent.stable_id
|
2022-05-18 15:47:52 +02:00
|
|
|
champs_repetition = Champ
|
|
|
|
.includes(:champs, :type_de_champ)
|
|
|
|
.where(dossier: self, type_de_champ: { stable_id: parent_stable_id })
|
|
|
|
|
|
|
|
champs_repetition.each do |champ_repetition|
|
2021-10-28 13:05:55 +02:00
|
|
|
champ_repetition.champs.map(&:row).uniq.each do |row|
|
2022-05-23 11:23:14 +02:00
|
|
|
create_champ(target_coordinate, champ_repetition, row: row)
|
2021-10-13 14:41:05 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2022-05-23 11:23:14 +02:00
|
|
|
create_champ(target_coordinate, self)
|
2021-10-13 14:41:05 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-23 11:23:14 +02:00
|
|
|
def create_champ(target_coordinate, parent, row: nil)
|
|
|
|
params = { revision: target_coordinate.revision }
|
2022-05-18 15:47:52 +02:00
|
|
|
params[:row] = row if row
|
2022-05-23 11:23:14 +02:00
|
|
|
champ = target_coordinate
|
2022-05-18 15:47:52 +02:00
|
|
|
.type_de_champ
|
|
|
|
.build_champ(params)
|
|
|
|
parent.champs << champ
|
|
|
|
end
|
2021-10-13 14:41:05 +02:00
|
|
|
|
2022-05-23 11:23:14 +02:00
|
|
|
def delete_champs_for_revision(stable_id)
|
2022-05-18 15:47:52 +02:00
|
|
|
Champ
|
2022-05-23 11:23:14 +02:00
|
|
|
.joins(:type_de_champ)
|
|
|
|
.where(dossier: self, types_de_champ: { stable_id: stable_id })
|
2021-10-13 14:41:05 +02:00
|
|
|
.destroy_all
|
|
|
|
end
|
|
|
|
end
|