feat(tickets): add TicketFilterFormController, TicketFilterFormService, and TicketFilterFormResource with routes and tests

This commit is contained in:
2026-08-31 10:57:05 -03:00
parent af516ce9e3
commit 34a0a14404
5 changed files with 375 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Domains\Forms\Controllers\AdminApp;
use App\Domains\Forms\Resources\TicketFilterFormResource;
use App\Domains\Forms\Services\TicketFilterFormService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class TicketFilterFormController extends Controller
{
public function __construct(private readonly TicketFilterFormService $formService) {}
public function __invoke(Request $request): TicketFilterFormResource
{
$tenant = $request->user('sanctum')->tenant()->firstOrFail();
return TicketFilterFormResource::make($this->formService->get($tenant));
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Domains\Forms\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class TicketFilterFormResource extends JsonResource
{
/** @return array<string, mixed> */
public function toArray(Request $request): array
{
return [
'code' => $this->resource['code'],
'action' => $this->resource['action'],
'method' => $this->resource['method'],
'fields' => $this->resource['fields'],
];
}
}

View File

@@ -0,0 +1,118 @@
<?php
namespace App\Domains\Forms\Services;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Ticket\Models\Ticket;
class TicketFilterFormService
{
private const FIESTA_FUTBOL_INFANTIL = 'fiesta_futbol_infantil';
public function __construct(private readonly TicketFormService $ticketFormService) {}
/** @return array<string, mixed> */
public function get(Tenant $tenant): array
{
$fields = $this->commonFields();
if ($tenant->codigo === self::FIESTA_FUTBOL_INFANTIL) {
$fields = [
...$this->fiestaFutbolInfantilFields($tenant),
...$fields,
];
}
return [
'code' => 'tickets_filter',
'action' => '/api/v1/adminapp/tenant/tickets',
'method' => 'GET',
'fields' => $fields,
];
}
/** @return list<array<string, mixed>> */
private function fiestaFutbolInfantilFields(Tenant $tenant): array
{
$form = $this->ticketFormService->get($tenant);
return [
[
'name' => 'category',
'label' => 'Categoría',
'type' => 'select',
'required' => false,
'default' => null,
'placeholder' => 'Categoría',
'options' => array_map(
fn (array $category): array => [
'value' => $category['value'],
'label' => $category['label'],
'children' => [
'field' => 'product',
'disabled' => $category['products'] === [],
'options' => array_map(
fn (array $product): array => [
'value' => $product['value'],
'label' => $product['label'],
'children' => [
'field' => 'type',
'disabled' => $product['types'] === [],
'options' => $product['types'],
],
],
$category['products'],
),
],
],
$form['categories'],
),
],
$this->dependentSelect('product', 'Producto', 'category'),
$this->dependentSelect('type', 'Tipo', 'product'),
];
}
/** @return list<array<string, mixed>> */
private function commonFields(): array
{
return [
[
'name' => 'date',
'label' => 'Fecha',
'type' => 'date',
'required' => false,
'default' => null,
],
[
'name' => 'status',
'label' => 'Estado',
'type' => 'select',
'required' => false,
'default' => null,
'placeholder' => 'Estado',
'options' => [
['value' => Ticket::STATUS_ACTIVE, 'label' => 'Activo'],
['value' => Ticket::STATUS_USED, 'label' => 'Usado'],
['value' => Ticket::STATUS_EXPIRED, 'label' => 'Vencido'],
],
],
];
}
/** @return array<string, mixed> */
private function dependentSelect(string $name, string $label, string $dependency): array
{
return [
'name' => $name,
'label' => $label,
'type' => 'select',
'required' => false,
'default' => null,
'placeholder' => $label,
'depends_on' => $dependency,
'disabled' => true,
'options' => [],
];
}
}

View File

@@ -5,6 +5,7 @@ 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;
use App\Domains\Forms\Controllers\AdminApp\TicketFilterFormController;
use App\Domains\Forms\Controllers\AdminApp\TicketFormController;
use Illuminate\Support\Facades\Route;
@@ -14,6 +15,9 @@ Route::prefix('v1/adminapp/forms')
Route::get('event', EventFormController::class);
Route::get('sale', SaleFormController::class);
Route::get('staff', StaffFormController::class);
Route::get('tickets-filter', TicketFilterFormController::class)
->middleware('tenant.menu:adminapp.tickets')
->name('adminapp.forms.tickets-filter');
Route::get(
'fiesta-futbol-infantil/ticket',
TicketFormController::class

View File

@@ -0,0 +1,213 @@
<?php
namespace Tests\Feature\Forms;
use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Auth\Models\User;
use App\Domains\Authorization\Enums\RoleCode;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Menu\Models\Menu;
use App\Domains\Tenant\Models\Tenant;
use Database\Seeders\AttributeSeeder;
use Database\Seeders\AuthorizationSeeder;
use Database\Seeders\FiestaFutbolInfantilProductSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class AdminAppTicketFilterFormControllerTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seed(AuthorizationSeeder::class);
}
public function test_authentication_is_required(): void
{
$this->getJson('/api/v1/adminapp/forms/tickets-filter')->assertUnauthorized();
}
public function test_the_tenant_must_have_the_tickets_menu(): void
{
$tenant = $this->createTenant('tenant_without_tickets');
Sanctum::actingAs($this->createAdminAppUser($tenant));
$this->getJson('/api/v1/adminapp/forms/tickets-filter')->assertNotFound();
}
public function test_it_returns_the_common_ticket_filter_fields_for_other_tenants(): void
{
$tenant = $this->createTenant('another_tenant');
$this->grantTicketsMenu($tenant);
Sanctum::actingAs($this->createAdminAppUser($tenant));
$this->getJson('/api/v1/adminapp/forms/tickets-filter')
->assertOk()
->assertExactJson([
'data' => [
'code' => 'tickets_filter',
'action' => '/api/v1/adminapp/tenant/tickets',
'method' => 'GET',
'fields' => [
[
'name' => 'date',
'label' => 'Fecha',
'type' => 'date',
'required' => false,
'default' => null,
],
[
'name' => 'status',
'label' => 'Estado',
'type' => 'select',
'required' => false,
'default' => null,
'placeholder' => 'Estado',
'options' => [
['value' => 'active', 'label' => 'Activo'],
['value' => 'used', 'label' => 'Usado'],
['value' => 'expired', 'label' => 'Vencido'],
],
],
],
],
]);
}
public function test_it_returns_the_complete_nested_form_for_fiesta_futbol_infantil(): void
{
$tenant = $this->createFiestaFutbolInfantilTenant();
$this->grantTicketsMenu($tenant);
Sanctum::actingAs($this->createAdminAppUser($tenant));
$response = $this->getJson('/api/v1/adminapp/forms/tickets-filter')
->assertOk()
->assertJsonPath('data.fields.0.name', 'category')
->assertJsonPath('data.fields.1.name', 'product')
->assertJsonPath('data.fields.1.depends_on', 'category')
->assertJsonPath('data.fields.2.name', 'type')
->assertJsonPath('data.fields.2.depends_on', 'product')
->assertJsonPath('data.fields.3.name', 'date')
->assertJsonPath('data.fields.4.name', 'status');
$categories = collect($response->json('data.fields.0.options'));
$this->assertSame(
['Entradas', 'Camping', 'Comida', 'Merchandising'],
$categories->pluck('label')->all(),
);
$entries = $categories->firstWhere('value', 'entradas');
$this->assertSame('Abono', $entries['children']['options'][0]['label']);
$this->assertTrue($entries['children']['options'][0]['children']['disabled']);
$camping = $categories->firstWhere('value', 'alojamientos');
$this->assertSame(
['Carpa', 'Motorhome'],
collect($camping['children']['options'])->pluck('label')->all(),
);
$this->assertTrue($camping['children']['options'][0]['children']['disabled']);
$merchandise = $categories->firstWhere('value', 'merchandising');
$this->assertSame('Camiseta', $merchandise['children']['options'][0]['label']);
$this->assertSame(
['Verde', 'Blanco'],
collect($merchandise['children']['options'][0]['children']['options'])->pluck('label')->all(),
);
}
public function test_a_food_schedule_is_only_returned_when_a_variant_exists_for_the_date(): void
{
$tenant = $this->createFiestaFutbolInfantilTenant();
$this->grantTicketsMenu($tenant);
Sanctum::actingAs($this->createAdminAppUser($tenant));
$date = $tenant->eventDates()->orderByDesc('date')->firstOrFail();
$food = CatalogItem::query()
->where('tenant_code', $tenant->codigo)
->where('slug', 'comida')
->with([
'itemAttributes.attribute.options',
'variants.definitions.itemAttribute.attribute.options',
'variants.eventDates',
'variants.eventDate',
])
->firstOrFail();
$food->variants
->filter(fn ($variant): bool => $variant->selectedEventDates()->contains('id', $date->id)
&& $variant->selectionValues()->get('horario') === 'Almuerzo')
->each->delete();
$response = $this->getJson('/api/v1/adminapp/forms/tickets-filter')->assertOk();
$foodCategory = collect($response->json('data.fields.0.options'))->firstWhere('value', 'comidas');
$dateProduct = collect($foodCategory['children']['options'])
->firstWhere('value', (string) $date->id);
$schedules = collect($dateProduct['children']['options'])->pluck('value');
$this->assertNotContains('Almuerzo', $schedules);
$this->assertContains('Desayuno', $schedules);
$this->assertContains('Cena', $schedules);
}
private function createFiestaFutbolInfantilTenant(): Tenant
{
$tenant = $this->createTenant('fiesta_futbol_infantil');
$this->seed([AttributeSeeder::class, FiestaFutbolInfantilProductSeeder::class]);
return $tenant->refresh();
}
private function createTenant(string $code): Tenant
{
$headerLogo = $this->createAttachment("{$code}-header.png");
$footerLogo = $this->createAttachment("{$code}-footer.png");
return Tenant::query()->create([
'codigo' => $code,
'nombre' => ucfirst($code),
'dominio' => "{$code}.test",
'primary_color' => '#111111',
'secondary_color' => '#222222',
'danger_color' => '#333333',
'success_color' => '#444444',
'header_bg_color' => '#ffffff',
'footer_bg_color' => '#ffffff',
'header_logo_id' => $headerLogo->id,
'footer_logo_id' => $footerLogo->id,
]);
}
private function createAttachment(string $filename): Attachment
{
return Attachment::query()->create([
'path' => "tests/{$filename}",
'filename' => $filename,
'type' => AttachmentType::Image,
'mime_type' => 'image/png',
]);
}
private function createAdminAppUser(Tenant $tenant): User
{
return User::factory()->create([
'rol_codigo' => RoleCode::AdminApp->value,
'tenant_codigo' => $tenant->codigo,
]);
}
private function grantTicketsMenu(Tenant $tenant): void
{
$menu = Menu::query()->create([
'code' => 'adminapp.tickets',
'label' => 'Tickets',
'route' => '/admin/tickets',
]);
$tenant->menues()->attach($menu->code);
}
}