feat(models): SupplierLead model + factory (raw-payload incoming webhooks)

SaaS-level модель для supplier_leads (Plan 2/5 Task 2).
belongsTo(SupplierProject) + array cast на raw_payload + datetime *_at.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Дмитрий
2026-05-10 18:24:45 +03:00
parent f5c7c29301
commit aa37f4cbed
3 changed files with 163 additions and 0 deletions
+66
View File
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace App\Models;
use Database\Factories\SupplierLeadFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* Raw-payload входящих webhook'ов от поставщика crm.bp-gr.ru.
*
* SaaS-level таблица (без RLS) на момент INSERT'а tenant ещё не определён;
* routing к Лидерра-проектам происходит в RouteSupplierLeadJob через
* supplier_project_id + LeadRouter.
*
* Spec: docs/superpowers/specs/2026-05-10-supplier-integration-design.md §5.1
*
* @mixin IdeHelperSupplierLead
*/
class SupplierLead extends Model
{
/** @use HasFactory<SupplierLeadFactory> */
use HasFactory;
protected $table = 'supplier_leads';
public $timestamps = false;
protected $fillable = [
'supplier_project_id',
'platform',
'raw_payload',
'vid',
'phone',
'received_at',
'source',
'processed_at',
'deals_created_count',
'error',
];
protected function casts(): array
{
return [
'raw_payload' => 'array',
'received_at' => 'datetime',
'processed_at' => 'datetime',
'vid' => 'integer',
'deals_created_count' => 'integer',
];
}
/** @return BelongsTo<SupplierProject, $this> */
public function supplierProject(): BelongsTo
{
return $this->belongsTo(SupplierProject::class);
}
protected static function newFactory(): SupplierLeadFactory
{
return SupplierLeadFactory::new();
}
}
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Database\Factories;
use App\Models\SupplierLead;
use App\Models\SupplierProject;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<SupplierLead>
*/
class SupplierLeadFactory extends Factory
{
protected $model = SupplierLead::class;
public function definition(): array
{
$platform = $this->faker->randomElement(['B1', 'B2', 'B3']);
$vid = $this->faker->unique()->numberBetween(100_000_000, 999_999_999);
$phone = '7'.$this->faker->numerify('##########');
return [
'supplier_project_id' => SupplierProject::factory(),
'platform' => $platform,
'raw_payload' => [
'vid' => $vid,
'project' => $platform.'_test.example.com',
'tag' => 'test-tag',
'phone' => $phone,
'phones' => [$phone],
'time' => now()->getTimestamp(),
],
'vid' => $vid,
'phone' => $phone,
'received_at' => now(),
'source' => 'webhook',
'processed_at' => null,
'deals_created_count' => null,
'error' => null,
];
}
}
@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
use App\Models\SupplierLead;
use App\Models\SupplierProject;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Carbon;
uses(DatabaseTransactions::class);
it('factory creates a valid SupplierLead', function (): void {
$supplierProject = SupplierProject::factory()->create([
'platform' => 'B1',
'signal_type' => 'site',
'unique_key' => 'vashinvestor.ru',
]);
$lead = SupplierLead::factory()->create([
'supplier_project_id' => $supplierProject->id,
'platform' => 'B1',
]);
expect($lead->id)->toBeInt()->toBeGreaterThan(0);
expect($lead->platform)->toBe('B1');
expect($lead->raw_payload)->toBeArray();
expect($lead->received_at)->toBeInstanceOf(Carbon::class);
});
it('belongsTo supplier_project', function (): void {
$supplierProject = SupplierProject::factory()->create();
$lead = SupplierLead::factory()->create([
'supplier_project_id' => $supplierProject->id,
]);
expect($lead->supplierProject->id)->toBe($supplierProject->id);
});
it('casts raw_payload to array and *_at to Carbon', function (): void {
$lead = SupplierLead::factory()->create([
'raw_payload' => ['vid' => 12345, 'project' => 'B1_test.ru'],
'processed_at' => now(),
]);
expect($lead->raw_payload)->toBe(['vid' => 12345, 'project' => 'B1_test.ru']);
expect($lead->processed_at)->toBeInstanceOf(Carbon::class);
});
it('factory respects deals_created_count nullable default', function (): void {
$lead = SupplierLead::factory()->create();
expect($lead->deals_created_count)->toBeNull();
expect($lead->processed_at)->toBeNull();
});