Added the ability to send notifications by email when a new review is added.

This commit is contained in:
2024-07-27 00:29:28 +05:00
parent c84ed9f12b
commit 46da639055
7 changed files with 112 additions and 5 deletions

View File

@@ -0,0 +1,60 @@
<?php declare(strict_types=1);
namespace App\Notifications;
use App\Enums\Site\ProjectSection;
use App\Models\ProjectFeedback;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
final class ReviewAdded extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*/
public function __construct(
private readonly ProjectFeedback $feedback
) { }
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
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<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
//
];
}
}

View File

@@ -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 @@ class AppServiceProvider extends ServiceProvider
});
$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) {

View File

@@ -5,32 +5,44 @@ namespace App\Services\Site;
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'));
}