feat(ticket): add TicketController, TicketResource, and related routes with tests
This commit is contained in:
24
app/Domains/Ticket/Controllers/TicketController.php
Normal file
24
app/Domains/Ticket/Controllers/TicketController.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Ticket\Controllers;
|
||||
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Domains\Ticket\Models\Ticket;
|
||||
use App\Domains\Ticket\Resources\TicketResource;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class TicketController extends Controller
|
||||
{
|
||||
public function index(Request $request, Tenant $tenant): JsonResponse
|
||||
{
|
||||
$tickets = Ticket::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->where('user_id', $request->user()->getKey())
|
||||
->orderByDesc('id')
|
||||
->get();
|
||||
|
||||
return TicketResource::collection($tickets)->response();
|
||||
}
|
||||
}
|
||||
31
app/Domains/Ticket/Resources/TicketResource.php
Normal file
31
app/Domains/Ticket/Resources/TicketResource.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Ticket\Resources;
|
||||
|
||||
use App\Domains\Ticket\Models\Ticket;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/** @mixin Ticket */
|
||||
class TicketResource extends JsonResource
|
||||
{
|
||||
/** @return array<string, mixed> */
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'tenant_code' => $this->tenant_code,
|
||||
'ticket' => $this->ticket,
|
||||
'name' => $this->name,
|
||||
'description' => $this->description,
|
||||
'source_catalog_item_id' => $this->source_catalog_item_id,
|
||||
'source_variant_id' => $this->source_variant_id,
|
||||
'starts_at' => $this->starts_at,
|
||||
'expires_at' => $this->expires_at,
|
||||
'used_at' => $this->used_at,
|
||||
'is_valid' => $this->is_valid,
|
||||
'is_expired' => $this->is_expired,
|
||||
'is_used' => $this->is_used,
|
||||
];
|
||||
}
|
||||
}
|
||||
10
app/Domains/Ticket/routes/api.php
Normal file
10
app/Domains/Ticket/routes/api.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
use App\Domains\Ticket\Controllers\TicketController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::prefix('tenants/{tenant:codigo}')
|
||||
->middleware('auth:sanctum')
|
||||
->group(function (): void {
|
||||
Route::get('tickets', [TicketController::class, 'index']);
|
||||
});
|
||||
@@ -8,3 +8,4 @@ require __DIR__.'/../app/Domains/Purchase/routes/api.php';
|
||||
require __DIR__.'/../app/Domains/Tenant/routes/api.php';
|
||||
require __DIR__.'/../app/Domains/Integration/routes/api.php';
|
||||
require __DIR__.'/../app/Domains/Menu/routes/api.php';
|
||||
require __DIR__.'/../app/Domains/Ticket/routes/api.php';
|
||||
|
||||
104
tests/Feature/Ticket/TicketControllerTest.php
Normal file
104
tests/Feature/Ticket/TicketControllerTest.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Ticket;
|
||||
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Auth\Models\User;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Domains\Ticket\Models\Ticket;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TicketControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_an_authenticated_user_can_list_their_tickets_for_the_tenant(): void
|
||||
{
|
||||
$tenant = $this->createTenant('current');
|
||||
$user = User::factory()->create();
|
||||
$olderTicket = $this->createTicket($tenant, $user, 'Older ticket');
|
||||
$newerTicket = $this->createTicket($tenant, $user, 'Newer ticket');
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->getJson("/api/tenants/{$tenant->codigo}/tickets")
|
||||
->assertOk()
|
||||
->assertJsonCount(2, 'data')
|
||||
->assertJsonPath('data.0.id', $newerTicket->id)
|
||||
->assertJsonPath('data.0.name', 'Newer ticket')
|
||||
->assertJsonPath('data.0.is_valid', true)
|
||||
->assertJsonPath('data.0.is_expired', false)
|
||||
->assertJsonPath('data.0.is_used', false)
|
||||
->assertJsonPath('data.1.id', $olderTicket->id)
|
||||
->assertJsonMissingPath('meta')
|
||||
->assertJsonMissingPath('links');
|
||||
}
|
||||
|
||||
public function test_it_does_not_include_tickets_from_other_users_or_tenants(): void
|
||||
{
|
||||
$tenant = $this->createTenant('current');
|
||||
$otherTenant = $this->createTenant('other');
|
||||
$user = User::factory()->create();
|
||||
$otherUser = User::factory()->create();
|
||||
$visibleTicket = $this->createTicket($tenant, $user, 'Visible');
|
||||
$this->createTicket($tenant, $otherUser, 'Other user');
|
||||
$this->createTicket($otherTenant, $user, 'Other tenant');
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->getJson("/api/tenants/{$tenant->codigo}/tickets")
|
||||
->assertOk()
|
||||
->assertJsonCount(1, 'data')
|
||||
->assertJsonPath('data.0.id', $visibleTicket->id);
|
||||
}
|
||||
|
||||
public function test_authentication_is_required_to_list_tickets(): void
|
||||
{
|
||||
$tenant = $this->createTenant('current');
|
||||
|
||||
$this->getJson("/api/tenants/{$tenant->codigo}/tickets")
|
||||
->assertUnauthorized();
|
||||
}
|
||||
|
||||
private function createTicket(Tenant $tenant, User $user, string $name): Ticket
|
||||
{
|
||||
return Ticket::query()->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'ticket' => (string) Str::uuid(),
|
||||
'name' => $name,
|
||||
'description' => "Description for {$name}",
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createTenant(string $code): Tenant
|
||||
{
|
||||
$headerLogo = $this->createAttachment("{$code}-header");
|
||||
$footerLogo = $this->createAttachment("{$code}-footer");
|
||||
|
||||
return Tenant::query()->create([
|
||||
'codigo' => $code,
|
||||
'nombre' => ucfirst($code),
|
||||
'dominio' => "{$code}.local",
|
||||
'primary_color' => '#000000',
|
||||
'secondary_color' => '#000000',
|
||||
'danger_color' => '#000000',
|
||||
'success_color' => '#000000',
|
||||
'header_bg_color' => '#000000',
|
||||
'footer_bg_color' => '#000000',
|
||||
'header_logo_id' => $headerLogo->id,
|
||||
'footer_logo_id' => $footerLogo->id,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createAttachment(string $name): Attachment
|
||||
{
|
||||
return Attachment::query()->create([
|
||||
'path' => "test/{$name}.png",
|
||||
'filename' => "{$name}.png",
|
||||
'type' => AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user