ADD EV3-473 gyártó-alias adatmodell és feloldás az árlista feldolgozóban
Külön producer_aliases tábla (normalized_name unique) és ProducerAliasResolver az ütközés-védelemhez. A PricelistFileProcessService::getProducerLookupMap() az élő gyártónevek mellé az aliasokat is feloldja, élő névnek elsőbbséggel.
This commit is contained in:
parent
80b3efae70
commit
f9a03ad87a
@ -2,4 +2,10 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
class Producer extends BaseAuditable {}
|
class Producer extends BaseAuditable
|
||||||
|
{
|
||||||
|
public function aliases()
|
||||||
|
{
|
||||||
|
return $this->hasMany(ProducerAlias::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
11
app/Models/ProducerAlias.php
Normal file
11
app/Models/ProducerAlias.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
class ProducerAlias extends BaseAuditable
|
||||||
|
{
|
||||||
|
public function producer()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Producer::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -8,6 +8,7 @@
|
|||||||
use App\Models\PriceList;
|
use App\Models\PriceList;
|
||||||
use App\Models\PricelistFile;
|
use App\Models\PricelistFile;
|
||||||
use App\Support\NameNormalizer;
|
use App\Support\NameNormalizer;
|
||||||
|
use App\Support\ProducerAliasResolver;
|
||||||
use App\Enums\PricelistFileStatusEnum;
|
use App\Enums\PricelistFileStatusEnum;
|
||||||
use App\Models\PricelistFileLine;
|
use App\Models\PricelistFileLine;
|
||||||
use App\Enums\PricelistFileLineStatusEnum;
|
use App\Enums\PricelistFileLineStatusEnum;
|
||||||
@ -871,12 +872,20 @@ protected function getProducerLookupMap(): array
|
|||||||
// Az összevont (archive) és a törölt gyártók kimaradnak: enélkül a
|
// Az összevont (archive) és a törölt gyártók kimaradnak: enélkül a
|
||||||
// duplikáció-takarítás hatástalan lenne, mert a beolvasztott rekord
|
// duplikáció-takarítás hatástalan lenne, mert a beolvasztott rekord
|
||||||
// továbbra is visszakerülne a feloldásba.
|
// továbbra is visszakerülne a feloldásba.
|
||||||
return \App\Models\Producer::query()
|
$producerMap = \App\Models\Producer::query()
|
||||||
->whereNotIn('status', [DbStatusFieldEnum::archive, DbStatusFieldEnum::deleted])
|
->whereNotIn('status', [DbStatusFieldEnum::archive, DbStatusFieldEnum::deleted])
|
||||||
->get()
|
->get()
|
||||||
->keyBy(fn($p) => $this->normalizeForComparison($p->name))
|
->keyBy(fn($p) => $this->normalizeForComparison($p->name))
|
||||||
->map->id
|
->map->id
|
||||||
->toArray();
|
->toArray();
|
||||||
|
|
||||||
|
// Az árlisták a beolvasztott, régi gyártóneveket is használják. A producer-térkép
|
||||||
|
// áll a `+` operátor BAL oldalán, hogy egyezés esetén az ő bejegyzése nyerjen
|
||||||
|
// (a `+` a bal oldali értéket tartja meg ütköző kulcsnál) - az alias sosem írhat
|
||||||
|
// felül élő gyártónevet.
|
||||||
|
$aliasMap = (new ProducerAliasResolver)->lookupMap();
|
||||||
|
|
||||||
|
return $producerMap + $aliasMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
68
app/Support/ProducerAliasResolver.php
Normal file
68
app/Support/ProducerAliasResolver.php
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Support;
|
||||||
|
|
||||||
|
use App\Enums\DbStatusFieldEnum;
|
||||||
|
use App\Models\Producer;
|
||||||
|
use App\Models\ProducerAlias;
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gyártó-alias feloldás és ütközés-védelem.
|
||||||
|
*
|
||||||
|
* Az alias sosem írhat felül élő gyártónevet: ezt a feloldási sorrend (pontos név ->
|
||||||
|
* normalizált név -> alias) ÉS ez az osztály is önállóan biztosítja, hogy egy alias
|
||||||
|
* írásakor se keletkezhessen ütközés.
|
||||||
|
*/
|
||||||
|
class ProducerAliasResolver
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* normalized_name => producer_id, csak aktív (nem archive/deleted) gyártóhoz
|
||||||
|
* tartozó aliasokra.
|
||||||
|
*/
|
||||||
|
public function lookupMap(): array
|
||||||
|
{
|
||||||
|
return ProducerAlias::query()
|
||||||
|
->whereHas('producer', fn ($q) => $q->whereNotIn('status', [
|
||||||
|
DbStatusFieldEnum::archive, DbStatusFieldEnum::deleted,
|
||||||
|
]))
|
||||||
|
->pluck('producer_id', 'normalized_name')
|
||||||
|
->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dob, ha a név normalizált alakja ütközik egy aktív gyártó nevével vagy egy
|
||||||
|
* másik alias-szal. $ignoreProducerId: az a gyártó, amelyikhez az alias éppen
|
||||||
|
* készül - a saját neve nem számít ütközésnek.
|
||||||
|
*/
|
||||||
|
public function assertNameAvailable(string $name, ?int $ignoreProducerId = null): void
|
||||||
|
{
|
||||||
|
$normalized = NameNormalizer::normalize($name);
|
||||||
|
|
||||||
|
if ($normalized === '') {
|
||||||
|
throw new RuntimeException('Az alias neve nem lehet üres.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$collidingProducer = Producer::query()
|
||||||
|
->whereNotIn('status', [DbStatusFieldEnum::archive, DbStatusFieldEnum::deleted])
|
||||||
|
->when($ignoreProducerId, fn ($q) => $q->where('id', '!=', $ignoreProducerId))
|
||||||
|
->get(['id', 'name'])
|
||||||
|
->first(fn ($p) => NameNormalizer::normalize($p->name) === $normalized);
|
||||||
|
|
||||||
|
if ($collidingProducer) {
|
||||||
|
throw new RuntimeException(
|
||||||
|
"A(z) \"{$name}\" név ütközik a(z) #{$collidingProducer->id} ({$collidingProducer->name}) aktív gyártóval."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$collidingAlias = ProducerAlias::where('normalized_name', $normalized)
|
||||||
|
->when($ignoreProducerId, fn ($q) => $q->where('producer_id', '!=', $ignoreProducerId))
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($collidingAlias) {
|
||||||
|
throw new RuntimeException(
|
||||||
|
"A(z) \"{$name}\" név már alias a(z) #{$collidingAlias->producer_id} gyártóhoz."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('producer_aliases', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('producer_id')->constrained('producers')->cascadeOnDelete();
|
||||||
|
$table->string('name');
|
||||||
|
$table->string('normalized_name')->unique();
|
||||||
|
$table->string('source')->default('backfill');
|
||||||
|
$table->timestamps();
|
||||||
|
$table->userIdFields();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('producer_aliases');
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -634,6 +634,26 @@ CREATE TABLE `pricelist_files` (
|
|||||||
CONSTRAINT `pricelist_files_supplier_id_foreign` FOREIGN KEY (`supplier_id`) REFERENCES `suppliers` (`id`) ON DELETE SET NULL
|
CONSTRAINT `pricelist_files_supplier_id_foreign` FOREIGN KEY (`supplier_id`) REFERENCES `suppliers` (`id`) ON DELETE SET NULL
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||||
|
DROP TABLE IF EXISTS `producer_aliases`;
|
||||||
|
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||||
|
/*!40101 SET character_set_client = utf8mb4 */;
|
||||||
|
CREATE TABLE `producer_aliases` (
|
||||||
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
||||||
|
`producer_id` bigint(20) unsigned NOT NULL,
|
||||||
|
`name` varchar(255) NOT NULL,
|
||||||
|
`normalized_name` varchar(255) NOT NULL,
|
||||||
|
`source` varchar(255) NOT NULL DEFAULT 'backfill',
|
||||||
|
`created_at` timestamp NULL DEFAULT NULL,
|
||||||
|
`updated_at` timestamp NULL DEFAULT NULL,
|
||||||
|
`created_by` int(10) unsigned DEFAULT NULL,
|
||||||
|
`updated_by` int(10) unsigned DEFAULT NULL,
|
||||||
|
`deleted_by` int(10) unsigned DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
UNIQUE KEY `producer_aliases_normalized_name_unique` (`normalized_name`),
|
||||||
|
KEY `producer_aliases_producer_id_foreign` (`producer_id`),
|
||||||
|
CONSTRAINT `producer_aliases_producer_id_foreign` FOREIGN KEY (`producer_id`) REFERENCES `producers` (`id`) ON DELETE CASCADE
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||||
DROP TABLE IF EXISTS `producers`;
|
DROP TABLE IF EXISTS `producers`;
|
||||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||||
/*!40101 SET character_set_client = utf8mb4 */;
|
/*!40101 SET character_set_client = utf8mb4 */;
|
||||||
@ -1180,4 +1200,5 @@ INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (888,'2026_08_10_09
|
|||||||
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (889,'2026_08_10_090100_add_execution_fields_to_pricelist_file_lines_table',28);
|
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (889,'2026_08_10_090100_add_execution_fields_to_pricelist_file_lines_table',28);
|
||||||
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (890,'2026_08_16_090000_create_deployment_packages_table',29);
|
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (890,'2026_08_16_090000_create_deployment_packages_table',29);
|
||||||
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (892,'2026_08_24_172116_add_meter_unit_to_seller_and_amount_unit_columns',30);
|
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (892,'2026_08_24_172116_add_meter_unit_to_seller_and_amount_unit_columns',30);
|
||||||
|
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (893,'2026_09_18_120000_create_producer_aliases_table',31);
|
||||||
commit;
|
commit;
|
||||||
|
|||||||
@ -2,9 +2,11 @@
|
|||||||
|
|
||||||
use App\Enums\DbStatusFieldEnum;
|
use App\Enums\DbStatusFieldEnum;
|
||||||
use App\Models\Producer;
|
use App\Models\Producer;
|
||||||
|
use App\Models\ProducerAlias;
|
||||||
use App\Services\PriceListService;
|
use App\Services\PriceListService;
|
||||||
use App\Services\PricelistFileProcessService;
|
use App\Services\PricelistFileProcessService;
|
||||||
use App\Support\NameNormalizer;
|
use App\Support\NameNormalizer;
|
||||||
|
use App\Support\ProducerAliasResolver;
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
@ -111,3 +113,76 @@ function makeProducer(string $name, string $status = DbStatusFieldEnum::active):
|
|||||||
expect($lookup['DANONE'] ?? null)->toBe($active->id)
|
expect($lookup['DANONE'] ?? null)->toBe($active->id)
|
||||||
->and(in_array($merged->id, $lookup, true))->toBeFalse();
|
->and(in_array($merged->id, $lookup, true))->toBeFalse();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function makeAlias(Producer $producer, string $name, string $source = 'backfill'): ProducerAlias
|
||||||
|
{
|
||||||
|
return ProducerAlias::create([
|
||||||
|
'producer_id' => $producer->id,
|
||||||
|
'name' => $name,
|
||||||
|
'normalized_name' => NameNormalizer::normalize($name),
|
||||||
|
'source' => $source,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
test('az árlista feldolgozó az alias-t is feloldja a gyártó lookup térképében', function () {
|
||||||
|
$active = makeProducer('Kőröstej Kft.');
|
||||||
|
makeAlias($active, 'Kőröstej');
|
||||||
|
|
||||||
|
$lookup = callPrivate(app(PricelistFileProcessService::class), 'getProducerLookupMap');
|
||||||
|
|
||||||
|
expect($lookup[NameNormalizer::normalize('Kőröstej')] ?? null)->toBe($active->id);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('az alias sosem írja felül élő gyártó nevét a lookup térképben', function () {
|
||||||
|
$winner = makeProducer('Danone');
|
||||||
|
$other = makeProducer('Bonduelle');
|
||||||
|
// Direkt ütköztetett alias-t szúrunk be a resolver megkerülésével, hogy a
|
||||||
|
// térképépítés defenzív viselkedését - nem csak az író oldali guardot -
|
||||||
|
// ellenőrizzük.
|
||||||
|
makeAlias($other, 'Danone');
|
||||||
|
|
||||||
|
$lookup = callPrivate(app(PricelistFileProcessService::class), 'getProducerLookupMap');
|
||||||
|
|
||||||
|
expect($lookup[NameNormalizer::normalize('Danone')])->toBe($winner->id);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('az archivált gyártóhoz tartozó alias kimarad a feloldásból', function () {
|
||||||
|
$archived = makeProducer('Régi Név', DbStatusFieldEnum::archive);
|
||||||
|
makeAlias($archived, 'Régi Név');
|
||||||
|
|
||||||
|
$lookup = callPrivate(app(PricelistFileProcessService::class), 'getProducerLookupMap');
|
||||||
|
|
||||||
|
expect($lookup[NameNormalizer::normalize('Régi Név')] ?? null)->toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('az assertNameAvailable dob, ha a név élő gyártóval ütközik', function () {
|
||||||
|
makeProducer('Danone');
|
||||||
|
|
||||||
|
expect(fn () => (new ProducerAliasResolver)->assertNameAvailable('danone '))
|
||||||
|
->toThrow(RuntimeException::class);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('az assertNameAvailable dob, ha a név egy másik alias-szal ütközik', function () {
|
||||||
|
$producer = makeProducer('Kőröstej Kft.');
|
||||||
|
$other = makeProducer('Bonduelle');
|
||||||
|
makeAlias($producer, 'Kőröstej');
|
||||||
|
|
||||||
|
expect(fn () => (new ProducerAliasResolver)->assertNameAvailable('Kőröstej', $other->id))
|
||||||
|
->toThrow(RuntimeException::class);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('az assertNameAvailable nem tekinti ütközésnek a gyártó saját nevét', function () {
|
||||||
|
$producer = makeProducer('Danone');
|
||||||
|
|
||||||
|
(new ProducerAliasResolver)->assertNameAvailable('Danone', $producer->id);
|
||||||
|
|
||||||
|
expect(true)->toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('az assertNameAvailable nem dob valódi új névre', function () {
|
||||||
|
makeProducer('Danone');
|
||||||
|
|
||||||
|
(new ProducerAliasResolver)->assertNameAvailable('Bonduelle');
|
||||||
|
|
||||||
|
expect(true)->toBeTrue();
|
||||||
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user