Merge pull request #59 from sgmap/mandatory_attachment

Mandatory attachment
This commit is contained in:
gregoirenovel 2017-04-19 17:05:23 +02:00 committed by GitHub
commit 0f914ce77d
7 changed files with 112 additions and 111 deletions

View file

@ -5,7 +5,8 @@ class Backoffice::PrivateFormulairesController < ApplicationController
dossier = current_gestionnaire.dossiers.find(params[:dossier_id]) dossier = current_gestionnaire.dossiers.find(params[:dossier_id])
unless params[:champs].nil? unless params[:champs].nil?
champs_service_errors = ChampsService.save_champs dossier.champs_private, params ChampsService.save_champs dossier.champs_private, params
champs_service_errors = ChampsService.build_error_messages(dossier.champs_private)
if champs_service_errors.empty? if champs_service_errors.empty?
flash[:notice] = "Formulaire enregistré" flash[:notice] = "Formulaire enregistré"

View file

@ -28,13 +28,8 @@ class Users::DescriptionController < UsersController
procedure = dossier.procedure procedure = dossier.procedure
return head :forbidden unless dossier.can_be_initiated? return head :forbidden unless dossier.can_be_initiated?
check_mandatory_fields = !draft_submission?
if params[:champs] ChampsService.save_champs(dossier.champs, params) if params[:champs]
champs_service_errors =
ChampsService.save_champs(dossier.champs, params, check_mandatory_fields)
return redirect_to_description_with_errors(dossier, champs_service_errors) if champs_service_errors.any?
end
if procedure.cerfa_flag? && params[:cerfa_pdf] if procedure.cerfa_flag? && params[:cerfa_pdf]
cerfa = Cerfa.new(content: params[:cerfa_pdf], dossier: dossier, user: current_user) cerfa = Cerfa.new(content: params[:cerfa_pdf], dossier: dossier, user: current_user)
@ -44,6 +39,13 @@ class Users::DescriptionController < UsersController
errors_upload = PiecesJustificativesService.upload!(dossier, current_user, params) errors_upload = PiecesJustificativesService.upload!(dossier, current_user, params)
return redirect_to_description_with_errors(dossier, errors_upload) if errors_upload.any? return redirect_to_description_with_errors(dossier, errors_upload) if errors_upload.any?
if params[:champs] && !draft_submission?
errors =
ChampsService.build_error_messages(dossier.champs) +
PiecesJustificativesService.missing_pj_error_messages(dossier)
return redirect_to_description_with_errors(dossier, errors) if errors.any?
end
if draft_submission? if draft_submission?
flash.notice = 'Votre brouillon a bien été sauvegardé.' flash.notice = 'Votre brouillon a bien été sauvegardé.'
redirect_to url_for(controller: :dossiers, action: :index, liste: :brouillon) redirect_to url_for(controller: :dossiers, action: :index, liste: :brouillon)

View file

@ -1,11 +1,14 @@
class ChampsService class ChampsService
class << self class << self
def save_champs(champs, params, check_mandatory = true) def save_champs(champs, params)
fill_champs(champs, params) fill_champs(champs, params)
champs.select(&:changed?).each(&:save) champs.select(&:changed?).each(&:save)
end
check_mandatory ? build_error_messages(champs) : [] def build_error_messages(champs)
champs.select(&:mandatory_and_blank?)
.map { |c| "Le champ #{c.libelle} doit être rempli." }
end end
private private
@ -28,10 +31,5 @@ class ChampsService
def extract_minute(champ_id, h) def extract_minute(champ_id, h)
h[:time_minute]["'#{champ_id}'"] h[:time_minute]["'#{champ_id}'"]
end end
def build_error_messages(champs)
champs.select(&:mandatory_and_blank?)
.map { |c| "Le champ #{c.libelle} doit être rempli." }
end
end end
end end

View file

@ -13,8 +13,6 @@ class PiecesJustificativesService
errors += without_virus errors += without_virus
.map { |tpj, content| save_pj(content, dossier, tpj, user) } .map { |tpj, content| save_pj(content, dossier, tpj, user) }
.compact() .compact()
errors += missing_pj_error_messages(dossier)
end end
def self.upload_one! dossier, user, params def self.upload_one! dossier, user, params

View file

