Remove obsolete Livewire fuel search components and consolidate pricing tiers
- Delete unused Livewire Search test and fuel type select Blade component - Move subscription webhook listener from EventServiceProvider to AppServiceProvider - Add FUEL_TYPES global config to app layout for client-side use - Add Billable trait to User model and include email_verified_at in fillable - Implement monthly/annual cadence toggle with pricing display and smart CTA routing on homepage - Update VerifyApiKeyMiddlewareTest to use e10 instead of petrol - Refactor PollFuelPrices to auto-refresh stale stations based on last_seen_at - Add incremental polling with cached timestamp and effective-start-timestamp param to FuelPriceService - Normalize amenities/fuel_types from API objects to flat arrays, skip stations missing required fields - Log response body on API failures in ApiLogger - Default homepage sort to 'reliable' instead of 'price'
This commit is contained in:
@@ -2,20 +2,28 @@
|
||||
|
||||
namespace App\Filament\Resources;
|
||||
|
||||
use App\Enums\FuelType;
|
||||
use App\Enums\PlanTier;
|
||||
use App\Filament\NavigationGroup;
|
||||
use App\Filament\Resources\UserResource\Pages\EditUser;
|
||||
use App\Filament\Resources\UserResource\Pages\ListUsers;
|
||||
use App\Models\Plan;
|
||||
use App\Models\User;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\TernaryFilter;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class UserResource extends Resource
|
||||
{
|
||||
@@ -28,12 +36,89 @@ class UserResource extends Resource
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema->components([
|
||||
Toggle::make('is_admin')
|
||||
->label('Admin')
|
||||
->helperText('Grants access to this admin panel.'),
|
||||
TextInput::make('postcode')
|
||||
->label('Postcode')
|
||||
->maxLength(8),
|
||||
Section::make('Profile')->columns(2)->schema([
|
||||
TextInput::make('name')
|
||||
->required()
|
||||
->maxLength(255),
|
||||
TextInput::make('email')
|
||||
->email()
|
||||
->required()
|
||||
->maxLength(255),
|
||||
TextInput::make('postcode')
|
||||
->maxLength(8),
|
||||
Select::make('preferred_fuel_type')
|
||||
->label('Preferred fuel type')
|
||||
->options(collect(FuelType::cases())
|
||||
->mapWithKeys(fn (FuelType $t) => [$t->value => $t->label()])
|
||||
->all())
|
||||
->required(),
|
||||
]),
|
||||
|
||||
Section::make('Access')->columns(2)->schema([
|
||||
Toggle::make('is_admin')
|
||||
->label('Admin')
|
||||
->helperText('Grants access to this admin panel.'),
|
||||
DateTimePicker::make('email_verified_at')
|
||||
->label('Email verified at'),
|
||||
]),
|
||||
|
||||
Section::make('Subscription')->columns(2)->schema([
|
||||
Select::make('tier')
|
||||
->label('Tier')
|
||||
->options([
|
||||
PlanTier::Free->value => 'Free',
|
||||
PlanTier::Basic->value => 'Basic',
|
||||
PlanTier::Plus->value => 'Plus',
|
||||
PlanTier::Pro->value => 'Pro',
|
||||
])
|
||||
->required()
|
||||
->live()
|
||||
->dehydrated(false)
|
||||
->afterStateHydrated(fn (Select $component, ?User $record) => $component
|
||||
->state($record ? Plan::resolveForUser($record)->name : PlanTier::Free->value)),
|
||||
Select::make('cadence')
|
||||
->label('Billing Cadence')
|
||||
->options([
|
||||
'monthly' => 'Monthly',
|
||||
'annual' => 'Annual',
|
||||
])
|
||||
->default('monthly')
|
||||
->dehydrated(false)
|
||||
->visible(fn (callable $get): bool => ($get('tier') ?? '') !== PlanTier::Free->value)
|
||||
->helperText('Only applies when assigning a paid tier. Real Stripe subscriptions are not modified.'),
|
||||
]),
|
||||
|
||||
Section::make('Security')->columns(2)->schema([
|
||||
DateTimePicker::make('two_factor_confirmed_at')
|
||||
->label('2FA confirmed at')
|
||||
->disabled(),
|
||||
TextInput::make('password')
|
||||
->label('Set new password')
|
||||
->password()
|
||||
->revealable()
|
||||
->minLength(8)
|
||||
->dehydrated(fn (?string $state): bool => filled($state))
|
||||
->dehydrateStateUsing(fn (string $state): string => bcrypt($state))
|
||||
->helperText('Leave blank to keep current password.')
|
||||
->afterStateHydrated(fn (TextInput $component) => $component->state(null)),
|
||||
]),
|
||||
|
||||
Section::make('Billing')->columns(3)->schema([
|
||||
TextInput::make('stripe_id')
|
||||
->label('Stripe customer ID')
|
||||
->disabled(),
|
||||
TextInput::make('pm_type')
|
||||
->label('Payment method')
|
||||
->disabled(),
|
||||
TextInput::make('pm_last_four')
|
||||
->label('Card last 4')
|
||||
->disabled(),
|
||||
]),
|
||||
|
||||
Section::make('Timestamps')->columns(2)->schema([
|
||||
DateTimePicker::make('created_at')->disabled(),
|
||||
DateTimePicker::make('updated_at')->disabled(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -44,6 +129,16 @@ class UserResource extends Resource
|
||||
TextColumn::make('name')->searchable()->sortable(),
|
||||
TextColumn::make('email')->searchable()->sortable(),
|
||||
TextColumn::make('postcode')->placeholder('—'),
|
||||
TextColumn::make('tier')
|
||||
->label('Tier')
|
||||
->state(fn (User $record): string => Plan::resolveForUser($record)->name)
|
||||
->badge()
|
||||
->colors([
|
||||
'gray' => 'free',
|
||||
'primary' => 'basic',
|
||||
'warning' => 'plus',
|
||||
'success' => 'pro',
|
||||
]),
|
||||
IconColumn::make('is_admin')
|
||||
->label('Admin')
|
||||
->boolean(),
|
||||
@@ -62,6 +157,53 @@ class UserResource extends Resource
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel any existing admin-granted subscription, then (if a paid tier
|
||||
* was requested) insert a fresh synthetic active subscription row.
|
||||
*/
|
||||
public static function applyTier(User $user, string $tier, string $cadence): void
|
||||
{
|
||||
$hasRealStripeSubscription = $user->subscriptions()
|
||||
->where('stripe_id', 'not like', 'admin_%')
|
||||
->whereIn('stripe_status', ['active', 'trialing', 'past_due'])
|
||||
->exists();
|
||||
|
||||
if ($hasRealStripeSubscription) {
|
||||
throw new \RuntimeException(
|
||||
"User {$user->email} has an active Stripe subscription — modify it through the Stripe dashboard, not the admin panel."
|
||||
);
|
||||
}
|
||||
|
||||
$user->subscriptions()->where('stripe_id', 'like', 'admin_%')->delete();
|
||||
|
||||
if ($tier === PlanTier::Free->value) {
|
||||
self::bustPlanCache($user);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$priceId = config("services.stripe.prices.{$tier}.{$cadence}") ?? "price_admin_{$tier}_{$cadence}";
|
||||
|
||||
$user->subscriptions()->create([
|
||||
'type' => 'default',
|
||||
'stripe_id' => 'admin_'.Str::uuid(),
|
||||
'stripe_status' => 'active',
|
||||
'stripe_price' => $priceId,
|
||||
'quantity' => 1,
|
||||
]);
|
||||
|
||||
self::bustPlanCache($user);
|
||||
}
|
||||
|
||||
protected static function bustPlanCache(User $user): void
|
||||
{
|
||||
if (Cache::supportsTags()) {
|
||||
Cache::tags(['plans'])->flush();
|
||||
} else {
|
||||
Cache::forget("plan_for_user_{$user->id}");
|
||||
}
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
|
||||
Reference in New Issue
Block a user