e401491947
Task 4 — integration:
- handle() / createDealCopyForProject() — +5-й параметр LedgerService.
- Заменён старый balance_leads-- + BalanceTransaction блок на
\$ledger->chargeForDelivery(\$tenant, \$deal, \$lead) с try/catch для
InsufficientBalanceException (Log::warning + rethrow; auto-pause flow
в Task 6).
- LeadRouter::matchEligibleProjects — расширен фильтр tenant balance с
(balance_leads > 0) на (balance_leads > 0 OR balance_rub > 0), чтобы
rub-only tenant дошёл до LedgerService (single arbiter for dual-balance).
- 4 E2E теста в tests/Feature/Supplier/RouteSupplierLeadJobBillingTest.php:
prepaid charge + BalanceTransaction (carry-over M-2), rub charge + BT,
supplier_lead_costs gap-fix (2 deal-копии), retry idempotency.
Plan 4 Task 3 carry-overs (минорные правки по code-review d2030f9):
- I-2: PHPDoc на LedgerService::chargeForDelivery — @throws + @precondition
(caller wraps в DB::transaction с lockForUpdate Tenant).
- I-4: trim() на raw_payload['project'] в resolveSupplierId (defense
against whitespace).
Прочие правки:
- tests/Feature/Jobs/RouteSupplierLeadJobTest.php — +PricingTierSeeder
в beforeEach + +5-й LedgerService параметр в runRouteJob().
- tests/Feature/Integration/SupplierLeadFlowTest.php — +PricingTierSeeder
в beforeEach (test использует full webhook→job pipeline).
- tests/Feature/Services/LeadRouterTest.php — rename теста про balance_leads
→ \"zero in BOTH balance_leads AND balance_rub\" + новый тест
\"rub-only tenant ДОЛЖЕН пройти\".
- phpstan-baseline.neon — +5 entries для TestCall::seed() + Tenant/LeadCharge
property.notFound в новых файлах (IDE helper @mixin re-generation —
отдельная задача).
Метрики: Pint clean, PHPStan 0 errors, Pest 646/643+3 skipped/0 failed
(21.1s parallel). Plan 4 Task 4 закрыт.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
164 lines
6.0 KiB
PHP
164 lines
6.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Jobs\RouteSupplierLeadJob;
|
|
use App\Models\BalanceTransaction;
|
|
use App\Models\Deal;
|
|
use App\Models\LeadCharge;
|
|
use App\Models\Project;
|
|
use App\Models\Supplier;
|
|
use App\Models\SupplierLead;
|
|
use App\Models\SupplierProject;
|
|
use App\Models\Tenant;
|
|
use App\Services\Billing\LedgerService;
|
|
use App\Services\DuplicateDetector;
|
|
use App\Services\LeadRouter;
|
|
use App\Services\NotificationService;
|
|
use App\Services\SupplierProjects\SupplierProjectResolver;
|
|
use Database\Seeders\PricingTierSeeder;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Tests\Concerns\SharesSupplierPdo;
|
|
|
|
uses(DatabaseTransactions::class);
|
|
uses(SharesSupplierPdo::class);
|
|
|
|
beforeEach(function (): void {
|
|
$this->seed(PricingTierSeeder::class);
|
|
DB::statement("SELECT set_config('app.current_tenant_id', '0', true)");
|
|
});
|
|
|
|
/**
|
|
* Подготовка sharing-flow: N тенантов с указанными балансами, каждый —
|
|
* со своим Project, привязанным к одному supplierProject (платформа B1, site).
|
|
*
|
|
* @param array<int, array<string, mixed>> $balances
|
|
* @return array{tenants: array<int, Tenant>, projects: array<int, Project>, lead: SupplierLead, supplier: Supplier}
|
|
*/
|
|
function prepareSharingFlow(int $tenantsCount, array $balances): array
|
|
{
|
|
/** @var array<int, Tenant> $tenants */
|
|
$tenants = [];
|
|
/** @var array<int, Project> $projects */
|
|
$projects = [];
|
|
|
|
$supplier = Supplier::where('code', 'b1')->first();
|
|
$supplierProject = SupplierProject::factory()->create([
|
|
'platform' => 'B1',
|
|
'signal_type' => 'site',
|
|
'unique_key' => 'example.com',
|
|
// NB: supplier_projects has NO supplier_id column; LedgerService resolves
|
|
// supplier via platform → suppliers.code mapping.
|
|
]);
|
|
|
|
for ($i = 0; $i < $tenantsCount; $i++) {
|
|
$tenant = Tenant::factory()->create($balances[$i]);
|
|
$project = Project::factory()->create([
|
|
'tenant_id' => $tenant->id,
|
|
'signal_type' => 'site',
|
|
'signal_identifier' => 'example.com',
|
|
'supplier_b1_project_id' => $supplierProject->id,
|
|
'is_active' => true,
|
|
'daily_limit_target' => 10,
|
|
'effective_daily_limit_today' => 10,
|
|
'delivered_today' => 0,
|
|
'delivery_days_mask' => 127,
|
|
'region_mask' => 255,
|
|
]);
|
|
$tenants[] = $tenant;
|
|
$projects[] = $project;
|
|
}
|
|
|
|
$vid = random_int(100_000_000, 999_999_999);
|
|
$lead = SupplierLead::factory()->create([
|
|
'vid' => $vid,
|
|
'phone' => '79991234567',
|
|
'raw_payload' => ['vid' => $vid, 'project' => 'B1_example.com', 'phone' => '79991234567', 'time' => time()],
|
|
'supplier_project_id' => $supplierProject->id,
|
|
'received_at' => now(),
|
|
]);
|
|
|
|
return ['tenants' => $tenants, 'projects' => $projects, 'lead' => $lead, 'supplier' => $supplier];
|
|
}
|
|
|
|
function dispatchJob(int $supplierLeadId): void
|
|
{
|
|
(new RouteSupplierLeadJob($supplierLeadId))->handle(
|
|
app(LeadRouter::class),
|
|
app(SupplierProjectResolver::class),
|
|
app(DuplicateDetector::class),
|
|
app(NotificationService::class),
|
|
app(LedgerService::class),
|
|
);
|
|
}
|
|
|
|
it('charges prepaid for tenant with balance_leads > 0 + writes BalanceTransaction', function (): void {
|
|
$ctx = prepareSharingFlow(1, [['balance_leads' => 5, 'balance_rub' => '0.00', 'delivered_in_month' => 0]]);
|
|
|
|
dispatchJob($ctx['lead']->id);
|
|
|
|
$tenant = $ctx['tenants'][0]->fresh();
|
|
expect((int) $tenant->balance_leads)->toBe(4);
|
|
expect($tenant->delivered_in_month)->toBe(1);
|
|
|
|
$charge = LeadCharge::first();
|
|
expect($charge)->not->toBeNull();
|
|
expect($charge->charge_source)->toBe('prepaid');
|
|
expect($charge->price_per_lead_kopecks)->toBe(0);
|
|
|
|
// BalanceTransaction (carry-over M-2 assertion)
|
|
$tx = BalanceTransaction::where('type', BalanceTransaction::TYPE_LEAD_CHARGE)->first();
|
|
expect($tx)->not->toBeNull();
|
|
expect((int) $tx->amount_leads)->toBe(-1);
|
|
expect((int) $tx->balance_leads_after)->toBe(4);
|
|
});
|
|
|
|
it('charges rub for tenant with balance_leads=0 and balance_rub >= price + writes BalanceTransaction', function (): void {
|
|
$ctx = prepareSharingFlow(1, [['balance_leads' => 0, 'balance_rub' => '1000.00', 'delivered_in_month' => 0]]);
|
|
|
|
dispatchJob($ctx['lead']->id);
|
|
|
|
$tenant = $ctx['tenants'][0]->fresh();
|
|
expect((string) $tenant->balance_rub)->toBe('500.00');
|
|
expect($tenant->delivered_in_month)->toBe(1);
|
|
|
|
$charge = LeadCharge::first();
|
|
expect($charge->charge_source)->toBe('rub');
|
|
expect($charge->price_per_lead_kopecks)->toBe(50000);
|
|
|
|
// BalanceTransaction (carry-over M-2 assertion)
|
|
$tx = BalanceTransaction::where('type', BalanceTransaction::TYPE_LEAD_CHARGE)->first();
|
|
expect($tx)->not->toBeNull();
|
|
expect((string) $tx->amount_rub)->toBe('-500.00');
|
|
expect((string) $tx->balance_rub_after)->toBe('500.00');
|
|
});
|
|
|
|
it('writes supplier_lead_costs for each delivered deal copy (gap-fix)', function (): void {
|
|
$ctx = prepareSharingFlow(2, [
|
|
['balance_leads' => 5, 'balance_rub' => '0.00', 'delivered_in_month' => 0],
|
|
['balance_leads' => 0, 'balance_rub' => '1000.00', 'delivered_in_month' => 0],
|
|
]);
|
|
|
|
dispatchJob($ctx['lead']->id);
|
|
|
|
$costs = DB::table('supplier_lead_costs')->get();
|
|
expect($costs)->toHaveCount(2);
|
|
foreach ($costs as $cost) {
|
|
expect((int) $cost->supplier_id)->toBe($ctx['supplier']->id);
|
|
expect((string) $cost->cost_rub)->toBe($ctx['supplier']->cost_rub);
|
|
}
|
|
});
|
|
|
|
it('retry idempotency: повторный run не дублирует lead_charges', function (): void {
|
|
$ctx = prepareSharingFlow(1, [['balance_leads' => 5, 'balance_rub' => '0.00']]);
|
|
$leadId = $ctx['lead']->id;
|
|
|
|
dispatchJob($leadId);
|
|
dispatchJob($leadId); // повторный — processed_at guard защищает
|
|
|
|
expect(LeadCharge::count())->toBe(1);
|
|
expect(Deal::count())->toBe(1);
|
|
expect((int) $ctx['tenants'][0]->fresh()->balance_leads)->toBe(4);
|
|
});
|