141 lines
6.2 KiB
PHP
141 lines
6.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Models\Deal;
|
|
use App\Models\Project;
|
|
use App\Models\Tenant;
|
|
use Carbon\Carbon;
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
uses(DatabaseTransactions::class);
|
|
|
|
/**
|
|
* Вспомогательная функция: создать сделку с заданными параметрами.
|
|
*
|
|
* Фабрика Deal::factory() по умолчанию: received_at = now() (текущий месяц,
|
|
* партиция deals_2026_05 существует). is_test = false, deleted_at = null.
|
|
* Для тестовых дат subDays(1..6) — всё в мае 2026, партиция есть.
|
|
*/
|
|
function makeDashboardDeal(
|
|
Tenant $tenant,
|
|
Project $project,
|
|
string $status,
|
|
Carbon|CarbonImmutable $receivedAt,
|
|
?Carbon $deletedAt = null,
|
|
bool $isTest = false,
|
|
): Deal {
|
|
return Deal::factory()->create([
|
|
'tenant_id' => $tenant->id,
|
|
'project_id' => $project->id,
|
|
'status' => $status,
|
|
'received_at' => $receivedAt,
|
|
'deleted_at' => $deletedAt,
|
|
'is_test' => $isTest,
|
|
]);
|
|
}
|
|
|
|
it('422 без tenant_id', function () {
|
|
$this->getJson('/api/dashboard/summary')->assertStatus(422);
|
|
});
|
|
|
|
it('404 для несуществующего тенанта', function () {
|
|
$this->getJson('/api/dashboard/summary?tenant_id=999999')->assertStatus(404);
|
|
});
|
|
|
|
it('возвращает структуру summary с range по умолчанию 7d', function () {
|
|
$tenant = Tenant::factory()->create([
|
|
'limits' => ['max_projects' => 10],
|
|
'balance_rub' => '14250.00',
|
|
'balance_leads' => 285,
|
|
]);
|
|
$this->getJson("/api/dashboard/summary?tenant_id={$tenant->id}")
|
|
->assertOk()
|
|
->assertJsonPath('range', '7d')
|
|
->assertJsonPath('balance.amount_rub', '14250.00')
|
|
->assertJsonStructure([
|
|
'range',
|
|
'leads_received' => ['value', 'delta_pct', 'delta_dir'],
|
|
'conversion' => ['value', 'delta_pp', 'delta_dir'],
|
|
'active_projects' => ['active', 'limit'],
|
|
'balance' => ['amount_rub', 'runway_days', 'runway_leads'],
|
|
'activity' => ['points', 'labels', 'max'],
|
|
'funnel',
|
|
]);
|
|
});
|
|
|
|
it('leads_received считает только сделки окна, без deleted и is_test', function () {
|
|
$tenant = Tenant::factory()->create();
|
|
$project = Project::factory()->create(['tenant_id' => $tenant->id]);
|
|
// 3 живые сделки в окне 7d + 1 deleted + 1 is_test + 1 вне окна (8 дней назад)
|
|
makeDashboardDeal($tenant, $project, 'new', now()->subDays(1));
|
|
makeDashboardDeal($tenant, $project, 'new', now()->subDays(2));
|
|
makeDashboardDeal($tenant, $project, 'won', now()->subDays(3));
|
|
makeDashboardDeal($tenant, $project, 'new', now()->subDays(1), deletedAt: now());
|
|
makeDashboardDeal($tenant, $project, 'new', now()->subDays(1), isTest: true);
|
|
makeDashboardDeal($tenant, $project, 'new', now()->subDays(8));
|
|
|
|
$this->getJson("/api/dashboard/summary?tenant_id={$tenant->id}&range=7d")
|
|
->assertOk()
|
|
->assertJsonPath('leads_received.value', 3);
|
|
});
|
|
|
|
it('conversion = доля статуса won в окне', function () {
|
|
$tenant = Tenant::factory()->create();
|
|
$project = Project::factory()->create(['tenant_id' => $tenant->id]);
|
|
makeDashboardDeal($tenant, $project, 'won', now()->subDays(1));
|
|
makeDashboardDeal($tenant, $project, 'new', now()->subDays(1));
|
|
makeDashboardDeal($tenant, $project, 'new', now()->subDays(1));
|
|
makeDashboardDeal($tenant, $project, 'new', now()->subDays(1));
|
|
// 1 won из 4 → 25.0%; PHP json_encode кодирует 25.0 как 25 (без дроби)
|
|
$this->getJson("/api/dashboard/summary?tenant_id={$tenant->id}")
|
|
->assertOk()
|
|
->assertJsonPath('conversion.value', 25);
|
|
});
|
|
|
|
it('active_projects считает archived_at IS NULL AND is_active=true + limit из limits', function () {
|
|
$tenant = Tenant::factory()->create(['limits' => ['max_projects' => 10]]);
|
|
Project::factory()->create(['tenant_id' => $tenant->id, 'archived_at' => null, 'is_active' => true]);
|
|
Project::factory()->create(['tenant_id' => $tenant->id, 'archived_at' => null, 'is_active' => true]);
|
|
Project::factory()->create(['tenant_id' => $tenant->id, 'archived_at' => now(), 'is_active' => true]);
|
|
Project::factory()->create(['tenant_id' => $tenant->id, 'archived_at' => null, 'is_active' => false]);
|
|
$this->getJson("/api/dashboard/summary?tenant_id={$tenant->id}")
|
|
->assertOk()
|
|
->assertJsonPath('active_projects.active', 2)
|
|
->assertJsonPath('active_projects.limit', 10);
|
|
});
|
|
|
|
it('funnel группирует живые сделки по статусу', function () {
|
|
$tenant = Tenant::factory()->create();
|
|
$project = Project::factory()->create(['tenant_id' => $tenant->id]);
|
|
makeDashboardDeal($tenant, $project, 'new', now()->subDays(1));
|
|
makeDashboardDeal($tenant, $project, 'new', now()->subDays(1));
|
|
makeDashboardDeal($tenant, $project, 'won', now()->subDays(1));
|
|
$this->getJson("/api/dashboard/summary?tenant_id={$tenant->id}")
|
|
->assertOk()
|
|
->assertJsonPath('funnel.new', 2)
|
|
->assertJsonPath('funnel.won', 1);
|
|
});
|
|
|
|
it('activity возвращает 7 точек и 7 меток', function () {
|
|
$tenant = Tenant::factory()->create();
|
|
$this->getJson("/api/dashboard/summary?tenant_id={$tenant->id}")
|
|
->assertOk()
|
|
->assertJsonCount(7, 'activity.points')
|
|
->assertJsonCount(7, 'activity.labels');
|
|
});
|
|
|
|
it('runway_days использует фикс. 7д-окно независимо от range', function () {
|
|
// balance_leads = 70; 7 сделок за последние 7 дней → avgDaily=1 → runway=70.
|
|
// Баг: range=today → $curLeads=1 → avgDaily=1/7≈0.143 → runway≈490 (неверно).
|
|
$tenant = Tenant::factory()->create(['balance_leads' => 70]);
|
|
$project = Project::factory()->create(['tenant_id' => $tenant->id]);
|
|
for ($i = 0; $i <= 6; $i++) {
|
|
makeDashboardDeal($tenant, $project, 'new', now()->subDays($i));
|
|
}
|
|
$this->getJson("/api/dashboard/summary?tenant_id={$tenant->id}&range=today")
|
|
->assertOk()
|
|
->assertJsonPath('balance.runway_days', 70);
|
|
});
|