refactor(catalog): add ticket_label to item attributes and update related logic
This commit is contained in:
@@ -14,6 +14,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
'allow_multi_select',
|
||||
'sort_order',
|
||||
'show_in_selector',
|
||||
'ticket_label',
|
||||
])]
|
||||
class ItemAttribute extends Model
|
||||
{
|
||||
|
||||
@@ -85,6 +85,7 @@ class CatalogItemDetailResource extends JsonResource
|
||||
'nombre' => $attribute->nombre,
|
||||
'sort_order' => $itemAttribute->sort_order,
|
||||
'show_in_selector' => $itemAttribute->show_in_selector,
|
||||
'ticket_label' => $itemAttribute->ticket_label,
|
||||
'is_required' => $attribute->is_required,
|
||||
'allow_multi_select' => $itemAttribute->allow_multi_select,
|
||||
'metadata_schema' => $attribute->metadata_schema,
|
||||
|
||||
@@ -38,6 +38,7 @@ class CatalogService
|
||||
$attributeCodes = $data['attribute_codes'] ?? [];
|
||||
$multiSelectAttributeCodes = $data['multi_select_attribute_codes'] ?? [];
|
||||
$hiddenAttributeCodes = $data['hidden_attribute_codes'] ?? [];
|
||||
$ticketAttributeLabels = $data['ticket_attribute_labels'] ?? [];
|
||||
$components = $data['components'] ?? [];
|
||||
$hasDirectStock = array_key_exists('real_stock', $data);
|
||||
$realStock = (int) ($data['real_stock'] ?? 0);
|
||||
@@ -73,6 +74,14 @@ class CatalogService
|
||||
]);
|
||||
}
|
||||
|
||||
if (array_diff(array_keys($ticketAttributeLabels), $attributeCodes) !== []) {
|
||||
throw ValidationException::withMessages([
|
||||
'ticket_attribute_labels' => [
|
||||
__('api.catalog.ticket_label_attribute_not_on_item'),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
$this->validateUniqueVariantCombinations($variants, $attributeCodes);
|
||||
|
||||
if ($type === CatalogItemType::Bundle) {
|
||||
@@ -98,6 +107,7 @@ class CatalogService
|
||||
$data['attribute_codes'],
|
||||
$data['multi_select_attribute_codes'],
|
||||
$data['hidden_attribute_codes'],
|
||||
$data['ticket_attribute_labels'],
|
||||
$data['components'],
|
||||
$data['real_stock'],
|
||||
$data['reserved_stock'],
|
||||
@@ -124,6 +134,7 @@ class CatalogService
|
||||
$attributeCodes,
|
||||
$multiSelectAttributeCodes,
|
||||
$hiddenAttributeCodes,
|
||||
$ticketAttributeLabels,
|
||||
)
|
||||
: [];
|
||||
|
||||
@@ -461,6 +472,7 @@ class CatalogService
|
||||
* @param array<int, string> $attributeCodes
|
||||
* @param array<int, string> $multiSelectAttributeCodes
|
||||
* @param array<int, string> $hiddenAttributeCodes
|
||||
* @param array<string, string> $ticketAttributeLabels
|
||||
* @return array<string, ItemAttribute>
|
||||
*/
|
||||
private function createItemAttributes(
|
||||
@@ -468,6 +480,7 @@ class CatalogService
|
||||
array $attributeCodes,
|
||||
array $multiSelectAttributeCodes = [],
|
||||
array $hiddenAttributeCodes = [],
|
||||
array $ticketAttributeLabels = [],
|
||||
): array {
|
||||
$itemAttributes = [];
|
||||
$attributeCodes = array_values(array_unique($attributeCodes));
|
||||
@@ -492,6 +505,7 @@ class CatalogService
|
||||
'attribute_id' => $attribute->id,
|
||||
'allow_multi_select' => in_array($attributeCode, $multiSelectAttributeCodes, true),
|
||||
'show_in_selector' => ! in_array($attributeCode, $hiddenAttributeCodes, true),
|
||||
'ticket_label' => $ticketAttributeLabels[$attributeCode] ?? null,
|
||||
]);
|
||||
|
||||
$itemAttributes[$attributeCode] = $itemAttribute;
|
||||
|
||||
@@ -29,19 +29,27 @@ class TicketPresentationResolver
|
||||
return $catalogItem->nombre;
|
||||
}
|
||||
|
||||
$properties = $variant->selectionOptions()
|
||||
->flatMap(function (array $option): array {
|
||||
if (array_is_list($option)) {
|
||||
return collect($option)
|
||||
->pluck('label')
|
||||
->filter(fn ($label): bool => is_string($label) && $label !== '')
|
||||
->all();
|
||||
$itemAttributes = $variant->catalogItem->itemAttributes;
|
||||
$properties = $variant->selectionOptions($itemAttributes)
|
||||
->map(function (array $option, string $attributeCode) use ($itemAttributes): ?string {
|
||||
$labels = collect(array_is_list($option) ? $option : [$option])
|
||||
->pluck('label')
|
||||
->filter(fn ($label): bool => is_string($label) && $label !== '')
|
||||
->implode(', ');
|
||||
|
||||
if ($labels === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$label = $option['label'] ?? null;
|
||||
$ticketLabel = $itemAttributes->first(
|
||||
fn ($itemAttribute): bool => $itemAttribute->attribute?->codigo === $attributeCode,
|
||||
)?->ticket_label;
|
||||
|
||||
return is_string($label) && $label !== '' ? [$label] : [];
|
||||
return is_string($ticketLabel) && trim($ticketLabel) !== ''
|
||||
? trim($ticketLabel).' '.$labels
|
||||
: $labels;
|
||||
})
|
||||
->filter()
|
||||
->values();
|
||||
|
||||
return $properties->isEmpty()
|
||||
|
||||
Reference in New Issue
Block a user