Administrateur can be assign accompagnateur on each of his procedure.

This commit is contained in:
Xavier J 2016-05-24 16:39:39 +02:00
parent 1923f2aec1
commit 1fcb33104d
19 changed files with 385 additions and 74 deletions

View file

@ -31,6 +31,7 @@
//= require bootstrap-wysihtml5/locales/fr-FR
$(document).on('page:load', scroll_to);
$(document).ready(scroll_to);

View file

@ -1,11 +1,30 @@
class Admin::AccompagnateursController < AdminController
include SmartListing::Helper::ControllerExtensions
helper SmartListing::Helper
before_action :retrieve_procedure
def show
assign_scope = @procedure.gestionnaires
@accompagnateurs_assign = smart_listing_create :accompagnateurs_assign,
assign_scope,
partial: "admin/accompagnateurs/list_assign",
array: true
not_assign_scope = current_administrateur.gestionnaires.where.not(id: assign_scope.ids)
not_assign_scope = not_assign_scope.where("email LIKE '%#{params[:filter]}%'") if params[:filter]
@accompagnateurs_not_assign = smart_listing_create :accompagnateurs_not_assign,
not_assign_scope,
partial: "admin/accompagnateurs/list_not_assign",
array: true
end
def update
AccompagnateurService.change_assignement! Gestionnaire.find(params[:accompagnateur_id]), Procedure.find(params[:procedure_id]), params[:to]
flash.notice = "Assignement effectué"
redirect_to admin_procedure_accompagnateurs_path, procedure_id: params[:procedure_id]
end
end

View file

@ -12,23 +12,19 @@ class Admin::GestionnairesController < AdminController
def create
@gestionnaire = Gestionnaire.create(create_gestionnaire_params)
@gestionnaire = Gestionnaire.find_by_email(params[:gestionnaire][:email])
if @gestionnaire.errors.messages.empty?
@gestionnaire.administrateurs.push current_administrateur
flash.notice = 'Gestionnaire ajouté'
GestionnaireMailer.new_gestionnaire(@gestionnaire.email, @gestionnaire.password).deliver_now!
if @gestionnaire.nil?
new_gestionnaire!
else
flash.alert = @gestionnaire.errors.full_messages.join('<br />').html_safe
assign_gestionnaire!
end
redirect_to admin_gestionnaires_path
end
def destroy
Gestionnaire.find(params[:id]).destroy
Gestionnaire.find(params[:id]).administrateurs.delete current_administrateur
redirect_to admin_gestionnaires_path
end
@ -36,5 +32,31 @@ class Admin::GestionnairesController < AdminController
def create_gestionnaire_params
params.require(:gestionnaire).permit(:email)
.merge(password: SecureRandom.hex(5))
.merge(administrateurs: [current_administrateur])
end
private
def new_gestionnaire!
@gestionnaire = Gestionnaire.create(create_gestionnaire_params)
if @gestionnaire.errors.messages.empty?
flash.notice = 'Accompagnateur ajouté'
GestionnaireMailer.new_gestionnaire(@gestionnaire.email, @gestionnaire.password).deliver_now!
GestionnaireMailer.new_assignement(@gestionnaire.email, current_administrateur.email).deliver_now!
else
flash.alert = @gestionnaire.errors.full_messages.join('<br />').html_safe
end
end
def assign_gestionnaire!
if current_administrateur.gestionnaires.include? @gestionnaire
flash.alert = 'Accompagnateur déjà ajouté'
else
GestionnaireMailer.new_assignement(@gestionnaire.email, current_administrateur.email).deliver_now!
@gestionnaire.administrateurs.push current_administrateur
flash.notice = 'Accompagnateur ajouté'
#TODO Mailer no assign_to
end
end
end

View file

@ -4,15 +4,19 @@ class GestionnaireMailer < ApplicationMailer
send_mail email, password, "Vous avez été nommé accompagnateur sur la plateforme TPS"
end
def new_assignement email, email_admin
send_mail email, email_admin, "Vous avez été assigné à un nouvel administrateur sur la plateforme TPS"
end
private
def vars_mailer email, password
@password = password
def vars_mailer email, args
@args = args
@email = email
end
def send_mail email, password, subject
vars_mailer email, password
def send_mail email, args, subject
vars_mailer email, args
mail(from: "tps@apientreprise.fr", to: email,
subject: subject)

View file

