Add tier feature design spec, annual billing, fuel type normalization, and admin subscription management
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (8.3) (push) Has been cancelled
tests / ci (8.4) (push) Has been cancelled
tests / ci (8.5) (push) Has been cancelled

- Add comprehensive tier feature matrix spec defining Free/Basic/Plus/Pro capabilities across recommendations, predictions, history, logs, tools, and family sharing
- Add `stripe_price_id_annual` column to plans table and rename existing column to `stripe_price_id_monthly`
- Normalize legacy fuel type aliases (petrol→e10, diesel→b7_standard) in users table
- Implement BillingController with checkout, portal, success/cancel routes supporting monthly/annual cadence
- Add admin subscription assignment in Filament user edit page with admin-granted subscription support
- Add DowngradeUserOnSubscriptionDeleted listener to disable WhatsApp/SMS preferences on subscription cancellation
- Add MissedNotificationsOverview widget to Filament user detail page
- Add PollFuelPricesTest covering auto-refresh scenarios
- Add PriceReliability enum with reliability classification based on price age
- Add fuelTypes.js constants file exporting FUEL_TYPES from window global
This commit is contained in:
Ovidiu U
2026-04-20 14:13:03 +01:00
parent 5acb99c9e3
commit d29f3e6487
12 changed files with 680 additions and 0 deletions

View File

@@ -0,0 +1,120 @@
<?php
use App\Models\Station;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
uses(RefreshDatabase::class);
beforeEach(function (): void {
Cache::put('fuel_finder_access_token', 'tok', 3540);
});
it('auto-refreshes station metadata when the stations table is empty', function (): void {
Http::fake([
'*/pfs?*' => Http::sequence()
->push([[
'node_id' => 'sta1',
'trading_name' => 'Village Garage',
'brand_name' => 'Village Garage',
'is_same_trading_and_brand_name' => true,
'is_motorway_service_station' => false,
'is_supermarket_service_station' => false,
'temporary_closure' => false,
'permanent_closure' => false,
'permanent_closure_date' => null,
'public_phone_number' => null,
'location' => [
'address_line_1' => '1 High Street',
'address_line_2' => null,
'city' => 'London',
'county' => null,
'country' => 'England',
'postcode' => 'SW1A 1AA',
'latitude' => 51.5,
'longitude' => -0.1,
],
'amenities' => [],
'opening_times' => null,
'fuel_types' => ['E10'],
]])
->push([]),
'*/pfs/fuel-prices*' => Http::sequence()
->push([[
'node_id' => 'sta1',
'fuel_prices' => [[
'fuel_type' => 'E10',
'price' => 142.9,
'price_last_updated' => '2026-04-04T10:00:00.000Z',
'price_change_effective_timestamp' => '2026-04-04T10:00:00.000Z',
]],
]])
->push([]),
]);
$this->artisan('fuel:poll')->assertSuccessful();
expect(Station::find('sta1'))->not->toBeNull();
});
it('auto-refreshes station metadata when the last refresh was yesterday', function (): void {
Station::factory()->create([
'node_id' => 'sta1',
'last_seen_at' => now()->subDay()->endOfDay(),
]);
Http::fake([
'*/pfs?*' => Http::sequence()
->push([[
'node_id' => 'sta1',
'trading_name' => 'Refreshed Name',
'brand_name' => 'Refreshed Name',
'is_same_trading_and_brand_name' => true,
'is_motorway_service_station' => false,
'is_supermarket_service_station' => false,
'temporary_closure' => false,
'permanent_closure' => false,
'permanent_closure_date' => null,
'public_phone_number' => null,
'location' => [
'address_line_1' => '1 High Street',
'address_line_2' => null,
'city' => 'London',
'county' => null,
'country' => 'England',
'postcode' => 'SW1A 1AA',
'latitude' => 51.5,
'longitude' => -0.1,
],
'amenities' => [],
'opening_times' => null,
'fuel_types' => ['E10'],
]])
->push([]),
'*/pfs/fuel-prices*' => Http::sequence()
->push([])
->push([]),
]);
$this->artisan('fuel:poll')->assertSuccessful();
expect(Station::find('sta1')->trading_name)->toBe('Refreshed Name');
});
it('skips the station metadata refresh when already refreshed today', function (): void {
Station::factory()->create([
'node_id' => 'sta1',
'last_seen_at' => now(),
]);
Http::fake([
'*/pfs/fuel-prices*' => Http::sequence()
->push([])
->push([]),
]);
$this->artisan('fuel:poll')->assertSuccessful();
Http::assertNotSent(fn ($request) => str_contains($request->url(), '/pfs?'));
});

View File

