|
10 | 10 | use App\Jobs\ReviewPluginRepository; |
11 | 11 | use App\Jobs\SyncPlugin; |
12 | 12 | use App\Models\Plugin; |
| 13 | +use App\Models\PluginLicense; |
| 14 | +use App\Models\User; |
| 15 | +use App\Notifications\PluginGranted; |
13 | 16 | use App\Notifications\PluginReviewChecksIncomplete; |
14 | 17 | use Filament\Forms; |
15 | 18 | use Filament\Forms\Form; |
@@ -272,6 +275,62 @@ public static function table(Table $table): Table |
272 | 275 | ->send(); |
273 | 276 | }), |
274 | 277 |
|
| 278 | + Tables\Actions\Action::make('grantToUser') |
| 279 | + ->label('Grant to User') |
| 280 | + ->icon('heroicon-o-gift') |
| 281 | + ->color('success') |
| 282 | + ->form([ |
| 283 | + Forms\Components\Select::make('user_id') |
| 284 | + ->label('User') |
| 285 | + ->searchable() |
| 286 | + ->getSearchResultsUsing(function (string $search): array { |
| 287 | + return User::query() |
| 288 | + ->where('name', 'like', "%{$search}%") |
| 289 | + ->orWhere('email', 'like', "%{$search}%") |
| 290 | + ->limit(50) |
| 291 | + ->get() |
| 292 | + ->mapWithKeys(fn (User $user) => [$user->id => "{$user->name} ({$user->email})"]) |
| 293 | + ->toArray(); |
| 294 | + }) |
| 295 | + ->required(), |
| 296 | + ]) |
| 297 | + ->action(function (Plugin $record, array $data): void { |
| 298 | + $user = User::findOrFail($data['user_id']); |
| 299 | + |
| 300 | + $existingLicense = $user->pluginLicenses() |
| 301 | + ->where('plugin_id', $record->id) |
| 302 | + ->exists(); |
| 303 | + |
| 304 | + if ($existingLicense) { |
| 305 | + Notification::make() |
| 306 | + ->title('User already has a license for this plugin') |
| 307 | + ->warning() |
| 308 | + ->send(); |
| 309 | + |
| 310 | + return; |
| 311 | + } |
| 312 | + |
| 313 | + PluginLicense::create([ |
| 314 | + 'user_id' => $user->id, |
| 315 | + 'plugin_id' => $record->id, |
| 316 | + 'price_paid' => 0, |
| 317 | + 'currency' => 'USD', |
| 318 | + 'is_grandfathered' => true, |
| 319 | + 'purchased_at' => now(), |
| 320 | + ]); |
| 321 | + |
| 322 | + $user->getPluginLicenseKey(); |
| 323 | + $user->notify(new PluginGranted($record)); |
| 324 | + |
| 325 | + Notification::make() |
| 326 | + ->title("Granted '{$record->name}' license to {$user->name}") |
| 327 | + ->success() |
| 328 | + ->send(); |
| 329 | + }) |
| 330 | + ->modalHeading('Grant Plugin to User') |
| 331 | + ->modalDescription(fn (Plugin $record): string => "Grant '{$record->name}' to a user for free.") |
| 332 | + ->modalSubmitActionLabel('Grant'), |
| 333 | + |
275 | 334 | Tables\Actions\Action::make('runReviewChecks') |
276 | 335 | ->label('Run Review Checks') |
277 | 336 | ->icon('heroicon-o-clipboard-document-check') |
|
0 commit comments