Skip to content

Commit

Permalink
feat: email forward
Browse files Browse the repository at this point in the history
  • Loading branch information
blt950 committed Oct 10, 2023
1 parent fb97ab0 commit 49aead7
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 12 deletions.
17 changes: 17 additions & 0 deletions app/Http/Controllers/FeedbackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace App\Http\Controllers;

use anlutro\LaravelSettings\Facade as Setting;
use App\Models\Feedback;
use App\Models\Position;
use App\Models\User;
use App\Notifications\FeedbackNotification;
use Illuminate\Http\Request;

class FeedbackController extends Controller
Expand All @@ -16,6 +18,11 @@ class FeedbackController extends Controller
*/
public function create()
{

if (! Setting::get('feedbackEnabled')) {
return redirect()->route('dashboard')->withErrors('Feedback is currently disabled.');
}

$positions = Position::all();
$controllers = User::all();

Expand All @@ -29,6 +36,11 @@ public function create()
*/
public function store(Request $request)
{

if (! Setting::get('feedbackEnabled')) {
return redirect()->route('dashboard')->withErrors('Feedback is currently disabled.');
}

$data = $request->validate([
'position' => 'nullable|exists:positions,callsign',
'controller' => 'nullable|exists:users,id',
Expand All @@ -48,6 +60,11 @@ public function store(Request $request)
'reference_position_id' => isset($position) ? $position->id : null,
]);

// Forward email if configured
if (Setting::get('feedbackForwardEmail')) {
$feedback->notify(new FeedbackNotification($feedback));
}

return redirect()->route('dashboard')->with('success', 'Feedback submitted!');

}
Expand Down
12 changes: 1 addition & 11 deletions app/Mail/StaffNoticeMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Mail;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
Expand All @@ -13,25 +12,16 @@ class StaffNoticeMail extends Mailable

private $mailSubject;

private $user;

private $textLines;

/**
* Create a new message instance.
*
* @param Endorsement $endorsement
* @param array $textLines an array of markdown lines to add
* @param string $contactMail optional contact e-mail to put in footer
* @param string $actionUrl optinal action button url
* @param string $actionText optional action button text
* @param string $actionColor optional bootstrap button color override
* @return void
*/
public function __construct(string $mailSubject, User $user, array $textLines)
public function __construct(string $mailSubject, array $textLines)
{
$this->mailSubject = $mailSubject;
$this->user = $user;
$this->textLines = $textLines;
}

Expand Down
2 changes: 2 additions & 0 deletions app/Models/Feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class Feedback extends Model
{
use HasFactory;
use Notifiable;

protected $guarded = [];

Expand Down
82 changes: 82 additions & 0 deletions app/Notifications/FeedbackNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace App\Notifications;

use anlutro\LaravelSettings\Facade as Setting;
use App\Mail\StaffNoticeMail;
use App\Models\Endorsement;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;

class FeedbackNotification extends Notification implements ShouldQueue
{
use Queueable;

private $feedback;

/**
* Create a new notification instance.
*
* @param Endorsement $endorsement
*/
public function __construct($feedback)
{
$this->feedback = $feedback;
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail', 'database'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return EndorsementMail
*/
public function toMail($notifiable)
{

if (! Setting::get('feedbackEnabled')) {
return false;
}

$position = isset($this->feedback->referencePosition) ? $this->feedback->referencePosition->callsign : 'N/A';
$controller = isset($this->feedback->referenceUser) ? $this->feedback->referenceUser->name : 'N/A';

$textLines = [
'New feedback has been submitted by ' . $this->feedback->submitter->name . ' (' . $this->feedback->submitter->id . ').',
'*You can reply to this email to respond directly.*',
'- **Controller**: ' . $controller,
'- **Position**: ' . $position,
'### Feedback',
$this->feedback->feedback,

];

$feedbackForward = Setting::get('feedbackForwardEmail');

return (new StaffNoticeMail('Feedback submited', $textLines))
->to($feedbackForward)
->replyTo($this->feedback->submitter->email, $this->feedback->submitter->name);
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [];
}
}
2 changes: 1 addition & 1 deletion app/Notifications/InactiveOnlineStaffNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function toMail($notifiable)
'All admins and moderators in area in question has been notified.',
];

return (new StaffNoticeMail('Unauthorized network logon recorded', $this->user, $textLines))
return (new StaffNoticeMail('Unauthorized network logon recorded', $textLines))
->to($this->sendTo->pluck('email'));
}

Expand Down

0 comments on commit 49aead7

Please sign in to comment.