Files
pricebuddy/app/Filament/Concerns/HasScraperTrait.php
Jeremy Graham 57f7369647 feat: store option to treat a missing price as out of stock
Some stores (e.g. Amazon) remove the price element when a product is
out of stock. A missing price was a scrape error, so the product could
not be created, and AI self-healing could move the price selector onto
an unrelated price on the page.

Add a per-store 'Treat a missing price as out of stock' setting. When
it is on, ScrapeUrl::resolveStockStatus() reads a page with a title but
no price as OutOfStock, so creation succeeds and healing does not run.

Fixes #222
2026-10-01 20:51:10 +10:00

106 lines
4.9 KiB
PHP

<?php
namespace App\Filament\Concerns;
use App\Enums\AiFeature;
use App\Enums\Icons;
use App\Enums\ScraperService;
use App\Enums\ScraperStrategyType;
use App\Services\Helpers\IntegrationHelper;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Get;
use Illuminate\Support\HtmlString;
trait HasScraperTrait
{
protected static function makeStrategyInput(string $key, ?string $default = null, bool $required = true): array
{
$typeField = Select::make($key.'.type')
->label('Type')
->options(ScraperStrategyType::class)
->default(ScraperStrategyType::Selector->value)
->hintIcon(Icons::Help->value, 'How to get the value')
->live();
$valueField = TextInput::make($key.'.value')
->label('Value')
->default($default)
->hintIcon(Icons::Help->value, fn (Get $get) => ScraperStrategyType::getValueHelp($get($key.'.type')))
->live();
if ($required) {
$typeField = $typeField->required();
$valueField = $valueField->required();
}
$valueField = $valueField->hidden(fn (Get $get) => ! ScraperStrategyType::needsValue($get($key.'.type')));
return [
$typeField,
$valueField,
TextInput::make($key.'.prepend')
->label('Prepend')
->hidden(fn (Get $get) => ! ScraperStrategyType::needsValue($get($key.'.type')))
->hintIcon(Icons::Help->value, 'Optionally prepend a static value to the extracted value'),
TextInput::make($key.'.append')
->label('Append')
->hidden(fn (Get $get) => ! ScraperStrategyType::needsValue($get($key.'.type')))
->hintIcon(Icons::Help->value, 'Optionally append a static value to the extracted value'),
];
}
protected static function getScraperSettings(): Section
{
return Section::make('Scraper service')->schema([
Radio::make('settings.scraper_service')
->options(ScraperService::class)
->descriptions([
ScraperService::Http->value => 'Faster and less resource intensive. Use this for JSON strategy',
ScraperService::Api->value => 'Slower but good for scraping JavaScript rendered pages',
])
->reactive()
->default(ScraperService::Http),
Textarea::make('settings.scraper_service_settings')
->label('Settings')
->hint(new HtmlString('One option per line. <a href="https://github.com/jez500/seleniumbase-scrapper#api-endpoints" target="_blank">Read docs</a>'))
->hidden(fn (Get $get) => $get('settings.scraper_service') !== ScraperService::Api->value)
->rows(4)
->placeholder("device=Desktop Firefox\nsleep=1000"),
Toggle::make('settings.missing_price_out_of_stock')
->label('Treat a missing price as out of stock')
->helperText('For stores that remove the price when a product is out of stock (e.g. Amazon). A page with a title but no price is marked out of stock instead of failing. A broken price selector will also look like out of stock.')
->columnSpanFull(),
Toggle::make('settings.ai_extraction_enabled')
->label('Enable AI price extraction')
->helperText('Use AI to recover a price when the normal scrape finds none.')
->live()
->hidden(fn (): bool => ! IntegrationHelper::isFeatureEnabled(AiFeature::Extraction))
->columnSpanFull(),
Select::make('settings.ai_provider_id')
->label('AI provider')
->options(fn (): array => collect(IntegrationHelper::getAiProviders())
->mapWithKeys(fn ($provider): array => [$provider->id => $provider->name])
->all())
->default(fn (): ?string => IntegrationHelper::getActiveAiProvider()?->id)
->required(fn (Get $get): bool => (bool) $get('settings.ai_extraction_enabled'))
->visible(fn (Get $get): bool => (bool) $get('settings.ai_extraction_enabled'))
->columnSpanFull(),
Toggle::make('settings.ai_self_healing_disabled')
->label('Disable AI self-healing for this store')
->helperText('Prevent AI from automatically repairing this store\'s scraper config when a scrape fails.')
->hidden(fn (): bool => ! IntegrationHelper::isFeatureEnabled(AiFeature::Healing))
->columnSpanFull(),
])->description('Advanced scraper service settings')->columns(2);
}
}