mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
Stores are shared by all users, but the stores API only returned the caller's
own stores. Stores with no creator, such as Amazon AU from the seed data, were
missing from every user's results.
- GET /api/stores, filter[domain], filter[domains] and GET /api/stores/{id}
return every store.
- The urls and products includes return only the caller's rows. The user
include returns the creator only to the creator.
- A new StorePolicy lets any user create and edit a store. Only an admin can
delete one, in the API (403) and in the Filament UI.
- user_id now records who created a store. The server sets it, and an edit
does not change it. Store::creating() fills it from the logged-in user, else
the oldest admin. Adding a URL in a job passes the user who added it.
- A migration sets existing NULL user_id values to the oldest admin.
311 lines
9.2 KiB
PHP
311 lines
9.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Casts\StoreScraperStrategySetCast;
|
|
use App\Dto\StoreScraperStrategySetDto;
|
|
use App\Enums\Role;
|
|
use App\Enums\ScraperService;
|
|
use App\Services\Helpers\CurrencyHelper;
|
|
use Database\Factories\StoreFactory;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\HtmlString;
|
|
use Illuminate\Support\Str;
|
|
use Spatie\Sluggable\HasSlug;
|
|
use Spatie\Sluggable\SlugOptions;
|
|
|
|
/**
|
|
* @property string $name
|
|
* @property string $initials
|
|
* @property array $domains
|
|
* @property HtmlString $domains_html
|
|
* @property StoreScraperStrategySetDto $scrape_strategy
|
|
* @property array $settings
|
|
* @property string $scraper_service
|
|
* @property array $scraper_options
|
|
* @property bool $ai_extraction_enabled
|
|
* @property string|null $ai_provider_id
|
|
* @property bool $ai_self_healing_disabled
|
|
* @property bool $missing_price_out_of_stock
|
|
* @property string $locale
|
|
* @property string $currency
|
|
* @property Collection $urls
|
|
* @property Collection $products
|
|
* @property ?User $user
|
|
* @property ?string $cookies
|
|
*/
|
|
class Store extends Model
|
|
{
|
|
/** @use HasFactory<StoreFactory> */
|
|
use HasFactory;
|
|
|
|
use HasSlug;
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'initials',
|
|
'domains',
|
|
'scrape_strategy',
|
|
'settings',
|
|
'notes',
|
|
'user_id',
|
|
'cookies',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'domains' => 'array',
|
|
'scrape_strategy' => StoreScraperStrategySetCast::class,
|
|
'settings' => 'array',
|
|
];
|
|
}
|
|
|
|
public static function booted()
|
|
{
|
|
// Stores are shared. user_id records who created the store. Jobs and the CLI
|
|
// have no logged-in user, so they fall back to the oldest admin.
|
|
static::creating(function (Store $store) {
|
|
$store->user_id ??= auth()->id() ?? self::defaultCreatorId();
|
|
});
|
|
|
|
static::deleted(function (Store $store) {
|
|
// Get all products before deleting URLs (since products() relationship uses URLs).
|
|
$products = Product::whereIn('id', function ($query) use ($store) {
|
|
$query->select('product_id')
|
|
->from('urls')
|
|
->where('store_id', $store->id);
|
|
})->get();
|
|
|
|
// Delete URLs individually to trigger model events (which cascade delete prices).
|
|
$store->urls->each->delete();
|
|
|
|
// Update price cache for all affected products.
|
|
$products->each(function (Product $product): void {
|
|
$product->updatePriceCache();
|
|
$product->updateInsightsCache();
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* The creator for a store made without a user: the oldest admin, else the oldest user.
|
|
*/
|
|
public static function defaultCreatorId(): ?int
|
|
{
|
|
$id = User::query()->where('role', Role::Admin)->orderBy('id')->value('id')
|
|
?? User::query()->orderBy('id')->value('id');
|
|
|
|
return $id === null ? null : (int) $id;
|
|
}
|
|
|
|
public function getSlugOptions(): SlugOptions
|
|
{
|
|
return SlugOptions::create()
|
|
->generateSlugsFrom('name')
|
|
->saveSlugsTo('slug');
|
|
}
|
|
|
|
/***************************************************
|
|
* Relationships.
|
|
**************************************************/
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function urls(): HasMany
|
|
{
|
|
return $this->hasMany(Url::class);
|
|
}
|
|
|
|
public function products(): HasManyThrough
|
|
{
|
|
return $this->hasManyThrough(
|
|
Product::class,
|
|
Url::class,
|
|
'store_id',
|
|
'id',
|
|
'id',
|
|
'product_id'
|
|
);
|
|
}
|
|
|
|
/***************************************************
|
|
* Scopes.
|
|
**************************************************/
|
|
|
|
public function scopeDomainFilter(Builder $query, string|array|null $domains): Builder
|
|
{
|
|
$domains = array_values(array_filter(
|
|
Arr::wrap($domains),
|
|
fn ($domain) => filled($domain)
|
|
));
|
|
|
|
if ($domains === []) {
|
|
return $query->whereRaw('1 = 0');
|
|
}
|
|
|
|
// `domains` is normally an array of objects, but the store API and StoreFactory::forUrl()
|
|
// can also store a single bare object. MySQL's JSON_CONTAINS matches a bare-object needle against
|
|
// both shapes, but Postgres' @> only matches like with like, so check both needles.
|
|
return $query->where(function (Builder $subQuery) use ($domains) {
|
|
foreach ($domains as $domain) {
|
|
$subQuery->orWhereJsonContains('domains', ['domain' => $domain])
|
|
->orWhereJsonContains('domains', [['domain' => $domain]]);
|
|
}
|
|
});
|
|
}
|
|
|
|
/***************************************************
|
|
* Attributes.
|
|
**************************************************/
|
|
|
|
public function initials(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: function ($value) {
|
|
if (! empty($value)) {
|
|
return strtoupper($value);
|
|
}
|
|
|
|
$parts = explode(' ', Str::slug($this->name, ' '));
|
|
$initials = count($parts) > 1
|
|
? collect(explode(' ', Str::slug($this->name, ' ')))
|
|
->map(fn ($part) => Str::substr($part, 0, 1))
|
|
->take(2)->join('')
|
|
: Str::substr($this->name, 0, 2);
|
|
|
|
return strtoupper($initials);
|
|
}
|
|
);
|
|
}
|
|
|
|
public function domainsHtml(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => new HtmlString(Str::limit(collect($this->domains)
|
|
->pluck('domain')
|
|
->join(', ')))
|
|
);
|
|
}
|
|
|
|
public function scraperService(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: function () {
|
|
return data_get($this->settings, 'scraper_service', ScraperService::Http->value);
|
|
}
|
|
);
|
|
}
|
|
|
|
public function aiExtractionEnabled(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn (): bool => (bool) data_get($this->settings, 'ai_extraction_enabled', false),
|
|
);
|
|
}
|
|
|
|
public function aiProviderId(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn (): ?string => data_get($this->settings, 'ai_provider_id'),
|
|
);
|
|
}
|
|
|
|
public function aiSelfHealingDisabled(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn (): bool => (bool) data_get($this->settings, 'ai_self_healing_disabled', false),
|
|
);
|
|
}
|
|
|
|
public function missingPriceOutOfStock(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn (): bool => (bool) data_get($this->settings, 'missing_price_out_of_stock', false),
|
|
);
|
|
}
|
|
|
|
public function scraperOptions(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: function () {
|
|
return collect(explode(PHP_EOL, data_get($this->settings, 'scraper_service_settings', '')))
|
|
->filter(fn ($option) => ! empty($option) && Str::contains($option, '='))
|
|
->mapWithKeys(function ($option) {
|
|
$parts = explode('=', $option);
|
|
|
|
return [data_get($parts, 0) => data_get($parts, 1)];
|
|
})
|
|
->toArray();
|
|
}
|
|
);
|
|
}
|
|
|
|
public function testUrl(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => data_get($this->settings, 'test_url', ''),
|
|
);
|
|
}
|
|
|
|
public function locale(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => data_get($this->settings, 'locale_settings.locale', CurrencyHelper::getLocale()),
|
|
);
|
|
}
|
|
|
|
public function currency(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => data_get($this->settings, 'locale_settings.currency', CurrencyHelper::getCurrency()),
|
|
);
|
|
}
|
|
|
|
/***************************************************
|
|
* Helpers.
|
|
**************************************************/
|
|
|
|
public function hasDomain($domain): bool
|
|
{
|
|
return collect($this->domains)
|
|
->pluck('domain')
|
|
->contains($domain);
|
|
}
|
|
|
|
public function getAiHealFailedAt(): ?Carbon
|
|
{
|
|
$value = data_get($this->settings, 'ai_heal_failed_at');
|
|
|
|
return filled($value) ? Carbon::parse($value) : null;
|
|
}
|
|
|
|
public function markAiHealFailed(): void
|
|
{
|
|
$this->settings = array_merge($this->settings ?? [], [
|
|
'ai_heal_failed_at' => now()->toIso8601String(),
|
|
]);
|
|
$this->save();
|
|
}
|
|
|
|
public function clearAiHealFailed(): void
|
|
{
|
|
$settings = $this->settings ?? [];
|
|
unset($settings['ai_heal_failed_at']);
|
|
$this->settings = $settings;
|
|
$this->save();
|
|
}
|
|
}
|