b6b5b0bc1f
Полный сценарий: 1 webhook на B1_vashinvestor.ru → 3 deal-копии у 3 активных tenant'ов + счётчики + balance. Paused tenant пропущен. Orphan supplier_project создаёт stub, processed_at установлен, deals_created_count=0. Запуск через Laravel sync queue (default test env) — без Bus::fake(). Spec §5-§6 e2e validation. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
115 lines
3.7 KiB
PHP
115 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Models\Deal;
|
|
use App\Models\Project;
|
|
use App\Models\SupplierLead;
|
|
use App\Models\SupplierProject;
|
|
use App\Models\SystemSetting;
|
|
use App\Models\Tenant;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
uses(DatabaseTransactions::class);
|
|
|
|
beforeEach(function (): void {
|
|
SystemSetting::query()->where('key', 'supplier_webhook_secret')
|
|
->update(['value' => 'test-secret-32chars-aaaaaaaaaaaaaa']);
|
|
});
|
|
|
|
it('end-to-end: 1 webhook → 3 deal copies for 3 active tenants', function (): void {
|
|
$supplier = SupplierProject::factory()->create([
|
|
'platform' => 'B1',
|
|
'signal_type' => 'site',
|
|
'unique_key' => 'vashinvestor.ru',
|
|
]);
|
|
|
|
$tenants = collect();
|
|
$projects = collect();
|
|
for ($i = 0; $i < 3; $i++) {
|
|
$t = Tenant::factory()->create(['balance_leads' => 100]);
|
|
$tenants->push($t);
|
|
$projects->push(Project::factory()->create([
|
|
'tenant_id' => $t->id,
|
|
'supplier_b1_project_id' => $supplier->id,
|
|
'signal_type' => 'site',
|
|
'signal_identifier' => 'vashinvestor.ru',
|
|
'is_active' => true,
|
|
'delivered_today' => 0,
|
|
'delivered_in_month' => 0,
|
|
'delivery_days_mask' => 127,
|
|
'region_mask' => 255,
|
|
'region_mode' => 'include',
|
|
'daily_limit_target' => 10,
|
|
'effective_daily_limit_today' => null,
|
|
]));
|
|
}
|
|
|
|
// 4-й tenant — paused
|
|
$pausedTenant = Tenant::factory()->create(['balance_leads' => 100]);
|
|
Project::factory()->create([
|
|
'tenant_id' => $pausedTenant->id,
|
|
'supplier_b1_project_id' => $supplier->id,
|
|
'signal_type' => 'site',
|
|
'signal_identifier' => 'vashinvestor.ru',
|
|
'is_active' => false,
|
|
]);
|
|
|
|
$vid = 432176649;
|
|
$response = $this->postJson('/api/webhook/supplier/test-secret-32chars-aaaaaaaaaaaaaa', [
|
|
'vid' => $vid,
|
|
'project' => 'B1_vashinvestor.ru',
|
|
'tag' => 'Ваш инвестор',
|
|
'phone' => '79991234567',
|
|
'phones' => ['79991234567'],
|
|
'time' => time(),
|
|
]);
|
|
|
|
$response->assertStatus(202);
|
|
|
|
foreach ($projects as $i => $project) {
|
|
$tenant = $tenants[$i];
|
|
DB::statement("SET LOCAL app.current_tenant_id = '{$tenant->id}'");
|
|
|
|
$deals = Deal::query()
|
|
->where('tenant_id', $tenant->id)
|
|
->where('source_crm_id', $vid)
|
|
->get();
|
|
expect($deals)->toHaveCount(1, "tenant {$tenant->id} expected 1 deal copy");
|
|
|
|
$deal = $deals->first();
|
|
expect($deal->phone)->toBe('79991234567');
|
|
expect($deal->project_id)->toBe($project->id);
|
|
expect($deal->duplicate_of_id)->toBeNull();
|
|
|
|
expect($tenant->fresh()->balance_leads)->toBe(99);
|
|
expect($project->fresh()->delivered_today)->toBe(1);
|
|
expect($project->fresh()->delivered_in_month)->toBe(1);
|
|
}
|
|
|
|
DB::statement("SET LOCAL app.current_tenant_id = '{$pausedTenant->id}'");
|
|
expect(Deal::query()
|
|
->where('tenant_id', $pausedTenant->id)
|
|
->where('source_crm_id', $vid)
|
|
->count())->toBe(0);
|
|
});
|
|
|
|
it('end-to-end: lead orphan (no matching projects) — 0 deals, lead stored', function (): void {
|
|
$vid = 999_888_777;
|
|
|
|
$response = $this->postJson('/api/webhook/supplier/test-secret-32chars-aaaaaaaaaaaaaa', [
|
|
'vid' => $vid,
|
|
'project' => 'B1_orphan.ru',
|
|
'phone' => '79991234567',
|
|
'time' => time(),
|
|
]);
|
|
|
|
$response->assertStatus(202);
|
|
|
|
$lead = SupplierLead::where('vid', $vid)->firstOrFail();
|
|
expect($lead->processed_at)->not->toBeNull();
|
|
expect($lead->deals_created_count)->toBe(0);
|
|
expect($lead->supplier_project_id)->not->toBeNull(); // resolveOrStub stub
|
|
});
|