@@ -0,0 +1,66 @@
<?php
use App\Filament\Resources\UserResource\Pages\EditUser;
use App\Models\Plan;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function (): void {
$this->artisan('db:seed', ['--class' => 'PlanSeeder']);
$admin = User::factory()->create(['is_admin' => true]);
$this->actingAs($admin);
});
it('creates an admin-granted subscription when a paid tier is chosen', function (): void {
$user = User::factory()->create();
Livewire::test(EditUser::class, ['record' => $user->id])
->fillForm(['tier' => 'basic', 'cadence' => 'monthly'])
->call('save')
->assertHasNoFormErrors();
$subscription = $user->subscriptions()->first();
expect($user->subscriptions()->count())->toBe(1)
->and($subscription->stripe_id)->toStartWith('admin_')
->and($subscription->stripe_status)->toBe('active')
->and(Plan::resolveForUser($user->fresh())->name)->toBe('basic');
});
it('removes any admin-granted subscription when tier is set to free', function (): void {
$user = User::factory()->create();
Livewire::test(EditUser::class, ['record' => $user->id])
->fillForm(['tier' => 'basic', 'cadence' => 'monthly'])
->call('save');
expect($user->subscriptions()->count())->toBe(1);
Livewire::test(EditUser::class, ['record' => $user->fresh()->id])
->fillForm(['tier' => 'free'])
->call('save');
expect($user->subscriptions()->count())->toBe(0)
->and(Plan::resolveForUser($user->fresh())->name)->toBe('free');
});
it('refuses to change tier when the user has a real Stripe subscription', function (): void {
$user = User::factory()->create();
$user->subscriptions()->create([
'type' => 'default',
'stripe_id' => 'sub_real_stripe_id',
'stripe_status' => 'active',
'stripe_price' => 'price_plus_monthly',
'quantity' => 1,
]);
Livewire::test(EditUser::class, ['record' => $user->id])
->fillForm(['tier' => 'pro', 'cadence' => 'monthly'])
->call('save');
$adminCount = $user->subscriptions()->where('stripe_id', 'like', 'admin_%')->count();
expect($adminCount)->toBe(0)
->and($user->subscriptions()->count())->toBe(1);
});

View File

@@ -0,0 +1,57 @@
<?php
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function (): void {
config([
'services.stripe.prices.basic.monthly' => 'price_test_basic_monthly',
'services.stripe.prices.basic.annual' => 'price_test_basic_annual',
'services.stripe.prices.plus.monthly' => 'price_test_plus_monthly',
'services.stripe.prices.plus.annual' => 'price_test_plus_annual',
'services.stripe.prices.pro.monthly' => null,
'services.stripe.prices.pro.annual' => null,
]);
});
it('requires authentication for the checkout route', function (): void {
$this->get('/billing/checkout/basic/monthly')->assertRedirect('/login');
});
it('requires authentication for the portal route', function (): void {
$this->get('/billing/portal')->assertRedirect('/login');
});
it('rejects an unknown tier', function (): void {
$this->actingAs(User::factory()->create())
->get('/billing/checkout/titanium/monthly')
->assertNotFound();
});
it('rejects an unknown cadence', function (): void {
$this->actingAs(User::factory()->create())
->get('/billing/checkout/basic/weekly')
->assertNotFound();
});
it('returns 404 when no price is configured for the tier + cadence', function (): void {
$this->actingAs(User::factory()->create())
->get('/billing/checkout/pro/monthly')
->assertNotFound();
});
it('redirects the success route to the dashboard', function (): void {
$this->actingAs(User::factory()->create())
->get('/billing/success')
->assertRedirect(route('dashboard'))
->assertSessionHas('status', 'subscription_started');
});
it('redirects the cancel route to the dashboard', function (): void {
$this->actingAs(User::factory()->create())
->get('/billing/cancel')
->assertRedirect(route('dashboard'))
->assertSessionHas('status', 'subscription_cancelled');
});

View File

@@ -0,0 +1,42 @@
<?php
use App\Listeners\DowngradeUserOnSubscriptionDeleted;
use App\Models\User;
use App\Models\UserNotificationPreference;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Cashier\Events\WebhookReceived;
uses(RefreshDatabase::class);
it('disables whatsapp and sms preferences when a stripe subscription is deleted', function (): void {
$user = User::factory()->create(['stripe_id' => 'cus_test_123']);
UserNotificationPreference::query()->insert([
['user_id' => $user->id, 'channel' => 'whatsapp', 'fuel_type' => 'E10', 'enabled' => true, 'created_at' => now(), 'updated_at' => now()],
['user_id' => $user->id, 'channel' => 'sms', 'fuel_type' => 'E10', 'enabled' => true, 'created_at' => now(), 'updated_at' => now()],
['user_id' => $user->id, 'channel' => 'email', 'fuel_type' => 'E10', 'enabled' => true, 'created_at' => now(), 'updated_at' => now()],
]);
(new DowngradeUserOnSubscriptionDeleted)->handle(new WebhookReceived([
'type' => 'customer.subscription.deleted',
'data' => ['object' => ['customer' => 'cus_test_123']],
]));
expect(UserNotificationPreference::where('user_id', $user->id)->where('channel', 'whatsapp')->value('enabled'))->toBeFalse();
expect(UserNotificationPreference::where('user_id', $user->id)->where('channel', 'sms')->value('enabled'))->toBeFalse();
expect(UserNotificationPreference::where('user_id', $user->id)->where('channel', 'email')->value('enabled'))->toBeTrue();
});
it('ignores non-deletion webhook events', function (): void {
$user = User::factory()->create(['stripe_id' => 'cus_test_456']);
UserNotificationPreference::query()->insert([
['user_id' => $user->id, 'channel' => 'whatsapp', 'fuel_type' => 'E10', 'enabled' => true, 'created_at' => now(), 'updated_at' => now()],
]);
(new DowngradeUserOnSubscriptionDeleted)->handle(new WebhookReceived([
'type' => 'customer.subscription.updated',
'data' => ['object' => ['customer' => 'cus_test_456']],
]));
expect(UserNotificationPreference::where('user_id', $user->id)->where('channel', 'whatsapp')->value('enabled'))->toBeTrue();
});