feat(attestation): already published without publication when procedure is draft

This commit is contained in:
Colin Darie 2024-06-06 14:48:50 +02:00
parent 5bda9d0c63
commit ce998f7864
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
4 changed files with 82 additions and 14 deletions

View file

@ -66,7 +66,7 @@ module Administrateurs
render :update && return render :update && return
end end
if @attestation_template.published? if @attestation_template.published? && should_edit_draft?
@attestation_template = @attestation_template.dup @attestation_template = @attestation_template.dup
@attestation_template.state = :draft @attestation_template.state = :draft
@attestation_template.procedure = @procedure @attestation_template.procedure = @procedure
@ -125,9 +125,16 @@ module Administrateurs
def retrieve_attestation_template def retrieve_attestation_template
v2s = @procedure.attestation_templates_v2 v2s = @procedure.attestation_templates_v2
@attestation_template = v2s.find(&:draft?) || v2s.find(&:published?) || @procedure.build_attestation_template(version: 2, json_body: AttestationTemplate::TIPTAP_BODY_DEFAULT, state: :draft) @attestation_template = v2s.find(&:draft?) || v2s.find(&:published?) || build_default_attestation
end end
def build_default_attestation
state = should_edit_draft? ? :draft : :published
@procedure.build_attestation_template(version: 2, json_body: AttestationTemplate::TIPTAP_BODY_DEFAULT, activated: true, state:)
end
def should_edit_draft? = !@procedure.brouillon?
def editor_params def editor_params
params.required(:attestation_template).permit(:activated, :official_layout, :label_logo, :label_direction, :tiptap_body, :footer, :logo, :signature, :activated, :state) params.required(:attestation_template).permit(:activated, :official_layout, :label_logo, :label_direction, :tiptap_body, :footer, :logo, :signature, :activated, :state)
end end

View file

@ -3,13 +3,19 @@
%p.flex.justify-between.align-center.fr-text-default--warning %p.flex.justify-between.align-center.fr-text-default--warning
%span %span
= dsfr_icon("fr-icon-warning-fill fr-mr-1v") = dsfr_icon("fr-icon-warning-fill fr-mr-1v")
Les modifications effectuées ne seront appliquées quà la prochaine publication. - if @procedure.attestation_templates.many?
%span Les modifications effectuées ne seront appliquées quà la prochaine publication.
- else
Lattestation ne sera délivrée quaprès sa publication.
%span.no-wrap
= link_to reset_admin_procedure_attestation_template_v2_path(@procedure), class: "fr-btn fr-btn--secondary fr-ml-2w", method: :post do - if @procedure.attestation_templates.many?
Réinitialiser les modifications = link_to reset_admin_procedure_attestation_template_v2_path(@procedure), class: "fr-btn fr-btn--secondary fr-ml-2w", method: :post do
Réinitialiser les modifications
%button.fr-btn.fr-ml-2w{ form: "attestation-template", name: field_name(:attestation_template, :state), value: "published", %button.fr-btn.fr-ml-2w{ form: "attestation-template", name: field_name(:attestation_template, :state), value: "published",
data: { 'disable-with': "Publication en cours…", controller: 'autosave-submit' } } data: { 'disable-with': "Publication en cours…", controller: 'autosave-submit' } }
Publier les modifications - if @procedure.attestation_templates.many?
Publier les modifications
- else
Publier

View file