@ -18,24 +18,24 @@
- else - else
%input{ type: 'file', name: 'cerfa_pdf', id: 'cerfa_pdf', accept: PieceJustificative.accept_format, :max_file_size => 6.megabytes } %input{ type: 'file', name: 'cerfa_pdf', id: 'cerfa_pdf', accept: PieceJustificative.accept_format, :max_file_size => 6.megabytes }
- dossier.types_de_piece_justificative.order('order_place ASC').each do |type_de_piece_justificative| - dossier.types_de_piece_justificative.order('order_place ASC').each do |tpj|
%tr %tr
%th.piece-libelle %th.piece-libelle
= type_de_piece_justificative.libelle = tpj.mandatory ? tpj.libelle + ' *' : tpj.libelle
%td %td
- unless type_de_piece_justificative.lien_demarche.blank? - unless tpj.lien_demarche.blank?
%em %em
Récupérer le formulaire vierge pour mon dossier : Récupérer le formulaire vierge pour mon dossier :
= link_to "Télécharger", type_de_piece_justificative.lien_demarche, target: :blank = link_to "Télécharger", tpj.lien_demarche, target: :blank
%td %td
- if type_de_piece_justificative.api_entreprise - if tpj.api_entreprise
%span.text-success{ id: "piece_justificative_#{type_de_piece_justificative.id}" } Nous l'avons récupéré pour vous. %span.text-success{ id: "piece_justificative_#{tpj.id}" } Nous l'avons récupéré pour vous.
- else - else
- if dossier.retrieve_last_piece_justificative_by_type(type_de_piece_justificative.id).nil? - if dossier.retrieve_last_piece_justificative_by_type(tpj.id).nil?
= file_field_tag "piece_justificative_#{type_de_piece_justificative.id}", accept: PieceJustificative.accept_format, :max_file_size => 6.megabytes = file_field_tag "piece_justificative_#{tpj.id}", accept: PieceJustificative.accept_format, :max_file_size => 6.megabytes
- else - else
%span.btn.btn-sm.btn-file.btn-success %span.btn.btn-sm.btn-file.btn-success
Modifier Modifier
= file_field_tag "piece_justificative_#{type_de_piece_justificative.id}", accept: PieceJustificative.accept_format, :max_file_size => 6.megabytes = file_field_tag "piece_justificative_#{tpj.id}", accept: PieceJustificative.accept_format, :max_file_size => 6.megabytes

View file

