From a867c9a998419debb0b14f84a7b283cb47c851bd Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Mon, 18 Sep 2023 12:43:14 +0200 Subject: [PATCH] feat(instructeurs): notification badge when a new export has been generated Co-Authored-By: Lisa Durand --- .../instructeurs/procedures_controller.rb | 23 ++++++++++ .../instructeurs/procedures/_header.html.haml | 2 + .../locales/views/instructeurs/header/en.yml | 1 + .../locales/views/instructeurs/header/fr.yml | 1 + .../procedures_controller_spec.rb | 42 +++++++++++++++++++ 5 files changed, 69 insertions(+) diff --git a/app/controllers/instructeurs/procedures_controller.rb b/app/controllers/instructeurs/procedures_controller.rb index 69d37c755..322c8ff1c 100644 --- a/app/controllers/instructeurs/procedures_controller.rb +++ b/app/controllers/instructeurs/procedures_controller.rb @@ -90,6 +90,8 @@ module Instructeurs @has_termine_notifications = notifications[:termines].present? @not_archived_notifications_dossier_ids = notifications[:en_cours] + notifications[:termines] + @has_export_notification = notify_exports? + @filtered_sorted_ids = procedure_presentation.filtered_sorted_ids(dossiers, statut, count: dossiers_count) page = params[:page].presence || 1 @@ -225,6 +227,10 @@ module Instructeurs def exports @procedure = procedure @exports = Export.for_groupe_instructeurs(groupe_instructeur_ids).order(updated_at: :desc) + cookies.encrypted[cookies_export_key] = { + value: DateTime.current, + expires: Export::MAX_DUREE_GENERATION + Export::MAX_DUREE_CONSERVATION_EXPORT + } end def email_usagers @@ -353,5 +359,22 @@ module Instructeurs def bulk_message_params params.require(:bulk_message).permit(:body) end + + def notify_exports? + last_seen_at = begin + DateTime.parse(cookies.encrypted[cookies_export_key]) + rescue + nil + end + + scope = Export.generated.for_groupe_instructeurs(groupe_instructeur_ids) + scope = scope.where(updated_at: last_seen_at...) if last_seen_at + + scope.exists? + end + + def cookies_export_key + "exports_#{@procedure.id}_seen_at" + end end end diff --git a/app/views/instructeurs/procedures/_header.html.haml b/app/views/instructeurs/procedures/_header.html.haml index f4c81264f..986199d30 100644 --- a/app/views/instructeurs/procedures/_header.html.haml +++ b/app/views/instructeurs/procedures/_header.html.haml @@ -24,3 +24,5 @@ = link_to t('instructeurs.dossiers.header.banner.administrators_list'), administrateurs_instructeur_procedure_path(procedure), class: 'header-link' | = link_to t('instructeurs.dossiers.header.banner.exports_list'), exports_instructeur_procedure_path(procedure), class: 'header-link' + - if @has_export_notification + %span.notifications{ 'aria-label': t('instructeurs.dossiers.header.banner.exports_notification_label') } diff --git a/config/locales/views/instructeurs/header/en.yml b/config/locales/views/instructeurs/header/en.yml index dd9c5a960..dbec9594d 100644 --- a/config/locales/views/instructeurs/header/en.yml +++ b/config/locales/views/instructeurs/header/en.yml @@ -13,6 +13,7 @@ en: notification_management: notification management administrators_list: administrators list exports_list: exports list + exports_notification_label: A new export is ready to download statistics: statistics instructeurs: instructors contact_users: contact users (draft) diff --git a/config/locales/views/instructeurs/header/fr.yml b/config/locales/views/instructeurs/header/fr.yml index d7fbf0df4..9b81dfc5f 100644 --- a/config/locales/views/instructeurs/header/fr.yml +++ b/config/locales/views/instructeurs/header/fr.yml @@ -13,6 +13,7 @@ fr: notification_management: gestion des notifications administrators_list: voir les administrateurs exports_list: voir les exports + exports_notification_label: Un nouvel export est prêt à être téléchargé statistics: statistiques instructeurs: instructeurs contact_users: contacter les usagers (brouillon) diff --git a/spec/controllers/instructeurs/procedures_controller_spec.rb b/spec/controllers/instructeurs/procedures_controller_spec.rb index f5cd4bf24..ab9ee9743 100644 --- a/spec/controllers/instructeurs/procedures_controller_spec.rb +++ b/spec/controllers/instructeurs/procedures_controller_spec.rb @@ -466,6 +466,48 @@ describe Instructeurs::ProceduresController, type: :controller do it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([archived_dossier].map(&:id)) } end end + + context 'exports notification' do + context 'without generated export' do + before do + create(:export, :pending, groupe_instructeurs: [gi_2]) + + subject + end + + it { expect(assigns(:has_export_notification)).to be(false) } + end + + context 'with generated export' do + render_views + before do + create(:export, :generated, groupe_instructeurs: [gi_2], updated_at: 1.minute.ago) + + if exports_seen_at + cookies.encrypted["exports_#{procedure.id}_seen_at"] = exports_seen_at.to_datetime.to_s + end + + subject + end + + context 'without cookie' do + let(:exports_seen_at) { nil } + it { expect(assigns(:has_export_notification)).to be(true) } + end + + context 'with cookie in past' do + let(:exports_seen_at) { 1.hour.ago } + it { expect(assigns(:has_export_notification)).to be(true) } + + it { expect(response.body).to match(/Un nouvel export est prêt/) } + end + + context 'with cookie set after last generated export' do + let(:exports_seen_at) { 10.seconds.ago } + it { expect(assigns(:has_export_notification)).to be(false) } + end + end + end end end