46 lines
1.6 KiB
PHP
46 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Catalog\Resources;
|
|
|
|
use App\Domains\Catalog\Enums\InventoryPolicy;
|
|
use App\Domains\Catalog\Models\CatalogItem;
|
|
use App\Domains\Catalog\Models\Variant;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
/** @mixin CatalogItem */
|
|
class CatalogSearchItemResource extends JsonResource
|
|
{
|
|
/** @return array<string, mixed> */
|
|
public function toArray(Request $request): array
|
|
{
|
|
$attachment = $this->attachments->first()
|
|
?? $this->variants
|
|
->flatMap(fn (Variant $variant) => $variant->attachments)
|
|
->first();
|
|
|
|
return [
|
|
'id' => $this->id,
|
|
'type' => $this->type->value,
|
|
'nombre' => $this->nombre,
|
|
'descripcion' => $this->descripcion,
|
|
'precio' => $this->precio,
|
|
'image' => $attachment?->getTemporaryUrl(1440),
|
|
'stock_tecnico' => $this->availableStock(),
|
|
'variants' => $this->variants
|
|
->map(fn (Variant $variant): array => [
|
|
'id' => $variant->id,
|
|
'stock_tecnico' => $this->inventory_policy === InventoryPolicy::Unlimited
|
|
? null
|
|
: $variant->inventory?->availableStock(),
|
|
'values' => $variant->definitions
|
|
->mapWithKeys(fn ($definition) => [
|
|
$definition->itemAttribute?->attribute?->codigo => $definition->value,
|
|
])
|
|
->filter(fn ($value, $key): bool => $key !== null),
|
|
])
|
|
->values(),
|
|
];
|
|
}
|
|
}
|