Files
Jeremy 039c96a1d7 fix: address review findings on store currency and locale
Hide `settings` from sparse fieldset responses. The stores list handler
force-selects the column so the `currency` and `locale` accessors resolve,
but StoreTransformer emits every loaded attribute, so an internal workaround
leaked per-store scraper configuration into a response that asked for
`id,name`. Hide it again after pagination unless it was explicitly requested.

Fall back to the app defaults when a store's locale override is present but
empty. The accessors use data_get(), which only falls back when the key is
absent, so `locale_settings: {currency: null, locale: null}` serialised as
`currency: null` and `locale: ""` — both rejected by Intl.NumberFormat.
Matches the existing guard in MetaExtractionResource.
2026-08-08 11:48:21 +10:00

42 lines
1.6 KiB
PHP

<?php
namespace App\Http\Resources;
use App\Models\Store;
use App\Services\Helpers\CurrencyHelper;
use App\Services\Helpers\LocaleHelper;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* @property Store $resource
*/
class StoreResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->resource->getKey(),
'name' => $this->resource->name,
'slug' => $this->resource->slug,
'initials' => $this->resource->initials,
'domains' => $this->resource->domains,
// Exposed as `scrape_settings` in the API; the model attribute is `scrape_strategy`.
// The name differs intentionally to keep the public contract stable.
'scrape_settings' => $this->resource->scrape_strategy,
'settings' => $this->resource->settings,
// Resolved accessor values, not the raw `settings.locale_settings` blob, which is
// absent on most stores. ISO 4217 code and BCP-47 tag, so clients can format prices.
// `?:` covers locale_settings holding an explicit null/empty value, which the
// data_get() in the accessors returns as-is rather than falling back.
'currency' => $this->resource->currency ?: CurrencyHelper::getCurrency(),
'locale' => LocaleHelper::toBcp47($this->resource->locale ?: CurrencyHelper::getLocale()),
];
}
}