From 9149e0fcd70f7abd100e407817fd7a91af70fd95 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 14 Dec 2022 09:17:09 +0100 Subject: [PATCH] feat(revision): allow more dossiers to rebase --- app/models/concerns/dossier_rebase_concern.rb | 48 ++++--- app/models/procedure.rb | 2 +- app/models/procedure_revision.rb | 2 +- spec/models/dossier_rebase_concern_spec.rb | 119 ++++++++++++++++-- spec/models/procedure_revision_spec.rb | 1 + 5 files changed, 141 insertions(+), 31 deletions(-) diff --git a/app/models/concerns/dossier_rebase_concern.rb b/app/models/concerns/dossier_rebase_concern.rb index afe2e4c12..401451528 100644 --- a/app/models/concerns/dossier_rebase_concern.rb +++ b/app/models/concerns/dossier_rebase_concern.rb @@ -9,6 +9,10 @@ module DossierRebaseConcern end end + def rebase_later + DossierRebaseJob.perform_later(self) + end + def can_rebase? revision != procedure.published_revision && (brouillon? || accepted_en_construction_changes? || accepted_en_instruction_changes?) @@ -21,34 +25,32 @@ module DossierRebaseConcern private def accepted_en_construction_changes? - en_construction? && pending_changes.all? { |change| accepted_en_construction_change?(change) } + en_construction? && pending_changes.all? { |change| accepted_change?(change) } end def accepted_en_instruction_changes? - en_instruction? && pending_changes.all? { |change| accepted_en_instruction_change?(change) } + en_instruction? && pending_changes.all? { |change| accepted_change?(change) } end - def accepted_en_construction_change?(change) - if 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 + def accepted_change?(change) + return true if change[:private] + return true if change[:op].in?([:remove, :move]) + return !change[:mandatory] if change[:op] == :add + + case change[:attribute] + when :drop_down_options + (change[:from] - change[:to]).empty? + when :drop_down_other + !change[:from] && change[:to] + when :mandatory + change[:from] && !change[:to] + when :type_champ, :condition false + else + true end end - def accepted_en_instruction_change?(change) - false - end - def rebase # revision we are rebasing to target_revision = procedure.published_revision @@ -93,12 +95,14 @@ module DossierRebaseConcern def apply(change, champs) case change[:attribute] when :type_champ - champs.each { |champ| champ.piece_justificative_file.purge_later } # FIX ME: change updated_at + champs.each { |champ| purge_piece_justificative_file(champ) } GeoArea.where(champ: champs).destroy_all + Etablissement.where(champ: champs).destroy_all { type: "Champs::#{change[:to].classify}Champ", value: nil, + value_json: nil, external_id: nil, data: nil } @@ -152,4 +156,8 @@ module DossierRebaseConcern .where(dossier: self, types_de_champ: { stable_id: stable_id }) .destroy_all end + + def purge_piece_justificative_file(champ) + ActiveStorage::Attachment.where(id: champ.piece_justificative_file.ids).delete_all + end end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index da98e1e4b..708b6bf4d 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -783,7 +783,7 @@ class Procedure < ApplicationRecord end dossiers .state_not_termine - .find_each { |dossier| DossierRebaseJob.perform_later(dossier) } + .find_each(&:rebase_later) end def reset_draft_revision! diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 78a9d2cfc..27f09a6a8 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -252,7 +252,7 @@ class ProcedureRevision < ApplicationRecord end added = (to_sids - from_sids).map do |sid| - { model: :type_de_champ, op: :add, label: to_h[sid].libelle, private: to_h[sid].private?, _position: to_sids.index(sid), stable_id: sid } + { model: :type_de_champ, op: :add, label: to_h[sid].libelle, private: to_h[sid].private?, mandatory: to_h[sid].mandatory?, _position: to_sids.index(sid), stable_id: sid } end kept = from_sids.intersection(to_sids) diff --git a/spec/models/dossier_rebase_concern_spec.rb b/spec/models/dossier_rebase_concern_spec.rb index f2332bda3..29fa88ba9 100644 --- a/spec/models/dossier_rebase_concern_spec.rb +++ b/spec/models/dossier_rebase_concern_spec.rb @@ -1,12 +1,11 @@ describe Dossier do describe '#can_rebase?' do - let(:procedure) { create(:procedure, :with_type_de_champ_mandatory, :with_yes_no, attestation_template: build(:attestation_template)) } + let(:procedure) { create(:procedure, :with_type_de_champ_mandatory, :with_type_de_champ_private, :with_yes_no) } let(:attestation_template) { procedure.draft_revision.attestation_template.find_or_revise! } let(:type_de_champ) { procedure.active_revision.types_de_champ_public.find { |tdc| !tdc.mandatory? } } + let(:private_type_de_champ) { procedure.active_revision.types_de_champ_private.first } let(:mandatory_type_de_champ) { procedure.active_revision.types_de_champ_public.find(&:mandatory?) } - before { Flipper.enable(:procedure_revisions, procedure) } - context 'en_construction' do let(:dossier) { create(:dossier, :en_construction, procedure: procedure) } @@ -26,6 +25,23 @@ describe Dossier do dossier.reload end + it 'should be true' do + expect(dossier.pending_changes).not_to be_empty + expect(dossier.can_rebase?).to be_truthy + end + end + + context 'with added mandatory type de champ' do + before do + procedure.draft_revision.add_type_de_champ({ + type_champ: TypeDeChamp.type_champs.fetch(:text), + libelle: "Un champ text", + mandatory: true + }) + procedure.publish_revision! + dossier.reload + end + it 'should be false' do expect(dossier.pending_changes).not_to be_empty expect(dossier.can_rebase?).to be_falsey @@ -58,6 +74,34 @@ describe Dossier do end end + context 'with type de champ change type' do + context 'type de champ public' do + before do + procedure.draft_revision.find_and_ensure_exclusive_use(type_de_champ.stable_id).update(type_champ: :checkbox) + procedure.publish_revision! + dossier.reload + end + + it 'should be false' do + expect(dossier.pending_changes).not_to be_empty + expect(dossier.can_rebase?).to be_falsey + end + end + + context 'type de champ private' do + before do + procedure.draft_revision.find_and_ensure_exclusive_use(private_type_de_champ.stable_id).update(type_champ: :checkbox) + procedure.publish_revision! + dossier.reload + end + + it 'should be true' do + expect(dossier.pending_changes).not_to be_empty + expect(dossier.can_rebase?).to be_truthy + end + end + end + context 'with removed type de champ' do before do procedure.draft_revision.remove_type_de_champ(type_de_champ.stable_id) @@ -91,15 +135,19 @@ describe Dossier do dossier.reload end - it 'should be false' do + it 'should be true' do expect(dossier.pending_changes).not_to be_empty - expect(dossier.can_rebase?).to be_falsey + expect(dossier.can_rebase?).to be_truthy end end - context 'with removed type de champ' do + context 'with added mandatory type de champ' do before do - procedure.draft_revision.remove_type_de_champ(type_de_champ.stable_id) + procedure.draft_revision.add_type_de_champ({ + type_champ: TypeDeChamp.type_champs.fetch(:text), + libelle: "Un champ text", + mandatory: true + }) procedure.publish_revision! dossier.reload end @@ -117,11 +165,65 @@ describe Dossier do dossier.reload end + it 'should be true' do + expect(dossier.pending_changes).not_to be_empty + expect(dossier.can_rebase?).to be_truthy + end + end + + context 'with type de champ made mandatory' do + before do + procedure.draft_revision.find_and_ensure_exclusive_use(type_de_champ.stable_id).update(mandatory: true) + procedure.publish_revision! + dossier.reload + end + it 'should be false' do expect(dossier.pending_changes).not_to be_empty expect(dossier.can_rebase?).to be_falsey end end + + context 'with type de champ change type' do + context 'type de champ public' do + before do + procedure.draft_revision.find_and_ensure_exclusive_use(type_de_champ.stable_id).update(type_champ: :checkbox) + procedure.publish_revision! + dossier.reload + end + + it 'should be false' do + expect(dossier.pending_changes).not_to be_empty + expect(dossier.can_rebase?).to be_falsey + end + end + + context 'type de champ private' do + before do + procedure.draft_revision.find_and_ensure_exclusive_use(private_type_de_champ.stable_id).update(type_champ: :checkbox) + procedure.publish_revision! + dossier.reload + end + + it 'should be true' do + expect(dossier.pending_changes).not_to be_empty + expect(dossier.can_rebase?).to be_truthy + end + end + end + + context 'with removed type de champ' do + before do + procedure.draft_revision.remove_type_de_champ(type_de_champ.stable_id) + procedure.publish_revision! + dossier.reload + end + + it 'should be true' do + expect(dossier.pending_changes).not_to be_empty + expect(dossier.can_rebase?).to be_truthy + end + end end end @@ -330,8 +432,7 @@ describe Dossier do it { expect { subject }.to change { first_champ.data }.from({ 'a' => 1 }).to(nil) } it { expect { subject }.to change { first_champ.geo_areas.count }.from(1).to(0) } it { expect { subject }.to change { first_champ.piece_justificative_file.attached? }.from(true).to(false) } - # pb with pj.purge_later - xit { expect { subject }.not_to change { first_champ.updated_at }.from(Time.zone.parse('01/01/1901')) } + it { expect { subject }.not_to change { first_champ.updated_at }.from(Time.zone.parse('01/01/1901')) } end end diff --git a/spec/models/procedure_revision_spec.rb b/spec/models/procedure_revision_spec.rb index d311c2b45..9d9e2e4aa 100644 --- a/spec/models/procedure_revision_spec.rb +++ b/spec/models/procedure_revision_spec.rb @@ -433,6 +433,7 @@ describe ProcedureRevision do op: :add, label: "Un champ text", private: false, + mandatory: false, stable_id: new_tdc.stable_id } ])