Add join token expiry action in event modal
This commit is contained in:
@@ -6,19 +6,23 @@ use App\Filament\Clusters\DailyOps\DailyOpsCluster;
|
||||
use App\Filament\Resources\EventResource\Pages;
|
||||
use App\Filament\Resources\EventResource\RelationManagers\EventPackagesRelationManager;
|
||||
use App\Models\Event;
|
||||
use App\Models\EventJoinToken;
|
||||
use App\Models\EventJoinTokenEvent;
|
||||
use App\Models\EventType;
|
||||
use App\Models\Tenant;
|
||||
use App\Services\Audit\SuperAdminAuditLogger;
|
||||
use App\Services\EventJoinTokenService;
|
||||
use App\Support\JoinTokenLayoutRegistry;
|
||||
use BackedEnum;
|
||||
use Carbon\Carbon;
|
||||
use Filament\Actions;
|
||||
use Filament\Forms\Components\DatePicker;
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\KeyValue;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables;
|
||||
@@ -164,7 +168,100 @@ class EventResource extends Resource
|
||||
->modalHeading(__('admin.events.modal.join_link_heading'))
|
||||
->modalSubmitActionLabel(__('admin.common.close'))
|
||||
->modalWidth('xl')
|
||||
->modalContent(function ($record) {
|
||||
->registerModalActions([
|
||||
Actions\Action::make('extend_join_token_expiry')
|
||||
->label(__('admin.events.join_link.extend_expiry'))
|
||||
->icon('heroicon-o-clock')
|
||||
->color('warning')
|
||||
->size('xs')
|
||||
->modalHeading(function (Actions\Action $action, Event $record): string {
|
||||
$token = static::resolveJoinTokenFromAction($record, $action);
|
||||
|
||||
return $token
|
||||
? __('admin.events.join_link.extend_expiry_heading', [
|
||||
'label' => $token->label ?: __('admin.events.join_link.token_default', ['id' => $token->id]),
|
||||
])
|
||||
: __('admin.events.join_link.extend_expiry_heading_fallback');
|
||||
})
|
||||
->schema(function (Event $record): array {
|
||||
$minimumExpiry = app(EventJoinTokenService::class)->minimumExpiryForEvent($record);
|
||||
$rules = [
|
||||
'date',
|
||||
'after:now',
|
||||
];
|
||||
|
||||
if ($minimumExpiry) {
|
||||
$rules[] = 'after_or_equal:'.$minimumExpiry->toDateTimeString();
|
||||
}
|
||||
|
||||
return [
|
||||
DateTimePicker::make('expires_at')
|
||||
->label(__('admin.events.join_link.extend_expiry_label'))
|
||||
->required()
|
||||
->seconds(false)
|
||||
->rules($rules)
|
||||
->helperText($minimumExpiry
|
||||
? __('admin.events.join_link.extend_expiry_min', [
|
||||
'date' => $minimumExpiry->isoFormat('LLL'),
|
||||
])
|
||||
: null),
|
||||
];
|
||||
})
|
||||
->fillForm(function (Actions\Action $action, Event $record): array {
|
||||
$token = static::resolveJoinTokenFromAction($record, $action);
|
||||
|
||||
if (! $token) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
'expires_at' => $token->expires_at,
|
||||
];
|
||||
})
|
||||
->action(function (array $data, Actions\Action $action, Event $record): void {
|
||||
$token = static::resolveJoinTokenFromAction($record, $action);
|
||||
|
||||
if (! $token) {
|
||||
Notification::make()
|
||||
->title(__('admin.events.join_link.extend_expiry_missing'))
|
||||
->danger()
|
||||
->send();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$expiresAt = $data['expires_at'] ?? null;
|
||||
|
||||
if (! $expiresAt) {
|
||||
Notification::make()
|
||||
->title(__('admin.events.join_link.extend_expiry_missing_date'))
|
||||
->danger()
|
||||
->send();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$resolvedExpiry = $expiresAt instanceof Carbon
|
||||
? $expiresAt
|
||||
: Carbon::parse($expiresAt);
|
||||
|
||||
$token->forceFill([
|
||||
'expires_at' => $resolvedExpiry,
|
||||
])->save();
|
||||
|
||||
app(SuperAdminAuditLogger::class)->recordModelMutation(
|
||||
'updated',
|
||||
$token,
|
||||
source: static::class
|
||||
);
|
||||
|
||||
Notification::make()
|
||||
->title(__('admin.events.join_link.extend_expiry_success'))
|
||||
->success()
|
||||
->send();
|
||||
}),
|
||||
])
|
||||
->modalContent(function (Actions\Action $action, $record) {
|
||||
$tokens = $record->joinTokens()
|
||||
->orderByDesc('created_at')
|
||||
->get();
|
||||
@@ -253,6 +350,7 @@ class EventResource extends Resource
|
||||
return view('filament.events.join-link', [
|
||||
'event' => $record,
|
||||
'tokens' => $tokens,
|
||||
'action' => $action,
|
||||
]);
|
||||
}),
|
||||
])
|
||||
@@ -303,6 +401,19 @@ class EventResource extends Resource
|
||||
return is_string($name) ? $name : '';
|
||||
}
|
||||
|
||||
private static function resolveJoinTokenFromAction(Event $record, Actions\Action $action): ?EventJoinToken
|
||||
{
|
||||
$tokenId = $action->getArguments()['token_id'] ?? null;
|
||||
|
||||
if (! $tokenId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $record->joinTokens()
|
||||
->whereKey($tokenId)
|
||||
->first();
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
|
||||
Reference in New Issue
Block a user