Skip to content

Commit

Permalink
feat: add email notification
Browse files Browse the repository at this point in the history
  • Loading branch information
ast21 committed Sep 3, 2024
1 parent a2998ad commit 85b1750
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 2 deletions.
6 changes: 5 additions & 1 deletion config/admin-kit-feedbacks.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php

// The label can be a translation key
return [
'email_notification' => [
'enabled' => env('ADMIN_KIT_FEEDBACKS_EMAIL_NOTIFICATION_ENABLED', false),
'addresses' => env('ADMIN_KIT_FEEDBACKS_EMAIL_NOTIFICATION_ADDRESSES', '[email protected]'),
],

'fields' => [
'name' => [
'rules' => 'required|string|max:255',
Expand Down
4 changes: 4 additions & 0 deletions resources/views/mail/feedback-notification.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>Получена новая заявка</h1>
@foreach($fields as $key => $value)
<p>{{ $key }}: {{ $value }}</p>
@endforeach
17 changes: 17 additions & 0 deletions src/Events/FeedbackSaved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace AdminKit\Feedbacks\Events;

use AdminKit\Feedbacks\Models\Feedback;

class FeedbackSaved
{
public Feedback $feedback;

public function __construct(Feedback $feedback)
{
$this->feedback = $feedback;
}
}
10 changes: 10 additions & 0 deletions src/FeedbacksServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace AdminKit\Feedbacks;

use AdminKit\Feedbacks\Commands\FeedbacksCommand;
use AdminKit\Feedbacks\Listeners\NotifyAboutNewFeedback;
use AdminKit\Feedbacks\Providers\RouteServiceProvider;
use AdminKit\Feedbacks\Events\FeedbackSaved;
use Illuminate\Support\Facades\Event;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand Down Expand Up @@ -32,4 +35,11 @@ public function registeringPackage()
{
$this->app->register(RouteServiceProvider::class);
}

public function bootingPackage()
{
if (config('admin-kit-feedbacks.email_notification.enabled')) {
Event::listen(FeedbackSaved::class, NotifyAboutNewFeedback::class);
}
}
}
28 changes: 28 additions & 0 deletions src/Listeners/NotifyAboutNewFeedback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace AdminKit\Feedbacks\Listeners;

use AdminKit\Feedbacks\Mail\FeedbackNotificationMail;
use AdminKit\Feedbacks\Events\FeedbackSaved;
use Illuminate\Support\Facades\Mail;

class NotifyAboutNewFeedback
{
/**
* Create the event listener.
*/
public function __construct()
{
//
}

/**
* Handle the event.
*/
public function handle(FeedbackSaved $event): void
{
$fields = $event->feedback->fields;

Check failure on line 24 in src/Listeners/NotifyAboutNewFeedback.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property AdminKit\Feedbacks\Models\Feedback::$fields.

Mail::queue(new FeedbackNotificationMail($fields));
}
}
26 changes: 26 additions & 0 deletions src/Mail/FeedbackNotificationMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace AdminKit\Feedbacks\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class FeedbackNotificationMail extends Mailable
{
use Queueable, SerializesModels;

public function __construct(public array $fields)
{
}

public function build()
{
$emailList = explode(',', trim(config('admin-kit-feedbacks.email_notification.addresses')));

return $this
->to($emailList)
->subject('Получена новая заявка с сайта')
->view('admin-kit-feedbacks::mail.feedback-notification', $this->fields);
}
}
5 changes: 4 additions & 1 deletion src/UI/API/Controllers/FeedbackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@
use AdminKit\Feedbacks\Models\Feedback;
use AdminKit\Feedbacks\UI\API\Requests\FeedbackStoreRequest;
use Illuminate\Http\Response;
use AdminKit\Feedbacks\Events\FeedbackSaved;

class FeedbackController extends Controller
{
public function store(FeedbackStoreRequest $request): Response
{
Feedback::query()
$feedback = Feedback::query()
->create(
collect(['fields' => $request->validated()])
->merge(['locale' => app()->getLocale()])
->toArray()
);

event(new FeedbackSaved($feedback));

return response('OK');
}
}

0 comments on commit 85b1750

Please sign in to comment.