f78ab4af3d
Создавать проекты можно всегда; баланс проверяется в момент ЗАПУСКА (создать-и-запустить / запустить / возобновить одиночно и пачкой / автоподбор) под замком на клиента (без гонок). Не хватает — проект остаётся на паузе с меткой preflight_blocked_at, клиенту сообщение в рублях (сколько пополнить). Групповой запуск «сколько влезло». Нет активного тарифа на дату → запуск запрещён (fail-closed). Гейт реквизитов первого проекта добавлен и в автоподбор. - LaunchBalanceGate — единый гейт вместо 3 копий preflight (ProjectController store/update, AutopodborController), под DB::transaction + lockForUpdate(Tenant). - ProjectService::create($launch) + новый setActive(); bulk resume «сколько влезло». - AutopodborProjectCreator: пачка в транзакции через общий ProjectService::create. - Идемпотентность box/phone_type миграций автоподбора (Schema::hasColumn guard). - Тест-инфра: afterRefreshingDatabase восстанавливает месячные партиции. Тесты фичи 40/40 зелёные. Спека и план — docs/superpowers. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
60 lines
2.4 KiB
PHP
60 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Models\PricingTier;
|
|
use App\Models\Project;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use Tests\Concerns\SharesSupplierPdo;
|
|
|
|
uses(DatabaseTransactions::class);
|
|
uses(SharesSupplierPdo::class);
|
|
|
|
beforeEach(function () {
|
|
PricingTier::create([
|
|
'tier_no' => 1,
|
|
'leads_in_tier' => null,
|
|
'price_per_lead_kopecks' => 10000,
|
|
'is_active' => true,
|
|
'effective_from' => now()->toDateString(),
|
|
]);
|
|
});
|
|
|
|
it('rejects limit raise beyond balance with unified payload', function () {
|
|
$t = Tenant::factory()->create(['balance_rub' => '300.00', 'delivered_in_month' => 0]); // 3 лида
|
|
$u = User::factory()->for($t)->create();
|
|
$p = Project::factory()->for($t)->create(['is_active' => true, 'daily_limit_target' => 2, 'preflight_blocked_at' => null]);
|
|
|
|
$resp = $this->actingAs($u)->patchJson("/api/projects/{$p->id}", ['daily_limit_target' => 10]);
|
|
|
|
$resp->assertStatus(409);
|
|
$resp->assertJsonPath('error', 'balance_insufficient');
|
|
$resp->assertJsonPath('balance.topup_rub', '700.00'); // не хватает 7 лидов × 100 ₽
|
|
expect($p->fresh()->daily_limit_target)->toBe(2); // лимит НЕ изменился
|
|
});
|
|
|
|
it('allows limit raise when balance sufficient', function () {
|
|
$t = Tenant::factory()->create(['balance_rub' => '1000.00', 'delivered_in_month' => 0]); // 10 лидов
|
|
$u = User::factory()->for($t)->create();
|
|
$p = Project::factory()->for($t)->create(['is_active' => true, 'daily_limit_target' => 2, 'preflight_blocked_at' => null]);
|
|
|
|
$resp = $this->actingAs($u)->patchJson("/api/projects/{$p->id}", ['daily_limit_target' => 5]);
|
|
|
|
$resp->assertOk();
|
|
expect($p->fresh()->daily_limit_target)->toBe(5);
|
|
});
|
|
|
|
it('allows limit raise on paused project without gate check', function () {
|
|
// Паузированный проект: не активный, гейт не нужен
|
|
$t = Tenant::factory()->create(['balance_rub' => '0.00', 'delivered_in_month' => 0]);
|
|
$u = User::factory()->for($t)->create();
|
|
$p = Project::factory()->for($t)->create(['is_active' => false, 'daily_limit_target' => 2, 'preflight_blocked_at' => null]);
|
|
|
|
$resp = $this->actingAs($u)->patchJson("/api/projects/{$p->id}", ['daily_limit_target' => 10]);
|
|
|
|
$resp->assertOk();
|
|
expect($p->fresh()->daily_limit_target)->toBe(10);
|
|
});
|