Giant Update
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Notification\Adapters;
|
||||
|
||||
use App\Models\NotificationChannel;
|
||||
use App\Models\NotificationDelivery;
|
||||
use App\Models\User;
|
||||
use App\Models\UserNotification;
|
||||
use App\Services\Notification\Contracts\NotificationChannelAdapter;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class SystemAdapter implements NotificationChannelAdapter
|
||||
{
|
||||
public function send(NotificationDelivery $delivery): array
|
||||
{
|
||||
$message = $delivery->message;
|
||||
$user = $message->recipient instanceof User
|
||||
? $message->recipient
|
||||
: ($message->recipient?->user ?? null);
|
||||
if (! $user) {
|
||||
throw ValidationException::withMessages(['recipient' => 'Penerima tidak memiliki akun aplikasi.']);
|
||||
}
|
||||
|
||||
$notification = UserNotification::create([
|
||||
'uuid' => Str::uuid(),
|
||||
'tenant_id' => $message->tenant_id,
|
||||
'user_id' => $user->id,
|
||||
'notification_message_id' => $message->id,
|
||||
'title' => $delivery->rendered_subject ?: 'Notifikasi',
|
||||
'body' => $delivery->rendered_body,
|
||||
'action_url' => data_get($message->context, 'action_url'),
|
||||
'icon' => data_get($message->context, 'icon', 'cil-bell'),
|
||||
]);
|
||||
$settings = NotificationChannel::where('tenant_id', $message->tenant_id)
|
||||
->where('channel', 'system')->where('enabled', true)->where('is_default', true)
|
||||
->first()?->settings ?? [];
|
||||
$maximum = max(20, (int) ($settings['maximum_notifications_per_user'] ?? 500));
|
||||
UserNotification::where('user_id', $user->id)
|
||||
->whereNotIn('id', UserNotification::where('user_id', $user->id)->latest('id')->limit($maximum)->pluck('id'))
|
||||
->delete();
|
||||
|
||||
return ['provider_message_id' => $notification->uuid, 'response' => ['stored' => true]];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user