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:
@@ -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