Avis: move assign_gestionnaire logic to avis model

This commit is contained in:
Simon Lehericey 2017-07-20 16:30:11 +02:00
parent fe1b97b738
commit 7016bdc49a
4 changed files with 52 additions and 26 deletions

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

@ -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) }
@ -28,4 +29,12 @@ class Avis < ApplicationRecord
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

@ -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

@ -98,4 +98,23 @@ RSpec.describe Avis, type: :model do
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