Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/controllers/notifications_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class NotificationsController < ApplicationController
before_action :set_notification, only: [ :show, :resend ]
before_action :set_notification, only: [ :show, :update, :resend ]

def index
authorize!
Expand All @@ -21,6 +21,12 @@ def show
authorize! @notification
end

def update
authorize! @notification
@notification.update!(notification_params)
head :ok
end

def resend
authorize! @notification, to: :resend?

Expand Down Expand Up @@ -53,4 +59,8 @@ def resend
def set_notification
@notification = Notification.find(params[:id])
end

def notification_params
params.require(:notification).permit(:responded)
end
end
4 changes: 4 additions & 0 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ def resendable?
!kind.in?(DEVISE_KINDS)
end

def contact_us?
kind.in?(%w[contact_us contact_us_fyi])
end

def delivered?
delivered_at.present?
end
Expand Down
4 changes: 4 additions & 0 deletions app/policies/notification_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def show?
admin? || owner?
end

def update?
admin?
end

def resend?
admin? && record.resendable?
end
Expand Down
20 changes: 20 additions & 0 deletions app/views/notifications/_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<th class="px-4 py-3 text-left font-medium text-gray-600">Recipient</th>
<th class="px-4 py-3 text-left font-medium text-gray-600">Subject</th>
<th class="px-4 py-3 text-left font-medium text-gray-600">Record</th>
<th class="px-4 py-3 text-center font-medium text-gray-600">Responded</th>
<th class="px-4 py-3 text-right font-medium text-gray-600"></th>
</tr>
</thead>
Expand Down Expand Up @@ -51,6 +52,25 @@
<% end %>
</td>

<!-- Responded -->
<td class="px-4 py-3 text-center"
data-controller="autosave"
data-autosave-url-value="<%= notification_path(notification) %>"
data-autosave-id-value="<%= notification.id %>"
data-action="change->autosave#save">
<% if notification.contact_us? %>
<input type="hidden" name="notification[responded]" value="0">
<input type="checkbox"
name="notification[responded]"
value="1"
<%= "checked" if notification.responded? %>
class="w-5 h-5 rounded border-gray-300 text-green-600 accent-green-600 focus:ring-green-500 cursor-pointer"
title="Responded to by staff">
<% else %>
<span class="text-gray-300">—</span>
<% end %>
</td>

<!-- Actions -->
<td class="px-4 py-3 text-right whitespace-nowrap">
<%= link_to "View",
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
resources :comments, only: [ :index, :create ]
end
resources :faqs
resources :notifications, only: [ :index, :show ] do
resources :notifications, only: [ :index, :show, :update ] do
member do
post :resend
end
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20260417120000_add_responded_to_notifications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddRespondedToNotifications < ActiveRecord::Migration[8.1]
def up
add_column :notifications, :responded, :boolean, default: false, null: false
end

def down
remove_column :notifications, :responded, if_exists: true
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.1].define(version: 2026_03_12_120000) do
ActiveRecord::Schema[8.1].define(version: 2026_04_17_120000) do
create_table "action_text_mentions", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.bigint "action_text_rich_text_id", null: false
t.datetime "created_at", null: false
Expand Down Expand Up @@ -614,6 +614,7 @@
t.integer "parent_notification_id"
t.string "recipient_email", null: false
t.string "recipient_role", null: false
t.boolean "responded", default: false, null: false
t.integer "root_notification_id"
t.datetime "updated_at", precision: nil, null: false
t.index ["kind"], name: "index_notifications_on_kind"
Expand Down
14 changes: 14 additions & 0 deletions spec/models/notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@
end
end

describe "#contact_us?" do
it "returns true for contact_us kind" do
expect(build(:notification, kind: "contact_us").contact_us?).to be true
end

it "returns true for contact_us_fyi kind" do
expect(build(:notification, kind: "contact_us_fyi").contact_us?).to be true
end

it "returns false for other kinds" do
expect(build(:notification, kind: "reset_password_fyi").contact_us?).to be false
end
end

describe '#resend?' do
it 'returns true when notification has a parent' do
parent = create(:notification)
Expand Down
20 changes: 20 additions & 0 deletions spec/policies/notification_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ def policy_for(record: nil, user:)
end
end

describe "#update?" do
context "with admin user" do
subject { policy_for(record: notification, user: admin_user) }

it { is_expected.to be_allowed_to(:update?) }
end

context "with owner user" do
subject { policy_for(record: notification, user: regular_user) }

it { is_expected.not_to be_allowed_to(:update?) }
end

context "with no user" do
subject { policy_for(record: notification, user: nil) }

it { is_expected.not_to be_allowed_to(:update?) }
end
end

describe "#resend?" do
context "with admin user" do
subject { policy_for(record: notification, user: admin_user) }
Expand Down
43 changes: 43 additions & 0 deletions spec/requests/notifications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,49 @@
end
end

describe "PATCH /notifications/:id" do
let(:contact_notification) { create(:notification, kind: "contact_us_fyi", recipient_email: regular_user.email) }

context "as an admin" do
before { sign_in admin }

it "marks the notification as responded" do
patch notification_path(contact_notification), params: { notification: { responded: "1" } }

expect(response).to have_http_status(:ok)
expect(contact_notification.reload.responded).to be(true)
end

it "marks the notification as not responded" do
contact_notification.update!(responded: true)

patch notification_path(contact_notification), params: { notification: { responded: "0" } }

expect(response).to have_http_status(:ok)
expect(contact_notification.reload.responded).to be(false)
end
end

context "as a regular user" do
before { sign_in regular_user }

it "does not allow updating" do
patch notification_path(contact_notification), params: { notification: { responded: "1" } }

expect(contact_notification.reload.responded).to be(false)
expect(response).to redirect_to(root_path)
end
end

context "as a guest" do
it "redirects to sign in" do
patch notification_path(contact_notification), params: { notification: { responded: "1" } }

expect(response).to redirect_to(new_user_session_path)
end
end
end

describe "POST /notifications/:id/resend" do
context "as an admin" do
before { sign_in admin }
Expand Down
Loading