Skip to content

Commit 38ccd32

Browse files
simonhampclaude
andauthored
Add product licenses relation manager to user admin page (#290)
Shows all products a user has purchased or been comped, with filtering, creation (for comping), and deletion support. Also adds the missing is_comped migration for product_licenses and updates the sales view to reference the actual column. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent efc61e4 commit 38ccd32

6 files changed

Lines changed: 251 additions & 1 deletion

File tree

app/Filament/Resources/UserResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public static function getRelations(): array
119119
{
120120
return [
121121
RelationManagers\PluginLicensesRelationManager::class,
122+
RelationManagers\ProductLicensesRelationManager::class,
122123
RelationManagers\LicensesRelationManager::class,
123124
RelationManagers\SubscriptionsRelationManager::class,
124125
];
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace App\Filament\Resources\UserResource\RelationManagers;
4+
5+
use Filament\Forms;
6+
use Filament\Forms\Form;
7+
use Filament\Resources\RelationManagers\RelationManager;
8+
use Filament\Tables;
9+
use Filament\Tables\Table;
10+
11+
class ProductLicensesRelationManager extends RelationManager
12+
{
13+
protected static string $relationship = 'productLicenses';
14+
15+
protected static ?string $title = 'Products';
16+
17+
public function form(Form $form): Form
18+
{
19+
return $form
20+
->schema([
21+
Forms\Components\Select::make('product_id')
22+
->relationship('product', 'name')
23+
->searchable()
24+
->preload()
25+
->required(),
26+
Forms\Components\Toggle::make('is_comped')
27+
->label('Comped')
28+
->default(true),
29+
Forms\Components\DateTimePicker::make('purchased_at')
30+
->default(now()),
31+
]);
32+
}
33+
34+
public function table(Table $table): Table
35+
{
36+
return $table
37+
->columns([
38+
Tables\Columns\TextColumn::make('product.name')
39+
->label('Product')
40+
->searchable()
41+
->sortable()
42+
->fontFamily('mono'),
43+
Tables\Columns\TextColumn::make('price_paid')
44+
->label('Price Paid')
45+
->money('usd', divideBy: 100)
46+
->sortable(),
47+
Tables\Columns\IconColumn::make('is_comped')
48+
->label('Comped')
49+
->boolean(),
50+
Tables\Columns\TextColumn::make('purchased_at')
51+
->dateTime()
52+
->sortable(),
53+
])
54+
->defaultSort('purchased_at', 'desc')
55+
->filters([
56+
Tables\Filters\TernaryFilter::make('is_comped')
57+
->label('Comped'),
58+
])
59+
->headerActions([
60+
Tables\Actions\CreateAction::make()
61+
->mutateFormDataUsing(function (array $data): array {
62+
$data['price_paid'] = 0;
63+
$data['currency'] = 'USD';
64+
65+
return $data;
66+
}),
67+
])
68+
->actions([
69+
Tables\Actions\DeleteAction::make(),
70+
]);
71+
}
72+
}

app/Models/ProductLicense.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ protected function casts(): array
5353
{
5454
return [
5555
'price_paid' => 'integer',
56+
'is_comped' => 'boolean',
5657
'purchased_at' => 'datetime',
5758
];
5859
}

database/migrations/2026_03_19_190031_create_sales_view.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function up(): void
3333
NULL AS bundle_name,
3434
pdl.price_paid,
3535
pdl.currency,
36-
0 AS is_comped,
36+
pdl.is_comped,
3737
pdl.purchased_at,
3838
pdl.created_at,
3939
pdl.updated_at
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('product_licenses', function (Blueprint $table) {
15+
$table->boolean('is_comped')->default(false)->after('currency')->index();
16+
});
17+
}
18+
19+
/**
20+
* Reverse the migrations.
21+
*/
22+
public function down(): void
23+
{
24+
Schema::table('product_licenses', function (Blueprint $table) {
25+
$table->dropIndex(['is_comped']);
26+
$table->dropColumn('is_comped');
27+
});
28+
}
29+
};
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
3+
namespace Tests\Feature\Filament;
4+
5+
use App\Filament\Resources\UserResource\Pages\EditUser;
6+
use App\Filament\Resources\UserResource\RelationManagers\ProductLicensesRelationManager;
7+
use App\Models\Product;
8+
use App\Models\ProductLicense;
9+
use App\Models\User;
10+
use Illuminate\Foundation\Testing\RefreshDatabase;
11+
use Livewire\Livewire;
12+
use Tests\TestCase;
13+
14+
class ProductLicensesRelationManagerTest extends TestCase
15+
{
16+
use RefreshDatabase;
17+
18+
private User $admin;
19+
20+
private User $user;
21+
22+
protected function setUp(): void
23+
{
24+
parent::setUp();
25+
26+
$this->admin = User::factory()->create(['email' => 'admin@test.com']);
27+
config(['filament.users' => ['admin@test.com']]);
28+
29+
$this->user = User::factory()->create();
30+
}
31+
32+
public function test_it_lists_product_licenses_for_user(): void
33+
{
34+
$licenses = ProductLicense::factory()->count(3)->create([
35+
'user_id' => $this->user->id,
36+
]);
37+
38+
Livewire::actingAs($this->admin)
39+
->test(ProductLicensesRelationManager::class, [
40+
'ownerRecord' => $this->user,
41+
'pageClass' => EditUser::class,
42+
])
43+
->assertCanSeeTableRecords($licenses)
44+
->assertCountTableRecords(3);
45+
}
46+
47+
public function test_it_does_not_show_other_users_licenses(): void
48+
{
49+
$otherUser = User::factory()->create();
50+
51+
ProductLicense::factory()->create([
52+
'user_id' => $this->user->id,
53+
]);
54+
55+
ProductLicense::factory()->create([
56+
'user_id' => $otherUser->id,
57+
]);
58+
59+
Livewire::actingAs($this->admin)
60+
->test(ProductLicensesRelationManager::class, [
61+
'ownerRecord' => $this->user,
62+
'pageClass' => EditUser::class,
63+
])
64+
->assertCountTableRecords(1);
65+
}
66+
67+
public function test_it_shows_comped_status(): void
68+
{
69+
ProductLicense::factory()->create([
70+
'user_id' => $this->user->id,
71+
'is_comped' => true,
72+
'price_paid' => 0,
73+
]);
74+
75+
Livewire::actingAs($this->admin)
76+
->test(ProductLicensesRelationManager::class, [
77+
'ownerRecord' => $this->user,
78+
'pageClass' => EditUser::class,
79+
])
80+
->assertCountTableRecords(1);
81+
}
82+
83+
public function test_it_can_create_a_comped_product_license(): void
84+
{
85+
$product = Product::factory()->create();
86+
87+
Livewire::actingAs($this->admin)
88+
->test(ProductLicensesRelationManager::class, [
89+
'ownerRecord' => $this->user,
90+
'pageClass' => EditUser::class,
91+
])
92+
->callTableAction('create', data: [
93+
'product_id' => $product->id,
94+
'is_comped' => true,
95+
'purchased_at' => now()->toDateTimeString(),
96+
])
97+
->assertHasNoTableActionErrors();
98+
99+
$this->assertDatabaseHas('product_licenses', [
100+
'user_id' => $this->user->id,
101+
'product_id' => $product->id,
102+
'is_comped' => true,
103+
'price_paid' => 0,
104+
'currency' => 'USD',
105+
]);
106+
}
107+
108+
public function test_it_can_delete_a_product_license(): void
109+
{
110+
$license = ProductLicense::factory()->create([
111+
'user_id' => $this->user->id,
112+
]);
113+
114+
Livewire::actingAs($this->admin)
115+
->test(ProductLicensesRelationManager::class, [
116+
'ownerRecord' => $this->user,
117+
'pageClass' => EditUser::class,
118+
])
119+
->callTableAction('delete', $license)
120+
->assertHasNoTableActionErrors();
121+
122+
$this->assertDatabaseMissing('product_licenses', [
123+
'id' => $license->id,
124+
]);
125+
}
126+
127+
public function test_it_can_filter_by_comped_status(): void
128+
{
129+
ProductLicense::factory()->create([
130+
'user_id' => $this->user->id,
131+
'is_comped' => true,
132+
]);
133+
134+
ProductLicense::factory()->create([
135+
'user_id' => $this->user->id,
136+
'is_comped' => false,
137+
]);
138+
139+
Livewire::actingAs($this->admin)
140+
->test(ProductLicensesRelationManager::class, [
141+
'ownerRecord' => $this->user,
142+
'pageClass' => EditUser::class,
143+
])
144+
->filterTable('is_comped', true)
145+
->assertCountTableRecords(1);
146+
}
147+
}

0 commit comments

Comments
 (0)