@ -0,0 +1,12 @@
class AccompagnateurService
ASSIGN = 'assign'
NOT_ASSIGN = 'not_assign'
def self.change_assignement! accompagnateur, procedure, to
if to == ASSIGN
AssignTo.create(gestionnaire: accompagnateur, procedure: procedure)
elsif to == NOT_ASSIGN
AssignTo.delete_all(gestionnaire: accompagnateur, procedure: procedure)
end
end
end

View file

@ -0,0 +1,20 @@
.row{style:'height: 34px'}
- unless smart_listing.empty?
%table.table#liste_gestionnaire
%thead
%th Enlever
%th#email{style:'text-align: right'} Email
- @accompagnateurs_assign.each do |accompagnateur|
%tr
%td.col-md-1.col-lg-1.center
%a.btn.btn-primary{href: "#{admin_procedure_accompagnateurs_path(procedure_id: @procedure.id, accompagnateur_id: accompagnateur.id, to: AccompagnateurService::NOT_ASSIGN)}", 'data-method' => 'put'}
.fa.fa-arrow-left
%td{style:'padding-top: 11px; font-size:15px; text-align:right'}= accompagnateur.email
= smart_listing.paginate
= smart_listing.pagination_per_page_links
- else
%h4.center
Aucun d'affecté

View file

@ -0,0 +1,27 @@
= smart_listing_controls_for(:accompagnateurs_not_assign, {class: "form-inline text-right"}) do
.form-group.filter.input-append
= text_field_tag :filter, '', class: "search form-control",
placeholder: "Recherche...", autocomplete: :off
%button.btn.btn-primary{type: :submit}
%span.glyphicon.glyphicon-search
- unless smart_listing.empty?
%table.table#liste_gestionnaire
%thead
%th#email Email
%th Ajouter
- @accompagnateurs_not_assign.each do |accompagnateur|
%tr
%td.col-md-11.col-lg-11{style:'padding-top: 11px; font-size:15px'}= accompagnateur.email
%td.center
%a.btn.btn-success{href: "#{admin_procedure_accompagnateurs_path(procedure_id: @procedure.id, accompagnateur_id: accompagnateur.id, to: AccompagnateurService::ASSIGN)}", 'data-method' => 'put'}
.fa.fa-arrow-right
= smart_listing.paginate
= smart_listing.pagination_per_page_links
- else
%h4.center
Aucun de disponible

View file

@ -6,4 +6,14 @@
= render partial: 'admin/procedures/navbar', locals: {active: 'Accompagnateurs'}
#accompagnateur_form
plop
.row
.col-md-6.col-lg-6
%h3.text-info Disponibles
= smart_listing_render :accompagnateurs_not_assign
.col-md-6.col-lg-6
%h3.text-success Affectés
= smart_listing_render :accompagnateurs_assign

View file

@ -0,0 +1,3 @@
<%= smart_listing_update :accompagnateurs_not_assign %>
<%= smart_listing_update :accompagnateurs_assign %>

View file

@ -0,0 +1,11 @@
Bienvenue sur la plateforme TPS
Vous venez d'être assigné à un administrateur sur la plateforme TPS. Voici quelques informations utiles :
URL : https://tps.apientreprise.fr/gestionnaires/sign_in
Email administrateur : <%= @args %>
Bonne journée,
---
L'équipe TPS - tps@apientreprise.fr

View file

@ -2,9 +2,9 @@ Bienvenue sur la plateforme TPS
Vous venez d'être nommé accompagnateur sur la plateforme TPS. Pour mémoire, voici quelques informations utiles :
URL : https://tps.apientreprise.fr/gestionnaires/sign_in
URL : <%= new_gestionnaire_session_url %>
Login : <%= @email %>
Mot de passe : <%= @password %>
Mot de passe : <%= @args %>
Bonne journée,

View file

@ -91,11 +91,15 @@ Rails.application.routes.draw do
resource :accompagnateurs, only: [:show, :update]
resources :types_de_champ, only: [:destroy]
resource :pieces_justificatives, only: [:show, :update]
resources :pieces_justificatives, only: :destroy
end
namespace :accompagnateurs do
get 'show' #delete after fixed tests admin/accompagnateurs/show_spec without this line
end
resources :gestionnaires, only: [:index, :create, :destroy]
end

View file

