feat(migrations): add migration for grouping Pyme Rural tenants and updating client associations

This commit is contained in:
2026-09-21 09:35:40 -03:00
parent 9a14b8fbbd
commit 14c81fec1b
8 changed files with 490 additions and 11 deletions

View File

@@ -0,0 +1,93 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
private const CLIENT_CODE = 'pyme_rural';
private const CLIENT_NAME = 'Pyme Rural';
private const PREVIOUS_CLIENT_CODE = 'onticket';
private const TENANT_CODES = [
'fiesta_futbol_infantil',
'desfile_pura_tendencia',
];
public function up(): void
{
DB::transaction(function (): void {
$clientId = $this->clientId(self::CLIENT_CODE, self::CLIENT_NAME);
$sourceClientIds = DB::table('tenants')
->whereIn('codigo', self::TENANT_CODES)
->pluck('client_id')
->unique();
$this->copyIntegrations($sourceClientIds, $clientId);
DB::table('tenants')
->whereIn('codigo', self::TENANT_CODES)
->update(['client_id' => $clientId]);
});
}
public function down(): void
{
DB::transaction(function (): void {
$previousClientId = $this->clientId(self::PREVIOUS_CLIENT_CODE, 'OnTicket');
$pymeRuralClientId = DB::table('clients')
->where('code', self::CLIENT_CODE)
->value('id');
if ($pymeRuralClientId !== null) {
$this->copyIntegrations(collect([$pymeRuralClientId]), $previousClientId);
}
DB::table('tenants')
->whereIn('codigo', self::TENANT_CODES)
->update(['client_id' => $previousClientId]);
});
}
private function clientId(string $code, string $name): int
{
$clientId = DB::table('clients')->where('code', $code)->value('id');
if ($clientId === null) {
return (int) DB::table('clients')->insertGetId([
'code' => $code,
'name' => $name,
'created_at' => now(),
'updated_at' => now(),
]);
}
DB::table('clients')->where('id', $clientId)->update([
'name' => $name,
'updated_at' => now(),
]);
return (int) $clientId;
}
private function copyIntegrations(iterable $sourceClientIds, int $targetClientId): void
{
foreach ($sourceClientIds as $sourceClientId) {
DB::table('client_integrations')
->where('client_id', $sourceClientId)
->orderBy('id')
->get()
->each(function (object $integration) use ($targetClientId): void {
DB::table('client_integrations')->insertOrIgnore([
'client_id' => $targetClientId,
'integration_code' => $integration->integration_code,
'integration_instance_id' => $integration->integration_instance_id,
'created_at' => $integration->created_at,
'updated_at' => $integration->updated_at,
]);
});
}
}
};

View File

@@ -12,9 +12,9 @@ use App\Domains\Commerce\Catalog\Models\CatalogItem;
use App\Domains\Commerce\Catalog\Models\FeaturedGroup;
use App\Domains\Commerce\Catalog\Services\CatalogService;
use App\Domains\Core\Client\Models\Client;
use App\Domains\Ticketing\Desfile\Services\InvitationPurchaseProvisioner;
use App\Domains\Core\Tenant\Models\Tenant;
use App\Domains\Core\Tenant\Services\TenantService;
use App\Domains\Ticketing\Desfile\Services\InvitationPurchaseProvisioner;
use Illuminate\Database\Seeder;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
@@ -22,7 +22,7 @@ use RuntimeException;
class DesfilePuraTendenciaSeeder extends Seeder
{
private const ONTICKET_CLIENT_CODE = 'onticket';
private const PYME_RURAL_CLIENT_CODE = 'pyme_rural';
private const TENANT_CODE = 'desfile_pura_tendencia';
@@ -34,9 +34,9 @@ class DesfilePuraTendenciaSeeder extends Seeder
public function run(): void
{
$client = Client::query()->firstOrCreate(
['code' => self::ONTICKET_CLIENT_CODE],
['name' => 'OnTicket'],
$client = Client::query()->updateOrCreate(
['code' => self::PYME_RURAL_CLIENT_CODE],
['name' => 'Pyme Rural'],
);
$tenant = Tenant::query()->where('codigo', self::TENANT_CODE)->first();

View File

@@ -2,13 +2,13 @@
namespace Database\Seeders;
use App\Shared\Attachable\Models\Attachment;
use App\Shared\Attachable\Services\AttachmentService;
use App\Domains\Core\Client\Models\Client;
use App\Domains\Core\Tenant\Models\Tenant;
use App\Domains\Core\Tenant\Models\AdminWebsiteType;
use App\Domains\Core\Tenant\Models\Tenant;
use App\Domains\Core\Tenant\Services\TenantService;
use App\Domains\Core\Tenant\Services\WebsiteExtraService;
use App\Shared\Attachable\Models\Attachment;
use App\Shared\Attachable\Services\AttachmentService;
use Illuminate\Database\Seeder;
use Illuminate\Http\UploadedFile;
use RuntimeException;
@@ -18,6 +18,8 @@ class TenantSeeder extends Seeder
{
private const ONTICKET_CLIENT_CODE = 'onticket';
private const PYME_RURAL_CLIENT_CODE = 'pyme_rural';
private const SONDER_CLIENT_CODE = 'sonder';
private const SOCIAL_MEDIA = [
@@ -60,6 +62,11 @@ class TenantSeeder extends Seeder
['name' => 'OnTicket'],
);
$pymeRuralClient = Client::query()->updateOrCreate(
['code' => self::PYME_RURAL_CLIENT_CODE],
['name' => 'Pyme Rural'],
);
$onTicketType = AdminWebsiteType::query()->where('codigo', 'onticket')->firstOrFail();
$onTicketTenant = Tenant::query()->where('codigo', 'onticket')->first();
@@ -199,7 +206,7 @@ class TenantSeeder extends Seeder
->delete();
$this->tenantService->create([
'client_id' => $onTicketClient->id,
'client_id' => $pymeRuralClient->id,
'codigo' => 'fiesta_futbol_infantil',
'nombre' => 'Fiesta Fútbol Infantil',
'dominio' => $fiestaDomain,