feat(variant): refactor selection values to expose options with labels; update related resources and tests for consistency
This commit is contained in:
@@ -7,7 +7,6 @@ use App\Domains\Catalog\Enums\InventoryPolicy;
|
|||||||
use App\Domains\Catalog\Models\Variant;
|
use App\Domains\Catalog\Models\Variant;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
use Illuminate\Support\Collection;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @mixin CartItem
|
* @mixin CartItem
|
||||||
@@ -46,35 +45,13 @@ class CartItemResource extends JsonResource
|
|||||||
'stock_tecnico' => $this->catalogItem->inventory_policy === InventoryPolicy::Unlimited
|
'stock_tecnico' => $this->catalogItem->inventory_policy === InventoryPolicy::Unlimited
|
||||||
? null
|
? null
|
||||||
: $variant->inventory->availableStock(),
|
: $variant->inventory->availableStock(),
|
||||||
'values' => $this->variantValues($variant),
|
'values' => $variant->selectionOptions(),
|
||||||
])
|
])
|
||||||
->values(),
|
->values(),
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return Collection<string, string|array<int, string>> */
|
|
||||||
private function variantValues(Variant $variant): Collection
|
|
||||||
{
|
|
||||||
$values = $variant->selectionValues();
|
|
||||||
$eventDates = $variant->selectedEventDates();
|
|
||||||
|
|
||||||
if ($eventDates->isNotEmpty()) {
|
|
||||||
$labels = $eventDates
|
|
||||||
->map(fn ($eventDate): string => $eventDate->date->format('d/m/Y').' · '
|
|
||||||
.substr($eventDate->time_start, 0, 5).' a '
|
|
||||||
.substr($eventDate->time_end, 0, 5))
|
|
||||||
->values();
|
|
||||||
|
|
||||||
$values->put(
|
|
||||||
'event_date',
|
|
||||||
$labels->count() === 1 ? $labels->first() : $labels->all(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $values;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function formatMoney(float|int|string|null $amount): string
|
protected function formatMoney(float|int|string|null $amount): string
|
||||||
{
|
{
|
||||||
return number_format((float) ($amount ?? 0), 2, '.', '');
|
return number_format((float) ($amount ?? 0), 2, '.', '');
|
||||||
|
|||||||
@@ -108,12 +108,12 @@ class CartService
|
|||||||
'items.catalogItem.attachments',
|
'items.catalogItem.attachments',
|
||||||
'items.catalogItem.inventory',
|
'items.catalogItem.inventory',
|
||||||
'items.catalogItem.variants.inventory',
|
'items.catalogItem.variants.inventory',
|
||||||
'items.catalogItem.variants.definitions.itemAttribute.attribute',
|
'items.catalogItem.variants.definitions.itemAttribute.attribute.options',
|
||||||
'items.catalogItem.variants.eventDates',
|
'items.catalogItem.variants.eventDates',
|
||||||
'items.catalogItem.variants.eventDate',
|
'items.catalogItem.variants.eventDate',
|
||||||
'items.variant.attachments',
|
'items.variant.attachments',
|
||||||
'items.variant.inventory',
|
'items.variant.inventory',
|
||||||
'items.variant.definitions.itemAttribute.attribute',
|
'items.variant.definitions.itemAttribute.attribute.options',
|
||||||
'items.variant.eventDates',
|
'items.variant.eventDates',
|
||||||
'items.variant.eventDate',
|
'items.variant.eventDate',
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -149,6 +149,57 @@ class Variant extends Model
|
|||||||
return $values;
|
return $values;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Collection<string, array{value: string, label: string}|array<int, array{value: string, label: string}>>
|
||||||
|
*/
|
||||||
|
public function selectionOptions(): Collection
|
||||||
|
{
|
||||||
|
$options = $this->definitions
|
||||||
|
->groupBy('item_attribute_id')
|
||||||
|
->mapWithKeys(function (Collection $definitions): array {
|
||||||
|
$itemAttribute = $definitions->first()?->itemAttribute;
|
||||||
|
$attribute = $itemAttribute?->attribute;
|
||||||
|
$attributeCode = $attribute?->codigo;
|
||||||
|
|
||||||
|
if ($attributeCode === null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$values = $definitions
|
||||||
|
->pluck('value')
|
||||||
|
->values()
|
||||||
|
->map(function (string $value) use ($attribute): array {
|
||||||
|
$attributeOption = $attribute->options->firstWhere('value', $value);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'value' => $value,
|
||||||
|
'label' => $attributeOption?->label ?? $value,
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
return [
|
||||||
|
$attributeCode => $itemAttribute->allow_multi_select
|
||||||
|
? $values->all()
|
||||||
|
: $values->first(),
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
$eventDateOptions = $this->selectedEventDates()
|
||||||
|
->map(fn (EventDate $eventDate): array => [
|
||||||
|
'value' => (string) $eventDate->id,
|
||||||
|
'label' => $eventDate->date->format('d/m/Y'),
|
||||||
|
])
|
||||||
|
->values();
|
||||||
|
|
||||||
|
if ($eventDateOptions->count() === 1) {
|
||||||
|
$options->put('event_date', $eventDateOptions->first());
|
||||||
|
} elseif ($eventDateOptions->isNotEmpty()) {
|
||||||
|
$options->put('event_date', $eventDateOptions->all());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $options;
|
||||||
|
}
|
||||||
|
|
||||||
/** @return Collection<int, EventDate> */
|
/** @return Collection<int, EventDate> */
|
||||||
public function selectedEventDates(): Collection
|
public function selectedEventDates(): Collection
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ class CatalogFeaturedItemResource extends JsonResource
|
|||||||
'stock_tecnico' => $catalogItem->inventory_policy === InventoryPolicy::Unlimited
|
'stock_tecnico' => $catalogItem->inventory_policy === InventoryPolicy::Unlimited
|
||||||
? null
|
? null
|
||||||
: $variant->inventory->availableStock(),
|
: $variant->inventory->availableStock(),
|
||||||
'values' => $variant->selectionValues(),
|
'values' => $variant->selectionOptions(),
|
||||||
])
|
])
|
||||||
->values(),
|
->values(),
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -104,9 +104,7 @@ class CatalogItemDetailResource extends JsonResource
|
|||||||
->map(fn ($eventDate, int $index): array => [
|
->map(fn ($eventDate, int $index): array => [
|
||||||
'id' => $eventDate->id,
|
'id' => $eventDate->id,
|
||||||
'value' => (string) $eventDate->id,
|
'value' => (string) $eventDate->id,
|
||||||
'label' => $eventDate->date->format('d/m/Y').' · '
|
'label' => $eventDate->date->format('d/m/Y'),
|
||||||
.substr($eventDate->time_start, 0, 5).' a '
|
|
||||||
.substr($eventDate->time_end, 0, 5),
|
|
||||||
'sort_order' => $index,
|
'sort_order' => $index,
|
||||||
'validity_time_id' => null,
|
'validity_time_id' => null,
|
||||||
'validity_time' => null,
|
'validity_time' => null,
|
||||||
@@ -156,7 +154,7 @@ class CatalogItemDetailResource extends JsonResource
|
|||||||
/** @return array<string, mixed> */
|
/** @return array<string, mixed> */
|
||||||
private function variantData(Variant $variant): array
|
private function variantData(Variant $variant): array
|
||||||
{
|
{
|
||||||
$values = $variant->selectionValues();
|
$values = $variant->selectionOptions();
|
||||||
$eventDates = $variant->selectedEventDates();
|
$eventDates = $variant->selectedEventDates();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ class CatalogItemResource extends JsonResource
|
|||||||
'descripcion' => $variant->getDescription(),
|
'descripcion' => $variant->getDescription(),
|
||||||
'precio' => number_format($variant->getPrice(), 2, '.', ''),
|
'precio' => number_format($variant->getPrice(), 2, '.', ''),
|
||||||
'real_stock' => $variant->inventory?->real_stock,
|
'real_stock' => $variant->inventory?->real_stock,
|
||||||
'values' => $variant->selectionValues(),
|
'values' => $variant->selectionOptions(),
|
||||||
'images' => $variant->attachments
|
'images' => $variant->attachments
|
||||||
->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
|
->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
|
||||||
->values(),
|
->values(),
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ class CatalogSearchItemResource extends JsonResource
|
|||||||
'stock_tecnico' => $this->inventory_policy === InventoryPolicy::Unlimited
|
'stock_tecnico' => $this->inventory_policy === InventoryPolicy::Unlimited
|
||||||
? null
|
? null
|
||||||
: $variant->inventory?->availableStock(),
|
: $variant->inventory?->availableStock(),
|
||||||
'values' => $variant->selectionValues(),
|
'values' => $variant->selectionOptions(),
|
||||||
])
|
])
|
||||||
->values(),
|
->values(),
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -153,7 +153,7 @@ class CatalogService
|
|||||||
'variants.attachments',
|
'variants.attachments',
|
||||||
'variants.eventDate',
|
'variants.eventDate',
|
||||||
'variants.eventDates',
|
'variants.eventDates',
|
||||||
'variants.definitions.itemAttribute.attribute',
|
'variants.definitions.itemAttribute.attribute.options',
|
||||||
'bundleComponents.catalogItem',
|
'bundleComponents.catalogItem',
|
||||||
'bundleComponents.variant.catalogItem',
|
'bundleComponents.variant.catalogItem',
|
||||||
]);
|
]);
|
||||||
@@ -176,7 +176,7 @@ class CatalogService
|
|||||||
'variants.eventDate',
|
'variants.eventDate',
|
||||||
'variants.eventDates',
|
'variants.eventDates',
|
||||||
'variants.definitions' => fn ($query) => $query->orderBy('id'),
|
'variants.definitions' => fn ($query) => $query->orderBy('id'),
|
||||||
'variants.definitions.itemAttribute.attribute',
|
'variants.definitions.itemAttribute.attribute.options',
|
||||||
'bundleComponents.catalogItem.inventory',
|
'bundleComponents.catalogItem.inventory',
|
||||||
'bundleComponents.variant.inventory',
|
'bundleComponents.variant.inventory',
|
||||||
'bundleComponents.variant.definitions.itemAttribute.attribute',
|
'bundleComponents.variant.definitions.itemAttribute.attribute',
|
||||||
@@ -231,7 +231,7 @@ class CatalogService
|
|||||||
'variants.attachments',
|
'variants.attachments',
|
||||||
'variants.eventDate',
|
'variants.eventDate',
|
||||||
'variants.eventDates',
|
'variants.eventDates',
|
||||||
'variants.definitions.itemAttribute.attribute',
|
'variants.definitions.itemAttribute.attribute.options',
|
||||||
'bundleComponents.catalogItem',
|
'bundleComponents.catalogItem',
|
||||||
'bundleComponents.variant.catalogItem',
|
'bundleComponents.variant.catalogItem',
|
||||||
])
|
])
|
||||||
@@ -265,7 +265,7 @@ class CatalogService
|
|||||||
'variants.attachments',
|
'variants.attachments',
|
||||||
'variants.eventDate',
|
'variants.eventDate',
|
||||||
'variants.eventDates',
|
'variants.eventDates',
|
||||||
'variants.definitions.itemAttribute.attribute',
|
'variants.definitions.itemAttribute.attribute.options',
|
||||||
'bundleComponents.catalogItem',
|
'bundleComponents.catalogItem',
|
||||||
'bundleComponents.variant.catalogItem',
|
'bundleComponents.variant.catalogItem',
|
||||||
])
|
])
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ class FeaturedGroupService
|
|||||||
'variants.attachments',
|
'variants.attachments',
|
||||||
'variants.eventDate',
|
'variants.eventDate',
|
||||||
'variants.eventDates',
|
'variants.eventDates',
|
||||||
'variants.definitions.itemAttribute.attribute',
|
'variants.definitions.itemAttribute.attribute.options',
|
||||||
'bundleComponents.catalogItem',
|
'bundleComponents.catalogItem',
|
||||||
'bundleComponents.variant.catalogItem',
|
'bundleComponents.variant.catalogItem',
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -117,17 +117,20 @@ class CatalogItemDetailControllerTest extends TestCase
|
|||||||
->assertOk()
|
->assertOk()
|
||||||
->assertJsonPath('data.variants.0.id', $firstVariant->id)
|
->assertJsonPath('data.variants.0.id', $firstVariant->id)
|
||||||
->assertJsonPath('data.variants.0.stock_tecnico', 4)
|
->assertJsonPath('data.variants.0.stock_tecnico', 4)
|
||||||
->assertJsonPath('data.variants.0.values.size', 'S')
|
->assertJsonPath('data.variants.0.values.size.value', 'S')
|
||||||
|
->assertJsonPath('data.variants.0.values.size.label', 'Small')
|
||||||
->assertJsonPath('data.variants.1.id', $secondVariant->id)
|
->assertJsonPath('data.variants.1.id', $secondVariant->id)
|
||||||
->assertJsonPath('data.variants.1.stock_tecnico', 7)
|
->assertJsonPath('data.variants.1.stock_tecnico', 7)
|
||||||
->assertJsonPath('data.variants.1.values.size', 'M')
|
->assertJsonPath('data.variants.1.values.size.value', 'M')
|
||||||
|
->assertJsonPath('data.variants.1.values.size.label', 'Medium')
|
||||||
->assertJsonPath('data.attributes.0.codigo', 'size')
|
->assertJsonPath('data.attributes.0.codigo', 'size')
|
||||||
->assertJsonPath('data.attributes.0.options.0.value', 'S')
|
->assertJsonPath('data.attributes.0.options.0.value', 'S')
|
||||||
->assertJsonPath('data.attributes.0.options.1.value', 'M')
|
->assertJsonPath('data.attributes.0.options.1.value', 'M')
|
||||||
->assertJsonCount(2, 'data.attributes.0.options')
|
->assertJsonCount(2, 'data.attributes.0.options')
|
||||||
->assertJsonPath('data.selected_variant.id', $secondVariant->id)
|
->assertJsonPath('data.selected_variant.id', $secondVariant->id)
|
||||||
->assertJsonPath('data.selected_variant.stock_tecnico', 7)
|
->assertJsonPath('data.selected_variant.stock_tecnico', 7)
|
||||||
->assertJsonPath('data.selected_variant.values.size', 'M')
|
->assertJsonPath('data.selected_variant.values.size.value', 'M')
|
||||||
|
->assertJsonPath('data.selected_variant.values.size.label', 'Medium')
|
||||||
->assertJsonCount(1, 'data.selected_variant.images');
|
->assertJsonCount(1, 'data.selected_variant.images');
|
||||||
$response
|
$response
|
||||||
->assertJsonMissingPath('data.stock_tecnico')
|
->assertJsonMissingPath('data.stock_tecnico')
|
||||||
@@ -207,10 +210,14 @@ class CatalogItemDetailControllerTest extends TestCase
|
|||||||
->assertJsonCount(2, 'data.attributes.0.options')
|
->assertJsonCount(2, 'data.attributes.0.options')
|
||||||
->assertJsonPath('data.attributes.0.options.0.id', $eventDate->id)
|
->assertJsonPath('data.attributes.0.options.0.id', $eventDate->id)
|
||||||
->assertJsonPath('data.attributes.0.options.0.value', (string) $eventDate->id)
|
->assertJsonPath('data.attributes.0.options.0.value', (string) $eventDate->id)
|
||||||
->assertJsonPath('data.attributes.0.options.0.label', '09/10/2026 · 09:00 a 18:00')
|
->assertJsonPath('data.attributes.0.options.0.label', '09/10/2026')
|
||||||
->assertJsonPath('data.attributes.0.options.1.id', $unusedEventDate->id)
|
->assertJsonPath('data.attributes.0.options.1.id', $unusedEventDate->id)
|
||||||
->assertJsonPath('data.attributes.0.options.1.value', (string) $unusedEventDate->id)
|
->assertJsonPath('data.attributes.0.options.1.value', (string) $unusedEventDate->id)
|
||||||
->assertJsonPath('data.variants.0.values.event_date', (string) $eventDate->id);
|
->assertJsonPath('data.variants.0.values.event_date.value', (string) $eventDate->id)
|
||||||
|
->assertJsonPath(
|
||||||
|
'data.variants.0.values.event_date.label',
|
||||||
|
'09/10/2026',
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertDatabaseCount('attribute_options', 0);
|
$this->assertDatabaseCount('attribute_options', 0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -186,6 +186,43 @@ class CatalogModelsTest extends TestCase
|
|||||||
$this->assertSame('2026-10-09', $variant->eventDate->date->format('Y-m-d'));
|
$this->assertSame('2026-10-09', $variant->eventDate->date->format('Y-m-d'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_variant_exposes_selection_values_with_api_labels(): void
|
||||||
|
{
|
||||||
|
$attribute = new Attribute;
|
||||||
|
$attribute->codigo = 'size';
|
||||||
|
$attribute->setRelation('options', new EloquentCollection([
|
||||||
|
new AttributeOption(['value' => 'M', 'label' => 'Medium']),
|
||||||
|
]));
|
||||||
|
|
||||||
|
$itemAttribute = new ItemAttribute;
|
||||||
|
$itemAttribute->allow_multi_select = false;
|
||||||
|
$itemAttribute->setRelation('attribute', $attribute);
|
||||||
|
|
||||||
|
$definition = new VariantDefinition(['value' => 'M']);
|
||||||
|
$definition->item_attribute_id = 1;
|
||||||
|
$definition->setRelation('itemAttribute', $itemAttribute);
|
||||||
|
|
||||||
|
$eventDate = new EventDate([
|
||||||
|
'date' => '2026-10-09',
|
||||||
|
'time_start' => '09:00:00',
|
||||||
|
'time_end' => '18:00:00',
|
||||||
|
]);
|
||||||
|
$eventDate->id = 20;
|
||||||
|
|
||||||
|
$variant = new Variant;
|
||||||
|
$variant->setRelation('definitions', new EloquentCollection([$definition]));
|
||||||
|
$variant->setRelation('eventDates', new EloquentCollection([$eventDate]));
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
['value' => 'M', 'label' => 'Medium'],
|
||||||
|
$variant->selectionOptions()->get('size'),
|
||||||
|
);
|
||||||
|
$this->assertSame(
|
||||||
|
['value' => '20', 'label' => '09/10/2026'],
|
||||||
|
$variant->selectionOptions()->get('event_date'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function test_inventory_maps_stock_without_a_polymorphic_owner(): void
|
public function test_inventory_maps_stock_without_a_polymorphic_owner(): void
|
||||||
{
|
{
|
||||||
$inventory = $this->trackedInventory(realStock: 10, reservedStock: 3);
|
$inventory = $this->trackedInventory(realStock: 10, reservedStock: 3);
|
||||||
|
|||||||
Reference in New Issue
Block a user