feat(revision): allow more dossiers to rebase

This commit is contained in:
Paul Chavard 2022-12-14 09:17:09 +01:00
parent 5dfb96383a
commit 9149e0fcd7
5 changed files with 141 additions and 31 deletions

View file

@ -9,6 +9,10 @@ module DossierRebaseConcern
end end
end end
def rebase_later
DossierRebaseJob.perform_later(self)
end
def can_rebase? def can_rebase?
revision != procedure.published_revision && revision != procedure.published_revision &&
(brouillon? || accepted_en_construction_changes? || accepted_en_instruction_changes?) (brouillon? || accepted_en_construction_changes? || accepted_en_instruction_changes?)
@ -21,34 +25,32 @@ module DossierRebaseConcern
private private
def accepted_en_construction_changes? 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 end
def accepted_en_instruction_changes? 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 end
def accepted_en_construction_change?(change) def accepted_change?(change)
if change[:op] == :move || change[:op] == :remove return true if change[:private]
true return true if change[:op].in?([:remove, :move])
elsif change[:op] == :update return !change[:mandatory] if change[:op] == :add
case change[:attribute]
when :carte_layers case change[:attribute]
true when :drop_down_options
when :mandatory (change[:from] - change[:to]).empty?
change[:from] && !change[:to] when :drop_down_other
else !change[:from] && change[:to]
false when :mandatory
end change[:from] && !change[:to]
else when :type_champ, :condition
false false
else
true
end end
end end
def accepted_en_instruction_change?(change)
false
end
def rebase def rebase
# revision we are rebasing to # revision we are rebasing to
target_revision = procedure.published_revision target_revision = procedure.published_revision
@ -93,12 +95,14 @@ module DossierRebaseConcern
def apply(change, champs) def apply(change, champs)
case change[:attribute] case change[:attribute]
when :type_champ 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 GeoArea.where(champ: champs).destroy_all
Etablissement.where(champ: champs).destroy_all
{ {
type: "Champs::#{change[:to].classify}Champ", type: "Champs::#{change[:to].classify}Champ",
value: nil, value: nil,
value_json: nil,
external_id: nil, external_id: nil,
data: nil data: nil
} }
@ -152,4 +156,8 @@ module DossierRebaseConcern
.where(dossier: self, types_de_champ: { stable_id: stable_id }) .where(dossier: self, types_de_champ: { stable_id: stable_id })
.destroy_all .destroy_all
end end
def purge_piece_justificative_file(champ)
ActiveStorage::Attachment.where(id: champ.piece_justificative_file.ids).delete_all
end
end end

View file

@ -783,7 +783,7 @@ class Procedure < ApplicationRecord
end end
dossiers dossiers
.state_not_termine .state_not_termine
.find_each { |dossier| DossierRebaseJob.perform_later(dossier) } .find_each(&:rebase_later)
end end
def reset_draft_revision! def reset_draft_revision!

View file

@ -252,7 +252,7 @@ class ProcedureRevision < ApplicationRecord
end end
added = (to_sids - from_sids).map do |sid| 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 end
kept = from_sids.intersection(to_sids) kept = from_sids.intersection(to_sids)

View file

@ -1,12 +1,11 @@
describe Dossier do describe Dossier do
describe '#can_rebase?' 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(: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(: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?) } let(:mandatory_type_de_champ) { procedure.active_revision.types_de_champ_public.find(&:mandatory?) }
before { Flipper.enable(:procedure_revisions, procedure) }
context 'en_construction' do context 'en_construction' do
let(:dossier) { create(:dossier, :en_construction, procedure: procedure) } let(:dossier) { create(:dossier, :en_construction, procedure: procedure) }
@ -26,6 +25,23 @@ describe Dossier do
dossier.reload dossier.reload
end 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 it 'should be false' do
expect(dossier.pending_changes).not_to be_empty expect(dossier.pending_changes).not_to be_empty
expect(dossier.can_rebase?).to be_falsey expect(dossier.can_rebase?).to be_falsey
@ -58,6 +74,34 @@ describe Dossier do
end end
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 context 'with removed type de champ' do
before do before do
procedure.draft_revision.remove_type_de_champ(type_de_champ.stable_id) procedure.draft_revision.remove_type_de_champ(type_de_champ.stable_id)
@ -91,15 +135,19 @@ describe Dossier do
dossier.reload dossier.reload
end end
it 'should be false' do it 'should be true' do
expect(dossier.pending_changes).not_to be_empty expect(dossier.pending_changes).not_to be_empty
expect(dossier.can_rebase?).to be_falsey expect(dossier.can_rebase?).to be_truthy
end end
end end
context 'with removed type de champ' do context 'with added mandatory type de champ' do
before 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! procedure.publish_revision!
dossier.reload dossier.reload
end end
@ -117,11 +165,65 @@ describe Dossier do
dossier.reload dossier.reload
end 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 it 'should be false' do
expect(dossier.pending_changes).not_to be_empty expect(dossier.pending_changes).not_to be_empty
expect(dossier.can_rebase?).to be_falsey expect(dossier.can_rebase?).to be_falsey
end end
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
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.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.geo_areas.count }.from(1).to(0) }
it { expect { subject }.to change { first_champ.piece_justificative_file.attached? }.from(true).to(false) } it { expect { subject }.to change { first_champ.piece_justificative_file.attached? }.from(true).to(false) }
# pb with pj.purge_later it { expect { subject }.not_to change { first_champ.updated_at }.from(Time.zone.parse('01/01/1901')) }
xit { expect { subject }.not_to change { first_champ.updated_at }.from(Time.zone.parse('01/01/1901')) }
end end
end end

View file

@ -433,6 +433,7 @@ describe ProcedureRevision do
op: :add, op: :add,
label: "Un champ text", label: "Un champ text",
private: false, private: false,
mandatory: false,
stable_id: new_tdc.stable_id stable_id: new_tdc.stable_id
} }
]) ])