From 85b1750ddf3f90acb0bf9aa1a3f49f77022c2eca Mon Sep 17 00:00:00 2001 From: Anastas Mironov Date: Tue, 3 Sep 2024 17:36:33 +0900 Subject: [PATCH] feat: add email notification --- config/admin-kit-feedbacks.php | 6 +++- .../mail/feedback-notification.blade.php | 4 +++ src/Events/FeedbackSaved.php | 17 +++++++++++ src/FeedbacksServiceProvider.php | 10 +++++++ src/Listeners/NotifyAboutNewFeedback.php | 28 +++++++++++++++++++ src/Mail/FeedbackNotificationMail.php | 26 +++++++++++++++++ src/UI/API/Controllers/FeedbackController.php | 5 +++- 7 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 resources/views/mail/feedback-notification.blade.php create mode 100644 src/Events/FeedbackSaved.php create mode 100644 src/Listeners/NotifyAboutNewFeedback.php create mode 100644 src/Mail/FeedbackNotificationMail.php diff --git a/config/admin-kit-feedbacks.php b/config/admin-kit-feedbacks.php index 5ac3380..39c7095 100644 --- a/config/admin-kit-feedbacks.php +++ b/config/admin-kit-feedbacks.php @@ -1,7 +1,11 @@ [ + 'enabled' => env('ADMIN_KIT_FEEDBACKS_EMAIL_NOTIFICATION_ENABLED', false), + 'addresses' => env('ADMIN_KIT_FEEDBACKS_EMAIL_NOTIFICATION_ADDRESSES', 'admin@example.com'), + ], + 'fields' => [ 'name' => [ 'rules' => 'required|string|max:255', diff --git a/resources/views/mail/feedback-notification.blade.php b/resources/views/mail/feedback-notification.blade.php new file mode 100644 index 0000000..e27660b --- /dev/null +++ b/resources/views/mail/feedback-notification.blade.php @@ -0,0 +1,4 @@ +

Получена новая заявка

+@foreach($fields as $key => $value) +

{{ $key }}: {{ $value }}

+@endforeach diff --git a/src/Events/FeedbackSaved.php b/src/Events/FeedbackSaved.php new file mode 100644 index 0000000..a2f765a --- /dev/null +++ b/src/Events/FeedbackSaved.php @@ -0,0 +1,17 @@ +feedback = $feedback; + } +} \ No newline at end of file diff --git a/src/FeedbacksServiceProvider.php b/src/FeedbacksServiceProvider.php index b81d4a0..cd7eebb 100644 --- a/src/FeedbacksServiceProvider.php +++ b/src/FeedbacksServiceProvider.php @@ -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; @@ -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); + } + } } diff --git a/src/Listeners/NotifyAboutNewFeedback.php b/src/Listeners/NotifyAboutNewFeedback.php new file mode 100644 index 0000000..d94d3e5 --- /dev/null +++ b/src/Listeners/NotifyAboutNewFeedback.php @@ -0,0 +1,28 @@ +feedback->fields; + + Mail::queue(new FeedbackNotificationMail($fields)); + } +} diff --git a/src/Mail/FeedbackNotificationMail.php b/src/Mail/FeedbackNotificationMail.php new file mode 100644 index 0000000..3dad8b9 --- /dev/null +++ b/src/Mail/FeedbackNotificationMail.php @@ -0,0 +1,26 @@ +to($emailList) + ->subject('Получена новая заявка с сайта') + ->view('admin-kit-feedbacks::mail.feedback-notification', $this->fields); + } +} diff --git a/src/UI/API/Controllers/FeedbackController.php b/src/UI/API/Controllers/FeedbackController.php index 906ee81..c0fa2ef 100644 --- a/src/UI/API/Controllers/FeedbackController.php +++ b/src/UI/API/Controllers/FeedbackController.php @@ -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'); } }