Merge pull request #620 from sgmap/rearrange_avis_logic

Rearrange avis logic
This commit is contained in:
LeSim 2017-07-21 11:32:43 +02:00 committed by GitHub
commit 0fef38201c
4 changed files with 70 additions and 30 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) }
@ -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

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

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