fix: sinkronisasi poin saat edit/detach kehadiran peserta kegiatan
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Filament\Resources\Activities\RelationManagers;
|
|||||||
use App\Models\MemberPoint;
|
use App\Models\MemberPoint;
|
||||||
use Filament\Actions\AttachAction;
|
use Filament\Actions\AttachAction;
|
||||||
use Filament\Actions\BulkActionGroup;
|
use Filament\Actions\BulkActionGroup;
|
||||||
|
use Filament\Actions\DetachAction;
|
||||||
use Filament\Actions\DetachBulkAction;
|
use Filament\Actions\DetachBulkAction;
|
||||||
use Filament\Actions\EditAction;
|
use Filament\Actions\EditAction;
|
||||||
use Filament\Forms\Components\Select;
|
use Filament\Forms\Components\Select;
|
||||||
@@ -63,7 +64,47 @@ class ParticipantsRelationManager extends RelationManager
|
|||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
])
|
])
|
||||||
->recordActions([EditAction::make()])
|
->recordActions([
|
||||||
->toolbarActions([BulkActionGroup::make([DetachBulkAction::make()])]);
|
EditAction::make()
|
||||||
|
->after(function (EditAction $action, $record, array $data) {
|
||||||
|
$activity = $this->getOwnerRecord();
|
||||||
|
$existing = MemberPoint::where('user_id', $record->id)
|
||||||
|
->where('source_type', 'activity')
|
||||||
|
->where('source_id', $activity->id)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if (($data['status'] ?? 'hadir') === 'hadir' && ! $existing) {
|
||||||
|
MemberPoint::create([
|
||||||
|
'user_id' => $record->id,
|
||||||
|
'points' => 10,
|
||||||
|
'reason' => "Hadir di kegiatan: {$activity->title}",
|
||||||
|
'source_type' => 'activity',
|
||||||
|
'source_id' => $activity->id,
|
||||||
|
]);
|
||||||
|
} elseif (($data['status'] ?? 'hadir') !== 'hadir' && $existing) {
|
||||||
|
$existing->delete();
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
DetachAction::make()
|
||||||
|
->after(function ($record) {
|
||||||
|
$activity = $this->getOwnerRecord();
|
||||||
|
MemberPoint::where('user_id', $record->id)
|
||||||
|
->where('source_type', 'activity')
|
||||||
|
->where('source_id', $activity->id)
|
||||||
|
->delete();
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
->toolbarActions([
|
||||||
|
BulkActionGroup::make([
|
||||||
|
DetachBulkAction::make()
|
||||||
|
->after(function ($records) {
|
||||||
|
$activity = $this->getOwnerRecord();
|
||||||
|
MemberPoint::where('source_type', 'activity')
|
||||||
|
->where('source_id', $activity->id)
|
||||||
|
->whereIn('user_id', $records->pluck('id'))
|
||||||
|
->delete();
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user