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); });