From 46da639055286f671a11023ca9160cdae8e8058d Mon Sep 17 00:00:00 2001 From: Leonid Nikitin Date: Sat, 27 Jul 2024 00:29:28 +0500 Subject: [PATCH] Added the ability to send notifications by email when a new review is added. --- app/application/.env.example | 8 ++- .../app/Notifications/ReviewAdded.php | 60 +++++++++++++++++++ .../app/Providers/AppServiceProvider.php | 12 +++- .../app/Services/Site/FeedbackService.php | 16 ++++- app/application/config/feedback.php | 9 +++ app/application/lang/en/notification.php | 6 ++ app/application/lang/ru/notification.php | 6 ++ 7 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 app/application/app/Notifications/ReviewAdded.php create mode 100644 app/application/config/feedback.php create mode 100644 app/application/lang/en/notification.php create mode 100644 app/application/lang/ru/notification.php diff --git a/app/application/.env.example b/app/application/.env.example index d32953a..0a05dc8 100644 --- a/app/application/.env.example +++ b/app/application/.env.example @@ -1,4 +1,4 @@ -APP_NAME=Laravel +APP_NAME="My Projects Website" APP_ENV=local APP_KEY= APP_DEBUG=true @@ -11,6 +11,10 @@ CAPTCHA_PRIVATE_TOKEN= CAPTCHA_STATIC_PATH=http://your-domain-captcha-or-IP:8081/captcha CAPTCHA_PUBLIC_TOKEN= +# Don't forget to configure MAIL_MAILER to send mail. +FEEDBACK_MAIL_NOTIFICATIONS=false +FEEDBACK_MAIL_TO= + APP_FORCE_HTTPS=false APP_DEFAULT_LOCALE=ru @@ -59,7 +63,7 @@ REDIS_HOST=app-redis REDIS_PASSWORD=null REDIS_PORT=6379 -MAIL_MAILER=log +MAIL_MAILER=smtp MAIL_HOST=127.0.0.1 MAIL_PORT=2525 MAIL_USERNAME=null diff --git a/app/application/app/Notifications/ReviewAdded.php b/app/application/app/Notifications/ReviewAdded.php new file mode 100644 index 0000000..739c3fc --- /dev/null +++ b/app/application/app/Notifications/ReviewAdded.php @@ -0,0 +1,60 @@ + + */ + public function via(object $notifiable): array + { + return ['mail']; + } + + /** + * Get the mail representation of the notification. + */ + public function toMail(object $notifiable): MailMessage + { + $project = $this->feedback->project; + + return (new MailMessage) + ->subject(__('notification.Review Added: :name', ['name' => $project->name])) + ->line(__('notification.Added a new review.')) + ->action(__('notification.Project :name', ['name' => $project->name]), \url(ProjectSection::Home->url($project))) + ->line( __('site.attributes.name') . ': ' . $this->feedback->name) + ->line( __('site.attributes.email') . ': ' . $this->feedback->email) + ->line(__('site.attributes.message') . ': ' . $this->feedback->message); + } + + /** + * Get the array representation of the notification. + * + * @return array + */ + public function toArray(object $notifiable): array + { + return [ + // + ]; + } +} diff --git a/app/application/app/Providers/AppServiceProvider.php b/app/application/app/Providers/AppServiceProvider.php index 670578a..85a768b 100644 --- a/app/application/app/Providers/AppServiceProvider.php +++ b/app/application/app/Providers/AppServiceProvider.php @@ -3,8 +3,10 @@ namespace App\Providers; use App\Enums\Morph; +use App\Services\ProjectFeedback\ProjectFeedbackCommandHandler; use App\Services\Search\CreateSearchInstanceCommand; use App\Services\Search\Search; +use App\Services\Site\FeedbackService; use App\Services\Storage\Image\ResizeCommandHandler; use App\Services\Storage\ImageService; use App\Services\Storage\StorageCommandHandler; @@ -31,7 +33,15 @@ public function register(): void }); $this->app->bind(StorageCommandHandler::class, function () { - return new StorageCommandHandler(disc: (string) config('storage.disk')); + return new StorageCommandHandler(disc: (string) \config('storage.disk')); + }); + + $this->app->bind(FeedbackService::class, function (Application $app) { + return new FeedbackService( + $app->make(ProjectFeedbackCommandHandler::class), + (bool) \config('feedback.mail_notifications', false), + \config('feedback.mail_to', null), + ); }); $this->app->bind(ImageService::class, function (Application $app) { diff --git a/app/application/app/Services/Site/FeedbackService.php b/app/application/app/Services/Site/FeedbackService.php index 7bbe72f..22170db 100644 --- a/app/application/app/Services/Site/FeedbackService.php +++ b/app/application/app/Services/Site/FeedbackService.php @@ -5,32 +5,44 @@ use App\Dto\Service\Site\Feedback\Send; use App\Models\Project; use App\Models\User; +use App\Notifications\ReviewAdded; use App\ServiceResults\ServiceResultError; use App\ServiceResults\ServiceResultSuccess; use App\Services\ProjectFeedback\ProjectFeedbackCommandHandler; use App\Services\Service; use App\Services\WebsiteTranslations; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Notification; final class FeedbackService extends Service { public function __construct( private readonly ProjectFeedbackCommandHandler $feedbackCommandHandler, + private readonly bool $isNotifications = false, + private readonly ?string $mailNotifications = null, ) { } public function send(Send $send, Project $project, WebsiteTranslations $websiteTranslations, ?User $user = null): ServiceResultError | ServiceResultSuccess { try { - DB::transaction(function () use ($send, $project, $user) { + $feedback = DB::transaction(function () use ($send, $project, $user) { $data = $this->getDataFeedback($send); $data['user_id'] = $user?->id; - $this->feedbackCommandHandler->handleStore($project, $data); + return $this->feedbackCommandHandler->handleStore($project, $data); }); } catch (\Throwable $e) { report($e); return $this->errService($websiteTranslations->translate('Server Error')); } + try { + if ($this->isNotifications) { + Notification::route('mail', $this->mailNotifications)->notify(new ReviewAdded($feedback)); + } + } catch (\Throwable $e) { + report($e); + } + return $this->ok($websiteTranslations->translate('site.Message sent successfully')); } diff --git a/app/application/config/feedback.php b/app/application/config/feedback.php new file mode 100644 index 0000000..5669bae --- /dev/null +++ b/app/application/config/feedback.php @@ -0,0 +1,9 @@ + (bool) env('FEEDBACK_MAIL_NOTIFICATIONS', false), + 'mail_to' => env('FEEDBACK_MAIL_TO', null), +]; diff --git a/app/application/lang/en/notification.php b/app/application/lang/en/notification.php new file mode 100644 index 0000000..490c6b1 --- /dev/null +++ b/app/application/lang/en/notification.php @@ -0,0 +1,6 @@ + 'Added a new review.', + 'Review Added: :name' => 'Review Added: :name', + 'Project :name' => 'Project :name', +]; diff --git a/app/application/lang/ru/notification.php b/app/application/lang/ru/notification.php new file mode 100644 index 0000000..5431735 --- /dev/null +++ b/app/application/lang/ru/notification.php @@ -0,0 +1,6 @@ + 'Добавили новый отзыв.', + 'Review Added: :name' => 'Отзыв добавлен: :name', + 'Project :name' => 'Проект :name', +];