refactor(catalog): rely on inventory for variant availability
This commit is contained in:
@@ -138,7 +138,6 @@ class CatalogController extends Controller
|
|||||||
$variantSelectionService->options(
|
$variantSelectionService->options(
|
||||||
$catalogItem,
|
$catalogItem,
|
||||||
$request->validated('selected_values', []),
|
$request->validated('selected_values', []),
|
||||||
array_map('intval', $request->validated('excluded_variant_ids', [])),
|
|
||||||
$includedVariantId,
|
$includedVariantId,
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -17,8 +17,6 @@ class CatalogVariantOptionsRequest extends FormRequest
|
|||||||
return [
|
return [
|
||||||
'selected_values' => ['sometimes', 'array'],
|
'selected_values' => ['sometimes', 'array'],
|
||||||
'selected_values.*' => ['nullable'],
|
'selected_values.*' => ['nullable'],
|
||||||
'excluded_variant_ids' => ['sometimes', 'array'],
|
|
||||||
'excluded_variant_ids.*' => ['integer', 'min:1', 'distinct'],
|
|
||||||
'cart_item_id' => ['sometimes', 'nullable', 'integer', 'min:1'],
|
'cart_item_id' => ['sometimes', 'nullable', 'integer', 'min:1'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,13 +11,11 @@ class VariantSelectionService
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param array<string, mixed> $selectedValues
|
* @param array<string, mixed> $selectedValues
|
||||||
* @param list<int> $excludedVariantIds
|
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
public function options(
|
public function options(
|
||||||
CatalogItem $catalogItem,
|
CatalogItem $catalogItem,
|
||||||
array $selectedValues,
|
array $selectedValues,
|
||||||
array $excludedVariantIds = [],
|
|
||||||
?int $includedVariantId = null,
|
?int $includedVariantId = null,
|
||||||
): array {
|
): array {
|
||||||
$catalogItem->load([
|
$catalogItem->load([
|
||||||
@@ -30,10 +28,7 @@ class VariantSelectionService
|
|||||||
'variants.definitions.itemAttribute.attribute.options',
|
'variants.definitions.itemAttribute.attribute.options',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$excluded = collect($excludedVariantIds)->map(fn ($id): int => (int) $id);
|
|
||||||
$variants = $catalogItem->visibleVariants($includedVariantId)
|
$variants = $catalogItem->visibleVariants($includedVariantId)
|
||||||
->reject(fn (Variant $variant): bool => $variant->id !== $includedVariantId
|
|
||||||
&& $excluded->contains($variant->id))
|
|
||||||
->values();
|
->values();
|
||||||
$normalizedSelections = collect($selectedValues)
|
$normalizedSelections = collect($selectedValues)
|
||||||
->mapWithKeys(fn ($value, string $key): array => [$key => $this->normalizeValue($value)])
|
->mapWithKeys(fn ($value, string $key): array => [$key => $this->normalizeValue($value)])
|
||||||
|
|||||||
@@ -335,15 +335,14 @@ class CatalogItemDetailControllerTest extends TestCase
|
|||||||
"/api/tenants/{$tenant->codigo}/catalog-items/{$item->id}/variant-options",
|
"/api/tenants/{$tenant->codigo}/catalog-items/{$item->id}/variant-options",
|
||||||
[
|
[
|
||||||
'selected_values' => ['sector' => 'A'],
|
'selected_values' => ['sector' => 'A'],
|
||||||
'excluded_variant_ids' => [$second->id],
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
->assertOk()
|
->assertOk()
|
||||||
->assertJsonPath('data.valid', true)
|
->assertJsonPath('data.valid', true)
|
||||||
->assertJsonPath('data.resolved_variant_id', null)
|
->assertJsonPath('data.resolved_variant_id', null)
|
||||||
->assertJsonCount(2, 'data.variants')
|
->assertJsonCount(3, 'data.variants')
|
||||||
->assertJsonCount(1, 'data.options.sector')
|
->assertJsonCount(1, 'data.options.sector')
|
||||||
->assertJsonCount(1, 'data.options.seat')
|
->assertJsonCount(2, 'data.options.seat')
|
||||||
->assertJsonPath('data.options.seat.0.value', '1');
|
->assertJsonPath('data.options.seat.0.value', '1');
|
||||||
|
|
||||||
$this->postJson(
|
$this->postJson(
|
||||||
|
|||||||
Reference in New Issue
Block a user