refactor: integrate category handling for entries and food items in services and tests

This commit is contained in:
2026-08-07 14:34:59 -03:00
parent e1ad27ecf0
commit 11776f0734
6 changed files with 70 additions and 7 deletions

View File

@@ -5,6 +5,7 @@ namespace App\Domains\FiestaFutbolInfantil\Services;
use App\Domains\Catalog\Enums\EventProductType;
use App\Domains\Catalog\Enums\InventoryPolicy;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Category;
use App\Domains\Catalog\Models\Inventory;
use App\Domains\Catalog\Models\Variant;
use App\Domains\Catalog\Services\CatalogService;
@@ -26,10 +27,14 @@ class EntryService
{
return DB::transaction(function () use ($tenant, $entries): Collection {
$reservedSlugs = [];
$category = Category::query()->firstOrCreate([
'tenant_code' => $tenant->codigo,
'nombre' => 'Entradas',
]);
return collect($entries)->map(function (array $entry, int $index) use ($tenant, &$reservedSlugs): CatalogItem {
return collect($entries)->map(function (array $entry, int $index) use ($tenant, $category, &$reservedSlugs): CatalogItem {
if (isset($entry['id'])) {
return $this->update($tenant, $entry, $index);
return $this->update($tenant, $category, $entry, $index);
}
$slug = $this->uniqueSlug($tenant, $entry['title'], $reservedSlugs);
@@ -40,6 +45,7 @@ class EntryService
'slug' => $slug,
'nombre' => $entry['title'],
'descripcion' => $entry['description'] ?? null,
'category_id' => $category->id,
'precio' => $entry['price'],
'event_product_type' => EventProductType::Entry->value,
'has_tickets' => true,
@@ -56,7 +62,7 @@ class EntryService
}
/** @param array<string, mixed> $entry */
private function update(Tenant $tenant, array $entry, int $index): CatalogItem
private function update(Tenant $tenant, Category $category, array $entry, int $index): CatalogItem
{
$catalogItem = CatalogItem::query()
->whereKey($entry['id'])
@@ -100,6 +106,7 @@ class EntryService
$catalogItem->update([
'nombre' => $entry['title'],
'descripcion' => $entry['description'] ?? null,
'category_id' => $category->id,
'precio' => $entry['price'],
'has_tickets' => true,
'inventory_policy' => InventoryPolicy::Tracked->value,

View File

@@ -7,6 +7,7 @@ use App\Domains\Catalog\Enums\InventoryPolicy;
use App\Domains\Catalog\Models\Attribute;
use App\Domains\Catalog\Models\AttributeOption;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Category;
use App\Domains\Catalog\Models\Inventory;
use App\Domains\Catalog\Models\ItemAttribute;
use App\Domains\Catalog\Models\Variant;
@@ -96,6 +97,10 @@ class FoodService
/** @param array<int, array<string, mixed>> $variants */
private function food(Tenant $tenant, array $variants): CatalogItem
{
$category = Category::query()->firstOrCreate([
'tenant_code' => $tenant->codigo,
'nombre' => 'Comidas',
]);
$food = CatalogItem::query()
->where('tenant_code', $tenant->codigo)
->where('slug', 'comida')
@@ -104,6 +109,7 @@ class FoodService
if ($food !== null) {
$food->update([
'category_id' => $category->id,
'event_product_type' => EventProductType::Product->value,
'inventory_policy' => InventoryPolicy::Tracked->value,
'has_tickets' => false,
@@ -117,6 +123,7 @@ class FoodService
'slug' => 'comida',
'nombre' => 'Comida',
'descripcion' => 'Comida',
'category_id' => $category->id,
'precio' => collect($variants)->min('price') ?? 0,
'event_product_type' => EventProductType::Product->value,
'inventory_policy' => InventoryPolicy::Tracked->value,