feat: tambah sistem poin anggota (kehadiran +10, artikel +5)
- Model MemberPoint + migration - PostObserver: +5 poin saat artikel dipublish - ActivityObserver: +10 poin saat peserta hadir di kegiatan - MemberPointResource: tampil di grup Organisasi - MemberPointSeeder + update ActivitySeeder dengan pivot status kehadiran - Update PermissionSeeder: anggota bisa lihat poin
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MemberPoints;
|
||||
|
||||
use App\Filament\Resources\MemberPoints\Pages\CreateMemberPoint;
|
||||
use App\Filament\Resources\MemberPoints\Pages\EditMemberPoint;
|
||||
use App\Filament\Resources\MemberPoints\Pages\ListMemberPoints;
|
||||
use App\Filament\Resources\MemberPoints\Schemas\MemberPointForm;
|
||||
use App\Filament\Resources\MemberPoints\Tables\MemberPointsTable;
|
||||
use App\Models\MemberPoint;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class MemberPointResource extends Resource
|
||||
{
|
||||
protected static ?string $model = MemberPoint::class;
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedStar;
|
||||
protected static string|\UnitEnum|null $navigationGroup = 'Organisasi';
|
||||
protected static ?string $navigationLabel = 'Poin Anggota';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return MemberPointForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return MemberPointsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListMemberPoints::route('/'),
|
||||
'create' => CreateMemberPoint::route('/create'),
|
||||
'edit' => EditMemberPoint::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MemberPoints\Pages;
|
||||
|
||||
use App\Filament\Resources\MemberPoints\MemberPointResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateMemberPoint extends CreateRecord
|
||||
{
|
||||
protected static string $resource = MemberPointResource::class;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MemberPoints\Pages;
|
||||
|
||||
use App\Filament\Resources\MemberPoints\MemberPointResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditMemberPoint extends EditRecord
|
||||
{
|
||||
protected static string $resource = MemberPointResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MemberPoints\Pages;
|
||||
|
||||
use App\Filament\Resources\MemberPoints\MemberPointResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListMemberPoints extends ListRecords
|
||||
{
|
||||
protected static string $resource = MemberPointResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MemberPoints\Schemas;
|
||||
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class MemberPointForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
//
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MemberPoints\Tables;
|
||||
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class MemberPointsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('user.name')->label('Anggota')->searchable()->sortable(),
|
||||
TextColumn::make('points')->label('Poin')->sortable()
|
||||
->color(fn ($state) => $state > 0 ? 'success' : 'danger'),
|
||||
TextColumn::make('reason')->label('Keterangan')->limit(50),
|
||||
TextColumn::make('source_type')->label('Sumber')->badge()
|
||||
->color(fn ($state) => match ($state) {
|
||||
'activity' => 'info',
|
||||
'post' => 'warning',
|
||||
default => 'gray',
|
||||
}),
|
||||
TextColumn::make('created_at')->label('Tanggal')->date('d M Y')->sortable(),
|
||||
])
|
||||
->defaultSort('created_at', 'desc')
|
||||
->filters([
|
||||
SelectFilter::make('source_type')->label('Sumber')
|
||||
->options(['activity' => 'Kegiatan', 'post' => 'Artikel']),
|
||||
])
|
||||
->toolbarActions([BulkActionGroup::make([DeleteBulkAction::make()])]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class MemberPoint extends Model
|
||||
{
|
||||
protected $fillable = ['user_id', 'points', 'reason', 'source_type', 'source_id'];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\MemberPoint;
|
||||
use Database\Factories\UserFactory;
|
||||
use Filament\Models\Contracts\FilamentUser;
|
||||
use Filament\Panel;
|
||||
@@ -54,6 +55,16 @@ class User extends Authenticatable implements FilamentUser
|
||||
return $this->hasMany(CashRecord::class, 'created_by');
|
||||
}
|
||||
|
||||
public function points(): HasMany
|
||||
{
|
||||
return $this->hasMany(MemberPoint::class);
|
||||
}
|
||||
|
||||
public function totalPoints(): int
|
||||
{
|
||||
return $this->points()->sum('points');
|
||||
}
|
||||
|
||||
public function canAccessPanel(Panel $panel): bool
|
||||
{
|
||||
return true;
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Observers;
|
||||
|
||||
use App\Models\Activity;
|
||||
use App\Models\ActivityLog;
|
||||
use App\Models\MemberPoint;
|
||||
use App\Services\NotificationService;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
@@ -66,4 +67,21 @@ class ActivityObserver
|
||||
'description' => "Kegiatan baru dibuat: {$activity->title}",
|
||||
]);
|
||||
}
|
||||
|
||||
public function pivotAttached(Activity $activity, string $relationName, array $pivotIds, array $pivotIdsAttributes): void
|
||||
{
|
||||
if ($relationName !== 'participants') return;
|
||||
|
||||
foreach ($pivotIdsAttributes as $userId => $attrs) {
|
||||
if (($attrs['status'] ?? 'hadir') === 'hadir') {
|
||||
MemberPoint::create([
|
||||
'user_id' => $userId,
|
||||
'points' => 10,
|
||||
'reason' => "Hadir di kegiatan: {$activity->title}",
|
||||
'source_type' => 'activity',
|
||||
'source_id' => $activity->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\MemberPoint;
|
||||
use App\Models\Post;
|
||||
|
||||
class PostObserver
|
||||
{
|
||||
public function updated(Post $post): void
|
||||
{
|
||||
if ($post->wasChanged('status') && $post->status === 'published') {
|
||||
MemberPoint::create([
|
||||
'user_id' => $post->user_id,
|
||||
'points' => 5,
|
||||
'reason' => "Artikel dipublikasi: {$post->title}",
|
||||
'source_type' => 'post',
|
||||
'source_id' => $post->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,12 @@ namespace App\Providers;
|
||||
|
||||
use App\Models\Activity;
|
||||
use App\Models\CashRecord;
|
||||
use App\Models\Post;
|
||||
use App\Models\User;
|
||||
use App\Models\Vote;
|
||||
use App\Observers\ActivityObserver;
|
||||
use App\Observers\CashRecordObserver;
|
||||
use App\Observers\PostObserver;
|
||||
use App\Observers\UserObserver;
|
||||
use App\Observers\VoteObserver;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
@@ -20,5 +22,6 @@ class AppServiceProvider extends ServiceProvider
|
||||
CashRecord::observe(CashRecordObserver::class);
|
||||
Activity::observe(ActivityObserver::class);
|
||||
Vote::observe(VoteObserver::class);
|
||||
Post::observe(PostObserver::class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user