From d8955f57e01d966486942f7c6c14b8bd1c0fe846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Mon, 25 May 2026 04:43:02 +0300 Subject: [PATCH] feat(billing-v2-c): one-time billing:preflight-initial-sweep MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Task 1.9 плана 2026-05-24-billing-v2-spec-c-preflight-vtb. Разовая artisan-команда для запуска при выкатке Spec C — прогоняет BalancePreflightSweepJob по всем тенантам, замораживает legacy- тенантов в минусе. Идемпотентна (sweep-job triggers только на active↔frozen переходах, стабильное состояние не трогает). TDD: 1 тест GREEN. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../BillingPreflightInitialSweepCommand.php | 37 ++++++++++++++++++ .../BillingPreflightInitialSweepTest.php | 38 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 app/app/Console/Commands/BillingPreflightInitialSweepCommand.php create mode 100644 app/tests/Feature/Billing/BillingPreflightInitialSweepTest.php diff --git a/app/app/Console/Commands/BillingPreflightInitialSweepCommand.php b/app/app/Console/Commands/BillingPreflightInitialSweepCommand.php new file mode 100644 index 00000000..59cb58a8 --- /dev/null +++ b/app/app/Console/Commands/BillingPreflightInitialSweepCommand.php @@ -0,0 +1,37 @@ +warn('Разовый преfflight всех тенантов. Запускать ОДИН раз после выкатки Spec C.'); + (new BalancePreflightSweepJob)->handle(); + $this->info('Initial sweep завершён.'); + + return self::SUCCESS; + } +} diff --git a/app/tests/Feature/Billing/BillingPreflightInitialSweepTest.php b/app/tests/Feature/Billing/BillingPreflightInitialSweepTest.php new file mode 100644 index 00000000..92b663e1 --- /dev/null +++ b/app/tests/Feature/Billing/BillingPreflightInitialSweepTest.php @@ -0,0 +1,38 @@ +create([ + 'tier_no' => 1, + 'leads_in_tier' => null, + 'price_per_lead_kopecks' => 5000, + 'is_active' => true, + 'effective_from' => now(), + ]); +}); + +it('freezes pre-existing underfunded tenant on first run', function () { + Mail::fake(); + // 0₽ + проекты на 25 лидов → должен быть заморожен. + $tenant = Tenant::factory()->create(['balance_rub' => '0.00', 'frozen_by_balance_at' => null]); + Project::factory()->for($tenant)->create(['is_active' => true, 'daily_limit_target' => 25]); + + $this->artisan('billing:preflight-initial-sweep')->assertSuccessful(); + + expect($tenant->fresh()->frozen_by_balance_at)->not->toBeNull(); + Mail::assertQueued(BalanceFrozenMail::class, fn ($mail) => $mail->tenant->id === $tenant->id); +});