72 lines
2.3 KiB
PHP
72 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\Users\Pages;
|
|
|
|
use App\Domain\Catalog\Offer;
|
|
use App\Domain\Entitlement\GrantIssuer;
|
|
use App\Domain\Identity\StaffRole;
|
|
use App\Filament\Resources\Users\UserResource;
|
|
use App\Filament\Support\CatalogOptions;
|
|
use App\Filament\Support\ReportsDomainRules;
|
|
use App\Models\User;
|
|
use Filament\Actions\Action;
|
|
use Filament\Facades\Filament;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\Textarea;
|
|
use Filament\Resources\Pages\EditRecord;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class EditUser extends EditRecord
|
|
{
|
|
use ReportsDomainRules;
|
|
|
|
protected static string $resource = UserResource::class;
|
|
|
|
protected function getHeaderActions(): array
|
|
{
|
|
return [
|
|
Action::make('grant')
|
|
->label('Berechtigung vergeben')
|
|
->visible(function (): bool {
|
|
$actor = Filament::auth()->user();
|
|
|
|
return $actor instanceof User && $actor->hasStaffRole(StaffRole::Kundenbetreuung);
|
|
})
|
|
->schema([
|
|
Select::make('offer_id')->label('Angebot')->options(fn (): array => CatalogOptions::offers())->required(),
|
|
Textarea::make('reason')->label('Begründung')->required(),
|
|
])
|
|
->action(function (array $data): void {
|
|
$this->report(function () use ($data): void {
|
|
app(GrantIssuer::class)->grantManually(
|
|
$this->record,
|
|
Offer::query()->findOrFail($data['offer_id']),
|
|
Filament::auth()->user(),
|
|
$data['reason'],
|
|
);
|
|
}, 'Die Berechtigung ist vergeben.');
|
|
}),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function mutateFormDataBeforeFill(array $data): array
|
|
{
|
|
$data['roles'] = $this->record->staffRoles->map(fn ($assignment) => $assignment->role->value)->all();
|
|
$data['status'] = $this->record->status->value;
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
protected function handleRecordUpdate(Model $record, array $data): Model
|
|
{
|
|
return UserResource::persist($record, $data, false);
|
|
}
|
|
}
|