feat(clients): add client ownership for tenants

This commit is contained in:
2026-08-18 16:16:06 -03:00
parent a8973f6171
commit 38e87fff6c
3 changed files with 15 additions and 31 deletions

View File

@@ -2,7 +2,6 @@
namespace App\Domains\Client\Models; namespace App\Domains\Client\Models;
use App\Domains\Integration\Models\ClientIntegration;
use App\Domains\Tenant\Models\Tenant; use App\Domains\Tenant\Models\Tenant;
use Illuminate\Database\Eloquent\Attributes\Fillable; use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
@@ -21,10 +20,4 @@ class Client extends Model
{ {
return $this->hasMany(Tenant::class); return $this->hasMany(Tenant::class);
} }
/** @return HasMany<ClientIntegration, $this> */
public function integrations(): HasMany
{
return $this->hasMany(ClientIntegration::class);
}
} }

View File

@@ -20,7 +20,6 @@ class ClientResource extends JsonResource
'codigo' => $tenant->codigo, 'codigo' => $tenant->codigo,
'nombre' => $tenant->nombre, 'nombre' => $tenant->nombre,
'dominio' => $tenant->dominio, 'dominio' => $tenant->dominio,
'base_path' => $tenant->base_path,
])), ])),
]; ];
} }

View File

@@ -9,35 +9,27 @@ return new class extends Migration
{ {
public function up(): void public function up(): void
{ {
if (! Schema::hasTable('clients')) { Schema::create('clients', function (Blueprint $table): void {
Schema::create('clients', function (Blueprint $table): void { $table->id();
$table->id(); $table->string('code')->unique();
$table->string('code')->unique(); $table->string('name');
$table->string('name'); $table->timestamps();
$table->timestamps(); });
});
}
if (! Schema::hasColumn('tenants', 'client_id')) { Schema::table('tenants', function (Blueprint $table): void {
Schema::table('tenants', function (Blueprint $table): void { $table->foreignId('client_id')->nullable()->after('id')->constrained('clients')->restrictOnDelete();
$table->foreignId('client_id')->nullable()->after('id')->constrained('clients')->restrictOnDelete(); });
});
}
DB::table('tenants') DB::table('tenants')
->select(['id', 'codigo', 'nombre', 'created_at', 'updated_at']) ->select(['id', 'codigo', 'nombre', 'created_at', 'updated_at'])
->orderBy('id') ->orderBy('id')
->each(function (object $tenant): void { ->each(function (object $tenant): void {
$clientId = DB::table('clients')->where('code', $tenant->codigo)->value('id'); $clientId = DB::table('clients')->insertGetId([
'code' => $tenant->codigo,
if ($clientId === null) { 'name' => $tenant->nombre,
$clientId = DB::table('clients')->insertGetId([ 'created_at' => $tenant->created_at ?? now(),
'code' => $tenant->codigo, 'updated_at' => $tenant->updated_at ?? now(),
'name' => $tenant->nombre, ]);
'created_at' => $tenant->created_at ?? now(),
'updated_at' => $tenant->updated_at ?? now(),
]);
}
DB::table('tenants')->where('id', $tenant->id)->update(['client_id' => $clientId]); DB::table('tenants')->where('id', $tenant->id)->update(['client_id' => $clientId]);
}); });