Class: LeanCms::NotificationSettingsController

Inherits:
ApplicationController show all
Includes:
Authorization
Defined in:
app/controllers/lean_cms/notification_settings_controller.rb

Instance Method Summary collapse

Instance Method Details

#editObject



7
8
9
# File 'app/controllers/lean_cms/notification_settings_controller.rb', line 7

def edit
  @settings = NotificationSetting.instance
end

#test_emailObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/lean_cms/notification_settings_controller.rb', line 67

def test_email
  @settings = NotificationSetting.instance

  unless @settings.email_enabled? && @settings.email_provider != 'none'
    flash[:alert] = 'Email notifications must be enabled and configured before testing.'
    redirect_to edit_lean_cms_notification_settings_path
    return
  end

  # Create a test form submission
  test_submission = LeanCms::FormSubmission.create!(
    form_type: 'contact',
    name: 'Test User',
    email: 'test@example.com',
    phone: '(555) 555-5555',
    company_name: 'Test Company',
    city: 'Test City',
    state: 'WI',
    zip: '54311',
    message: 'This is a test notification from the CMS settings page.',
    ip_address: request.remote_ip,
    status: :new_submission
  )

  # Trigger notification
  begin
    ContactFormNotifier.with(submission: test_submission).deliver_later(User.where(can_access_settings: true).limit(1))
    flash[:notice] = 'Test email notification sent successfully!'
  rescue StandardError => e
    Rails.logger.error "Test email error: #{e.message}"
    flash[:alert] = "Failed to send test email: #{e.message}"
  ensure
    # Clean up test submission
    test_submission.destroy
  end

  redirect_to edit_lean_cms_notification_settings_path
end

#test_smsObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/controllers/lean_cms/notification_settings_controller.rb', line 106

def test_sms
  @settings = NotificationSetting.instance

  unless @settings.sms_enabled?
    flash[:alert] = 'SMS notifications must be enabled before testing.'
    redirect_to edit_lean_cms_notification_settings_path
    return
  end

  # Create a test form submission
  test_submission = LeanCms::FormSubmission.create!(
    form_type: 'contact',
    name: 'Test User',
    email: 'test@example.com',
    phone: '(555) 555-5555',
    company_name: 'Test Company',
    city: 'Test City',
    state: 'WI',
    zip: '54311',
    message: 'This is a test SMS notification from the CMS settings page.',
    ip_address: request.remote_ip,
    status: :new_submission
  )

  # Trigger notification
  begin
    ContactFormNotifier.with(submission: test_submission).deliver_later(User.where(can_access_settings: true).limit(1))
    flash[:notice] = 'Test SMS notification sent successfully!'
  rescue StandardError => e
    Rails.logger.error "Test SMS error: #{e.message}"
    flash[:alert] = "Failed to send test SMS: #{e.message}"
  ensure
    # Clean up test submission
    test_submission.destroy
  end

  redirect_to edit_lean_cms_notification_settings_path
end

#updateObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/lean_cms/notification_settings_controller.rb', line 11

def update
  @settings = NotificationSetting.instance

  # Update basic toggles
  @settings.email_enabled = params[:email_enabled] == '1'
  @settings.sms_enabled = params[:sms_enabled] == '1'
  @settings.in_app_enabled = params[:in_app_enabled] == '1'
  @settings.email_provider = params[:email_provider] || 'none'

  # Update email credentials (only if provider is set and new value provided)
  # Password fields will be empty if not changed, so only update if present
  if params[:email_provider] == 'sendgrid'
    @settings.sendgrid_api_key = params[:sendgrid_api_key] if params[:sendgrid_api_key].present?
    @settings.mailgun_api_key = nil
    @settings.mailgun_domain = nil
  elsif params[:email_provider] == 'mailgun'
    @settings.mailgun_api_key = params[:mailgun_api_key] if params[:mailgun_api_key].present?
    @settings.mailgun_domain = params[:mailgun_domain] if params[:mailgun_domain].present?
    @settings.sendgrid_api_key = nil
  else
    # Only clear if switching away from a provider
    @settings.sendgrid_api_key = nil if @settings.email_provider == 'sendgrid'
    @settings.mailgun_api_key = nil if @settings.email_provider == 'mailgun'
    @settings.mailgun_domain = nil if @settings.email_provider == 'mailgun'
  end

  # Update SMS credentials
  if params[:sms_enabled] == '1'
    @settings. = params[:twilio_account_sid] if params[:twilio_account_sid].present?
    @settings.twilio_auth_token = params[:twilio_auth_token] if params[:twilio_auth_token].present?
    @settings.twilio_from_number = params[:twilio_from_number] if params[:twilio_from_number].present?
  else
    @settings. = nil
    @settings.twilio_auth_token = nil
    @settings.twilio_from_number = nil
  end

  # Update notification recipients
  if params[:notification_emails].present?
    emails = params[:notification_emails].split(',').map(&:strip).reject(&:blank?)
    @settings.notification_email_list = emails
  end

  if params[:notification_phones].present?
    phones = params[:notification_phones].split(',').map(&:strip).reject(&:blank?)
    @settings.notification_phone_list = phones
  end

  if @settings.save
    redirect_to edit_lean_cms_notification_settings_path, notice: 'Notification settings updated successfully.'
  else
    flash[:alert] = @settings.errors.full_messages.join(', ')
    render :edit, status: :unprocessable_entity
  end
end