diff --git a/app/models/champs/siret_champ.rb b/app/models/champs/siret_champ.rb index d968c85b7..5f9656385 100644 --- a/app/models/champs/siret_champ.rb +++ b/app/models/champs/siret_champ.rb @@ -2,4 +2,8 @@ class Champs::SiretChamp < Champ def search_terms etablissement.present? ? etablissement.search_terms : [value] end + + def mandatory_and_blank? + mandatory? && Siret.new(siret: value).invalid? + end end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 62b1c7a34..752179f10 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -193,16 +193,6 @@ class Procedure < ApplicationRecord end end - def clone_attachments(original, kopy) - if original.is_a?(TypeDeChamp) && original.piece_justificative_template.attached? - kopy.piece_justificative_template.attach({ - io: StringIO.new(original.piece_justificative_template.download), - filename: original.piece_justificative_template.blob.filename, - content_type: original.piece_justificative_template.blob.content_type - }) - end - end - def clone(admin, from_library) is_different_admin = !admin.owns?(self) @@ -222,8 +212,6 @@ class Procedure < ApplicationRecord procedure.remote_logo_url = self.logo_url procedure.lien_notice = nil - [:notice, :deliberation].each { |attachment| clone_attachment(procedure, attachment) } - procedure.types_de_champ += PiecesJustificativesService.types_pj_as_types_de_champ(self) if is_different_admin || from_library procedure.types_de_champ.each { |tdc| tdc.options&.delete(:old_pj) } @@ -255,6 +243,26 @@ class Procedure < ApplicationRecord procedure end + def clone_attachments(original, kopy) + if original.is_a?(TypeDeChamp) + clone_attachment(:piece_justificative_template, original, kopy) + elsif original.is_a?(Procedure) + clone_attachment(:notice, original, kopy) + clone_attachment(:deliberation, original, kopy) + end + end + + def clone_attachment(attribute, original, kopy) + original_attachment = original.send(attribute) + if original_attachment.attached? + kopy.send(attribute).attach({ + io: StringIO.new(original_attachment.download), + filename: original_attachment.blob.filename, + content_type: original_attachment.blob.content_type + }) + end + end + def whitelisted? whitelisted_at.present? end @@ -455,16 +463,6 @@ class Procedure < ApplicationRecord true end - def clone_attachment(cloned_procedure, attachment_symbol) - attachment = send(attachment_symbol) - if attachment.attached? - cloned_procedure.send(attachment_symbol).attach( - io: StringIO.new(attachment.download), - filename: attachment.filename - ) - end - end - def check_juridique if juridique_required? && (cadre_juridique.blank? && !deliberation.attached?) errors.add(:cadre_juridique, " : veuillez remplir le texte de loi ou la délibération") diff --git a/app/services/piece_justificative_to_champ_piece_jointe_migration_service.rb b/app/services/piece_justificative_to_champ_piece_jointe_migration_service.rb index 5d2ace62e..0778454f3 100644 --- a/app/services/piece_justificative_to_champ_piece_jointe_migration_service.rb +++ b/app/services/piece_justificative_to_champ_piece_jointe_migration_service.rb @@ -37,12 +37,12 @@ class PieceJustificativeToChampPieceJointeMigrationService pj = dossier.retrieve_last_piece_justificative_by_type(type_pj_id) if pj.present? + convert_pj_to_champ!(pj, champ) + champ.update( updated_at: pj.updated_at, created_at: pj.created_at ) - - convert_pj_to_champ!(pj, champ) else champ.update( updated_at: dossier.updated_at, diff --git a/app/views/shared/dossiers/editable_champs/_siret.html.haml b/app/views/shared/dossiers/editable_champs/_siret.html.haml index ca5bbac72..5dbb2520e 100644 --- a/app/views/shared/dossiers/editable_champs/_siret.html.haml +++ b/app/views/shared/dossiers/editable_champs/_siret.html.haml @@ -2,7 +2,9 @@ placeholder: champ.libelle, class: 'small-margin', data: { remote: true, debounce: true, url: champs_siret_path(form.index), params: { champ_id: champ&.id }.to_query, spinner: true }, - required: champ.mandatory? + required: champ.mandatory?, + pattern: "[0-9]{14}", + title: "Le numéro de SIRET doit comporter exactement 14 chiffres" .spinner.right-spinner.hidden %div{ class: "siret-info-#{form.index}" } - if champ.etablissement.present? diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index dc9382add..0ab8763f5 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -858,6 +858,33 @@ describe Dossier do end end + context "with mandatory SIRET champ" do + let(:type_de_champ) { create(:type_de_champ_siret, mandatory: true) } + let(:champ_siret) { create(:champ_siret, type_de_champ: type_de_champ) } + + before do + dossier.champs << champ_siret + end + + it 'should not have errors' do + errors = dossier.check_mandatory_champs + expect(errors).to be_empty + end + + context "and invalid SIRET" do + before do + champ_siret.update(value: "1234") + dossier.reload + end + + it 'should have errors' do + errors = dossier.check_mandatory_champs + expect(errors).not_to be_empty + expect(errors.first).to eq("Le champ #{champ_siret.libelle} doit être rempli.") + end + end + end + context "with champ repetition" do let(:procedure) { create(:procedure) } let(:type_de_champ_repetition) { create(:type_de_champ_repetition, mandatory: true) } diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 3148b3d76..1d70c8c29 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -489,6 +489,22 @@ describe Procedure do it 'should duplicate piece_justificative_template on a type_de_champ' do expect(subject.types_de_champ.where(type_champ: "piece_justificative").first.piece_justificative_template.attached?).to be true end + + context 'with a notice attached' do + let(:procedure) { create(:procedure, :with_notice, received_mail: received_mail, service: service) } + + it 'should duplicate notice' do + expect(subject.notice.attached?).to be true + end + end + + context 'with a deliberation attached' do + let(:procedure) { create(:procedure, :with_deliberation, received_mail: received_mail, service: service) } + + it 'should duplicate deliberation' do + expect(subject.deliberation.attached?).to be true + end + end end describe '#publish!' do