feat: Implement staff management features; add controller, service, resource, routes, and tests for staff and category assignments

This commit is contained in:
2026-08-04 16:20:30 -03:00
parent 3a039a6055
commit 2a15dc92be
18 changed files with 618 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
<?php
namespace Tests\Feature\Forms;
use App\Domains\Auth\Models\User;
use App\Domains\Authorization\Enums\RoleCode;
use App\Domains\Catalog\Models\Category;
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 AdminAppStaffFormControllerTest 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/staff')->assertUnauthorized();
}
public function test_adminapp_user_gets_only_its_tenant_categories(): void
{
$tenant = $this->createTenant('acme');
$otherTenant = $this->createTenant('other');
$category = Category::query()->create([
'tenant_code' => $tenant->codigo,
'nombre' => 'Bebidas',
]);
Category::query()->create([
'tenant_code' => $tenant->codigo,
'categoria_id' => $category->id,
'nombre' => 'Gaseosas',
]);
Category::query()->create([
'tenant_code' => $otherTenant->codigo,
'nombre' => 'Privada',
]);
Sanctum::actingAs(User::factory()->create([
'rol_codigo' => RoleCode::AdminApp->value,
'tenant_codigo' => $tenant->codigo,
]));
$this->getJson('/api/v1/adminapp/forms/staff')
->assertOk()
->assertJsonCount(1, 'data.categories')
->assertJsonPath('data.categories.0.id', $category->id)
->assertJsonPath('data.categories.0.nombre', 'Bebidas')
->assertJsonMissingPath('data.categories.0.categoria_id')
->assertJsonMissingPath('data.roles');
}
public function test_customer_cannot_get_staff_form(): void
{
Sanctum::actingAs(User::factory()->create([
'rol_codigo' => RoleCode::User->value,
]));
$this->getJson('/api/v1/adminapp/forms/staff')->assertForbidden();
}
private function createTenant(string $code): Tenant
{
return Tenant::query()->create([
'codigo' => $code,
'nombre' => ucfirst($code),
'dominio' => "{$code}.test",
'website_type_code' => 'onticket',
]);
}
}

View File

@@ -0,0 +1,138 @@
<?php
namespace Tests\Feature\Staff;
use App\Domains\Auth\Models\User;
use App\Domains\Authorization\Enums\RoleCode;
use App\Domains\Catalog\Models\Category;
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 StaffControllerTest extends TestCase
{
use RefreshDatabase;
private Tenant $tenant;
private User $admin;
protected function setUp(): void
{
parent::setUp();
$this->seed(AuthorizationSeeder::class);
WebsiteType::query()->create(['codigo' => 'onticket', 'nombre' => 'OnTicket']);
$this->tenant = Tenant::query()->create([
'codigo' => 'acme',
'nombre' => 'Acme',
'dominio' => 'acme.test',
'website_type_code' => 'onticket',
]);
$this->admin = User::factory()->create([
'rol_codigo' => RoleCode::AdminApp->value,
'tenant_codigo' => $this->tenant->codigo,
]);
}
public function test_adminapp_can_create_update_list_and_delete_staff_with_categories(): void
{
Sanctum::actingAs($this->admin);
$firstCategory = $this->createCategory('Bebidas');
$secondCategory = $this->createCategory('Comidas');
$response = $this->postJson('/api/v1/adminapp/tenant/staff', [
'nombre_apellido' => 'Ada Lovelace',
'dni' => '12345678',
'email' => 'ADA@example.test',
'category_ids' => [$firstCategory->id],
])->assertSuccessful()
->assertJsonPath('data.email', 'ada@example.test')
->assertJsonPath('data.role.codigo', RoleCode::Scanner->value)
->assertJsonPath('data.categories.0.id', $firstCategory->id);
$staffId = $response->json('data.id');
$this->assertDatabaseHas('category_scanners', [
'user_id' => $staffId,
'categoria_id' => $firstCategory->id,
]);
$this->getJson('/api/v1/adminapp/tenant/staff?search=ada')
->assertOk()
->assertJsonCount(1, 'data');
$this->putJson("/api/v1/adminapp/tenant/staff/{$staffId}", [
'nombre_apellido' => 'Ada Byron',
'dni' => '12345678',
'email' => 'ada@example.test',
'category_ids' => [$secondCategory->id],
])->assertOk()
->assertJsonPath('data.nombre_apellido', 'Ada Byron')
->assertJsonPath('data.categories.0.id', $secondCategory->id);
$this->assertDatabaseMissing('category_scanners', [
'user_id' => $staffId,
'categoria_id' => $firstCategory->id,
]);
$this->deleteJson("/api/v1/adminapp/tenant/staff/{$staffId}")->assertNoContent();
$this->assertDatabaseMissing('users', ['id' => $staffId]);
}
public function test_admin_cannot_assign_another_tenants_category(): void
{
Sanctum::actingAs($this->admin);
$otherTenant = Tenant::query()->create([
'codigo' => 'other',
'nombre' => 'Other',
'dominio' => 'other.test',
'website_type_code' => 'onticket',
]);
$foreignCategory = Category::query()->create([
'tenant_code' => $otherTenant->codigo,
'nombre' => 'Privada',
]);
$payload = [
'nombre_apellido' => 'Grace Hopper',
'dni' => '87654321',
'email' => 'grace@example.test',
'category_ids' => [$foreignCategory->id],
];
$this->postJson('/api/v1/adminapp/tenant/staff', $payload)
->assertUnprocessable()
->assertJsonValidationErrors('category_ids');
$parent = $this->createCategory('Local');
$child = Category::query()->create([
'tenant_code' => $this->tenant->codigo,
'categoria_id' => $parent->id,
'nombre' => 'Subcategoría',
]);
$payload['category_ids'] = [$child->id];
$this->postJson('/api/v1/adminapp/tenant/staff', $payload)
->assertUnprocessable()
->assertJsonValidationErrors('category_ids');
}
public function test_customer_cannot_manage_staff(): void
{
Sanctum::actingAs(User::factory()->create([
'rol_codigo' => RoleCode::User->value,
]));
$this->getJson('/api/v1/adminapp/tenant/staff')->assertForbidden();
}
private function createCategory(string $name): Category
{
return Category::query()->create([
'tenant_code' => $this->tenant->codigo,
'nombre' => $name,
]);
}
}