feat(food): implement FoodFormController and FoodFormService; add FoodFormResource for managing food options and routes

This commit is contained in:
2026-08-10 10:05:35 -03:00
parent ca605127d0
commit 1cd60f7021
9 changed files with 202 additions and 5 deletions

View File

@@ -12,6 +12,14 @@ class AccommodationResource extends JsonResource
/** @return array<string, mixed> */
public function toArray(Request $request): array
{
if ($this->resource === null) {
return [
'id' => null,
'name' => 'Alojamiento',
'variants' => [],
];
}
$typeAttribute = $this->itemAttributes
->first(fn ($itemAttribute) => $itemAttribute->attribute?->codigo === 'tipo_alojamiento');
$options = $typeAttribute?->attribute?->options?->keyBy('value') ?? collect();

View File

@@ -12,6 +12,14 @@ class FoodResource extends JsonResource
/** @return array<string, mixed> */
public function toArray(Request $request): array
{
if ($this->resource === null) {
return [
'id' => null,
'name' => 'Comida',
'variants' => [],
];
}
return [
'id' => $this->id,
'name' => $this->nombre,

View File

@@ -20,7 +20,7 @@ class AccommodationService
{
private const ATTRIBUTE_CODE = 'tipo_alojamiento';
public function current(Tenant $tenant): CatalogItem
public function current(Tenant $tenant): ?CatalogItem
{
return CatalogItem::query()
->where('tenant_code', $tenant->codigo)
@@ -31,7 +31,7 @@ class AccommodationService
'variants.inventory',
'variants.definitions',
])
->firstOrFail();
->first();
}
/**

View File

@@ -20,7 +20,7 @@ class FoodService
{
private const ATTRIBUTE_CODES = ['event_date', 'horario', 'servicio'];
public function current(Tenant $tenant): CatalogItem
public function current(Tenant $tenant): ?CatalogItem
{
return CatalogItem::query()
->where('tenant_code', $tenant->codigo)
@@ -32,7 +32,7 @@ class FoodService
'variants.eventDates',
'variants.definitions.itemAttribute.attribute',
])
->firstOrFail();
->first();
}
/**
@@ -282,7 +282,7 @@ class FoodService
/** @param Collection<string, ItemAttribute> $itemAttributes */
private function syncDefinitions(Variant $variant, Collection $itemAttributes, array $data): void
{
$definitionAttributes = $itemAttributes->only(['horario', 'servicio']);
$definitionAttributes = $itemAttributes->toBase()->only(['horario', 'servicio']);
$variant->definitions()->whereIn('item_attribute_id', $definitionAttributes->pluck('id'))->delete();
$variant->definitions()->createMany([
[

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Domains\Forms\Controllers\AdminApp;
use App\Domains\Forms\Resources\FoodFormResource;
use App\Domains\Forms\Services\FoodFormService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class FoodFormController extends Controller
{
public function __construct(protected FoodFormService $foodFormService) {}
public function __invoke(Request $request): FoodFormResource
{
return FoodFormResource::make(
$this->foodFormService->get(
$request->user('sanctum')->tenant()->firstOrFail()
)
);
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Domains\Forms\Resources;
use App\Domains\Catalog\Models\AttributeOption;
use App\Domains\Event\Models\EventDate;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Collection;
class FoodFormResource extends JsonResource
{
/** @return array<string, mixed> */
public function toArray(Request $request): array
{
return [
'event_dates' => $this->resource['event_dates']->map(
fn (EventDate $eventDate): array => [
'id' => $eventDate->id,
'date' => $eventDate->date->format('Y-m-d'),
]
)->values(),
'schedules' => $this->options($this->resource['schedules']),
'services' => $this->options($this->resource['services']),
];
}
/**
* @param Collection<int, AttributeOption> $options
* @return Collection<int, array{value: string, label: string}>
*/
private function options(Collection $options): Collection
{
return $options->map(fn (AttributeOption $option): array => [
'value' => $option->value,
'label' => $option->label,
])->values();
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace App\Domains\Forms\Services;
use App\Domains\Catalog\Models\Attribute;
use App\Domains\Catalog\Models\AttributeOption;
use App\Domains\Event\Models\EventDate;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Database\Eloquent\Collection;
class FoodFormService
{
/**
* @return array{
* event_dates: Collection<int, EventDate>,
* schedules: Collection<int, AttributeOption>,
* services: Collection<int, AttributeOption>
* }
*/
public function get(Tenant $tenant): array
{
$attributes = Attribute::query()
->where('tenant_codigo', $tenant->codigo)
->whereIn('codigo', ['horario', 'servicio'])
->with('options')
->get()
->keyBy('codigo');
return [
'event_dates' => $tenant->eventDates()->get(),
'schedules' => $attributes->get('horario')?->options ?? new Collection,
'services' => $attributes->get('servicio')?->options ?? new Collection,
];
}
}

View File

@@ -1,6 +1,7 @@
<?php
use App\Domains\Forms\Controllers\AdminApp\EventFormController;
use App\Domains\Forms\Controllers\AdminApp\FoodFormController;
use App\Domains\Forms\Controllers\AdminApp\MerchandiseFormController;
use App\Domains\Forms\Controllers\AdminApp\SaleFormController;
use App\Domains\Forms\Controllers\AdminApp\StaffFormController;
@@ -16,4 +17,8 @@ Route::prefix('v1/adminapp/forms')
'fiesta-futbol-infantil/merchandise',
MerchandiseFormController::class
);
Route::get(
'fiesta-futbol-infantil/food',
FoodFormController::class
);
});

View File

@@ -0,0 +1,80 @@
<?php
namespace Tests\Feature\Forms;
use App\Domains\Auth\Models\User;
use App\Domains\Authorization\Enums\RoleCode;
use App\Domains\Catalog\Models\Attribute;
use App\Domains\Shared\Enums\FieldType;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Tenant\Models\WebsiteType;
use Database\Seeders\AuthorizationSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class AdminAppFoodFormControllerTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seed(AuthorizationSeeder::class);
WebsiteType::query()->create(['codigo' => 'onticket', 'nombre' => 'OnTicket']);
}
public function test_authentication_is_required(): void
{
$this->getJson('/api/v1/adminapp/forms/fiesta-futbol-infantil/food')
->assertUnauthorized();
}
public function test_it_returns_event_dates_schedules_and_services_for_the_tenant(): void
{
$tenant = Tenant::query()->create([
'codigo' => 'fiesta',
'nombre' => 'Fiesta',
'dominio' => 'fiesta.test',
'website_type_code' => 'onticket',
]);
$tenant->eventDates()->create([
'date' => '2026-10-09',
'time_start' => '00:00',
'time_end' => '23:59',
]);
$this->createAttribute($tenant, 'horario', [
['value' => 'Cena', 'label' => 'Cena', 'sort_order' => 2],
['value' => 'Almuerzo', 'label' => 'Almuerzo', 'sort_order' => 1],
]);
$this->createAttribute($tenant, 'servicio', [
['value' => 'Comedor', 'label' => 'Comedor', 'sort_order' => 1],
]);
Sanctum::actingAs(User::factory()->create([
'rol_codigo' => RoleCode::AdminApp->value,
'tenant_codigo' => $tenant->codigo,
]));
$this->getJson('/api/v1/adminapp/forms/fiesta-futbol-infantil/food')
->assertOk()
->assertJsonCount(1, 'data.event_dates')
->assertJsonPath('data.event_dates.0.date', '2026-10-09')
->assertJsonPath('data.schedules.0.value', 'Almuerzo')
->assertJsonPath('data.schedules.1.value', 'Cena')
->assertJsonPath('data.services.0.value', 'Comedor');
}
/** @param array<int, array{value: string, label: string, sort_order: int}> $options */
private function createAttribute(Tenant $tenant, string $code, array $options): void
{
$attribute = Attribute::query()->create([
'tenant_codigo' => $tenant->codigo,
'codigo' => $code,
'nombre' => ucfirst($code),
'type' => FieldType::Select,
'is_required' => true,
]);
$attribute->options()->createMany($options);
}
}