2026-05-11 19:06:07 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
use App\Jobs\SyncSupplierProjectJob;
|
|
|
|
|
use App\Models\Project;
|
|
|
|
|
use App\Models\Tenant;
|
|
|
|
|
use App\Models\User;
|
2026-05-21 08:08:33 +03:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2026-05-11 19:06:07 +03:00
|
|
|
use Illuminate\Support\Facades\Queue;
|
|
|
|
|
|
|
|
|
|
beforeEach(fn () => Queue::fake());
|
|
|
|
|
|
2026-05-21 08:08:33 +03:00
|
|
|
it('destroy hard-deletes a project with no deals', function () {
|
2026-05-11 19:06:07 +03:00
|
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
|
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
|
$project = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)->deleteJson("/api/projects/{$project->id}")->assertNoContent();
|
|
|
|
|
|
2026-05-21 08:08:33 +03:00
|
|
|
expect(Project::find($project->id))->toBeNull();
|
2026-05-11 19:06:07 +03:00
|
|
|
});
|
|
|
|
|
|
2026-05-21 08:08:33 +03:00
|
|
|
it('destroy returns 422 if project has deals', function () {
|
2026-05-11 19:06:07 +03:00
|
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
|
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
2026-05-21 08:08:33 +03:00
|
|
|
$project = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
|
|
|
|
|
DB::table('deals')->insert([
|
|
|
|
|
'tenant_id' => $tenant->id, 'project_id' => $project->id,
|
|
|
|
|
'phone' => '79990001100', 'status' => 'new',
|
|
|
|
|
'received_at' => now(), 'created_at' => now(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)->deleteJson("/api/projects/{$project->id}")->assertStatus(422);
|
2026-05-11 19:06:07 +03:00
|
|
|
|
2026-05-21 08:08:33 +03:00
|
|
|
expect(Project::find($project->id))->not->toBeNull();
|
2026-05-11 19:06:07 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('sync re-dispatches SyncSupplierProjectJob', function () {
|
|
|
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
|
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
|
$project = Project::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)->postJson("/api/projects/{$project->id}/sync")
|
|
|
|
|
->assertStatus(202)
|
|
|
|
|
->assertJsonPath('queued', true);
|
|
|
|
|
|
|
|
|
|
Queue::assertPushed(SyncSupplierProjectJob::class);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('toggle-active flips is_active flag', function () {
|
|
|
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
|
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
|
$project = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)->patchJson("/api/projects/{$project->id}/toggle-active", ['is_active' => false])
|
|
|
|
|
->assertOk();
|
|
|
|
|
|
|
|
|
|
expect($project->fresh()->is_active)->toBeFalse();
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-22 16:52:30 +03:00
|
|
|
it('toggle-active dispatches supplier sync so pause/resume reaches the supplier (#10)', function () {
|
|
|
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
|
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
|
$project = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)->patchJson("/api/projects/{$project->id}/toggle-active", ['is_active' => false])
|
|
|
|
|
->assertOk();
|
|
|
|
|
|
|
|
|
|
Queue::assertPushed(SyncSupplierProjectJob::class, fn ($job) => $job->projectId === $project->id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('bulk pause dispatches supplier sync for each affected project (#10)', function () {
|
|
|
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
|
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
|
$p1 = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
|
|
|
|
|
$p2 = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)->postJson('/api/projects/bulk', [
|
|
|
|
|
'action' => 'pause', 'ids' => [$p1->id, $p2->id],
|
|
|
|
|
])->assertOk()->assertJsonPath('updated', 2);
|
|
|
|
|
|
|
|
|
|
Queue::assertPushed(SyncSupplierProjectJob::class, fn ($job) => in_array($job->projectId, [$p1->id, $p2->id], true));
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-11 19:06:07 +03:00
|
|
|
it('bulk pause sets is_active=false on multiple', function () {
|
|
|
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
|
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
|
$p1 = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
|
|
|
|
|
$p2 = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)->postJson('/api/projects/bulk', [
|
|
|
|
|
'action' => 'pause', 'ids' => [$p1->id, $p2->id],
|
|
|
|
|
])->assertOk()->assertJsonPath('updated', 2);
|
|
|
|
|
|
|
|
|
|
expect($p1->fresh()->is_active)->toBeFalse();
|
|
|
|
|
expect($p2->fresh()->is_active)->toBeFalse();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('bulk filters out cross-tenant ids silently', function () {
|
|
|
|
|
$tenantA = Tenant::factory()->create();
|
|
|
|
|
$tenantB = Tenant::factory()->create();
|
|
|
|
|
$userA = User::factory()->create(['tenant_id' => $tenantA->id]);
|
|
|
|
|
$pA = Project::factory()->create(['tenant_id' => $tenantA->id, 'is_active' => true]);
|
|
|
|
|
$pB = Project::factory()->create(['tenant_id' => $tenantB->id, 'is_active' => true]);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($userA)->postJson('/api/projects/bulk', [
|
|
|
|
|
'action' => 'pause', 'ids' => [$pA->id, $pB->id],
|
|
|
|
|
])->assertOk()->assertJsonPath('updated', 1);
|
|
|
|
|
|
|
|
|
|
expect($pB->fresh()->is_active)->toBeTrue();
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-21 08:08:33 +03:00
|
|
|
it('bulk delete removes project with no deals', function () {
|
2026-05-11 19:06:07 +03:00
|
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
|
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
|
$p1 = Project::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)->postJson('/api/projects/bulk', [
|
2026-05-21 08:08:33 +03:00
|
|
|
'action' => 'delete', 'ids' => [$p1->id],
|
|
|
|
|
])->assertOk()->assertJsonPath('updated', 1);
|
2026-05-11 19:06:07 +03:00
|
|
|
|
2026-05-21 08:08:33 +03:00
|
|
|
expect(Project::find($p1->id))->toBeNull();
|
2026-05-11 19:06:07 +03:00
|
|
|
});
|
|
|
|
|
|
2026-05-12 14:38:59 +03:00
|
|
|
it('bulk rejects > 500 ids', function () {
|
2026-05-11 19:06:07 +03:00
|
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
|
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)->postJson('/api/projects/bulk', [
|
2026-05-12 14:38:59 +03:00
|
|
|
'action' => 'pause', 'ids' => range(1, 501),
|
2026-05-11 19:06:07 +03:00
|
|
|
])->assertStatus(422);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('bulk rejects unknown action', function () {
|
|
|
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
|
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)->postJson('/api/projects/bulk', [
|
|
|
|
|
'action' => 'destroy_all', 'ids' => [1],
|
|
|
|
|
])->assertStatus(422);
|
|
|
|
|
});
|