fix(staff): preserve scanner history on deletion

This commit is contained in:
2026-09-03 09:58:08 -03:00
parent c2233389d0
commit ddb8c3743f
6 changed files with 91 additions and 4 deletions

View File

@@ -2,6 +2,8 @@
namespace Tests\Feature\Staff;
use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Auth\Models\ResetPasswordAttempt;
use App\Domains\Auth\Models\User;
use App\Domains\Authorization\Enums\RoleCode;
@@ -9,9 +11,11 @@ use App\Domains\Catalog\Models\Category;
use App\Domains\Notification\Events\PasswordResetRequested;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Tenant\Models\WebsiteType;
use App\Domains\Ticket\Models\Ticket;
use Database\Seeders\AuthorizationSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Str;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
@@ -30,11 +34,21 @@ class StaffControllerTest extends TestCase
Event::fake([PasswordResetRequested::class]);
$this->seed(AuthorizationSeeder::class);
WebsiteType::query()->create(['codigo' => 'onticket', 'nombre' => 'OnTicket']);
$headerLogo = $this->createAttachment('header.png');
$footerLogo = $this->createAttachment('footer.png');
$this->tenant = Tenant::query()->create([
'codigo' => 'acme',
'nombre' => 'Acme',
'dominio' => 'acme.test',
'website_type_code' => 'onticket',
'primary_color' => '#111111',
'secondary_color' => '#222222',
'danger_color' => '#cc0000',
'success_color' => '#008800',
'header_bg_color' => '#ffffff',
'footer_bg_color' => '#ffffff',
'header_logo_id' => $headerLogo->id,
'footer_logo_id' => $footerLogo->id,
]);
$this->admin = User::factory()->create([
'rol_codigo' => RoleCode::AdminApp->value,
@@ -92,8 +106,33 @@ class StaffControllerTest extends TestCase
'categoria_id' => $firstCategory->id,
]);
$ticket = Ticket::query()->create([
'tenant_code' => $this->tenant->codigo,
'ticket' => (string) Str::uuid(),
'user_id' => $this->admin->id,
'used_at' => now(),
'scanner_user_id' => $staffId,
]);
$accessTokenId = User::query()
->findOrFail($staffId)
->createToken('scanner', ['scanner'])
->accessToken
->getKey();
$this->deleteJson("/api/v1/adminapp/tenant/staff/{$staffId}")->assertNoContent();
$this->assertDatabaseMissing('users', ['id' => $staffId]);
$this->assertSoftDeleted('users', ['id' => $staffId]);
$this->assertDatabaseMissing('personal_access_tokens', ['id' => $accessTokenId]);
$this->assertDatabaseHas('category_scanners', [
'user_id' => $staffId,
'categoria_id' => $secondCategory->id,
]);
$this->assertSame($staffId, $ticket->fresh()->scanner_user_id);
$this->assertSame('Ada Byron', $ticket->fresh()->scannerUser?->nombre_apellido);
$this->getJson('/api/v1/adminapp/tenant/staff')
->assertOk()
->assertJsonCount(0, 'data');
}
public function test_admin_cannot_assign_another_tenants_category(): void
@@ -104,6 +143,14 @@ class StaffControllerTest extends TestCase
'nombre' => 'Other',
'dominio' => 'other.test',
'website_type_code' => 'onticket',
'primary_color' => '#111111',
'secondary_color' => '#222222',
'danger_color' => '#cc0000',
'success_color' => '#008800',
'header_bg_color' => '#ffffff',
'footer_bg_color' => '#ffffff',
'header_logo_id' => $this->tenant->header_logo_id,
'footer_logo_id' => $this->tenant->footer_logo_id,
]);
$foreignCategory = Category::query()->create([
'tenant_code' => $otherTenant->codigo,
@@ -183,6 +230,16 @@ class StaffControllerTest extends TestCase
$this->getJson('/api/v1/adminapp/tenant/staff')->assertForbidden();
}
private function createAttachment(string $filename): Attachment
{
return Attachment::query()->create([
'path' => "test/{$filename}",
'filename' => $filename,
'type' => AttachmentType::Image,
'mime_type' => 'image/png',
]);
}
private function createCategory(string $name): Category
{
return Category::query()->create([