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>
67 lines
2.5 KiB
PHP
67 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Jobs\SyncSupplierProjectJob;
|
|
use App\Models\PricingTier;
|
|
use App\Models\Tenant;
|
|
use App\Services\Project\ProjectService;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use Illuminate\Support\Facades\Queue;
|
|
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('creates paused and blocked, no supplier sync, when balance insufficient', function () {
|
|
Queue::fake();
|
|
$t = Tenant::factory()->create(['balance_rub' => '100.00', 'delivered_in_month' => 0]); // 1 лид ёмкость
|
|
$p = app(ProjectService::class)->create($t, [
|
|
'name' => 'X', 'signal_type' => 'site', 'signal_identifier' => 'x.ru',
|
|
'daily_limit_target' => 5, 'delivery_days_mask' => 127,
|
|
], launch: true);
|
|
|
|
expect($p->is_active)->toBeFalse();
|
|
expect($p->preflight_blocked_at)->not->toBeNull();
|
|
expect($p->launch_deferred)->toBeTrue();
|
|
expect($p->gate_payload['deficit_leads'])->toBe(4);
|
|
Queue::assertNotPushed(SyncSupplierProjectJob::class);
|
|
});
|
|
|
|
it('creates active and syncs when balance sufficient', function () {
|
|
Queue::fake();
|
|
$t = Tenant::factory()->create(['balance_rub' => '1000.00', 'delivered_in_month' => 0]);
|
|
$p = app(ProjectService::class)->create($t, [
|
|
'name' => 'Y', 'signal_type' => 'site', 'signal_identifier' => 'y.ru',
|
|
'daily_limit_target' => 5, 'delivery_days_mask' => 127,
|
|
], launch: true);
|
|
|
|
expect($p->is_active)->toBeTrue();
|
|
expect($p->launch_deferred)->toBeFalse();
|
|
Queue::assertPushed(SyncSupplierProjectJob::class);
|
|
});
|
|
|
|
it('creates draft paused without gate when launch=false', function () {
|
|
Queue::fake();
|
|
$t = Tenant::factory()->create(['balance_rub' => '0.00', 'delivered_in_month' => 0]);
|
|
$p = app(ProjectService::class)->create($t, [
|
|
'name' => 'Z', 'signal_type' => 'site', 'signal_identifier' => 'z.ru',
|
|
'daily_limit_target' => 5, 'delivery_days_mask' => 127,
|
|
], launch: false);
|
|
|
|
expect($p->is_active)->toBeFalse();
|
|
expect($p->preflight_blocked_at)->toBeNull(); // черновик, не «удержан балансом»
|
|
expect($p->launch_deferred)->toBeFalse();
|
|
Queue::assertNotPushed(SyncSupplierProjectJob::class);
|
|
});
|