Merge branch 'dev'

This commit is contained in:
Mathieu Magnin 2017-08-02 10:16:06 +02:00
commit 513a38371d
18 changed files with 158 additions and 67 deletions

View file

@ -1,7 +1,7 @@
version: 2
jobs:
build:
parallelism: 2
parallelism: 3
docker:
- image: ruby:2.3.1
- image: postgres:9.4.1

View file

@ -109,6 +109,8 @@ gem 'prawn_rails', '~> 0.0.11'
gem 'chunky_png'
gem 'sentry-raven'
gem 'rack-mini-profiler'
group :test do
gem 'capybara'
gem 'launchy'

View file

@ -448,6 +448,8 @@ GEM
rack (2.0.1)
rack-handlers (0.7.3)
rack
rack-mini-profiler (0.10.5)
rack (>= 1.2.0)
rack-oauth2 (1.4.0)
activesupport (>= 2.3)
attr_required (>= 0.0.5)
@ -725,6 +727,7 @@ DEPENDENCIES
prawn_rails (~> 0.0.11)
pry-byebug
rack-handlers
rack-mini-profiler
rails (= 5.0.0.1)
rails-controller-testing
redis

View file

@ -16,12 +16,3 @@ html {
a {
text-decoration: none;
}
// remove dotted outline on firefox
:focus {
outline: none;
}
::-moz-focus-inner {
border: none;
}

View file

@ -5,6 +5,13 @@ class ApplicationController < ActionController::Base
before_action :check_browser
before_action :load_navbar_left_pannel_partial_url
before_action :set_raven_context
before_action :authorize_request_for_profiler
def authorize_request_for_profiler
if Rails.env.production? && administration_signed_in?
Rack::MiniProfiler.authorize_request
end
end
def default_url_options
return {protocol: 'https'} if Rails.env.staging? || Rails.env.production?

View file

@ -4,18 +4,10 @@ class Backoffice::AvisController < ApplicationController
before_action :check_avis_exists_and_email_belongs_to_avis, only: [:sign_up, :create_gestionnaire]
def create
avis = Avis.new(create_params.merge(claimant: current_gestionnaire))
avis.dossier = dossier
email = create_params[:email]
gestionnaire = Gestionnaire.find_by(email: email)
if gestionnaire
avis.gestionnaire = gestionnaire
avis.email = nil
end
avis = Avis.new(create_params.merge(claimant: current_gestionnaire, dossier: dossier))
if avis.save
flash[:notice] = "Votre demande d'avis a bien été envoyée à #{email}"
flash[:notice] = "Votre demande d'avis a bien été envoyée à #{create_params[:email]}"
end
redirect_to backoffice_dossier_path(dossier)

View file

@ -82,7 +82,7 @@ class Users::DossiersController < UsersController
individual.update_column :gender, @facade.dossier.france_connect_information.gender
individual.update_column :nom, @facade.dossier.france_connect_information.family_name
individual.update_column :prenom, @facade.dossier.france_connect_information.given_name
individual.update_column :birthdate, @facade.dossier.france_connect_information.birthdate.strftime("%d/%m/%Y")
individual.update_column :birthdate, @facade.dossier.france_connect_information.birthdate.iso8601
end
rescue ActiveRecord::RecordNotFound
@ -131,7 +131,10 @@ class Users::DossiersController < UsersController
def update
@facade = facade params[:dossier][:id]
if checked_autorisation_donnees?
if individual_errors.any?
flash.alert = individual_errors
redirect_to users_dossier_path(id: @facade.dossier.id)
else
unless Dossier.find(@facade.dossier.id).update_attributes update_params_with_formatted_birthdate
flash.alert = @facade.dossier.errors.full_messages
@ -143,9 +146,6 @@ class Users::DossiersController < UsersController
else
redirect_to url_for(controller: :description, action: :show, dossier_id: @facade.dossier.id)
end
else
flash.alert = 'Les conditions sont obligatoires.'
redirect_to users_dossier_path(id: @facade.dossier.id)
end
end
@ -191,24 +191,35 @@ class Users::DossiersController < UsersController
def update_params_with_formatted_birthdate
editable_params = update_params
# If the user was shown a date input field (if its browser supports it),
# the returned param will follow the YYYY-MM-DD pattern, which we need
# do convert to the DD/MM/YYYY pattern we use
if editable_params &&
editable_params[:individual_attributes] &&
editable_params[:individual_attributes][:birthdate] &&
editable_params[:individual_attributes][:birthdate] =~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/
editable_params[:individual_attributes][:birthdate]
original_birthdate = editable_params[:individual_attributes][:birthdate]
formatted_birthdate = I18n.l(original_birthdate.to_date, format: '%d/%m/%Y')
editable_params[:individual_attributes][:birthdate] = formatted_birthdate
iso_date = begin
Date.parse(editable_params[:individual_attributes][:birthdate]).iso8601
rescue
nil
end
editable_params[:individual_attributes][:birthdate] = iso_date
end
editable_params
end
def checked_autorisation_donnees?
update_params[:autorisation_donnees] == '1'
def individual_errors
errors = []
if update_params[:autorisation_donnees] != "1"
errors << "La validation des conditions d'utilisation est obligatoire"
end
if update_params[:individual_attributes].present? &&
!/^\d{4}\-\d{2}\-\d{2}$/.match(update_params[:individual_attributes][:birthdate]) &&
!/^\d{2}\/\d{2}\/\d{4}$/.match(update_params[:individual_attributes][:birthdate])
errors << "Le format de la date de naissance doit être JJ/MM/AAAA"
end
errors
end
def siret

View file

@ -3,6 +3,7 @@ class Avis < ApplicationRecord
belongs_to :gestionnaire
belongs_to :claimant, class_name: 'Gestionnaire'
before_create :try_to_assign_gestionnaire
after_create :notify_gestionnaire
scope :with_answer, -> { where.not(answer: nil) }
@ -14,10 +15,6 @@ class Avis < ApplicationRecord
gestionnaire.try(:email) || email
end
def notify_gestionnaire
AvisMailer.avis_invitation(self).deliver_now
end
def self.link_avis_to_gestionnaire(gestionnaire)
Avis.where(email: gestionnaire.email).update_all(email: nil, gestionnaire_id: gestionnaire.id)
end
@ -26,4 +23,18 @@ class Avis < ApplicationRecord
avis = Avis.find_by(id: avis_id)
avis.present? && avis.email == email
end
private
def notify_gestionnaire
AvisMailer.avis_invitation(self).deliver_now
end
def try_to_assign_gestionnaire
gestionnaire = Gestionnaire.find_by(email: email)
if gestionnaire
self.gestionnaire = gestionnaire
self.email = nil
end
end
end

View file

@ -58,7 +58,7 @@ class Champ < ActiveRecord::Base
def format_date_to_iso
date = begin
Date.parse(value).strftime("%F")
Date.parse(value).iso8601
rescue
nil
end

View file

@ -2,4 +2,5 @@ class Individual < ActiveRecord::Base
belongs_to :dossier
validates_uniqueness_of :dossier_id
validates :birthdate, format: { with: /\A\d{4}\-\d{2}\-\d{2}\z/, message: "La date n'est pas au format AAAA-MM-JJ" }, allow_nil: true
end

View file

@ -31,7 +31,7 @@
%label
%h4
Date de naissance *
= ff.date_field :birthdate, { class: 'form-control', placeholder: 'jj/mm/aaaa', required: true }
= ff.date_field :birthdate, { value: @facade.individual.birthdate, class: 'form-control', placeholder: 'jj/mm/aaaa', required: true }
%p
%label{ style: 'font-weight: normal;' }

View file

@ -0,0 +1,27 @@
namespace :'2017_07_26_clean_birthdate_on_individual' do
task clean: :environment do
# remove duplicates
duplicate_individuals = Individual.group("dossier_id").count.select{ |_dossier_id, count| count > 1 }.keys
duplicate_individuals.each { |dossier_id| Individual.where(dossier_id: dossier_id, nom: nil).delete_all }
# Match "" => nil
Individual.where(birthdate: "").update_all(birthdate: nil)
individuals_with_date = Individual.where.not(birthdate: nil)
# Match 31/12/2017 => 2017-12-31
individuals_with_date.select { |i| /^\d{2}\/\d{2}\/\d{4}$/.match(i.birthdate) }.each do |i|
puts "cleaning #{i.birthdate}"
i.update(birthdate: Date.parse(i.birthdate).iso8601) rescue nil
end
# Match 31/12/17 => 2017-12-31
individuals_with_date.select { |i| /^\d{2}\/\d{2}\/\d{2}$/.match(i.birthdate) }.each do |i|
puts "cleaning #{i.birthdate}"
new_date = Date.strptime(i.birthdate, "%d/%m/%y")
if new_date.year > 2017
new_date = new_date - 100.year
end
i.update(birthdate: new_date.iso8601)
end
end
end

View file

@ -2,32 +2,38 @@ require 'spec_helper'
describe Backoffice::AvisController, type: :controller do
describe '#POST create' do
let(:claimant){ create(:gestionnaire) }
let(:gestionnaire){ create(:gestionnaire) }
let!(:dossier){ create(:dossier, state: 'received') }
let!(:assign_to){ create(:assign_to, gestionnaire: gestionnaire, procedure: dossier.procedure )}
let!(:dossier) do
dossier = create(:dossier, state: 'received')
claimant.procedures << [dossier.procedure]
dossier
end
subject { post :create, params: { dossier_id: dossier.id, avis: { email: gestionnaire.email, introduction: "Bonjour, regardez ce joli dossier." } } }
subject do
post :create, params: { dossier_id: dossier.id, avis: { email: gestionnaire.email, introduction: "Bonjour, regardez ce joli dossier." } }
end
context 'when gestionnaire is not authenticated' do
it { is_expected.to redirect_to new_user_session_path }
it { expect{ subject }.to_not change(Avis, :count) }
before { subject }
it { expect(response).to redirect_to new_user_session_path }
it { expect(Avis.count).to eq(0) }
end
context 'when gestionnaire is authenticated' do
let(:created_avis) { Avis.last }
before do
sign_in gestionnaire
sign_in claimant
subject
end
context 'When gestionnaire is known' do
it { is_expected.to redirect_to backoffice_dossier_path(dossier.id) }
it { expect{ subject }.to change(Avis, :count).by(1) }
it do
subject
expect(gestionnaire.avis.last).to_not eq(nil)
expect(gestionnaire.avis.last.email).to eq(nil)
expect(gestionnaire.avis.last.dossier_id).to eq(dossier.id)
end
end
it { expect(response).to redirect_to backoffice_dossier_path(dossier.id) }
it { expect(Avis.count).to eq(1) }
it { expect(created_avis.dossier_id).to eq(dossier.id) }
it { expect(created_avis.gestionnaire).to eq(gestionnaire) }
it { expect(created_avis.claimant).to eq(claimant) }
end
end

View file

@ -8,7 +8,7 @@ describe StatsController, type: :controller do
FactoryGirl.create(:procedure, :created_at => 2.months.ago, :updated_at => 62.days.ago)
FactoryGirl.create(:procedure, :created_at => 2.months.ago, :updated_at => 62.days.ago)
FactoryGirl.create(:procedure, :created_at => 2.months.ago, :updated_at => 31.days.ago)
FactoryGirl.create(:procedure, :created_at => 2.months.ago, :updated_at => 1.day.ago)
FactoryGirl.create(:procedure, :created_at => 2.months.ago, :updated_at => Time.now)
@controller = StatsController.new
allow(@controller).to receive(:administration_signed_in?).and_return(false)
@ -18,7 +18,7 @@ describe StatsController, type: :controller do
subject { @controller.send(:last_four_months_hash, association, :updated_at) }
it { expect(subject).to eq([
it { expect(subject).to match_array([
[I18n.l(62.days.ago.beginning_of_month, format: "%B %Y"), 2],
[I18n.l(31.days.ago.beginning_of_month, format: "%B %Y"), 1]
])

View file

@ -358,7 +358,9 @@ describe Users::DossiersController, type: :controller do
end
describe 'PUT #update' do
let(:params) { {id: dossier_id, dossier: {id: dossier_id, autorisation_donnees: autorisation_donnees}} }
let(:params) { { id: dossier_id, dossier: { id: dossier_id, autorisation_donnees: autorisation_donnees, individual_attributes: individual_params } } }
let(:individual_params) { { gender: 'M.', nom: 'Julien', prenom: 'Xavier', birthdate: birthdate } }
let(:birthdate) { '20/01/1991' }
subject { put :update, params: params }
before do
@ -367,8 +369,7 @@ describe Users::DossiersController, type: :controller do
end
context 'when procedure is for individual' do
let(:params) { {id: dossier_id, dossier: {id: dossier_id, autorisation_donnees: '1', individual_attributes: individual_params}} }
let(:individual_params) { {gender: 'M.', nom: 'Julien', prenom: 'Xavier', birthdate: '20/01/1991'} }
let(:autorisation_donnees) { "1" }
let(:procedure) { create(:procedure, :published, for_individual: true) }
before do
@ -378,8 +379,16 @@ describe Users::DossiersController, type: :controller do
it { expect(dossier.individual.gender).to eq 'M.' }
it { expect(dossier.individual.nom).to eq 'Julien' }
it { expect(dossier.individual.prenom).to eq 'Xavier' }
it { expect(dossier.individual.birthdate).to eq '20/01/1991' }
it { expect(dossier.individual.birthdate).to eq '1991-01-20' }
it { expect(dossier.procedure.for_individual).to eq true }
context "and birthdate is ISO (YYYY-MM-DD) formatted" do
let(:birthdate) { "1991-11-01" }
before do
dossier.reload
end
it { expect(dossier.individual.birthdate).to eq '1991-11-01' }
end
end
context 'when Checkbox is checked' do
@ -411,7 +420,7 @@ describe Users::DossiersController, type: :controller do
context 'when Checkbox is not checked' do
let(:autorisation_donnees) { '0' }
it 'uses flash alert to display message' do
expect(flash[:alert]).to have_content('Les conditions sont obligatoires.')
expect(flash[:alert]).to have_content('La validation des conditions d\'utilisation est obligatoire')
end
it "doesn't update dossier autorisation_donnees" do

View file

@ -3,6 +3,6 @@ FactoryGirl.define do
gender 'M.'
nom 'Julien'
prenom 'Xavier'
birthdate '20/01/1991'
birthdate '1991-11-01'
end
end

View file

@ -25,7 +25,7 @@ feature 'As a User I wanna create a dossier' do
page.find_by_id('etape_suivante').click
fill_in "champs_#{procedure_for_individual.dossiers.last.champs.first.id}", with: 'contenu du champ 1'
page.find_by_id('suivant').click
expect(user.dossiers.first.individual.birthdate).to eq("14/10/1987")
expect(user.dossiers.first.individual.birthdate).to eq("1987-10-14")
expect(page).to have_current_path(users_dossier_recapitulatif_path(procedure_for_individual.dossiers.last.id.to_s), only_path: true)
end
@ -36,7 +36,7 @@ feature 'As a User I wanna create a dossier' do
page.find_by_id('etape_suivante').click
fill_in "champs_#{procedure_for_individual.dossiers.last.champs.first.id}", with: 'contenu du champ 1'
page.find_by_id('suivant').click
expect(user.dossiers.first.individual.birthdate).to eq("14/10/1987")
expect(user.dossiers.first.individual.birthdate).to eq("1987-10-14")
expect(page).to have_current_path(users_dossier_recapitulatif_path(procedure_for_individual.dossiers.last.id.to_s), only_path: true)
end
end

View file

@ -86,4 +86,35 @@ RSpec.describe Avis, type: :model do
end
end
end
describe '#notify_gestionnaire' do
context 'when an avis is created' do
before do
avis_invitation_double = double('avis_invitation', deliver_now: true)
allow(AvisMailer).to receive(:avis_invitation).and_return(avis_invitation_double)
Avis.create(claimant: claimant, email: 'email@l.com')
end
it { expect(AvisMailer).to have_received(:avis_invitation) }
end
end
describe '#try_to_assign_gestionnaire' do
let!(:gestionnaire) { create(:gestionnaire) }
let(:avis) { Avis.create(claimant: claimant, email: email, dossier: create(:dossier)) }
context 'when the email belongs to a gestionnaire' do
let(:email) { gestionnaire.email }
it { expect(avis.gestionnaire).to eq(gestionnaire) }
it { expect(avis.email).to be_nil }
end
context 'when the email does not belongs to a gestionnaire' do
let(:email) { 'unknown@email' }
it { expect(avis.gestionnaire).to be_nil }
it { expect(avis.email).to eq(email) }
end
end
end