diff --git a/app/models/instructeur.rb b/app/models/instructeur.rb index fdea0cc7b..d580694ae 100644 --- a/app/models/instructeur.rb +++ b/app/models/instructeur.rb @@ -152,6 +152,8 @@ class Instructeur < ApplicationRecord h = { nb_en_construction: groupe.dossiers.en_construction.count, + nb_en_instruction: groupe.dossiers.en_instruction.count, + nb_accepted: groupe.dossiers.accepte.where(processed_at: Time.zone.yesterday.beginning_of_day..Time.zone.yesterday.end_of_day).count, nb_notification: notifications_for_procedure(procedure, :not_archived).count } @@ -161,6 +163,14 @@ class Instructeur < ApplicationRecord acc << h end + [["en_instruction", h[:nb_en_instruction]], ["accepte", h[:nb_accepted]]].each do |state, count| + if procedure.declarative_with_state == state && count > 0 + h[:procedure_id] = procedure.id + h[:procedure_libelle] = procedure.libelle + acc << h + end + end + acc end end diff --git a/app/views/instructeur_mailer/send_notifications.html.haml b/app/views/instructeur_mailer/send_notifications.html.haml index 50f76c55f..04a44c9fe 100644 --- a/app/views/instructeur_mailer/send_notifications.html.haml +++ b/app/views/instructeur_mailer/send_notifications.html.haml @@ -16,5 +16,12 @@ - if datum[:nb_notification] > 0 %br #{datum[:nb_notification]} #{'notification'.pluralize(datum[:nb_notification])} + - if datum[:nb_en_instruction] > 0 + %br + #{datum[:nb_en_instruction]} #{'dossier'.pluralize(datum[:nb_en_instruction])} + - if datum[:nb_accepted] > 0 + %br + #{datum[:nb_accepted]} #{'dossier'.pluralize(datum[:nb_accepted])} + = render partial: "layouts/mailers/signature" diff --git a/spec/models/instructeur_spec.rb b/spec/models/instructeur_spec.rb index 8e205cce8..e6de354c2 100644 --- a/spec/models/instructeur_spec.rb +++ b/spec/models/instructeur_spec.rb @@ -401,6 +401,8 @@ describe Instructeur, type: :model do expect(instructeur.email_notification_data).to eq([ { nb_en_construction: 1, + nb_en_instruction: 0, + nb_accepted: 0, nb_notification: 0, procedure_id: procedure_to_assign.id, procedure_libelle: procedure_to_assign.libelle @@ -420,6 +422,8 @@ describe Instructeur, type: :model do expect(instructeur.email_notification_data).to eq([ { nb_en_construction: 0, + nb_en_instruction: 0, + nb_accepted: 0, nb_notification: 3, procedure_id: procedure_to_assign.id, procedure_libelle: procedure_to_assign.libelle @@ -428,6 +432,75 @@ describe Instructeur, type: :model do end end + context 'when a declarated dossier in instruction exists' do + let!(:dossier) { create(:dossier, procedure: procedure_to_assign, state: Dossier.states.fetch(:en_construction)) } + + before do + procedure_to_assign.update(declarative_with_state: "en_instruction") + DeclarativeProceduresJob.new.perform + dossier.reload + end + + it { expect(procedure_to_assign.declarative_with_state).to eq("en_instruction") } + it { expect(dossier.state).to eq("en_instruction") } + it do + expect(instructeur.email_notification_data).to eq([ + { + nb_en_construction: 0, + nb_en_instruction: 1, + nb_accepted: 0, + nb_notification: 0, + procedure_id: procedure_to_assign.id, + procedure_libelle: procedure_to_assign.libelle + } + ]) + end + end + + context 'when a declarated dossier in accepte processed at today exists' do + let!(:dossier) { create(:dossier, procedure: procedure_to_assign, state: Dossier.states.fetch(:en_construction)) } + + before do + procedure_to_assign.update(declarative_with_state: "accepte") + DeclarativeProceduresJob.new.perform + dossier.reload + end + + it { expect(procedure_to_assign.declarative_with_state).to eq("accepte") } + it { expect(dossier.state).to eq("accepte") } + + it do + expect(instructeur.email_notification_data).to eq([]) + end + end + + context 'when a declarated dossier in accepte processed at yesterday exists' do + let!(:dossier) { create(:dossier, procedure: procedure_to_assign, state: Dossier.states.fetch(:en_construction)) } + + before do + procedure_to_assign.update(declarative_with_state: "accepte") + DeclarativeProceduresJob.new.perform + dossier.update(processed_at: Time.zone.yesterday.beginning_of_day) + dossier.reload + end + + it { expect(procedure_to_assign.declarative_with_state).to eq("accepte") } + it { expect(dossier.state).to eq("accepte") } + + it do + expect(instructeur.email_notification_data).to eq([ + { + nb_en_construction: 0, + nb_en_instruction: 0, + nb_accepted: 1, + nb_notification: 0, + procedure_id: procedure_to_assign.id, + procedure_libelle: procedure_to_assign.libelle + } + ]) + end + end + context 'otherwise' do it { expect(instructeur.email_notification_data).to eq([]) } end diff --git a/spec/services/notification_service_spec.rb b/spec/services/notification_service_spec.rb index e5db1502c..9cb1952ef 100644 --- a/spec/services/notification_service_spec.rb +++ b/spec/services/notification_service_spec.rb @@ -47,6 +47,49 @@ describe NotificationService do end end + context 'when a declarative dossier in instruction exists on this procedure' do + let!(:dossier) { create(:dossier, :en_construction, procedure: procedure) } + before do + procedure.update(declarative_with_state: "en_instruction") + DeclarativeProceduresJob.new.perform + dossier.reload + end + + it do + subject + expect(InstructeurMailer).to have_received(:send_notifications) + end + end + + context 'when a declarative dossier in accepte on yesterday exists on this procedure' do + let!(:dossier) { create(:dossier, :en_construction, procedure: procedure) } + before do + procedure.update(declarative_with_state: "accepte") + DeclarativeProceduresJob.new.perform + dossier.update(processed_at: Time.zone.yesterday.beginning_of_day) + dossier.reload + end + + it do + subject + expect(InstructeurMailer).to have_received(:send_notifications) + end + end + + context 'when a declarative dossier in accepte on today exists on this procedure' do + let!(:dossier) { create(:dossier, :en_construction, procedure: procedure) } + before do + procedure.update(declarative_with_state: "accepte") + DeclarativeProceduresJob.new.perform + dossier.reload + end + + it do + subject + expect(InstructeurMailer).not_to have_received(:send_notifications) + end + end + context 'when there is a notification on this procedure' do before do allow_any_instance_of(Instructeur).to receive(:notifications_for_procedure) diff --git a/spec/views/instructeur_mailer/send_notifications.html.haml_spec.rb b/spec/views/instructeur_mailer/send_notifications.html.haml_spec.rb index 0c8be4c27..82575aae8 100644 --- a/spec/views/instructeur_mailer/send_notifications.html.haml_spec.rb +++ b/spec/views/instructeur_mailer/send_notifications.html.haml_spec.rb @@ -16,6 +16,8 @@ describe 'instructeur_mailer/send_notifications.html.haml', type: :view do procedure_libelle: 'une superbe démarche', procedure_id: 213, nb_en_construction: 1, + nb_en_instruction: 0, + nb_accepted: 0, nb_notification: 0 } ] @@ -27,6 +29,50 @@ describe 'instructeur_mailer/send_notifications.html.haml', type: :view do it { expect(rendered).not_to have_text('notification') } end + context 'when there is one declarated dossier in instruction' do + let(:data) do + [ + { + procedure_libelle: 'une superbe démarche', + procedure_id: 213, + nb_en_construction: 0, + nb_en_instruction: 1, + nb_accepted: 0, + nb_notification: 0 + } + ] + end + + it { expect(rendered).to have_link('une superbe démarche', href: procedure_url(213)) } + it { expect(rendered).to have_text('une superbe démarche') } + it { expect(rendered).to have_text('1 dossier') } + it { expect(rendered).not_to have_text('notification') } + it { expect(rendered).not_to have_text('construction') } + it { expect(rendered).not_to have_text('accepte') } + end + + context 'when there is one declarated dossier in accepte' do + let(:data) do + [ + { + procedure_libelle: 'une superbe démarche', + procedure_id: 213, + nb_en_construction: 0, + nb_en_instruction: 0, + nb_accepted: 1, + nb_notification: 0 + } + ] + end + + it { expect(rendered).to have_link('une superbe démarche', href: procedure_url(213)) } + it { expect(rendered).to have_text('une superbe démarche') } + it { expect(rendered).to have_text('1 dossier') } + it { expect(rendered).not_to have_text('notification') } + it { expect(rendered).not_to have_text('construction') } + it { expect(rendered).not_to have_text('instruction') } + end + context 'when there is one notification' do let(:data) do [ @@ -34,6 +80,8 @@ describe 'instructeur_mailer/send_notifications.html.haml', type: :view do procedure_libelle: 'une superbe démarche', procedure_id: 213, nb_en_construction: 0, + nb_en_instruction: 0, + nb_accepted: 0, nb_notification: 1 } ]