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
end
if @attestation_template.published?
if @attestation_template.published? && should_edit_draft?
@attestation_template = @attestation_template.dup
@attestation_template.state = :draft
@attestation_template.procedure = @procedure
@ -125,9 +125,16 @@ module Administrateurs
def retrieve_attestation_template
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
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
params.required(:attestation_template).permit(:activated, :official_layout, :label_logo, :label_direction, :tiptap_body, :footer, :logo, :signature, :activated, :state)
end

View file

@ -3,13 +3,19 @@
%p.flex.justify-between.align-center.fr-text-default--warning
%span
= dsfr_icon("fr-icon-warning-fill fr-mr-1v")
Les modifications effectuées ne seront appliquées quà la prochaine publication.
%span
- if @procedure.attestation_templates.many?
Les modifications effectuées ne seront appliquées quà la prochaine publication.
- else
Lattestation ne sera délivrée quaprès sa publication.
= 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
%span.no-wrap
- if @procedure.attestation_templates.many?
= 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",
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
let(:admin) { create(:administrateur) }
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(:signature) { fixture_file_upload('spec/fixtures/files/black.png', 'image/png') }
@ -96,17 +96,21 @@ describe Administrateurs::AttestationTemplateV2sController, type: :controller do
end
describe 'GET edit' do
render_views
let(:attestation_template) { nil }
subject do
get :edit, params: { procedure_id: procedure.id }
response.body
end
context 'if an attestation template does not exists yet on the procedure' do
let(:attestation_template) { nil }
it 'creates new v2 attestation template' do
subject
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
@ -116,13 +120,51 @@ describe Administrateurs::AttestationTemplateV2sController, type: :controller do
it 'build new v2 attestation template' do
subject
expect(assigns(:attestation_template).version).to eq(2)
expect(assigns(:attestation_template)).to be_draft
end
end
context 'if attestation template already exist on v2' do
it 'assigns v2 attestation template' do
context 'attestation template published exist without draft' do
let(:attestation_template) { build(:attestation_template, :v2, :published) }
it 'mention publication' do
subject
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

View file

@ -71,6 +71,13 @@ describe 'As an administrateur, I want to manage the procedures attestation',
end
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
Flipper.enable(:attestation_v2)
@ -100,7 +107,7 @@ describe 'As an administrateur, I want to manage the procedures attestation',
attestation.present?
}
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é")
click_on "date de décision"
@ -139,6 +146,12 @@ describe 'As an administrateur, I want to manage the procedures attestation',
click_on "Publier"
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
context "tag in error" do