@ -1,7 +1,7 @@
describe Administrateurs::AttestationTemplateV2sController, type: :controller do describe Administrateurs::AttestationTemplateV2sController, type: :controller do
let(:admin) { create(:administrateur) } let(:admin) { create(:administrateur) }
let(:attestation_template) { build(:attestation_template, :v2) } let(:attestation_template) { build(:attestation_template, :v2) }
let(:procedure) { create(:procedure, administrateur: admin, attestation_template:, libelle: "Ma démarche") } let(:procedure) { create(:procedure, :published, administrateur: admin, attestation_template:, libelle: "Ma démarche") }
let(:logo) { fixture_file_upload('spec/fixtures/files/white.png', 'image/png') } let(:logo) { fixture_file_upload('spec/fixtures/files/white.png', 'image/png') }
let(:signature) { fixture_file_upload('spec/fixtures/files/black.png', 'image/png') } let(:signature) { fixture_file_upload('spec/fixtures/files/black.png', 'image/png') }
@ -96,17 +96,21 @@ describe Administrateurs::AttestationTemplateV2sController, type: :controller do
end end
describe 'GET edit' do describe 'GET edit' do
render_views
let(:attestation_template) { nil }
subject do subject do
get :edit, params: { procedure_id: procedure.id } get :edit, params: { procedure_id: procedure.id }
response.body response.body
end end
context 'if an attestation template does not exists yet on the procedure' do context 'if an attestation template does not exists yet on the procedure' do
let(:attestation_template) { nil }
it 'creates new v2 attestation template' do it 'creates new v2 attestation template' do
subject subject
expect(assigns(:attestation_template).version).to eq(2) expect(assigns(:attestation_template).version).to eq(2)
expect(assigns(:attestation_template)).to be_draft
expect(response.body).to have_button("Publier")
expect(response.body).not_to have_link("Réinitialiser les modifications")
end end
end end
@ -116,13 +120,51 @@ describe Administrateurs::AttestationTemplateV2sController, type: :controller do
it 'build new v2 attestation template' do it 'build new v2 attestation template' do
subject subject
expect(assigns(:attestation_template).version).to eq(2) expect(assigns(:attestation_template).version).to eq(2)
expect(assigns(:attestation_template)).to be_draft
end end
end end
context 'if attestation template already exist on v2' do context 'attestation template published exist without draft' do
it 'assigns v2 attestation template' do let(:attestation_template) { build(:attestation_template, :v2, :published) }
it 'mention publication' do
subject subject
expect(assigns(:attestation_template)).to eq(attestation_template) expect(assigns(:attestation_template)).to eq(attestation_template)
expect(response.body).not_to have_link("Réinitialiser les modifications")
expect(response.body).not_to have_button("Publier les modifications")
end
end
context 'attestation template draft already exist on v2' do
let(:attestation_template) { build(:attestation_template, :v2, :draft) }
it 'assigns this draft' do
subject
expect(assigns(:attestation_template)).to eq(attestation_template)
expect(response.body).not_to have_link("Réinitialiser les modifications")
expect(response.body).to have_button("Publier")
end
context 'and a published template also exists' do
before { create(:attestation_template, :v2, :published, procedure:) }
it 'mention publication' do
subject
expect(assigns(:attestation_template)).to eq(attestation_template)
expect(response.body).to have_link("Réinitialiser les modifications")
expect(response.body).to have_button("Publier les modifications")
end
end
end
context 'when procedure is draft' do
let(:procedure) { create(:procedure, :draft, administrateur: admin, attestation_template:, libelle: "Ma démarche") }
it 'built template is already live (published)' do
subject
expect(assigns(:attestation_template).version).to eq(2)
expect(assigns(:attestation_template)).to be_published
expect(response.body).not_to have_button(/Publier/)
end end
end end
end end

View file

@ -71,6 +71,13 @@ describe 'As an administrateur, I want to manage the procedures attestation',
end end
context 'Update attestation v2' do context 'Update attestation v2' do
let(:procedure) do
create(:procedure, :published,
administrateurs: [administrateur],
libelle: 'libellé de la procédure',
path: 'libelle-de-la-procedure')
end
before do before do
Flipper.enable(:attestation_v2) Flipper.enable(:attestation_v2)
@ -100,7 +107,7 @@ describe 'As an administrateur, I want to manage the procedures attestation',
attestation.present? attestation.present?
} }
expect(attestation.label_logo).to eq("System Test") expect(attestation.label_logo).to eq("System Test")
expect(attestation.activated?).to be_falsey expect(attestation.activated?).to be_truthy
expect(page).to have_content("Formulaire enregistré") expect(page).to have_content("Formulaire enregistré")
click_on "date de décision" click_on "date de décision"
@ -139,6 +146,12 @@ describe 'As an administrateur, I want to manage the procedures attestation',
click_on "Publier" click_on "Publier"
expect(attestation.reload).to be_published expect(attestation.reload).to be_published
expect(page).to have_text("Lattestation a été publiée")
fill_in "Intitulé de la direction", with: "plop"
click_on "Publier les modifications"
expect(procedure.reload.attestation_template.label_direction).to eq("plop")
expect(page).to have_text(/La nouvelle version de lattestation/)
end end
context "tag in error" do context "tag in error" do