@ -1,15 +1,15 @@
require 'spec_helper' require 'spec_helper'
describe ChampsService do describe ChampsService do
describe 'save_champs' do let!(:champ) { Champ.create(value: 'toto', type_de_champ: TypeDeChamp.new) }
let!(:champ) { Champ.create(value: 'toto', type_de_champ: TypeDeChamp.new) } let!(:champ_mandatory_empty) { Champ.create(type_de_champ: TypeDeChamp.new(libelle: 'mandatory', mandatory: true)) }
let!(:champ_mandatory_empty) { Champ.create(type_de_champ: TypeDeChamp.new(libelle: 'mandatory', mandatory: true)) } let!(:champ_datetime) do
let!(:champ_datetime) do champ_datetime = TypeDeChamp.new(type_champ: 'datetime')
champ_datetime = TypeDeChamp.new(type_champ: 'datetime') Champ.create(type_de_champ: champ_datetime)
Champ.create(type_de_champ: champ_datetime) end
end let!(:champs) { [champ, champ_mandatory_empty, champ_datetime] }
let!(:champs) { [champ, champ_mandatory_empty, champ_datetime] }
describe 'save_champs' do
before :each do before :each do
params_hash = { params_hash = {
champs: { champs: {
@ -19,31 +19,22 @@ describe ChampsService do
time_hour: { "'#{champ_datetime.id}'" => '12' }, time_hour: { "'#{champ_datetime.id}'" => '12' },
time_minute: { "'#{champ_datetime.id}'" => '24' } time_minute: { "'#{champ_datetime.id}'" => '24' }
} }
@errors = ChampsService.save_champs(champs, params_hash, check_mandatory) ChampsService.save_champs(champs, params_hash)
champs.each(&:reload) champs.each(&:reload)
end end
context 'check_mandatory is true' do it 'saves the changed champ' do
let(:check_mandatory) { true } expect(champ.value).to eq('yop')
it 'saves the changed champ' do
expect(champ.value).to eq('yop')
end
it 'parses and save the date' do
expect(champ_datetime.value).to eq('d 12:24')
end
it 'adds error for the missing mandatory champ' do
expect(@errors).to match(['Le champ mandatory doit être rempli.'])
end
end end
context 'check_mandatory is false' do it 'parses and save the date' do
let(:check_mandatory) { false } expect(champ_datetime.value).to eq('d 12:24')
end
end
it 'does not add errors' do describe 'build_error_message' do
expect(@errors).to match([]) it 'adds error for the missing mandatory champ' do
end expect(ChampsService.build_error_messages(champs)).to match(['Le champ mandatory doit être rempli.'])
end end
end end
end end

View file

@ -8,74 +8,85 @@ describe PiecesJustificativesService do
allow(ClamavService).to receive(:safe_file?).and_return(safe_file) allow(ClamavService).to receive(:safe_file?).and_return(safe_file)
end end
let(:hash) { {} }
let!(:tpj_not_mandatory) do
TypeDePieceJustificative.create(libelle: 'not mandatory', mandatory: false)
end
let!(:tpj_mandatory) do
TypeDePieceJustificative.create(libelle: 'justificatif', mandatory: true)
end
let(:procedure) { Procedure.create(types_de_piece_justificative: tpjs) }
let(:dossier) { Dossier.create(procedure: procedure) }
let(:errors) { PiecesJustificativesService.upload!(dossier, user, hash) }
let(:tpjs) { [tpj_not_mandatory] }
describe 'self.upload!' do describe 'self.upload!' do
let(:hash) { {} } context 'when no params are given' do
let!(:tpj_not_mandatory) do it { expect(errors).to eq([]) }
TypeDePieceJustificative.create(libelle: 'not mandatory', mandatory: false)
end
let!(:tpj_mandatory) do
TypeDePieceJustificative.create(libelle: 'justificatif', mandatory: true)
end
let(:procedure) { Procedure.create(types_de_piece_justificative: tpjs) }
let(:dossier) { Dossier.create(procedure: procedure) }
let(:errors) { PiecesJustificativesService.upload!(dossier, user, hash) }
context 'when no piece justificative is required' do
let(:tpjs) { [tpj_not_mandatory] }
context 'when no params are given' do
it { expect(errors).to eq([]) }
end
context 'when sometihing wrong with file save' do
let(:hash) do
{
"piece_justificative_#{tpj_not_mandatory.id}" =>
double(path: '', original_filename: 'filename')
}
end
it { expect(errors).to match(["le fichier filename (not mandatory) n'a pas pu être sauvegardé"]) }
end
context 'when a virus is provided' do
let(:safe_file) { false }
let(:hash) do
{
"piece_justificative_#{tpj_not_mandatory.id}" =>
double(path: '', original_filename: 'bad_file')
}
end
it { expect(errors).to match(['bad_file : virus détecté']) }
end
end end
context 'when a piece justificative is required' do context 'when there is something wrong with file save' do
let(:tpjs) { [tpj_mandatory] } let(:hash) do
{
context 'when no params are given' do "piece_justificative_#{tpj_not_mandatory.id}" =>
it { expect(errors).to match(['La pièce jointe justificatif doit être fournie.']) } double(path: '', original_filename: 'filename')
}
end end
context 'when the piece justificative is provided' do it { expect(errors).to match(["le fichier filename (not mandatory) n'a pas pu être sauvegardé"]) }
before :each do end
# we are messing around piece_justificative
# because directly doubling carrierwave params seems complicated
allow(PiecesJustificativesService).to receive(:save_pj).and_return(nil) context 'when a virus is provided' do
piece_justificative_double = double(type_de_piece_justificative: tpj_mandatory) let(:safe_file) { false }
expect(dossier).to receive(:pieces_justificatives).and_return([piece_justificative_double]) let(:hash) do
end {
"piece_justificative_#{tpj_not_mandatory.id}" =>
double(path: '', original_filename: 'bad_file')
}
end
let(:hash) do it { expect(errors).to match(['bad_file : virus détecté']) }
{ end
"piece_justificative_#{tpj_mandatory.id}" => double(path: '')
}
end
it { expect(errors).to match([]) } context 'when a regular file is provided' do
let(:content) { double(path: '', original_filename: 'filename') }
let(:hash) do
{
"piece_justificative_#{tpj_not_mandatory.id}" =>
content
}
end
before :each do
expect(PiecesJustificativesService).to receive(:save_pj)
.with(content, dossier, tpj_not_mandatory, user)
.and_return(nil)
end
it 'is saved' do
expect(errors).to match([])
end end
end end
end end
describe 'missing_pj_error_messages' do
let(:errors) { PiecesJustificativesService.missing_pj_error_messages(dossier) }
let(:tpjs) { [tpj_mandatory] }
context 'when no params are given' do
it { expect(errors).to match(['La pièce jointe justificatif doit être fournie.']) }
end
context 'when the piece justificative is provided' do
before :each do
# we are messing around piece_justificative
# because directly doubling carrierwave params seems complicated
piece_justificative_double = double(type_de_piece_justificative: tpj_mandatory)
expect(dossier).to receive(:pieces_justificatives).and_return([piece_justificative_double])
end
it { expect(errors).to match([]) }
end
end
end end