feat: Add notification system with BroadcastNotification class and database migration for notifications table
This commit is contained in:
parent
8aa5897c85
commit
1a3f0610a3
@ -15,8 +15,8 @@
|
||||
use Filament\Actions\ForceDeleteAction;
|
||||
use Filament\Actions\RestoreAction;
|
||||
use Filament\Forms\Components\CheckboxList;
|
||||
use Filament\Forms\Components\Radio;
|
||||
use Filament\Forms\Components\RichEditor;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
@ -64,13 +64,13 @@ public static function form(Schema $schema): Schema
|
||||
->required()
|
||||
->columnSpanFull(),
|
||||
|
||||
Select::make('type')
|
||||
->label('Tipe Pengumuman')
|
||||
Radio::make('type')
|
||||
->label('Tipe')
|
||||
->options(AnnouncementType::class)
|
||||
->required()
|
||||
->native(false)
|
||||
->default(AnnouncementType::PUBLIC->value)
|
||||
->live(),
|
||||
->live()
|
||||
->inline(),
|
||||
|
||||
CheckboxList::make('partnerMedia')
|
||||
->label('Kirim Kepada')
|
||||
@ -84,7 +84,8 @@ public static function form(Schema $schema): Schema
|
||||
->bulkToggleable()
|
||||
->selectAllAction(
|
||||
fn (Action $action) => $action->label('Pilih Semua Media'),
|
||||
),
|
||||
)
|
||||
->dehydrated(),
|
||||
|
||||
])
|
||||
->columns(1);
|
||||
@ -129,7 +130,8 @@ public static function table(Table $table): Table
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make()
|
||||
->modalWidth(Width::TwoExtraLarge),
|
||||
->modalWidth(Width::TwoExtraLarge)
|
||||
->visible(fn ($record) => $record->type === AnnouncementType::PUBLIC),
|
||||
|
||||
DeleteAction::make(),
|
||||
|
||||
|
||||
@ -2,7 +2,12 @@
|
||||
|
||||
namespace App\Filament\Resources\Publication\Announcements\Pages;
|
||||
|
||||
use App\Enums\AnnouncementType;
|
||||
use App\Filament\Resources\Publication\Announcements\AnnouncementResource;
|
||||
use App\Models\Company;
|
||||
use App\Models\PartnerMedia;
|
||||
use App\Models\User;
|
||||
use App\Notifications\BroadcastNotification;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ManageRecords;
|
||||
use Filament\Support\Enums\Width;
|
||||
@ -25,7 +30,26 @@ protected function getHeaderActions(): array
|
||||
$action->makeModalSubmitAction('createAnother', arguments: ['another' => true])
|
||||
->label('Simpan dan Tambah Lagi'),
|
||||
])
|
||||
->modalWidth(Width::TwoExtraLarge),
|
||||
->modalWidth(Width::TwoExtraLarge)
|
||||
->after(function (array $data) {
|
||||
if ($data['type'] === AnnouncementType::PUBLIC) {
|
||||
return;
|
||||
}
|
||||
|
||||
$payload = [
|
||||
'title' => $data['title'],
|
||||
'body' => $data['content'],
|
||||
];
|
||||
|
||||
$userIds = Company::whereIn(
|
||||
'id',
|
||||
PartnerMedia::whereIn('id', $data['partnerMedia'])->pluck('company_id')
|
||||
)->pluck('user_id');
|
||||
|
||||
User::whereIn('id', $userIds)->each(function ($user) use ($payload) {
|
||||
$user->notify(new BroadcastNotification($payload));
|
||||
});
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
31
app/Notifications/BroadcastNotification.php
Normal file
31
app/Notifications/BroadcastNotification.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use Filament\Notifications\Notification as FilamentNotification;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
class BroadcastNotification extends Notification
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
public function __construct(
|
||||
protected array $payload
|
||||
) {
|
||||
$this->payload = $payload;
|
||||
}
|
||||
|
||||
public function via($notifiable): array
|
||||
{
|
||||
return ['database'];
|
||||
}
|
||||
|
||||
public function toDatabase($notifiable): array
|
||||
{
|
||||
return FilamentNotification::make()
|
||||
->title($this->payload['title'])
|
||||
->body($this->payload['body'])
|
||||
->getDatabaseMessage();
|
||||
}
|
||||
}
|
||||
@ -99,6 +99,8 @@ public function panel(Panel $panel): Panel
|
||||
'Publikasi',
|
||||
'Pengaturan',
|
||||
'Pelindung',
|
||||
]);
|
||||
])
|
||||
->databaseNotifications()
|
||||
->databaseNotificationsPolling('30s');
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('notifications', function (Blueprint $table) {
|
||||
$table->uuid('id')->primary();
|
||||
$table->string('type');
|
||||
$table->morphs('notifiable');
|
||||
$table->text('data');
|
||||
$table->timestamp('read_at')->nullable();
|
||||
$table->timestamp('created_at')->useCurrent();
|
||||
$table->timestamp('updated_at')->nullable()->useCurrentOnUpdate();
|
||||
$table->softDeletes();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('notifications');
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user