backend notifications implementation

This commit is contained in:
Čarodej
2022-03-10 11:49:02 +01:00
parent 70d7f2f5bd
commit 64e80d387b
20 changed files with 617 additions and 280 deletions
@@ -0,0 +1,23 @@
<?php
namespace Domain\Notifications\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Response;
class FlushUserNotificationsController extends Controller
{
public function __invoke(): Response|Application|ResponseFactory
{
if (is_demo_account()) {
return response('Done', 204);
}
// Delete all notifications
auth()->user()->notifications()->delete();
return response('Done', 204);
}
}
@@ -0,0 +1,16 @@
<?php
namespace Domain\Notifications\Controllers;
use App\Http\Controllers\Controller;
use Domain\Notifications\Resources\NotificationCollection;
class GetUserNotificationsController extends Controller
{
public function __invoke(): NotificationCollection
{
return new NotificationCollection(
auth()->user()->notifications
);
}
}
@@ -0,0 +1,23 @@
<?php
namespace Domain\Notifications\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Response;
class MarkUserNotificationsAsReadController extends Controller
{
public function __invoke(): Response|Application|ResponseFactory
{
if (is_demo_account()) {
return response('Done', 204);
}
// Mark all notifications as read
auth()->user()->unreadNotifications()->update(['read_at' => now()]);
return response('Done', 204);
}
}
@@ -0,0 +1,16 @@
<?php
namespace Domain\Notifications\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class NotificationCollection extends ResourceCollection
{
public $collects = NotificationResource::class;
public function toArray($request): array
{
return [
'data' => $this->collection,
];
}
}
@@ -0,0 +1,32 @@
<?php
namespace Domain\Notifications\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class NotificationResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => [
'id' => $this->id,
'type' => $this->type,
'attributes' => [
'type' => $this->data['type'],
'title' => $this->data['title'],
'description' => $this->data['description'],
'action' => $this->data['action'] ?? null,
'created_at' => format_date($this->created_at, 'd. M. Y h:i'),
'read_at' => $this->read_at ? format_date($this->read_at, 'd. M. Y h:i') : null,
],
],
];
}
}
@@ -23,22 +23,16 @@ class UploadRequestFulfilledNotification extends Notification implements ShouldQ
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
public function via(mixed $notifiable): array
{
return ['mail', 'database'];
return ['mail', 'database', 'broadcast'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
public function toMail(mixed $notifiable): MailMessage
{
// TODO: add to language strings
return (new MailMessage)
@@ -59,7 +53,12 @@ class UploadRequestFulfilledNotification extends Notification implements ShouldQ
'title' => 'File Request Filled',
'description' => "Your file request for '{$this->uploadRequest->parent->name}' folder was filled successfully.",
'action' => [
'id' => $this->uploadRequest->id,
'type' => 'route',
'params' => [
'route' => 'Files',
'button' => 'Show Files',
'id' => $this->uploadRequest->id,
],
],
];
}