From 5347ac551856c0a83ba50f6f397fbd2246ab51da Mon Sep 17 00:00:00 2001 From: E98Developer Date: Fri, 18 Sep 2026 17:56:50 +0200 Subject: [PATCH] =?UTF-8?q?ADD=20EV3-473=20producers:alias-backfill=20-=20?= =?UTF-8?q?visszamen=C5=91leges=20alias-felt=C3=B6lt=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Két forrásból dolgozik: az archivált gyártók note mezőjéből (láncolást is követve, ha a célrekord maga is tovább olvadt), illetve a producers:dedupe --report JSON jelentéseiből, névalapú feloldással - a jelentésekben szereplő ID-k környezetenként eltérnek. Ütközés esetén kihagyja és jelenti a jelöltet, kétszeri futtatás nem hoz létre duplikált aliast. --- .../Commands/ProducersAliasBackfill.php | 269 ++++++++++++++++++ tests/Feature/ProducerAliasBackfillTest.php | 161 +++++++++++ 2 files changed, 430 insertions(+) create mode 100644 app/Console/Commands/ProducersAliasBackfill.php create mode 100644 tests/Feature/ProducerAliasBackfillTest.php diff --git a/app/Console/Commands/ProducersAliasBackfill.php b/app/Console/Commands/ProducersAliasBackfill.php new file mode 100644 index 0000000..40537c8 --- /dev/null +++ b/app/Console/Commands/ProducersAliasBackfill.php @@ -0,0 +1,269 @@ + (...)") - ez MINDIG + * a jelenlegi adatbázis saját, konzisztens ID-ire mutat, láncolást igényelhet + * (egy 1. körös célrekord a 2. körben maga is beolvadhatott). + * 2. A producers:dedupe --report kimeneteként keletkezett JSON jelentések (a döntési + * lapok, EV3-357 ticket-mappa). FONTOS: ezekben az ID-k környezetenként (fejlesztői/ + * d2d/éles) eltérnek, ezért a feloldás NÉVALAPÚ - a jelentés `final_name` mezőjét + * a jelenlegi adatbázis aktív gyártói között normalizált egyezéssel keressük meg. + */ +class ProducersAliasBackfill extends Command +{ + protected $signature = 'producers:alias-backfill + {reports?* : dedupe-jelentés JSON fájl(ok) útvonala (producers:dedupe --report kimenete)} + {--skip-archives : az archivált gyártók note mezejének feldolgozásának kihagyása} + {--apply : ténylegesen beszúrja az aliasokat (alapértelmezés: csak kimutatás)}'; + + protected $description = 'Alias rekordok visszamenőleges feltöltése a korábbi gyártó-összevonásokból'; + + public function handle(ProducerAliasResolver $resolver): int + { + $candidates = []; + + if (! $this->option('skip-archives')) { + $candidates = array_merge($candidates, $this->collectFromArchivedProducers()); + } + + foreach ($this->argument('reports') as $path) { + $candidates = array_merge($candidates, $this->collectFromReport($path)); + } + + if ($candidates === []) { + $this->info('Nincs feldolgozandó jelölt.'); + + return self::SUCCESS; + } + + [$toInsert, $alreadyExists, $conflicts] = $this->classify($resolver, $candidates); + + $this->summarize($toInsert, $alreadyExists, $conflicts); + + if (! $this->option('apply')) { + $this->newLine(); + $this->info('Ez csak kimutatás volt. A végrehajtáshoz: --apply'); + + return self::SUCCESS; + } + + DB::transaction(function () use ($toInsert) { + foreach ($toInsert as $row) { + ProducerAlias::create([ + 'producer_id' => $row['producer_id'], + 'name' => $row['name'], + 'normalized_name' => NameNormalizer::normalize($row['name']), + 'source' => 'backfill', + ]); + } + }); + + $this->newLine(); + $this->info(count($toInsert) . ' alias létrehozva.'); + + return self::SUCCESS; + } + + /** + * @return array + */ + private function collectFromArchivedProducers(): array + { + $candidates = []; + + $archived = Producer::query()->where('status', DbStatusFieldEnum::archive)->get(['id', 'name', 'note']); + + foreach ($archived as $producer) { + $visited = []; + $keeperId = $this->resolveActiveKeeperId($producer->id, $visited); + + if ($keeperId === null) { + $this->warn(" ! #{$producer->id} (\"{$producer->name}\") note-ja nem oldható fel aktív gyártóra."); + + continue; + } + + $candidates[] = [ + 'producer_id' => $keeperId, + 'name' => (string) $producer->name, + 'origin' => "archív #{$producer->id}", + ]; + } + + return $candidates; + } + + /** + * Egy archivált rekord note-jából kiolvassa a célrekordot, és ha az maga is + * archivált (2. körben tovább olvadt), a láncot az aktuális aktív rekordig követi. + */ + private function resolveActiveKeeperId(int $id, array &$visited): ?int + { + if (isset($visited[$id])) { + return null; // ciklusvédelem + } + $visited[$id] = true; + + $producer = Producer::find($id); + + if (! $producer) { + return null; + } + + if (! in_array($producer->status, [DbStatusFieldEnum::archive, DbStatusFieldEnum::deleted], true)) { + return $producer->id; + } + + if (! preg_match('/Összevonva ide:\s*#(\d+)/u', (string) $producer->note, $matches)) { + return null; + } + + return $this->resolveActiveKeeperId((int) $matches[1], $visited); + } + + /** + * @return array + */ + private function collectFromReport(string $path): array + { + if (! is_file($path)) { + $this->error("A jelentésfájl nem található: {$path}"); + + return []; + } + + $report = json_decode((string) file_get_contents($path), true); + + if (! is_array($report) || ! isset($report['merges'])) { + $this->error("A fájl nem érvényes producers:dedupe jelentés: {$path}"); + + return []; + } + + $activeByNormalized = $this->activeProducersByNormalizedName(); + $restoreProducers = $report['restore']['producers'] ?? []; + $candidates = []; + + foreach ($report['merges'] as $merge) { + $finalName = (string) ($merge['final_name'] ?? ''); + $targetId = $activeByNormalized[NameNormalizer::normalize($finalName)] ?? null; + + if ($targetId === null) { + $this->warn(" ! \"{$finalName}\" ({$path}) nem található aktív gyártóként ebben a környezetben."); + + continue; + } + + $names = array_column($merge['from'] ?? [], 'name'); + + // A jelentésben a keeper eredeti (átnevezés előtti) neve csak a + // restore.producers pillanatképben maradt meg - a merges tömb nem + // tartalmazza. + $keeperOriginalName = $restoreProducers[$merge['keeper_id']]['name'] ?? null; + if ($keeperOriginalName !== null) { + $names[] = (string) $keeperOriginalName; + } + + foreach (array_unique($names) as $name) { + if (NameNormalizer::normalize($name) === NameNormalizer::normalize($finalName)) { + continue; // ugyanaz, mint az élő név - felesleges alias lenne + } + + $candidates[] = [ + 'producer_id' => $targetId, + 'name' => $name, + 'origin' => basename($path), + ]; + } + } + + return $candidates; + } + + private function activeProducersByNormalizedName(): array + { + return Producer::query() + ->whereNotIn('status', [DbStatusFieldEnum::archive, DbStatusFieldEnum::deleted]) + ->get(['id', 'name']) + ->keyBy(fn ($p) => NameNormalizer::normalize($p->name)) + ->map->id + ->toArray(); + } + + /** + * Szétválogatja a jelölteket: mit kell beszúrni, mi van már fent (idempotencia - + * a parancs többször is lefuttatható ugyanazokkal a bemenetekkel), és mi ütközik. + * + * @return array{0: array, 1: array, 2: array} + */ + private function classify(ProducerAliasResolver $resolver, array $candidates): array + { + $toInsert = []; + $alreadyExists = []; + $conflicts = []; + $seen = []; // normalized_name => true, hogy egy futáson belül ne duplázzunk + + foreach ($candidates as $candidate) { + $normalized = NameNormalizer::normalize($candidate['name']); + + if (isset($seen[$normalized])) { + continue; + } + + $existing = ProducerAlias::where('normalized_name', $normalized) + ->where('producer_id', $candidate['producer_id']) + ->exists(); + + if ($existing) { + $alreadyExists[] = $candidate; + $seen[$normalized] = true; + + continue; + } + + try { + $resolver->assertNameAvailable($candidate['name'], $candidate['producer_id']); + $toInsert[] = $candidate; + } catch (RuntimeException $e) { + $conflicts[] = $candidate + ['reason' => $e->getMessage()]; + } + + $seen[$normalized] = true; + } + + return [$toInsert, $alreadyExists, $conflicts]; + } + + private function summarize(array $toInsert, array $alreadyExists, array $conflicts): void + { + if ($toInsert !== []) { + $this->table( + ['Forrás', 'Név', 'Célrekord'], + array_map(fn ($c) => [$c['origin'], $c['name'], '#' . $c['producer_id']], $toInsert) + ); + } + + $this->line('Beszúrandó: ' . count($toInsert) + . ' | már megvan: ' . count($alreadyExists) + . ' | ütközés: ' . count($conflicts)); + + foreach ($conflicts as $conflict) { + $this->warn(" ! \"{$conflict['name']}\" ({$conflict['origin']}): {$conflict['reason']}"); + } + } +} diff --git a/tests/Feature/ProducerAliasBackfillTest.php b/tests/Feature/ProducerAliasBackfillTest.php new file mode 100644 index 0000000..72c65b5 --- /dev/null +++ b/tests/Feature/ProducerAliasBackfillTest.php @@ -0,0 +1,161 @@ + + */ +function writeDedupeReportFixture(string $path, int $keeperId, string $finalName, array $fromNames, ?string $keeperOriginalName = null): void +{ + $report = [ + 'created_at' => now()->toDateTimeString(), + 'database' => 'fixture', + 'merges' => [[ + 'keeper_id' => $keeperId, + 'final_name' => $finalName, + 'from' => array_map(fn ($n, $i) => ['id' => 1000 + $i, 'name' => $n], $fromNames, array_keys($fromNames)), + ]], + 'restore' => [ + 'producers' => $keeperOriginalName !== null + ? [$keeperId => ['id' => $keeperId, 'name' => $keeperOriginalName]] + : [], + ], + ]; + + file_put_contents($path, json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); +} + +test('az archivált gyártó note-jából aliast hoz létre a célrekordhoz --apply mellett', function () { + $keeper = Producer::create(['name' => 'Kőröstej Kft.', 'status' => DbStatusFieldEnum::active, 'canSee' => 1]); + Producer::create([ + 'name' => 'Kőröstej', + 'status' => DbStatusFieldEnum::archive, + 'canSee' => 0, + 'note' => "Összevonva ide: #{$keeper->id} (Kőröstej Kft.) – 2026-09-15 producers:dedupe", + ]); + + $this->artisan('producers:alias-backfill', ['--apply' => true])->assertExitCode(0); + + expect(ProducerAlias::where('producer_id', $keeper->id)->pluck('name')->all())->toBe(['Kőröstej']); +}); + +test('--apply nélkül csak kimutat, nem ír adatbázist', function () { + $keeper = Producer::create(['name' => 'Danone', 'status' => DbStatusFieldEnum::active, 'canSee' => 1]); + Producer::create([ + 'name' => 'Danone ', + 'status' => DbStatusFieldEnum::archive, + 'canSee' => 0, + 'note' => "Összevonva ide: #{$keeper->id} (Danone) – 2026-09-15 producers:dedupe", + ]); + + $this->artisan('producers:alias-backfill')->assertExitCode(0); + + expect(ProducerAlias::count())->toBe(0); +}); + +test('a láncolt archiválást a jelenlegi aktív rekordig követi', function () { + // 1. kör: "Régi A" -> #B, de #B maga is beolvad a 2. körben #C-be. + $c = Producer::create(['name' => 'Végleges C', 'status' => DbStatusFieldEnum::active, 'canSee' => 1]); + $b = Producer::create([ + 'name' => 'Köztes B', + 'status' => DbStatusFieldEnum::archive, + 'canSee' => 0, + 'note' => "Összevonva ide: #{$c->id} (Végleges C) – 2026-09-16 producers:dedupe", + ]); + Producer::create([ + 'name' => 'Régi A', + 'status' => DbStatusFieldEnum::archive, + 'canSee' => 0, + 'note' => "Összevonva ide: #{$b->id} (Köztes B) – 2026-09-15 producers:dedupe", + ]); + + $this->artisan('producers:alias-backfill', ['--apply' => true])->assertExitCode(0); + + expect(ProducerAlias::where('producer_id', $c->id)->pluck('name')->sort()->values()->all()) + ->toBe(['Köztes B', 'Régi A']); +}); + +test('a feloldhatatlan note-ot kihagyja, nem szakítja meg a futást', function () { + Producer::create([ + 'name' => 'Árva Rekord', + 'status' => DbStatusFieldEnum::archive, + 'canSee' => 0, + 'note' => 'Nincs értelmezhető formátum', + ]); + + $this->artisan('producers:alias-backfill', ['--apply' => true])->assertExitCode(0); + + expect(ProducerAlias::count())->toBe(0); +}); + +test('a jelentésfájlból névalapon oldja fel a célrekordot', function () { + $keeper = Producer::create(['name' => 'Voyagex', 'status' => DbStatusFieldEnum::active, 'canSee' => 1]); + $path = storage_path('app/private/teszt_alias_backfill_report.json'); + // A "VOYAGEX" szándékosan hiányzik a várt eredményből: normalizálva megegyezik az + // élő "Voyagex" névvel, tehát a parancsnak redundánsként ki kell hagynia. + writeDedupeReportFixture($path, keeperId: 999, finalName: 'Voyagex', fromNames: ['Voyagex Kft', 'VOYAGEX']); + + $this->artisan('producers:alias-backfill', ['reports' => [$path], '--skip-archives' => true, '--apply' => true]) + ->assertExitCode(0); + + expect(ProducerAlias::where('producer_id', $keeper->id)->pluck('name')->sort()->values()->all()) + ->toBe(['Voyagex Kft']); + + @unlink($path); +}); + +test('a jelentésben az átnevezett keeper eredeti neve is aliassá válik', function () { + $keeper = Producer::create(['name' => 'Végleges Név Kft', 'status' => DbStatusFieldEnum::active, 'canSee' => 1]); + $path = storage_path('app/private/teszt_alias_backfill_rename.json'); + writeDedupeReportFixture( + $path, + keeperId: 999, + finalName: 'Végleges Név Kft', + fromNames: ['Elavult Név'], + keeperOriginalName: 'Régi Saját Név' + ); + + $this->artisan('producers:alias-backfill', ['reports' => [$path], '--skip-archives' => true, '--apply' => true]) + ->assertExitCode(0); + + expect(ProducerAlias::where('producer_id', $keeper->id)->pluck('name')->sort()->values()->all()) + ->toBe(['Elavult Név', 'Régi Saját Név']); + + @unlink($path); +}); + +test('ütköző jelöltet nem szúr be, csak jelenti', function () { + Producer::create(['name' => 'Bonduelle', 'status' => DbStatusFieldEnum::active, 'canSee' => 1]); + $keeper = Producer::create(['name' => 'Danone', 'status' => DbStatusFieldEnum::active, 'canSee' => 1]); + Producer::create([ + 'name' => 'Bonduelle', // ugyanaz a név, mint egy MÁSIK élő gyártóé - ütközik + 'status' => DbStatusFieldEnum::archive, + 'canSee' => 0, + 'note' => "Összevonva ide: #{$keeper->id} (Danone) – 2026-09-15 producers:dedupe", + ]); + + $this->artisan('producers:alias-backfill', ['--apply' => true])->assertExitCode(0); + + expect(ProducerAlias::count())->toBe(0); +}); + +test('kétszeri futtatás nem hoz létre duplikált aliast (idempotens)', function () { + $keeper = Producer::create(['name' => 'Danone', 'status' => DbStatusFieldEnum::active, 'canSee' => 1]); + Producer::create([ + 'name' => 'Danone ', + 'status' => DbStatusFieldEnum::archive, + 'canSee' => 0, + 'note' => "Összevonva ide: #{$keeper->id} (Danone) – 2026-09-15 producers:dedupe", + ]); + + $this->artisan('producers:alias-backfill', ['--apply' => true])->assertExitCode(0); + $this->artisan('producers:alias-backfill', ['--apply' => true])->assertExitCode(0); + + expect(ProducerAlias::count())->toBe(1); +});