New feature : User, Guest and Gestionnaire can be upload a document with theirs comments
This commit is contained in:
parent
fb2eeddf97
commit
b15c2bbb2b
19 changed files with 214 additions and 59 deletions
|
@ -11,8 +11,18 @@ class CommentairesController < ApplicationController
|
|||
@commentaire.dossier.next_step! 'user', 'comment' if current_user.email == @commentaire.dossier.user.email
|
||||
end
|
||||
|
||||
unless params[:piece_justificative].nil?
|
||||
pj = PiecesJustificativesService.upload_one! @commentaire.dossier, current_user, params
|
||||
|
||||
if pj.errors.empty?
|
||||
@commentaire.piece_justificative = pj
|
||||
else
|
||||
flash.alert = pj.errors.full_messages.join("<br>").html_safe
|
||||
end
|
||||
end
|
||||
|
||||
@commentaire.body = params['texte_commentaire']
|
||||
@commentaire.save
|
||||
@commentaire.save unless flash.alert
|
||||
|
||||
if is_gestionnaire?
|
||||
NotificationMailer.new_answer(@commentaire.dossier).deliver_now!
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
class Commentaire < ActiveRecord::Base
|
||||
belongs_to :dossier
|
||||
|
||||
belongs_to :piece_justificative
|
||||
end
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
class PieceJustificative < ActiveRecord::Base
|
||||
belongs_to :dossier
|
||||
belongs_to :type_de_piece_justificative
|
||||
has_one :commentaire
|
||||
|
||||
belongs_to :user
|
||||
|
||||
|
@ -10,6 +11,7 @@ class PieceJustificative < ActiveRecord::Base
|
|||
|
||||
mount_uploader :content, PieceJustificativeUploader
|
||||
validates :content, :file_size => {:maximum => 3.megabytes}
|
||||
validates :content, presence: true, allow_blank: false, allow_nil: false
|
||||
|
||||
def empty?
|
||||
content.blank?
|
||||
|
@ -17,7 +19,22 @@ class PieceJustificative < ActiveRecord::Base
|
|||
|
||||
def content_url
|
||||
unless content.url.nil?
|
||||
(Downloader.new content, type_de_piece_justificative.libelle).url
|
||||
(Downloader.new content,
|
||||
(type_de_piece_justificative.nil? ? content.file.original_filename : type_de_piece_justificative.libelle)).url
|
||||
end
|
||||
end
|
||||
|
||||
def self.accept_format
|
||||
" application/pdf,
|
||||
application/msword,
|
||||
application/vnd.openxmlformats-officedocument.wordprocessingml.document,
|
||||
application/vnd.ms-excel,
|
||||
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,
|
||||
application/vnd.ms-powerpoint,
|
||||
application/vnd.openxmlformats-officedocument.presentationml.presentation,
|
||||
application/vnd.oasis.opendocument.text,
|
||||
application/vnd.oasis.opendocument.presentation,
|
||||
application/vnd.oasis.opendocument.spreadsheet
|
||||
"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
class ClamavService
|
||||
def self.safe_file? path_file
|
||||
|
||||
if Rails.env == 'development'
|
||||
return CLAMAV[:response] if CLAMAV[:mock?]
|
||||
end
|
||||
|
||||
FileUtils.chmod 0666, path_file
|
||||
|
||||
client = ClamAV::Client.new
|
||||
|
|
|
@ -21,4 +21,20 @@ class PiecesJustificativesService
|
|||
end
|
||||
errors
|
||||
end
|
||||
|
||||
def self.upload_one! dossier, user, params
|
||||
if ClamavService.safe_file? params[:piece_justificative][:content].path
|
||||
piece_justificative = PieceJustificative.new(content: params[:piece_justificative][:content],
|
||||
dossier: dossier,
|
||||
type_de_piece_justificative: nil,
|
||||
user: user)
|
||||
|
||||
piece_justificative.save
|
||||
else
|
||||
piece_justificative = PieceJustificative.new
|
||||
piece_justificative.errors.add(:content, params[:piece_justificative][:content].original_filename+": <b>Virus détecté !!</b>")
|
||||
end
|
||||
|
||||
piece_justificative
|
||||
end
|
||||
end
|
|
@ -8,27 +8,9 @@
|
|||
-if @dossier.cerfa_available?
|
||||
%span.btn.btn-sm.btn-file.btn-success
|
||||
Modifier
|
||||
%input{type: 'file', name:'cerfa_pdf', id:'cerfa_pdf', accept: "application/pdf,
|
||||
application/msword,
|
||||
application/vnd.openxmlformats-officedocument.wordprocessingml.document,
|
||||
application/vnd.ms-excel,
|
||||
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,
|
||||
application/vnd.ms-powerpoint,
|
||||
application/vnd.openxmlformats-officedocument.presentationml.presentation,
|
||||
application/vnd.oasis.opendocument.text,
|
||||
application/vnd.oasis.opendocument.presentation,
|
||||
application/vnd.oasis.opendocument.spreadsheet", :max_file_size => 3.megabytes }
|
||||
%input{type: 'file', name:'cerfa_pdf', id:'cerfa_pdf', accept: PieceJustificative.accept_format, :max_file_size => 3.megabytes }
|
||||
-else
|
||||
%input{type: 'file', name:'cerfa_pdf', id:'cerfa_pdf', accept: "application/pdf,
|
||||
application/msword,
|
||||
application/vnd.openxmlformats-officedocument.wordprocessingml.document,
|
||||
application/vnd.ms-excel,
|
||||
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,
|
||||
application/vnd.ms-powerpoint,
|
||||
application/vnd.openxmlformats-officedocument.presentationml.presentation,
|
||||
application/vnd.oasis.opendocument.text,
|
||||
application/vnd.oasis.opendocument.presentation,
|
||||
application/vnd.oasis.opendocument.spreadsheet", :max_file_size => 3.megabytes }
|
||||
%input{type: 'file', name:'cerfa_pdf', id:'cerfa_pdf', accept: PieceJustificative.accept_format, :max_file_size => 3.megabytes }
|
||||
|
||||
- @dossier.types_de_piece_justificative.each do |type_de_piece_justificative|
|
||||
%tr
|
||||
|
@ -39,27 +21,9 @@
|
|||
%span.text-success{ id: "piece_justificative_#{type_de_piece_justificative.id}" } Nous l'avons récupéré pour vous.
|
||||
-else
|
||||
-if @dossier.retrieve_last_piece_justificative_by_type(type_de_piece_justificative.id).nil?
|
||||
= file_field_tag "piece_justificative_#{type_de_piece_justificative.id}", accept: " application/pdf,
|
||||
application/msword,
|
||||
application/vnd.openxmlformats-officedocument.wordprocessingml.document,
|
||||
application/vnd.ms-excel,
|
||||
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,
|
||||
application/vnd.ms-powerpoint,
|
||||
application/vnd.openxmlformats-officedocument.presentationml.presentation,
|
||||
application/vnd.oasis.opendocument.text,
|
||||
application/vnd.oasis.opendocument.presentation,
|
||||
application/vnd.oasis.opendocument.spreadsheet", :max_file_size => 3.megabytes
|
||||
= file_field_tag "piece_justificative_#{type_de_piece_justificative.id}", accept: PieceJustificative.accept_format, :max_file_size => 3.megabytes
|
||||
-else
|
||||
%span.btn.btn-sm.btn-file.btn-success
|
||||
Modifier
|
||||
= file_field_tag "piece_justificative_#{type_de_piece_justificative.id}", accept: " application/pdf,
|
||||
application/msword,
|
||||
application/vnd.openxmlformats-officedocument.wordprocessingml.document,
|
||||
application/vnd.ms-excel,
|
||||
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,
|
||||
application/vnd.ms-powerpoint,
|
||||
application/vnd.openxmlformats-officedocument.presentationml.presentation,
|
||||
application/vnd.oasis.opendocument.text,
|
||||
application/vnd.oasis.opendocument.presentation,
|
||||
application/vnd.oasis.opendocument.spreadsheet", :max_file_size => 3.megabytes
|
||||
= file_field_tag "piece_justificative_#{type_de_piece_justificative.id}", accept: PieceJustificative.accept_format, :max_file_size => 3.megabytes
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
.content#commentaires_flux{style:'width:100%;'}
|
||||
%div#commentaire_new{style: 'width:80%; margin-left:auto; margin-right:auto'}
|
||||
= form_tag(url_for({ controller: 'commentaires', action: :create, dossier_id: @facade.dossier.id }), class: 'form-inline', method: 'POST') do
|
||||
%textarea.form-control{id: 'texte_commentaire', class: 'wysihtml5', name: 'texte_commentaire', style: 'width: 100%; margin-bottom:2%', rows: '5', placeholder:"Dialoguer avec votre interlocuteur privilégié en charge de votre dossier."}
|
||||
%div#commentaire_new{style: 'width:80%; margin-left:auto; margin-right:auto; margin-bottom:7%'}
|
||||
= form_tag(url_for({ controller: 'commentaires', action: :create, dossier_id: @facade.dossier.id }), class: 'form-inline', method: 'POST', multipart: true) do
|
||||
%textarea.form-control{id: 'texte_commentaire', class: 'wysihtml5', name: 'texte_commentaire', style: 'width: 100%; margin-bottom:2%', rows: '5', placeholder:"Commentaire"}
|
||||
%h4.text-primary{style: 'margin-top: 0px'} Ajout un fichier
|
||||
= file_field_tag "piece_justificative[content]", accept: PieceJustificative.accept_format, style: 'float: left; margin-left: 20px'
|
||||
%input.form-control.btn.btn-success{:type => 'submit', :value => 'Poster', style: 'float:right'}
|
||||
%br
|
||||
%br
|
||||
|
||||
-@facade.commentaires.each do |com|
|
||||
%span.text-info#email_contact{style: 'font-weight:bold'}
|
||||
|
@ -12,7 +12,15 @@
|
|||
%span#created_at
|
||||
\-
|
||||
=com.created_at_fr
|
||||
- unless com.piece_justificative.nil?
|
||||
\-
|
||||
%span#piece_justificative
|
||||
%b
|
||||
= link_to com.piece_justificative.content.file.original_filename, com.piece_justificative.content_url, style:'color: green', target: '_blank'
|
||||
|
||||
%br
|
||||
.description#body
|
||||
=com.body.html_safe
|
||||
|
||||
|
||||
%br
|
||||
|
|
4
config/initializers/clamav.rb
Normal file
4
config/initializers/clamav.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
CLAMAV = Hashie::Mash.new ({
|
||||
mock?: true,
|
||||
response: true
|
||||
})
|
|
@ -0,0 +1,5 @@
|
|||
class AddPieceJustificativeReferenceToCommentaire < ActiveRecord::Migration
|
||||
def change
|
||||
add_reference :commentaires, :piece_justificative, references: :piece_justificatives
|
||||
end
|
||||
end
|
|
@ -11,7 +11,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20160317153115) do
|
||||
ActiveRecord::Schema.define(version: 20160419142017) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
@ -88,6 +88,7 @@ ActiveRecord::Schema.define(version: 20160317153115) do
|
|||
t.string "body"
|
||||
t.integer "dossier_id"
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "piece_justificative_id"
|
||||
end
|
||||
|
||||
add_index "commentaires", ["dossier_id"], name: "index_commentaires_on_dossier_id", using: :btree
|
||||
|
|
|
@ -181,7 +181,7 @@ describe API::V1::DossiersController do
|
|||
|
||||
describe 'piece justificative' do
|
||||
before do
|
||||
create :piece_justificative, dossier: dossier, type_de_piece_justificative: dossier.procedure.types_de_piece_justificative.first, user: dossier.user
|
||||
create :piece_justificative, :rib, dossier: dossier, type_de_piece_justificative: dossier.procedure.types_de_piece_justificative.first, user: dossier.user
|
||||
end
|
||||
|
||||
let(:field_list) { [
|
||||
|
|
|
@ -6,6 +6,10 @@ describe Backoffice::CommentairesController, type: :controller do
|
|||
let(:email_commentaire) { 'test@test.com' }
|
||||
let(:texte_commentaire) { 'Commentaire de test' }
|
||||
|
||||
before do
|
||||
allow(ClamavService).to receive(:safe_file?).and_return(true)
|
||||
end
|
||||
|
||||
describe '#POST create' do
|
||||
before do
|
||||
sign_in create(:gestionnaire)
|
||||
|
@ -17,6 +21,52 @@ describe Backoffice::CommentairesController, type: :controller do
|
|||
end
|
||||
end
|
||||
|
||||
context 'when document is upload whith a commentaire' do
|
||||
let(:document_upload) { Rack::Test::UploadedFile.new("./spec/support/files/piece_justificative_0.pdf", 'application/pdf') }
|
||||
|
||||
subject do
|
||||
post :create, dossier_id: dossier_id, email_commentaire: email_commentaire, texte_commentaire: texte_commentaire, piece_justificative: {content: document_upload}
|
||||
end
|
||||
|
||||
it 'create a new piece justificative' do
|
||||
expect { subject }.to change(PieceJustificative, :count).by(1)
|
||||
end
|
||||
|
||||
it 'clamav check the pj' do
|
||||
expect(ClamavService).to receive(:safe_file?)
|
||||
subject
|
||||
end
|
||||
|
||||
describe 'piece justificative created' do
|
||||
let(:pj) { PieceJustificative.last }
|
||||
|
||||
before do
|
||||
subject
|
||||
end
|
||||
|
||||
it 'not have a type de pj' do
|
||||
expect(pj.type_de_piece_justificative).to be_nil
|
||||
end
|
||||
|
||||
it 'content not be nil' do
|
||||
expect(pj.content).not_to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe 'commentaire created' do
|
||||
let(:commentaire) { Commentaire.last }
|
||||
|
||||
before do
|
||||
subject
|
||||
end
|
||||
|
||||
it 'have a piece justificative reference' do
|
||||
expect(commentaire.piece_justificative).not_to be_nil
|
||||
expect(commentaire.piece_justificative).to eq PieceJustificative.last
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'change dossier state after post a comment' do
|
||||
context 'gestionnaire is connected' do
|
||||
context 'when dossier is at state updated' do
|
||||
|
|
|
@ -6,6 +6,10 @@ describe Users::CommentairesController, type: :controller do
|
|||
let(:email_commentaire) { 'test@test.com' }
|
||||
let(:texte_commentaire) { 'Commentaire de test' }
|
||||
|
||||
before do
|
||||
allow(ClamavService).to receive(:safe_file?).and_return(true)
|
||||
end
|
||||
|
||||
describe '#POST create' do
|
||||
context 'création correct d\'un commentaire' do
|
||||
subject do
|
||||
|
@ -26,6 +30,53 @@ describe Users::CommentairesController, type: :controller do
|
|||
end
|
||||
end
|
||||
|
||||
context 'when document is upload whith a commentaire' do
|
||||
let(:document_upload) { Rack::Test::UploadedFile.new("./spec/support/files/piece_justificative_0.pdf", 'application/pdf') }
|
||||
|
||||
subject do
|
||||
sign_in dossier.user
|
||||
post :create, dossier_id: dossier_id, texte_commentaire: texte_commentaire, piece_justificative: {content: document_upload}
|
||||
end
|
||||
|
||||
it 'create a new piece justificative' do
|
||||
expect { subject }.to change(PieceJustificative, :count).by(1)
|
||||
end
|
||||
|
||||
it 'clamav check the pj' do
|
||||
expect(ClamavService).to receive(:safe_file?)
|
||||
subject
|
||||
end
|
||||
|
||||
describe 'piece justificative created' do
|
||||
let(:pj) { PieceJustificative.last }
|
||||
|
||||
before do
|
||||
subject
|
||||
end
|
||||
|
||||
it 'not have a type de pj' do
|
||||
expect(pj.type_de_piece_justificative).to be_nil
|
||||
end
|
||||
|
||||
it 'content not be nil' do
|
||||
expect(pj.content).not_to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe 'commentaire created' do
|
||||
let(:commentaire) { Commentaire.last }
|
||||
|
||||
before do
|
||||
subject
|
||||
end
|
||||
|
||||
it 'have a piece justificative reference' do
|
||||
expect(commentaire.piece_justificative).not_to be_nil
|
||||
expect(commentaire.piece_justificative).to eq PieceJustificative.last
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'change dossier state after post a comment' do
|
||||
context 'when user is connected' do
|
||||
context 'when dossier is at state replied' do
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
FactoryGirl.define do
|
||||
factory :piece_justificative do
|
||||
trait :rib do
|
||||
content '/chemin/vers/RIB'
|
||||
content Rack::Test::UploadedFile.new("./spec/support/files/RIB.pdf", 'application/pdf')
|
||||
end
|
||||
|
||||
trait :contrat do
|
||||
content '/chemin/vers/Contrat'
|
||||
content Rack::Test::UploadedFile.new("./spec/support/files/Contrat.pdf", 'application/pdf')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,4 +6,6 @@ describe Commentaire do
|
|||
it { is_expected.to have_db_column(:created_at) }
|
||||
it { is_expected.to have_db_column(:updated_at) }
|
||||
it { is_expected.to belong_to(:dossier) }
|
||||
|
||||
it { is_expected.to belong_to(:piece_justificative) }
|
||||
end
|
||||
|
|
|
@ -10,9 +10,17 @@ describe PieceJustificative do
|
|||
it { is_expected.to belong_to(:dossier) }
|
||||
it { is_expected.to belong_to(:type_de_piece_justificative) }
|
||||
it { is_expected.to belong_to(:user) }
|
||||
it { is_expected.to have_one(:commentaire) }
|
||||
|
||||
end
|
||||
|
||||
describe 'validations' do
|
||||
context 'content' do
|
||||
it { is_expected.not_to allow_value(nil).for(:content) }
|
||||
it { is_expected.not_to allow_value('').for(:content) }
|
||||
end
|
||||
end
|
||||
|
||||
describe 'delegation' do
|
||||
it { is_expected.to delegate_method(:libelle).to(:type_de_piece_justificative) }
|
||||
it { is_expected.to delegate_method(:api_entreprise).to(:type_de_piece_justificative) }
|
||||
|
@ -21,10 +29,7 @@ describe PieceJustificative do
|
|||
describe '#empty?' do
|
||||
let(:piece_justificative) { create(:piece_justificative, content: content) }
|
||||
subject { piece_justificative.empty? }
|
||||
context 'when content is nil' do
|
||||
let(:content) { nil }
|
||||
it { is_expected.to be_truthy }
|
||||
end
|
||||
|
||||
context 'when content exist' do
|
||||
let(:content) { File.open('./spec/support/files/piece_justificative_388.pdf') }
|
||||
it { is_expected.to be_falsey }
|
||||
|
|
BIN
spec/support/files/Contrat.pdf
Normal file
BIN
spec/support/files/Contrat.pdf
Normal file
Binary file not shown.
BIN
spec/support/files/RIB.pdf
Normal file
BIN
spec/support/files/RIB.pdf
Normal file
Binary file not shown.
|
@ -4,7 +4,11 @@ describe 'users/recapitulatif/_commentaires_flux.html.haml', type: :view do
|
|||
let(:dossier) { create(:dossier) }
|
||||
let(:dossier_id) { dossier.id }
|
||||
let(:email_commentaire) { 'mon_mail_de_commentaire@test.com' }
|
||||
let!(:commentaire) { create(:commentaire, dossier: dossier, email: email_commentaire, body: 'ma super description') }
|
||||
|
||||
let(:document_upload) { Rack::Test::UploadedFile.new("./spec/support/files/piece_justificative_0.pdf", 'application/pdf') }
|
||||
let(:pj) { create :piece_justificative, content: document_upload }
|
||||
|
||||
let!(:commentaire) { create(:commentaire, dossier: dossier, email: email_commentaire, body: 'ma super description', piece_justificative: pj) }
|
||||
let(:body) { 'Commentaire de test' }
|
||||
|
||||
before do
|
||||
|
@ -24,6 +28,12 @@ describe 'users/recapitulatif/_commentaires_flux.html.haml', type: :view do
|
|||
it 'le corps du commentaire est présent' do
|
||||
expect(rendered).to have_selector('div[class=description][id=body]')
|
||||
end
|
||||
|
||||
context 'when commentaire as PJ' do
|
||||
it 'commentaire present the link' do
|
||||
expect(rendered).to have_css('#piece_justificative')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'Affichage du formulaire de commentaire' do
|
||||
|
@ -34,5 +44,11 @@ describe 'users/recapitulatif/_commentaires_flux.html.haml', type: :view do
|
|||
it 'Champs de texte' do
|
||||
expect(rendered).to have_selector('textarea[id=texte_commentaire][name=texte_commentaire]')
|
||||
end
|
||||
|
||||
describe 'File input' do
|
||||
it 'have file_input tag' do
|
||||
expect(rendered).to have_css('#piece_justificative_content')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue