Browse Source

CLDC-3607: Allow support users to delete notifications (by setting their end date)

pull/2630/head
Rachael Booth 2 years ago
parent
commit
c301ae4d49
  1. 16
      app/controllers/notifications_controller.rb
  2. 7
      app/helpers/notifications_helper.rb
  3. 4
      app/views/home/index.html.erb
  4. 17
      app/views/notifications/_notification_home_section.html.erb
  5. 27
      app/views/notifications/delete_confirmation.html.erb
  6. 5
      config/locales/en.yml
  7. 2
      config/routes.rb
  8. 29
      spec/requests/notifications_controller_spec.rb

16
app/controllers/notifications_controller.rb

@ -62,6 +62,22 @@ class NotificationsController < ApplicationController
end
end
def delete_confirmation
@notification = Notification.find_by(id: params[:notification_id])
render_not_found and return unless @notification
render "notifications/delete_confirmation"
end
def delete
@notification = Notification.find_by(id: params[:notification_id])
render_not_found and return unless @notification
@notification.update!(end_date: Time.zone.now)
flash[:notice] = "The notification has been deleted"
redirect_to root_path
end
private
def notification_params

7
app/helpers/notifications_helper.rb

@ -35,6 +35,13 @@ module NotificationsHelper
render_normal_markdown(content)
end
def render_for_home(notification)
return render_normal_markdown(notification.title) unless notification.show_additional_page
content = "#{notification.title} \n[#{notification.link_text}](#{notification_path(notification)})"
render_normal_markdown(content)
end
private
def render_normal_markdown(content)

4
app/views/home/index.html.erb

@ -7,9 +7,7 @@
</div>
<% if @current_user.support? %>
<div class="govuk-grid-row">
<%= govuk_button_link_to "Create a new notification", new_notification_path %>
</div>
<%= render partial: "notifications/notification_home_section" %>
<% end %>
<div class="app-data-box-section govuk-grid-row">

17
app/views/notifications/_notification_home_section.html.erb

@ -0,0 +1,17 @@
<div class="govuk-grid-row">
<p class="govuk-!-font-weight-bold"><%== I18n.t("active_notifications", count: Notification.active.count) %></p>
<% Notification.active.each do |notification| %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-three-quarters">
<%== render_for_home(notification) %>
</div>
<div class="govuk-grid-column-one-quarter">
<%= govuk_link_to("Delete notification", notification_delete_confirmation_path(notification)) %>
</div>
</div>
<% end %>
</div>
<div class="govuk-grid-row">
<%= govuk_button_link_to "Create a new notification", new_notification_path %>
</div>

27
app/views/notifications/delete_confirmation.html.erb

@ -0,0 +1,27 @@
<% content_for :before_content do %>
<% content_for :title, "Are you sure you want to delete this notification?" %>
<%= govuk_back_link(href: :back) %>
<% end %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds-from-desktop">
<h1 class="govuk-heading-xl">
<%= content_for(:title) %>
</h1>
<p class="govuk-body"><%== render_for_summary("**Notification:** #{@notification.title}") %></p>
<p class="govuk-body">Users will no longer see this notification.</p>
<%= govuk_warning_text(text: "You will not be able to undo this action.") %>
<div class="govuk-button-group">
<%= govuk_button_to(
"Delete notification",
notification_delete_path(@notification),
method: :delete,
) %>
<%= govuk_button_link_to "Cancel", root_path, html: { method: :get }, secondary: true %>
</div>
</div>
</div>

5
config/locales/en.yml

@ -41,6 +41,11 @@ en:
create_password: "Create a password to finish setting up your account"
reset_password: "Reset your password"
active_notifications:
zero: "There are no active notifications"
one: "There is one active notification:"
other: "There are %{count} active notifications:"
activemodel:
errors:
models:

2
config/routes.rb

@ -141,6 +141,8 @@ Rails.application.routes.draw do
resources :notifications do
get "dismiss", to: "notifications#dismiss"
get "check-answers", to: "notifications#check_answers"
get "delete-confirmation", to: "notifications#delete_confirmation"
delete "delete", to: "notifications#delete"
end
resources :organisations do

29
spec/requests/notifications_controller_spec.rb

@ -101,6 +101,17 @@ RSpec.describe NotificationsController, type: :request do
end
end
end
describe "#delete" do
let(:notification) { create(:notification, end_date: nil) }
let(:request) { delete notification_delete_path(notification) }
it "sets end_date on the notification" do
request
notification.reload
expect(notification.end_date).to be < Time.zone.now
end
end
end
context "when user is signed in as a non-support user" do
@ -125,12 +136,28 @@ RSpec.describe NotificationsController, type: :request do
describe "#update" do
let(:notification) { create(:notification) }
let(:request) { patch notification_path(notification), params: { "notification": { title: "Test Create" } } }
let(:request) { patch notification_path(notification), params: { "notification": { title: "Test Update" } } }
it "returns not found" do
request
expect(response).to have_http_status(:not_found)
end
end
describe "#delete" do
let(:notification) { create(:notification, end_date: nil) }
let(:request) { delete notification_delete_path(notification) }
it "returns not found" do
request
expect(response).to have_http_status(:not_found)
end
it "does not set end_date on the notification" do
request
notification.reload
expect(notification.end_date).to be_nil
end
end
end
end

Loading…
Cancel
Save