2026-05-22 16:45:12 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2026-05-22 16:09:37 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 152-ФЗ: pd_processing_log 'created' записывается при создании сделки
|
|
|
|
|
* через исторический импорт (HistoricalImportService).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
use App\Models\ImportLog;
|
|
|
|
|
use App\Models\Tenant;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use App\Services\Import\CsvLeadsParser;
|
|
|
|
|
use App\Services\Import\HistoricalImportService;
|
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2026-05-23 20:20:54 +03:00
|
|
|
use Tests\Concerns\SharesSupplierPdo;
|
2026-05-22 16:09:37 +03:00
|
|
|
|
2026-05-23 20:20:54 +03:00
|
|
|
uses(DatabaseTransactions::class, SharesSupplierPdo::class);
|
|
|
|
|
// HistoricalImportService → MonthlyPartitionManager → CREATE через pgsql_supplier
|
|
|
|
|
// (см. MonthlyPartitionManager::DDL_CONNECTION). Нужен shared PDO.
|
2026-05-22 16:09:37 +03:00
|
|
|
|
|
|
|
|
it('writes pd_processing_log created on historical import for each new deal', function () {
|
|
|
|
|
$tenant = Tenant::factory()->create();
|
2026-05-22 16:45:12 +03:00
|
|
|
$user = User::factory()->for($tenant)->create();
|
2026-05-22 16:09:37 +03:00
|
|
|
DB::statement('SET app.current_tenant_id = '.$tenant->id);
|
|
|
|
|
|
|
|
|
|
$log = ImportLog::create([
|
|
|
|
|
'tenant_id' => $tenant->id,
|
2026-05-22 16:45:12 +03:00
|
|
|
'user_id' => $user->id,
|
|
|
|
|
'filename' => 'leads.csv',
|
2026-05-22 16:09:37 +03:00
|
|
|
'file_path' => 'imports/x.csv',
|
2026-05-22 16:45:12 +03:00
|
|
|
'dry_run' => false,
|
2026-05-22 16:09:37 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$header = "\xEF\xBB\xBF".'id,Проект,Тег проекта,Телефон,Создано,Напоминание,Комментарий,Состояние,Имя';
|
|
|
|
|
$rows = array_merge(
|
|
|
|
|
(new CsvLeadsParser)->parse($header."\n".'9901,Окна,окна,79161000001,2023/07/10 10:00:00,,,Новые,')->rows,
|
|
|
|
|
(new CsvLeadsParser)->parse($header."\n".'9902,Окна,окна,79161000002,2023/07/10 10:00:00,,,Новые,')->rows,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
app(HistoricalImportService::class)->import($tenant->id, $user->id, $log, $rows);
|
|
|
|
|
|
|
|
|
|
$pd = DB::table('pd_processing_log')
|
|
|
|
|
->where('action', 'created')
|
|
|
|
|
->where('purpose', 'lead_create_import_'.$log->id)
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
expect($pd)->toHaveCount(2);
|
|
|
|
|
|
|
|
|
|
foreach ($pd as $r) {
|
|
|
|
|
expect($r->subject_type)->toBe('lead')
|
|
|
|
|
->and((int) $r->actor_tenant_user_id)->toBe($user->id)
|
|
|
|
|
->and($r->actor_admin_user_id)->toBeNull()
|
|
|
|
|
->and($r->subject_id)->not->toBeNull()
|
|
|
|
|
->and((int) $r->tenant_id)->toBe($tenant->id);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('does NOT write pd_processing_log on dry_run import', function () {
|
|
|
|
|
$tenant = Tenant::factory()->create();
|
2026-05-22 16:45:12 +03:00
|
|
|
$user = User::factory()->for($tenant)->create();
|
2026-05-22 16:09:37 +03:00
|
|
|
DB::statement('SET app.current_tenant_id = '.$tenant->id);
|
|
|
|
|
|
|
|
|
|
$log = ImportLog::create([
|
|
|
|
|
'tenant_id' => $tenant->id,
|
2026-05-22 16:45:12 +03:00
|
|
|
'user_id' => $user->id,
|
|
|
|
|
'filename' => 'leads.csv',
|
2026-05-22 16:09:37 +03:00
|
|
|
'file_path' => 'imports/x.csv',
|
2026-05-22 16:45:12 +03:00
|
|
|
'dry_run' => true,
|
2026-05-22 16:09:37 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$header = "\xEF\xBB\xBF".'id,Проект,Тег проекта,Телефон,Создано,Напоминание,Комментарий,Состояние,Имя';
|
|
|
|
|
$rows = (new CsvLeadsParser)->parse($header."\n".'9903,Окна,окна,79161000003,2023/07/10 10:00:00,,,Новые,')->rows;
|
|
|
|
|
|
|
|
|
|
app(HistoricalImportService::class)->import($tenant->id, $user->id, $log, $rows);
|
|
|
|
|
|
|
|
|
|
$count = DB::table('pd_processing_log')
|
|
|
|
|
->where('purpose', 'lead_create_import_'.$log->id)
|
|
|
|
|
->count();
|
|
|
|
|
|
|
|
|
|
expect($count)->toBe(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('does NOT write pd_processing_log on import UPDATE (idempotent re-import)', function () {
|
|
|
|
|
$tenant = Tenant::factory()->create();
|
2026-05-22 16:45:12 +03:00
|
|
|
$user = User::factory()->for($tenant)->create();
|
2026-05-22 16:09:37 +03:00
|
|
|
DB::statement('SET app.current_tenant_id = '.$tenant->id);
|
|
|
|
|
|
|
|
|
|
$header = "\xEF\xBB\xBF".'id,Проект,Тег проекта,Телефон,Создано,Напоминание,Комментарий,Состояние,Имя';
|
|
|
|
|
|
|
|
|
|
// First import — creates the deal
|
|
|
|
|
$log1 = ImportLog::create([
|
|
|
|
|
'tenant_id' => $tenant->id,
|
2026-05-22 16:45:12 +03:00
|
|
|
'user_id' => $user->id,
|
|
|
|
|
'filename' => 'leads.csv',
|
2026-05-22 16:09:37 +03:00
|
|
|
'file_path' => 'imports/x.csv',
|
2026-05-22 16:45:12 +03:00
|
|
|
'dry_run' => false,
|
2026-05-22 16:09:37 +03:00
|
|
|
]);
|
|
|
|
|
$rows1 = (new CsvLeadsParser)->parse($header."\n".'9904,Окна,окна,79161000004,2023/07/10 10:00:00,,,Новые,')->rows;
|
|
|
|
|
app(HistoricalImportService::class)->import($tenant->id, $user->id, $log1, $rows1);
|
|
|
|
|
|
|
|
|
|
// Second import — updates the same deal
|
|
|
|
|
$log2 = ImportLog::create([
|
|
|
|
|
'tenant_id' => $tenant->id,
|
2026-05-22 16:45:12 +03:00
|
|
|
'user_id' => $user->id,
|
|
|
|
|
'filename' => 'leads2.csv',
|
2026-05-22 16:09:37 +03:00
|
|
|
'file_path' => 'imports/x2.csv',
|
2026-05-22 16:45:12 +03:00
|
|
|
'dry_run' => false,
|
2026-05-22 16:09:37 +03:00
|
|
|
]);
|
|
|
|
|
$rows2 = (new CsvLeadsParser)->parse($header."\n".'9904,Окна,окна,79161000004,2023/07/10 10:00:00,,,Оплачено,')->rows;
|
|
|
|
|
app(HistoricalImportService::class)->import($tenant->id, $user->id, $log2, $rows2);
|
|
|
|
|
|
|
|
|
|
// Only the first import wrote a pd log entry
|
|
|
|
|
$countLog1 = DB::table('pd_processing_log')
|
|
|
|
|
->where('action', 'created')
|
|
|
|
|
->where('purpose', 'lead_create_import_'.$log1->id)
|
|
|
|
|
->count();
|
|
|
|
|
$countLog2 = DB::table('pd_processing_log')
|
|
|
|
|
->where('action', 'created')
|
|
|
|
|
->where('purpose', 'lead_create_import_'.$log2->id)
|
|
|
|
|
->count();
|
|
|
|
|
|
|
|
|
|
expect($countLog1)->toBe(1)
|
|
|
|
|
->and($countLog2)->toBe(0);
|
|
|
|
|
});
|