From eeaefdcc792df88647dd85793fc1a1a344306c56 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Thu, 17 Mar 2016 17:33:38 +0100 Subject: [PATCH] Add reference to user who upload a cerfa or a piece justificative --- .../users/description_controller.rb | 5 +- app/models/cerfa.rb | 1 + app/models/piece_justificative.rb | 2 + app/models/user.rb | 2 + app/serializers/cerfa_serializer.rb | 2 +- app/serializers/dossier_serializer.rb | 1 + .../piece_justificative_serializer.rb | 2 + .../type_de_piece_justificative_serializer.rb | 1 - app/serializers/user_serializer.rb | 3 ++ .../dossiers/_modal_historique.html.haml | 4 +- ...317144949_piece_justificative_have_user.rb | 22 +++++++++ db/migrate/20160317153115_cerfa_have_user.rb | 22 +++++++++ db/schema.rb | 4 +- .../api/v1/dossiers_controller_spec.rb | 48 ++++++++++++++----- .../users/description_controller_spec.rb | 4 ++ spec/models/cerfa_spec.rb | 1 + spec/models/piece_justificative_spec.rb | 2 + spec/models/user_spec.rb | 2 + 18 files changed, 108 insertions(+), 20 deletions(-) create mode 100644 app/serializers/user_serializer.rb create mode 100644 db/migrate/20160317144949_piece_justificative_have_user.rb create mode 100644 db/migrate/20160317153115_cerfa_have_user.rb diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb index 7df6a0074..9c2ad7624 100644 --- a/app/controllers/users/description_controller.rb +++ b/app/controllers/users/description_controller.rb @@ -34,7 +34,7 @@ class Users::DescriptionController < UsersController if @procedure.cerfa_flag? unless params[:cerfa_pdf].nil? - cerfa = Cerfa.new(content: params[:cerfa_pdf], dossier: @dossier) + cerfa = Cerfa.new(content: params[:cerfa_pdf], dossier: @dossier, user: current_user) unless cerfa.save flash.now.alert = cerfa.errors.full_messages.join('
').html_safe return render 'show' @@ -60,7 +60,8 @@ class Users::DescriptionController < UsersController piece_justificative = PieceJustificative.new(content: params["piece_justificative_#{type_de_pieces_justificatives.id}"], dossier: @dossier, - type_de_piece_justificative: type_de_pieces_justificatives) + type_de_piece_justificative: type_de_pieces_justificatives, + user: current_user) unless piece_justificative.save flash.now.alert = piece_justificative.errors.full_messages.join('
').html_safe diff --git a/app/models/cerfa.rb b/app/models/cerfa.rb index bc06b2654..7dc020acb 100644 --- a/app/models/cerfa.rb +++ b/app/models/cerfa.rb @@ -1,5 +1,6 @@ class Cerfa < ActiveRecord::Base belongs_to :dossier + belongs_to :user mount_uploader :content, CerfaUploader validates :content, :file_size => {:maximum => 3.megabytes} diff --git a/app/models/piece_justificative.rb b/app/models/piece_justificative.rb index 8cd6d2a80..c75824f59 100644 --- a/app/models/piece_justificative.rb +++ b/app/models/piece_justificative.rb @@ -2,6 +2,8 @@ class PieceJustificative < ActiveRecord::Base belongs_to :dossier belongs_to :type_de_piece_justificative + belongs_to :user + delegate :api_entreprise, :libelle, to: :type_de_piece_justificative alias_attribute :type, :type_de_piece_justificative_id diff --git a/app/models/user.rb b/app/models/user.rb index 1697fc437..c1cc2f0ce 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,6 +9,8 @@ class User < ActiveRecord::Base has_many :dossiers, dependent: :destroy has_many :invites, dependent: :destroy + has_many :piece_justificative, dependent: :destroy + has_many :cerfa, dependent: :destroy has_one :france_connect_information, dependent: :destroy delegate :given_name, :family_name, :email_france_connect, :gender, :birthdate, :birthplace, :france_connect_particulier_id, to: :france_connect_information diff --git a/app/serializers/cerfa_serializer.rb b/app/serializers/cerfa_serializer.rb index 2b99ded67..2fbfa1be9 100644 --- a/app/serializers/cerfa_serializer.rb +++ b/app/serializers/cerfa_serializer.rb @@ -3,5 +3,5 @@ class CerfaSerializer < ActiveModel::Serializer attributes :created_at, :content_url => :url - + has_one :user end \ No newline at end of file diff --git a/app/serializers/dossier_serializer.rb b/app/serializers/dossier_serializer.rb index f4201c0a8..ad772388d 100644 --- a/app/serializers/dossier_serializer.rb +++ b/app/serializers/dossier_serializer.rb @@ -12,5 +12,6 @@ class DossierSerializer < ActiveModel::Serializer has_many :cerfa has_many :commentaires has_many :champs + has_many :pieces_justificatives has_many :types_de_piece_justificative end \ No newline at end of file diff --git a/app/serializers/piece_justificative_serializer.rb b/app/serializers/piece_justificative_serializer.rb index 086dab8e4..c4a3cbba0 100644 --- a/app/serializers/piece_justificative_serializer.rb +++ b/app/serializers/piece_justificative_serializer.rb @@ -1,5 +1,7 @@ class PieceJustificativeSerializer < ActiveModel::Serializer attributes :created_at, + :type_de_piece_justificative_id, :content_url => :url + has_one :user end \ No newline at end of file diff --git a/app/serializers/type_de_piece_justificative_serializer.rb b/app/serializers/type_de_piece_justificative_serializer.rb index 3a17661e7..57aae2903 100644 --- a/app/serializers/type_de_piece_justificative_serializer.rb +++ b/app/serializers/type_de_piece_justificative_serializer.rb @@ -3,5 +3,4 @@ class TypeDePieceJustificativeSerializer < ActiveModel::Serializer :libelle, :description - has_many :pieces_justificatives end \ No newline at end of file diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb new file mode 100644 index 000000000..2e74355ad --- /dev/null +++ b/app/serializers/user_serializer.rb @@ -0,0 +1,3 @@ +class UserSerializer < ActiveModel::Serializer + attributes :email +end \ No newline at end of file diff --git a/app/views/dossiers/_modal_historique.html.haml b/app/views/dossiers/_modal_historique.html.haml index 0b7b8fef7..27f470ea0 100644 --- a/app/views/dossiers/_modal_historique.html.haml +++ b/app/views/dossiers/_modal_historique.html.haml @@ -22,7 +22,7 @@ - @facade.cerfas_ordered.each do |cerfa| %tr %td.col-md-6.col-lg-4 - = cerfa.dossier.user.email + = cerfa.user.email %td.col-md-6.col-lg-4 = cerfa.created_at %td.col-md-6.col-lg-4 @@ -32,7 +32,7 @@ - @facade.dossier.retrieve_all_piece_justificative_by_type(type_de_piece_justificative.id).each do |piece_justificative| %tr %td.col-md-6.col-lg-4 - = piece_justificative.dossier.user.email + = piece_justificative.user.email %td.col-md-6.col-lg-4 = piece_justificative.created_at %td.col-md-6.col-lg-4 diff --git a/db/migrate/20160317144949_piece_justificative_have_user.rb b/db/migrate/20160317144949_piece_justificative_have_user.rb new file mode 100644 index 000000000..10ee2b45b --- /dev/null +++ b/db/migrate/20160317144949_piece_justificative_have_user.rb @@ -0,0 +1,22 @@ +class PieceJustificativeHaveUser < ActiveRecord::Migration + class PieceJustificative < ActiveRecord::Base + belongs_to :dossier + end + + class Dossier < ActiveRecord::Base + belongs_to :user + end + + class User < ActiveRecord::Base + + end + + def change + add_reference :pieces_justificatives, :user, references: :users + + PieceJustificative.all.each do |piece_justificative| + piece_justificative.user_id = piece_justificative.dossier.user.id + piece_justificative.save + end + end +end diff --git a/db/migrate/20160317153115_cerfa_have_user.rb b/db/migrate/20160317153115_cerfa_have_user.rb new file mode 100644 index 000000000..7a0710901 --- /dev/null +++ b/db/migrate/20160317153115_cerfa_have_user.rb @@ -0,0 +1,22 @@ +class CerfaHaveUser < ActiveRecord::Migration + class Cerfa < ActiveRecord::Base + belongs_to :dossier + end + + class Dossier < ActiveRecord::Base + belongs_to :user + end + + class User < ActiveRecord::Base + + end + + def change + add_reference :cerfas, :user, references: :users + + Cerfa.all.each do |cerfa| + cerfa.user_id = cerfa.dossier.user.id + cerfa.save + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 2416fea7b..275c4e305 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160317135217) do +ActiveRecord::Schema.define(version: 20160317153115) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -71,6 +71,7 @@ ActiveRecord::Schema.define(version: 20160317135217) do t.string "content" t.integer "dossier_id" t.datetime "created_at" + t.integer "user_id" end add_index "cerfas", ["dossier_id"], name: "index_cerfas_on_dossier_id", using: :btree @@ -200,6 +201,7 @@ ActiveRecord::Schema.define(version: 20160317135217) do t.integer "dossier_id" t.integer "type_de_piece_justificative_id" t.datetime "created_at" + t.integer "user_id" end add_index "pieces_justificatives", ["type_de_piece_justificative_id"], name: "index_pieces_justificatives_on_type_de_piece_justificative_id", using: :btree diff --git a/spec/controllers/api/v1/dossiers_controller_spec.rb b/spec/controllers/api/v1/dossiers_controller_spec.rb index 4950ac928..74bb896ec 100644 --- a/spec/controllers/api/v1/dossiers_controller_spec.rb +++ b/spec/controllers/api/v1/dossiers_controller_spec.rb @@ -116,7 +116,7 @@ describe API::V1::DossiersController do let!(:dossier) { Timecop.freeze(date_creation) { create(:dossier, :with_entreprise, procedure: procedure) } } let(:dossier_id) { dossier.id } let(:body) { JSON.parse(retour.body, symbolize_names: true) } - let(:field_list) { [:id, :nom_projet, :created_at, :updated_at, :description, :archived, :mandataire_social, :entreprise, :etablissement, :cerfa, :types_de_piece_justificative, :champs, :commentaires] } + let(:field_list) { [:id, :nom_projet, :created_at, :updated_at, :description, :archived, :mandataire_social, :entreprise, :etablissement, :cerfa, :types_de_piece_justificative, :pieces_justificatives, :champs, :commentaires] } subject { body[:dossier] } it 'return REST code 200', :show_in_doc do @@ -162,10 +162,6 @@ describe API::V1::DossiersController do end describe 'types_de_piece_justificative' do - before do - create :piece_justificative, dossier: dossier, type_de_piece_justificative: dossier.procedure.types_de_piece_justificative.first - end - let(:field_list) { [ :id, :libelle, @@ -180,16 +176,32 @@ describe API::V1::DossiersController do it { expect(subject.keys.include?(:id)).to be_truthy } it { expect(subject[:libelle]).to eq('RIB') } it { expect(subject[:description]).to eq('Releve identité bancaire') } + end + end - describe 'piece justificative' do - let(:field_list) { [ - :url, :created_at] } - subject { - super()[:pieces_justificatives].first } + 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 + end - it { expect(subject.keys.include?(:url)).to be_truthy } - it { expect(subject[:created_at]).not_to be_nil } - end + let(:field_list) { [ + :url, :created_at, :type_de_piece_justificative_id] } + subject { + super()[:pieces_justificatives].first } + + it { expect(subject.keys.include?(:url)).to be_truthy } + it { expect(subject[:created_at]).not_to be_nil } + it { expect(subject[:type_de_piece_justificative_id]).not_to be_nil } + + it { expect(subject.keys.include?(:user)).to be_truthy } + + describe 'user' do + let(:field_list) { [ + :url, :created_at, :type_de_piece_justificative_id] } + subject { + super()[:user] } + + it { expect(subject[:email]).not_to be_nil } end end @@ -244,6 +256,7 @@ describe API::V1::DossiersController do before do tmp_cerfa = dossier.cerfa.first tmp_cerfa.content = content + tmp_cerfa.user = dossier.user tmp_cerfa.save end @@ -251,6 +264,15 @@ describe API::V1::DossiersController do it { expect(subject[:created_at]).not_to be_nil } it { expect(subject[:url]).to match /^http:\/\/.*downloads.*_CERFA\.pdf$/ } + + describe 'user' do + let(:field_list) { [ + :url, :created_at, :type_de_piece_justificative_id] } + subject { + super()[:user] } + + it { expect(subject[:email]).not_to be_nil } + end end describe 'etablissement' do diff --git a/spec/controllers/users/description_controller_spec.rb b/spec/controllers/users/description_controller_spec.rb index 53ca4062d..88d6ffb13 100644 --- a/spec/controllers/users/description_controller_spec.rb +++ b/spec/controllers/users/description_controller_spec.rb @@ -142,6 +142,8 @@ describe Users::DescriptionController, type: :controller do it 'dossier_id' do expect(subject.dossier_id).to eq(dossier_id) end + + it { expect(subject.user).to eq user } end context 'les anciens CERFA PDF ne sont pas écrasées' do @@ -222,10 +224,12 @@ describe Users::DescriptionController, type: :controller do context 'for piece 0' do subject { dossier.retrieve_last_piece_justificative_by_type all_pj_type[0].to_s } it { expect(subject.content).not_to be_nil } + it { expect(subject.user).to eq user } end context 'for piece 1' do subject { dossier.retrieve_last_piece_justificative_by_type all_pj_type[1].to_s } it { expect(subject.content).not_to be_nil } + it { expect(subject.user).to eq user } end end end diff --git a/spec/models/cerfa_spec.rb b/spec/models/cerfa_spec.rb index 47d7f432d..233218fd1 100644 --- a/spec/models/cerfa_spec.rb +++ b/spec/models/cerfa_spec.rb @@ -8,6 +8,7 @@ describe Cerfa do describe 'associations' do it { is_expected.to belong_to(:dossier) } + it { is_expected.to belong_to(:user) } end describe 'empty?' do diff --git a/spec/models/piece_justificative_spec.rb b/spec/models/piece_justificative_spec.rb index feb2e515d..ab2ae4b15 100644 --- a/spec/models/piece_justificative_spec.rb +++ b/spec/models/piece_justificative_spec.rb @@ -9,6 +9,8 @@ describe PieceJustificative do describe 'associations' 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) } + end describe 'delegation' do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index bb9905a9b..aeedb4f69 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -21,6 +21,8 @@ describe User, type: :model do describe 'associations' do it { is_expected.to have_many(:dossiers) } it { is_expected.to have_many(:invites) } + it { is_expected.to have_many(:piece_justificative) } + it { is_expected.to have_many(:cerfa) } end describe '#find_for_france_connect' do let(:siret) { '00000000000000' }