@ -0,0 +1,5 @@
class AddForeignKeyAdmnistrateurGestionnaire < ActiveRecord::Migration
def change
add_index :administrateurs_gestionnaires, [:gestionnaire_id, :administrateur_id], unique: true, name: 'unique_couple_administrateur_gestionnaire'
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160519101018) do
ActiveRecord::Schema.define(version: 20160523163054) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -41,6 +41,7 @@ ActiveRecord::Schema.define(version: 20160519101018) do
end
add_index "administrateurs_gestionnaires", ["administrateur_id"], name: "index_administrateurs_gestionnaires_on_administrateur_id", using: :btree
add_index "administrateurs_gestionnaires", ["gestionnaire_id", "administrateur_id"], name: "unique_couple_administrateur_gestionnaire", unique: true, using: :btree
add_index "administrateurs_gestionnaires", ["gestionnaire_id"], name: "index_administrateurs_gestionnaires_on_gestionnaire_id", using: :btree
create_table "administrations", force: :cascade do |t|

View file

@ -0,0 +1,30 @@
require 'spec_helper'
describe Admin::AccompagnateursController, type: :controller do
let(:admin) { create(:administrateur) }
let(:procedure) { create :procedure, administrateur: admin }
let(:gestionnaire) { create :gestionnaire, administrateurs: [admin] }
before do
sign_in admin
end
describe 'GET #show' do
subject { get :show, procedure_id: procedure.id }
it { expect(subject.status).to eq(200) }
end
describe 'PUT #update' do
subject { put :update, accompagnateur_id: gestionnaire.id ,procedure_id: procedure.id }
it { expect(subject).to redirect_to admin_procedure_accompagnateurs_path(procedure_id: procedure.id) }
context 'when assignement is valid' do
before do
subject
end
it { expect(flash[:notice]).to be_present }
end
end
end

View file

@ -2,6 +2,9 @@ require 'spec_helper'
describe Admin::GestionnairesController, type: :controller do
let(:admin) { create(:administrateur) }
let(:email_2) { 'plip@octo.com' }
let(:admin_2) { create :administrateur, email: email_2 }
before do
sign_in admin
end
@ -42,6 +45,15 @@ describe Admin::GestionnairesController, type: :controller do
let(:email) { 'piou' }
it { expect(response.status).to eq(302) }
it { expect { response }.not_to change(Gestionnaire, :count) }
it { expect(flash[:alert]).to be_present }
describe 'Email Notification' do
it {
expect(GestionnaireMailer).not_to receive(:new_gestionnaire)
expect(GestionnaireMailer).not_to receive(:deliver_now!)
subject
}
end
end
context 'when email is empty' do
@ -51,65 +63,103 @@ describe Admin::GestionnairesController, type: :controller do
let(:email) { '' }
it { expect(response.status).to eq(302) }
it { expect { response }.not_to change(Gestionnaire, :count) }
it 'Notification email is not send' do
expect(GestionnaireMailer).not_to receive(:new_gestionnaire)
expect(GestionnaireMailer).not_to receive(:deliver_now!)
end
end
context ' when email already exists' do
let(:email) { 'test@plop.com' }
context 'when email is already assign at the admin' do
before do
create :gestionnaire, email: email, administrateurs: [admin]
subject
post :create, gestionnaire: { email: email }
end
it { expect(response.status).to eq(302) }
it { expect { response }.not_to change(Gestionnaire, :count) }
it { expect(flash[:alert]).to be_present }
describe 'Email notification' do
it 'is not sent when email already exists' do
expect(GestionnaireMailer).not_to receive(:new_gestionnaire)
expect(GestionnaireMailer).not_to receive(:deliver_now!)
subject
end
end
end
context 'when an other admin will add the same email' do
let(:gestionnaire) { Gestionnaire.find_by_email(email) }
before do
create :gestionnaire, email: email, administrateurs: [admin]
sign_out admin
sign_in admin_2
subject
end
it { expect(response.status).to eq(302) }
it { expect { response }.not_to change(Gestionnaire, :count) }
it { expect(flash[:notice]).to be_present }
it { expect(admin_2.gestionnaires).to include gestionnaire }
it { expect(gestionnaire.administrateurs.size).to eq 2 }
end
context 'Email notification' do
it 'Notification email is sent when email is valid' do
it 'Notification email is sent when accompagnateur is create' do
expect(GestionnaireMailer).to receive(:new_gestionnaire).and_return(GestionnaireMailer)
expect(GestionnaireMailer).to receive(:deliver_now!)
subject
end
context 'is not sent when email is not valid' do
let(:email) { 'testplop.com' }
it 'Notification email is sent when accompagnateur is assign' do
expect(GestionnaireMailer).to receive(:new_assignement).and_return(GestionnaireMailer)
expect(GestionnaireMailer).to receive(:deliver_now!)
subject
end
context 'when accompagnateur is assign at a new admin' do
before do
create :gestionnaire, email: email, administrateurs: [admin]
sign_out admin
sign_in admin_2
end
it {
expect(GestionnaireMailer).not_to receive(:new_gestionnaire)
expect(GestionnaireMailer).not_to receive(:deliver_now!)
expect(GestionnaireMailer).to receive(:new_assignement).and_return(GestionnaireMailer)
expect(GestionnaireMailer).to receive(:deliver_now!)
subject
}
end
it 'is not sent when email already exists' do
subject
expect(GestionnaireMailer).not_to receive(:new_gestionnaire)
expect(GestionnaireMailer).not_to receive(:deliver_now!)
subject
end
end
end
describe 'DELETE #destroy' do
let(:email) { 'test@plop.com' }
let!(:gestionnaire) { create :gestionnaire, email: email }
let!(:admin) { create :administrateur }
let!(:gestionnaire) { create :gestionnaire, email: email, administrateurs: [admin] }
subject { delete :destroy, id: gestionnaire.id }
context "when gestionaire_id is valid" do
before do
subject
admin.reload
gestionnaire.reload
end
it { expect(response.status).to eq(302) }
it { expect(response).to redirect_to admin_gestionnaires_path }
it { expect{Gestionnaire.find(gestionnaire.id)}.to raise_error ActiveRecord::RecordNotFound}
it { expect(admin.gestionnaires).not_to include gestionnaire }
it { expect(gestionnaire.administrateurs).not_to include admin }
end
it { expect{subject}.to change(Gestionnaire, :count).by(-1) }
it { expect { subject }.not_to change(Gestionnaire, :count) }
end
end

