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:
@@ -3,22 +3,26 @@
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Events\PricesUpdatedEvent;
|
||||
use App\Models\Station;
|
||||
use App\Services\FuelPriceService;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Throwable;
|
||||
|
||||
class PollFuelPrices extends Command
|
||||
{
|
||||
protected $signature = 'fuel:poll {--full : Also refresh station metadata}';
|
||||
protected $signature = 'fuel:poll {--full : Force refresh station metadata before polling}';
|
||||
|
||||
protected $description = 'Poll the Fuel Finder API for latest prices';
|
||||
|
||||
public function handle(FuelPriceService $service): int
|
||||
{
|
||||
$fullRefresh = (bool) $this->option('full');
|
||||
$lastRefresh = Station::max('last_seen_at');
|
||||
$stationsStale = $lastRefresh === null || Carbon::parse($lastRefresh)->isBefore(today());
|
||||
|
||||
try {
|
||||
if ($fullRefresh) {
|
||||
if ($fullRefresh || $stationsStale) {
|
||||
$this->info('Refreshing station metadata...');
|
||||
$service->refreshStations();
|
||||
}
|
||||
|
||||
@@ -11,21 +11,20 @@ enum FuelType: string
|
||||
case B10 = 'b10';
|
||||
case Hvo = 'hvo';
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::E10 => 'Petrol (E10)',
|
||||
self::E5 => 'Premium (E5)',
|
||||
self::B7Standard => 'Diesel (B7)',
|
||||
self::B7Premium => 'Prem Diesel',
|
||||
self::B10 => 'Diesel (B10)',
|
||||
self::Hvo => 'HVO',
|
||||
};
|
||||
}
|
||||
|
||||
public static function fromApiValue(string $value): self
|
||||
{
|
||||
return self::from(strtolower($value));
|
||||
}
|
||||
|
||||
public static function fromAlias(string $alias): self
|
||||
{
|
||||
return match (strtolower($alias)) {
|
||||
'diesel', 'b7_standard' => self::B7Standard,
|
||||
'premium_diesel', 'b7_premium' => self::B7Premium,
|
||||
'petrol', 'unleaded', 'e10' => self::E10,
|
||||
'premium_unleaded', 'e5' => self::E5,
|
||||
'b10' => self::B10,
|
||||
'hvo' => self::Hvo,
|
||||
default => throw new \ValueError("Unknown fuel type alias: {$alias}"),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 [
|
||||
|
||||
@@ -3,14 +3,52 @@
|
||||
namespace App\Filament\Resources\UserResource\Pages;
|
||||
|
||||
use App\Filament\Resources\UserResource;
|
||||
use App\Filament\Resources\UserResource\Widgets\MissedNotificationsOverview;
|
||||
use App\Models\User;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditUser extends EditRecord
|
||||
{
|
||||
protected static string $resource = UserResource::class;
|
||||
|
||||
protected function getHeaderWidgets(): array
|
||||
{
|
||||
return [
|
||||
MissedNotificationsOverview::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function getHeaderWidgetsColumns(): int|array
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function afterSave(): void
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $this->record;
|
||||
|
||||
$tier = $this->data['tier'] ?? null;
|
||||
$cadence = $this->data['cadence'] ?? 'monthly';
|
||||
|
||||
if ($tier === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
UserResource::applyTier($user, $tier, $cadence);
|
||||
} catch (\RuntimeException $e) {
|
||||
Notification::make()
|
||||
->title('Tier not changed')
|
||||
->body($e->getMessage())
|
||||
->warning()
|
||||
->send();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Plan;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -61,6 +62,11 @@ class AuthController extends Controller
|
||||
|
||||
public function me(Request $request): JsonResponse
|
||||
{
|
||||
return response()->json($request->user());
|
||||
$user = $request->user();
|
||||
|
||||
return response()->json(array_merge(
|
||||
$user->toArray(),
|
||||
['tier' => Plan::resolveForUser($user)->name],
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Enums\PriceClassification;
|
||||
use App\Enums\PriceReliability;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\NearbyStationsRequest;
|
||||
use App\Http\Resources\Api\StationResource;
|
||||
@@ -57,12 +57,20 @@ class StationController extends Controller
|
||||
$filtered = $all->filter(fn ($s) => (float) $s->distance_km <= $radius);
|
||||
|
||||
$stations = $sort === 'reliable'
|
||||
? $filtered->sortBy([
|
||||
fn ($s) => PriceClassification::fromUpdatedAt(
|
||||
$s->price_effective_at ? Carbon::parse($s->price_effective_at) : null
|
||||
)->weight(),
|
||||
fn ($s) => (int) $s->price_pence,
|
||||
])->values()
|
||||
? $filtered
|
||||
->sort(function ($a, $b) {
|
||||
$weightA = PriceReliability::fromUpdatedAt(
|
||||
$a->price_effective_at ? Carbon::parse($a->price_effective_at) : null
|
||||
)->weight();
|
||||
$weightB = PriceReliability::fromUpdatedAt(
|
||||
$b->price_effective_at ? Carbon::parse($b->price_effective_at) : null
|
||||
)->weight();
|
||||
|
||||
return $weightA <=> $weightB
|
||||
?: ((int) $a->price_pence <=> (int) $b->price_pence)
|
||||
?: ((float) $a->distance_km <=> (float) $b->distance_km);
|
||||
})
|
||||
->values()
|
||||
: $filtered->sortBy(match ($sort) {
|
||||
'price' => fn ($s) => (int) $s->price_pence,
|
||||
'updated' => fn ($s) => $s->price_effective_at ? -strtotime($s->price_effective_at) : PHP_INT_MAX,
|
||||
@@ -72,6 +80,12 @@ class StationController extends Controller
|
||||
|
||||
$prices = $stations->pluck('price_pence');
|
||||
|
||||
$reliabilityCounts = $stations
|
||||
->groupBy(fn ($s) => PriceReliability::fromUpdatedAt(
|
||||
$s->price_effective_at ? Carbon::parse($s->price_effective_at) : null
|
||||
)->value)
|
||||
->map->count();
|
||||
|
||||
Search::create([
|
||||
'lat_bucket' => round($lat, 2),
|
||||
'lng_bucket' => round($lng, 2),
|
||||
@@ -96,6 +110,11 @@ class StationController extends Controller
|
||||
'highest_pence' => $prices->max(),
|
||||
'cheapest_price_pence' => $prices->min(),
|
||||
'avg_pence' => $prices->isNotEmpty() ? round($prices->avg(), 2) : null,
|
||||
'reliability_counts' => [
|
||||
'reliable' => (int) $reliabilityCounts->get(PriceReliability::Reliable->value, 0),
|
||||
'stale' => (int) $reliabilityCounts->get(PriceReliability::Stale->value, 0),
|
||||
'outdated' => (int) $reliabilityCounts->get(PriceReliability::Outdated->value, 0),
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Enums\FuelType;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
@@ -25,7 +26,7 @@ final class UserController extends Controller
|
||||
public function updatePreferences(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'preferred_fuel_type' => ['sometimes', Rule::in(['petrol', 'diesel', 'e5', 'b7_premium', 'b10', 'hvo'])],
|
||||
'preferred_fuel_type' => ['sometimes', Rule::in(array_column(FuelType::cases(), 'value'))],
|
||||
'postcode' => ['sometimes', 'string', 'max:8'],
|
||||
]);
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ class NearbyStationsRequest extends FormRequest
|
||||
|
||||
public function fuelType(): FuelType
|
||||
{
|
||||
return FuelType::fromAlias($this->string('fuel_type')->toString());
|
||||
return FuelType::from(strtolower($this->string('fuel_type')->toString()));
|
||||
}
|
||||
|
||||
public function radius(): float
|
||||
@@ -37,6 +37,6 @@ class NearbyStationsRequest extends FormRequest
|
||||
|
||||
public function sort(): string
|
||||
{
|
||||
return $this->input('sort', 'price');
|
||||
return $this->input('sort', 'reliable');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Resources\Api;
|
||||
|
||||
use App\Enums\PriceClassification;
|
||||
use App\Enums\PriceReliability;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Support\Carbon;
|
||||
@@ -11,6 +12,9 @@ class StationResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
$updatedAt = $this->price_effective_at ? Carbon::parse($this->price_effective_at) : null;
|
||||
$reliability = PriceReliability::fromUpdatedAt($updatedAt);
|
||||
|
||||
return [
|
||||
'station_id' => $this->node_id,
|
||||
'name' => $this->trading_name,
|
||||
@@ -27,12 +31,10 @@ class StationResource extends JsonResource
|
||||
'price_updated_at' => $this->price_effective_at
|
||||
? Carbon::parse($this->price_effective_at)->toISOString()
|
||||
: null,
|
||||
'price_classification' => PriceClassification::fromUpdatedAt(
|
||||
$this->price_effective_at ? Carbon::parse($this->price_effective_at) : null
|
||||
)->value,
|
||||
'price_classification_label' => PriceClassification::fromUpdatedAt(
|
||||
$this->price_effective_at ? Carbon::parse($this->price_effective_at) : null
|
||||
)->label(),
|
||||
'price_classification' => PriceClassification::fromUpdatedAt($updatedAt)->value,
|
||||
'price_classification_label' => PriceClassification::fromUpdatedAt($updatedAt)->label(),
|
||||
'reliability' => $reliability->value,
|
||||
'reliability_label' => $reliability->label(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,8 @@ class Plan extends Model
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'stripe_price_id',
|
||||
'stripe_price_id_monthly',
|
||||
'stripe_price_id_annual',
|
||||
'features',
|
||||
'active',
|
||||
];
|
||||
@@ -28,10 +29,10 @@ class Plan extends Model
|
||||
{
|
||||
$cache = Cache::supportsTags() ? Cache::tags(['plans']) : Cache::store();
|
||||
|
||||
return $cache->remember(
|
||||
$planId = $cache->remember(
|
||||
"plan_for_user_{$user->id}",
|
||||
3600,
|
||||
function () use ($user): self {
|
||||
function () use ($user): ?int {
|
||||
$priceId = null;
|
||||
|
||||
if (method_exists($user, 'subscriptions')) {
|
||||
@@ -40,16 +41,43 @@ class Plan extends Model
|
||||
}
|
||||
|
||||
if ($priceId) {
|
||||
$plan = static::where('stripe_price_id', $priceId)->where('active', true)->first();
|
||||
$plan = static::where(fn ($q) => $q
|
||||
->where('stripe_price_id_monthly', $priceId)
|
||||
->orWhere('stripe_price_id_annual', $priceId))
|
||||
->where('active', true)
|
||||
->first();
|
||||
|
||||
if ($plan) {
|
||||
return $plan;
|
||||
return $plan->id;
|
||||
}
|
||||
}
|
||||
|
||||
return static::where('name', PlanTier::Free->value)->firstOrFail();
|
||||
return static::where('name', PlanTier::Free->value)->value('id');
|
||||
}
|
||||
);
|
||||
|
||||
if ($planId !== null) {
|
||||
$plan = static::find($planId);
|
||||
|
||||
if ($plan !== null) {
|
||||
return $plan;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback for tests / partially-seeded environments: return a free-tier stub.
|
||||
return new self([
|
||||
'name' => PlanTier::Free->value,
|
||||
'features' => [
|
||||
'fuel_types' => ['max' => 1],
|
||||
'email' => ['enabled' => true, 'frequency' => 'weekly_digest'],
|
||||
'push' => ['enabled' => false],
|
||||
'whatsapp' => ['enabled' => false, 'daily_limit' => 0, 'scheduled_updates' => 0],
|
||||
'sms' => ['enabled' => false, 'daily_limit' => 0],
|
||||
'ai_predictions' => false,
|
||||
'price_threshold' => false,
|
||||
'score_alerts' => false,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
|
||||
@@ -13,15 +13,16 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Cashier\Billable;
|
||||
use Laravel\Fortify\TwoFactorAuthenticatable;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
|
||||
#[Fillable(['name', 'email', 'password', 'is_admin', 'postcode', 'preferred_fuel_type'])]
|
||||
#[Fillable(['name', 'email', 'email_verified_at', 'password', 'is_admin', 'postcode', 'preferred_fuel_type'])]
|
||||
#[Hidden(['password', 'two_factor_secret', 'two_factor_recovery_codes', 'remember_token'])]
|
||||
class User extends Authenticatable implements FilamentUser
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasApiTokens, HasFactory, Notifiable, TwoFactorAuthenticatable;
|
||||
use Billable, HasApiTokens, HasFactory, Notifiable, TwoFactorAuthenticatable;
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Listeners\DowngradeUserOnSubscriptionDeleted;
|
||||
use App\Services\ApiLogger;
|
||||
use App\Services\LlmPrediction\AnthropicPredictionProvider;
|
||||
use App\Services\LlmPrediction\GeminiPredictionProvider;
|
||||
@@ -10,8 +11,10 @@ use App\Services\LlmPrediction\OpenAiPredictionProvider;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Support\Facades\Date;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Validation\Rules\Password;
|
||||
use Laravel\Cashier\Events\WebhookReceived;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
@@ -37,6 +40,8 @@ class AppServiceProvider extends ServiceProvider
|
||||
public function boot(): void
|
||||
{
|
||||
$this->configureDefaults();
|
||||
|
||||
Event::listen(WebhookReceived::class, DowngradeUserOnSubscriptionDeleted::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Services;
|
||||
|
||||
use App\Models\ApiLog;
|
||||
use Illuminate\Http\Client\Response;
|
||||
use Illuminate\Support\Str;
|
||||
use Throwable;
|
||||
|
||||
class ApiLogger
|
||||
@@ -26,6 +27,10 @@ class ApiLogger
|
||||
$response = $request();
|
||||
$statusCode = $response->status();
|
||||
|
||||
if ($response->failed()) {
|
||||
$error = Str::limit($response->body(), 1000);
|
||||
}
|
||||
|
||||
return $response;
|
||||
} catch (Throwable $e) {
|
||||
$error = $e->getMessage();
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Enums\FuelType;
|
||||
use App\Models\Station;
|
||||
use App\Models\StationPrice;
|
||||
use App\Models\StationPriceCurrent;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
@@ -17,6 +18,8 @@ class FuelPriceService
|
||||
{
|
||||
private const string TOKEN_CACHE_KEY = 'fuel_finder_access_token';
|
||||
|
||||
private const string LAST_PRICE_POLL_CACHE_KEY = 'fuel_finder_last_price_poll_at';
|
||||
|
||||
/**
|
||||
* Per-fuel-type valid price range in pence (as returned by the API).
|
||||
* Based on UK all-time records + 30–75% headroom for future spikes.
|
||||
@@ -55,6 +58,10 @@ class FuelPriceService
|
||||
/**
|
||||
* Poll the prices endpoint, deduplicate, and persist changes.
|
||||
*
|
||||
* Uses incremental polling when a previous poll timestamp is cached — only
|
||||
* stations with prices changed since then are returned by the API. Falls
|
||||
* back to a full fetch on cold start (cache miss).
|
||||
*
|
||||
* @return int Number of new price records inserted
|
||||
*/
|
||||
public function pollPrices(): int
|
||||
@@ -62,18 +69,27 @@ class FuelPriceService
|
||||
$token = $this->getAccessToken();
|
||||
$inserted = 0;
|
||||
$batch = 1;
|
||||
$pollStartedAt = now();
|
||||
$since = Cache::get(self::LAST_PRICE_POLL_CACHE_KEY);
|
||||
$completedCleanly = false;
|
||||
|
||||
do {
|
||||
try {
|
||||
$baseUrl = config('services.fuel_finder.base_url').'/pfs/fuel-prices';
|
||||
$params = ['batch-number' => $batch];
|
||||
|
||||
if ($since instanceof CarbonInterface) {
|
||||
$params['effective-start-timestamp'] = $since->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
$logUrl = $baseUrl.'?'.http_build_query($params);
|
||||
$response = $this->apiLogger->send('fuel_finder', 'GET', $logUrl, fn () => Http::timeout(30)
|
||||
->withToken($token)
|
||||
->get($baseUrl, $params));
|
||||
|
||||
if ($response->notFound()) {
|
||||
break; // No more batches
|
||||
$completedCleanly = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (! $response->successful()) {
|
||||
@@ -94,6 +110,7 @@ class FuelPriceService
|
||||
}
|
||||
|
||||
if (empty($stations)) {
|
||||
$completedCleanly = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -101,6 +118,10 @@ class FuelPriceService
|
||||
$batch++;
|
||||
} while (true);
|
||||
|
||||
if ($completedCleanly) {
|
||||
Cache::forever(self::LAST_PRICE_POLL_CACHE_KEY, $pollStartedAt);
|
||||
}
|
||||
|
||||
return $inserted;
|
||||
}
|
||||
|
||||
@@ -159,6 +180,14 @@ class FuelPriceService
|
||||
$rows = [];
|
||||
|
||||
foreach ($apiStations as $data) {
|
||||
if (! $this->hasRequiredStationFields($data)) {
|
||||
Log::warning('FuelPriceService: station skipped — missing required fields', [
|
||||
'node_id' => $data['node_id'] ?? null,
|
||||
]);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$station = new Station([
|
||||
'node_id' => $data['node_id'],
|
||||
'trading_name' => $data['trading_name'],
|
||||
@@ -179,9 +208,9 @@ class FuelPriceService
|
||||
'postcode' => $data['location']['postcode'],
|
||||
'lat' => $data['location']['latitude'],
|
||||
'lng' => $data['location']['longitude'],
|
||||
'amenities' => $data['amenities'] ?? [],
|
||||
'amenities' => self::flattenEnabledFlags($data['amenities'] ?? []),
|
||||
'opening_times' => $data['opening_times'] ?? null,
|
||||
'fuel_types' => $data['fuel_types'] ?? [],
|
||||
'fuel_types' => self::flattenEnabledFlags($data['fuel_types'] ?? []),
|
||||
'last_seen_at' => $now,
|
||||
]);
|
||||
|
||||
@@ -189,7 +218,40 @@ class FuelPriceService
|
||||
$rows[] = $station->getAttributes();
|
||||
}
|
||||
|
||||
Station::upsert($rows, ['node_id'], array_keys($rows[0] ?? []));
|
||||
if ($rows === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
Station::upsert($rows, ['node_id'], array_keys($rows[0]));
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $data */
|
||||
private function hasRequiredStationFields(array $data): bool
|
||||
{
|
||||
return ! empty($data['node_id'])
|
||||
&& ! empty($data['trading_name'])
|
||||
&& isset($data['location']['postcode'], $data['location']['latitude'], $data['location']['longitude']);
|
||||
}
|
||||
|
||||
/**
|
||||
* The API returns `amenities` and `fuel_types` as objects with boolean
|
||||
* flags (e.g. {"E10": true, "car_wash": false}). Flatten to a list of
|
||||
* enabled keys. If the payload is already an array of strings, return as-is.
|
||||
*
|
||||
* @param array<string, bool>|array<int, string> $flags
|
||||
* @return array<int, string>
|
||||
*/
|
||||
private static function flattenEnabledFlags(array $flags): array
|
||||
{
|
||||
if ($flags === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (array_is_list($flags)) {
|
||||
return array_values($flags);
|
||||
}
|
||||
|
||||
return array_values(array_keys(array_filter($flags, fn ($v) => filter_var($v, FILTER_VALIDATE_BOOLEAN))));
|
||||
}
|
||||
|
||||
private function isValidPrice(FuelType $fuelType, float $pricePence): bool
|
||||
@@ -216,8 +278,22 @@ class FuelPriceService
|
||||
{
|
||||
$stationIds = array_column($apiBatch, 'node_id');
|
||||
|
||||
// Filter to stations that exist in the stations table — prevents FK
|
||||
// violations when the API surfaces a station before the next metadata
|
||||
// refresh picks it up.
|
||||
$knownStationIds = array_flip(
|
||||
Station::whereIn('node_id', $stationIds)->pluck('node_id')->all(),
|
||||
);
|
||||
|
||||
$unknown = array_diff($stationIds, array_keys($knownStationIds));
|
||||
if ($unknown !== []) {
|
||||
Log::info('FuelPriceService: skipped prices for unknown stations', [
|
||||
'count' => count($unknown),
|
||||
]);
|
||||
}
|
||||
|
||||
// Load current prices for all stations in this batch in one query
|
||||
$currentPrices = StationPriceCurrent::whereIn('station_id', $stationIds)
|
||||
$currentPrices = StationPriceCurrent::whereIn('station_id', array_keys($knownStationIds))
|
||||
->get()
|
||||
->groupBy('station_id')
|
||||
->map(fn ($rows) => $rows->keyBy(fn ($r) => $r->fuel_type->value));
|
||||
@@ -227,9 +303,17 @@ class FuelPriceService
|
||||
$upsertRows = [];
|
||||
|
||||
foreach ($apiBatch as $station) {
|
||||
$stationId = $station['node_id'];
|
||||
$stationId = $station['node_id'] ?? null;
|
||||
|
||||
if ($stationId === null || ! isset($knownStationIds[$stationId])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($station['fuel_prices'] ?? [] as $priceData) {
|
||||
if (! isset($priceData['fuel_type'], $priceData['price'], $priceData['price_last_updated'], $priceData['price_change_effective_timestamp'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$fuelType = FuelType::fromApiValue($priceData['fuel_type']);
|
||||
} catch (ValueError) {
|
||||
|
||||
Reference in New Issue
Block a user