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
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,32 +25,30 @@ 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
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 :carte_layers
true
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
false
true
end
else
false
end
end
def accepted_en_instruction_change?(change)
false
end
def rebase
@ -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

View file

@ -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!

View file

@ -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)

View file

@ -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

View file

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