Skip to content

Commit e22d35d

Browse files
Add admin setting to toggle new application email notifications
- Add notify_new_applications boolean column to admins (default true) - Admins can toggle this in Settings page (linked in nav) - Super admins can also configure this for other admins - Mailer checks preference before sending new application emails Amp-Thread-ID: https://ampcode.com/threads/T-019c25c9-ecf2-77dc-a60d-84b7e0f3fcc8 Co-authored-by: Amp <[email protected]>
1 parent 2b62339 commit e22d35d

File tree

8 files changed

+82
-3
lines changed

8 files changed

+82
-3
lines changed

app/controllers/admin/admins_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def set_admin
7979
end
8080

8181
def admin_params
82-
permitted = [ :first_name, :last_name, :email ]
82+
permitted = [ :first_name, :last_name, :email, :notify_new_applications ]
8383
permitted << :super_admin if current_admin.super_admin?
8484
params.require(:admin).permit(permitted)
8585
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Admin::SettingsController < Admin::BaseController
2+
def show
3+
@admin = current_admin
4+
end
5+
6+
def update
7+
@admin = current_admin
8+
9+
if @admin.update(settings_params)
10+
redirect_to admin_settings_path, notice: "Settings updated successfully."
11+
else
12+
render :show, status: :unprocessable_entity
13+
end
14+
end
15+
16+
private
17+
18+
def settings_params
19+
params.require(:admin).permit(:notify_new_applications)
20+
end
21+
end

app/mailers/admin_mailer.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,21 @@ def new_application_notification(application)
77
@participant = application.participant
88
@event = application.event
99

10-
recipients = [ @event.contact_email ]
11-
recipients += Admin.where(super_admin: true).pluck(:email)
10+
recipients = []
11+
12+
# Add event owner if they want notifications
13+
event_owner = @event.admin
14+
if event_owner.notify_new_applications?
15+
recipients << event_owner.email
16+
end
17+
18+
# Add super admins who want notifications
19+
recipients += Admin.where(super_admin: true, notify_new_applications: true).pluck(:email)
20+
1221
recipients = recipients.uniq.compact
1322

23+
return if recipients.empty?
24+
1425
mail(
1526
to: recipients,
1627
subject: "New Visa Letter Application - #{@event.name}"

app/views/admin/admins/_form.html.erb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@
4040
</div>
4141
<% end %>
4242

43+
<div class="border-t pt-4 mt-4">
44+
<h3 class="text-sm font-medium text-gray-900 mb-3">Notification Preferences</h3>
45+
<div class="flex items-center">
46+
<%= f.check_box :notify_new_applications, class: "h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded" %>
47+
<%= f.label :notify_new_applications, class: "ml-2 block text-sm text-gray-700" do %>
48+
<span class="font-medium">New application emails</span>
49+
<span class="text-gray-500">— receive email when applications are submitted</span>
50+
<% end %>
51+
</div>
52+
</div>
53+
4354
<div class="flex justify-end space-x-3 pt-4 border-t">
4455
<%= link_to "Cancel", admin_admins_path, class: "px-4 py-2 border border-gray-300 rounded-md text-gray-700 hover:bg-gray-50" %>
4556
<%= f.submit admin.new_record? ? "Create Admin" : "Update Admin",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<div class="mb-6">
2+
<h1 class="text-2xl font-bold text-gray-900">Settings</h1>
3+
<p class="text-gray-500">Manage your notification preferences</p>
4+
</div>
5+
6+
<div class="bg-white rounded-lg shadow p-6 max-w-2xl">
7+
<%= form_with model: @admin, url: admin_settings_path, method: :patch, class: "space-y-6" do |f| %>
8+
<div>
9+
<h2 class="text-lg font-medium text-gray-900 mb-4">Email Notifications</h2>
10+
11+
<div class="space-y-4">
12+
<div class="flex items-start">
13+
<div class="flex items-center h-5">
14+
<%= f.check_box :notify_new_applications, class: "h-4 w-4 text-red-600 focus:ring-red-500 border-gray-300 rounded" %>
15+
</div>
16+
<div class="ml-3">
17+
<%= f.label :notify_new_applications, "New application notifications", class: "text-sm font-medium text-gray-700" %>
18+
<p class="text-sm text-gray-500">Receive an email when a new visa letter application is submitted for your events.</p>
19+
</div>
20+
</div>
21+
</div>
22+
</div>
23+
24+
<div class="pt-4 border-t">
25+
<%= f.submit "Save Settings", class: "bg-red-600 text-white py-2 px-4 rounded-md hover:bg-red-700 cursor-pointer" %>
26+
</div>
27+
<% end %>
28+
</div>

app/views/layouts/admin.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<span class="bg-yellow-500 text-yellow-900 text-xs px-2 py-1 rounded ml-1 font-semibold">Super Admin</span>
4444
<% end %>
4545
</span>
46+
<%= link_to "Settings", admin_settings_path, class: "text-gray-300 hover:text-white text-sm hover:underline" %>
4647
<%= link_to "View Site", root_path, class: "text-gray-300 hover:text-white text-sm hover:underline" %>
4748
<%= button_to "Sign Out", destroy_admin_session_path, method: :delete, class: "text-gray-300 hover:text-white text-sm hover:underline" %>
4849
</div>

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
namespace :admin do
5151
get "/", to: "dashboard#index", as: :dashboard
5252

53+
resource :settings, only: [ :show, :update ]
54+
5355
resources :events
5456
resources :visa_letter_applications, only: [ :index, :show ] do
5557
member do
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddEmailNotificationsToAdmins < ActiveRecord::Migration[8.1]
2+
def change
3+
add_column :admins, :notify_new_applications, :boolean, default: true, null: false
4+
end
5+
end

0 commit comments

Comments
 (0)