View file

@ -0,0 +1,32 @@
require 'spec_helper'
describe AccompagnateurService do
describe '#change_assignement!' do
let(:procedure) { create :procedure }
let(:accompagnateur) { create :gestionnaire }
subject { AccompagnateurService.change_assignement! accompagnateur, procedure, to }
context 'when accompagnateur is not assign at the procedure' do
let(:to) { AccompagnateurService::ASSIGN }
before do
subject
end
it { expect(accompagnateur.procedures).to include procedure }
end
context 'when accompagnateur is assign at the procedure' do
let(:to) { AccompagnateurService::NOT_ASSIGN }
before do
create :assign_to, gestionnaire: accompagnateur, procedure: procedure
subject
end
it { expect(accompagnateur.procedures).not_to include procedure }
end
end
end

View file

@ -0,0 +1,60 @@
require 'spec_helper'
describe 'admin/accompagnateurs/show.html.haml', type: :view do
let(:admin) { create(:administrateur) }
let(:procedure) { create :procedure, administrateur: admin }
let(:assign_gestionnaires) { procedure.gestionnaires }
let(:not_assign_gestionnaires) { admin.gestionnaires.where.not(id: assign_gestionnaires.ids) }
before do
assign(:procedure, procedure)
assign(:accompagnateurs_assign, (smart_listing_create :accompagnateurs_assign,
assign_gestionnaires,
partial: "admin/accompagnateurs/list_assign",
array: true))
assign(:accompagnateurs_not_assign, (smart_listing_create :accompagnateurs_not_assign,
not_assign_gestionnaires,
partial: "admin/accompagnateurs/list_not_assign",
array: true))
end
context 'when admin have none accompagnateur ' do
before do
render
end
it { expect(rendered).to have_content('Aucun de disponible') }
context 'when administrateur have none accompagnateur assign' do
it { expect(rendered).to have_content('Aucun d\'affecté') }
end
end
context 'when administrateur have two accompagnateur' do
let!(:accompagnateur_1) { create :gestionnaire, email: 'plop@plop.com', administrateurs: [admin] }
let!(:accompagnateur_2) { create :gestionnaire, email: 'plip@plop.com', administrateurs: [admin] }
before do
not_assign_gestionnaires.reload
assign_gestionnaires.reload
assign(:accompagnateurs_assign, (smart_listing_create :accompagnateurs_assign,
assign_gestionnaires,
partial: "admin/accompagnateurs/list_assign",
array: true))
assign(:accompagnateurs_not_assign, (smart_listing_create :accompagnateurs_not_assign,
not_assign_gestionnaires,
partial: "admin/accompagnateurs/list_not_assign",
array: true))
render
end
it { expect(rendered).to have_content(accompagnateur_1.email) }
it { expect(rendered).to have_content(accompagnateur_2.email) }
end
end