From d52529c84038440265d577e342042cc82ecdaf7a Mon Sep 17 00:00:00 2001 From: Georg Gadinger Date: Sat, 10 Dec 2022 15:56:07 +0100 Subject: [PATCH] mark dataexported notifications as read when visiting export page --- app/controllers/settings/export_controller.rb | 9 +++++++++ .../settings/export_controller_spec.rb | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/app/controllers/settings/export_controller.rb b/app/controllers/settings/export_controller.rb index 5656e614..1a8bc0a2 100644 --- a/app/controllers/settings/export_controller.rb +++ b/app/controllers/settings/export_controller.rb @@ -2,6 +2,7 @@ class Settings::ExportController < ApplicationController before_action :authenticate_user! + before_action :mark_notifications_as_read, only: %i[index] def index flash[:info] = t(".info") if current_user.export_processing @@ -17,4 +18,12 @@ class Settings::ExportController < ApplicationController redirect_to settings_export_path end + + private + + def mark_notifications_as_read + Notification::DataExported + .where(recipient: current_user, new: true) + .update_all(new: false) # rubocop:disable Rails/SkipsModelValidations + end end diff --git a/spec/controllers/settings/export_controller_spec.rb b/spec/controllers/settings/export_controller_spec.rb index 97cdae63..0750ed6e 100644 --- a/spec/controllers/settings/export_controller_spec.rb +++ b/spec/controllers/settings/export_controller_spec.rb @@ -15,6 +15,21 @@ describe Settings::ExportController, type: :controller do subject expect(response).to render_template(:index) end + + context "when user has a new DataExported notification" do + let(:notification) do + Notification::DataExported.create( + target_id: user.id, + target_type: "User::DataExport", + recipient: user, + new: true + ) + end + + it "marks the notification as read" do + expect { subject }.to change { notification.reload.new }.from(true).to(false) + end + end end end