feat(migrations): add migration for grouping Pyme Rural tenants and updating client associations
This commit is contained in:
@@ -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,
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user