Files
portal/app/tests/Feature/Project/ProjectUpdateDedupTest.php
T
Дмитрий 1be2d62f9e feat(supplier): group recompute + pause + source-change + root auto-link
Закрывает замечания заказчика (22.05.2026) по проектам/поставщику. Все 4 куска
имеют общий корень: online-синхронизация одного проекта работала с данными ЭТОГО
проекта, а не пересчитывала всю «группу» (проекты разных tenant'ов с одним
identifier) — отсюда переплата ×3 при изменении лимита, затирание регионов/дней
группы, неотправленная пауза, и осиротевшие проекты при смене источника.

1. Групповой пересчёт в SyncSupplierProjectJob::handleOnline (#1 при изменении,
   #2 дни, #3 регионы, C2/C3): union regions, computeOrder eligible,
   distributeForPlatform — те же расчёты, что в ночном syncGroup. Online и
   ночной теперь дают идентичный supplier-state, расхождение устранено.

2. Пауза #10:
   - ProjectController::toggleActive — диспатчит SyncSupplierProjectJob;
   - ProjectService::bulkPauseResume — диспатчит sync per project;
   - DTO status вычисляется из groupActive (paused когда группа без активных);
   - sp.inactive_since пишется при пересинке (для UI/DTO консистентности).

3. Смена источника #8/#9 в ProjectService::update:
   - до update снимается старый buildUniqueKeyAgnostic;
   - если изменился — отвязываем старые supplier_projects от этого project
     (pivot + legacy FK), DeleteSupplierProjectJob удаляет их у поставщика
     при отсутствии других потребителей, либо пересинкает агрегат.

4. Перенос auto-link корня из feat/root-domain-auto-link: новый
   App\Support\SupplierIdentifier::extractRootDomain + блоки auto-link в
   обоих джобах (online + nightly).

Тесты: TDD на каждый кусок. SyncSupplierProjectJobTest +2 (group recompute,
pause). ProjectUpdateDedupTest +1 (source detach + cleanup dispatch).
ProjectsActionsTest +2 (toggle + bulk pause dispatches).

Регрессия: 186/186 passed (Project/Plan5/Projects + Supplier), 502 assertions.

Деплой: дельтой на боевой (база = root-domain ветка; на боевом джобы СТАРЕЕ
main, deliver через копию изменённых файлов + config:cache + restart queue).

План: docs/superpowers/plans/2026-05-22-замечания-проекты-чеклист.md

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-22 16:52:30 +03:00

91 lines
4.4 KiB
PHP

<?php
declare(strict_types=1);
use App\Jobs\Supplier\DeleteSupplierProjectJob;
use App\Jobs\SyncSupplierProjectJob;
use App\Models\SupplierProject;
use App\Models\Tenant;
use App\Services\Project\ProjectService;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;
uses(DatabaseTransactions::class);
beforeEach(fn () => Queue::fake());
it('blocks update that collides source with another project of same tenant', function () {
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
$svc = app(ProjectService::class);
$a = $svc->create($tenant, ['name' => 'A', 'signal_type' => 'call', 'signal_identifier' => '79991110000', 'daily_limit_target' => 5, 'regions' => [], 'delivery_days_mask' => 31]);
$b = $svc->create($tenant, ['name' => 'B', 'signal_type' => 'call', 'signal_identifier' => '79992220000', 'daily_limit_target' => 5, 'regions' => [], 'delivery_days_mask' => 31]);
expect(fn () => $svc->update($b, ['signal_identifier' => '79991110000']))
->toThrow(HttpResponseException::class);
});
it('allows update keeping same source on the same project', function () {
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
$svc = app(ProjectService::class);
$a = $svc->create($tenant, ['name' => 'A', 'signal_type' => 'call', 'signal_identifier' => '79991110000', 'daily_limit_target' => 5, 'regions' => [], 'delivery_days_mask' => 31]);
$updated = $svc->update($a, ['signal_identifier' => '79991110000', 'daily_limit_target' => 7]);
expect($updated->daily_limit_target)->toBe(7);
});
it('changing source detaches old supplier_projects and dispatches their cleanup (#8)', function () {
// #8/#9 regression: changing signal_identifier left the old supplier_projects orphan
// (still linked via pivot, still alive at the supplier) — that is the "two projects
// instead of one" the owner reported. Fix: detach old key's sps from this project +
// dispatch DeleteSupplierProjectJob (cleans portal only if no other consumer remains).
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
$svc = app(ProjectService::class);
$p = $svc->create($tenant, [
'name' => 'X', 'signal_type' => 'call', 'signal_identifier' => '79991110000',
'daily_limit_target' => 5, 'regions' => [], 'delivery_days_mask' => 31,
]);
// Simulate the old source already synced — 3 sps linked via pivot + legacy FKs.
$oldIds = [];
foreach (['B1' => 'OLD1', 'B2' => 'OLD2', 'B3' => 'OLD3'] as $platform => $ext) {
$sp = SupplierProject::create([
'platform' => $platform, 'signal_type' => 'call', 'unique_key' => '79991110000',
'subject_code' => null, 'supplier_external_id' => $ext, 'current_limit' => 2,
'current_workdays' => [1, 2, 3, 4, 5], 'current_regions' => [], 'sync_status' => 'ok',
'last_synced_at' => now(),
]);
DB::table('project_supplier_links')->insert([
'project_id' => $p->id, 'supplier_project_id' => $sp->id, 'platform' => $platform, 'subject_code' => null,
]);
$oldIds[] = $sp->id;
$col = 'supplier_'.strtolower($platform).'_project_id';
$p->{$col} = $sp->id;
}
$p->save();
// Change source — should detach old sps, dispatch their cleanup, and dispatch the new-source sync.
$svc->update($p, ['signal_identifier' => '79993330000']);
// Old sps no longer linked to this project via pivot.
$stillLinked = DB::table('project_supplier_links')
->where('project_id', $p->id)
->whereIn('supplier_project_id', $oldIds)
->count();
expect($stillLinked)->toBe(0);
// Legacy FK columns pointing at old sps are cleared.
$fresh = $p->fresh();
expect($fresh->supplier_b1_project_id)->toBeNull();
expect($fresh->supplier_b2_project_id)->toBeNull();
expect($fresh->supplier_b3_project_id)->toBeNull();
// Cleanup of OLD sps dispatched (delete-or-resync per remaining-consumers rule).
Queue::assertPushed(DeleteSupplierProjectJob::class, function ($job) use ($oldIds) {
return ! array_diff($oldIds, $job->supplierProjectIds);
});
// The new source still gets a sync dispatch (existing needsResync path).
Queue::assertPushed(SyncSupplierProjectJob::class, fn ($job) => $job->projectId === $p->id);
});