feat(tenant): reintroduce tenant-specific props and instance values
Restore props support only for the Tenant domain using a tenant-scoped model instead of the removed generic Prop/Propable implementation. Add tenant prop definitions and instance values: - add TenantProp and TenantPropValue models - add tenant-only data type enum and prop validation rules - add migrations for tenant_props and tenant_prop_values tables - persist prop values by tenant_codigo + tenant_prop_codigo Expose tenant prop management through the Tenant domain: - add TenantPropController with CRUD endpoints - add Store/Update request classes for tenant prop definitions - register tenant-props routes Re-enable tenant prop syncing and serialization: - restore createWithProps/updateWithProps flows in Tenant - validate props on tenant create/update requests - include collapsed props in TenantResource - keep the implementation scoped to Tenant without reintroducing polymorphic props Add coverage for the new tenant prop behavior: - update bootstrap tenant feature test to assert prop values - add feature tests for tenant prop creation and validation
This commit is contained in:
@@ -7,6 +7,8 @@ use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use LogicException;
|
||||
|
||||
#[Fillable([
|
||||
'codigo',
|
||||
@@ -17,6 +19,27 @@ class Tenant extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $attributes
|
||||
* @param array<string, mixed> $props
|
||||
* @return static
|
||||
*/
|
||||
public static function createWithProps(array $attributes, array $props = []): static
|
||||
{
|
||||
unset($attributes['props']);
|
||||
|
||||
/** @var static $tenant */
|
||||
$tenant = DB::transaction(function () use ($attributes, $props): self {
|
||||
/** @var static $createdTenant */
|
||||
$createdTenant = static::query()->create($attributes);
|
||||
$createdTenant->syncPropValues($props);
|
||||
|
||||
return $createdTenant;
|
||||
});
|
||||
|
||||
return $tenant;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<Product, $this>
|
||||
*/
|
||||
@@ -24,4 +47,78 @@ class Tenant extends Model
|
||||
{
|
||||
return $this->hasMany(Product::class, 'tenant_codigo', 'codigo');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<TenantPropValue, $this>
|
||||
*/
|
||||
public function propValues(): HasMany
|
||||
{
|
||||
return $this->hasMany(TenantPropValue::class, 'tenant_codigo', 'codigo');
|
||||
}
|
||||
|
||||
public function getPropValue(TenantProp|string $prop): ?TenantPropValue
|
||||
{
|
||||
$resolvedProp = $this->resolveProp($prop);
|
||||
|
||||
return $this->propValues()
|
||||
->where('tenant_prop_codigo', $resolvedProp->codigo)
|
||||
->first();
|
||||
}
|
||||
|
||||
public function setPropValue(TenantProp|string $prop, mixed $value): TenantPropValue
|
||||
{
|
||||
$this->ensurePropValuesCanBeManaged();
|
||||
|
||||
$resolvedProp = $this->resolveProp($prop);
|
||||
|
||||
return $this->propValues()->updateOrCreate(
|
||||
['tenant_prop_codigo' => $resolvedProp->codigo],
|
||||
['value' => $value],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $props
|
||||
*/
|
||||
public function syncPropValues(array $props): void
|
||||
{
|
||||
foreach ($props as $codigo => $value) {
|
||||
$this->setPropValue((string) $codigo, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $attributes
|
||||
* @param array<string, mixed> $props
|
||||
* @return static
|
||||
*/
|
||||
public function updateWithProps(array $attributes, array $props = []): static
|
||||
{
|
||||
unset($attributes['props']);
|
||||
|
||||
DB::transaction(function () use ($attributes, $props): void {
|
||||
$this->update($attributes);
|
||||
$this->syncPropValues($props);
|
||||
});
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function ensurePropValuesCanBeManaged(): void
|
||||
{
|
||||
if (! $this->exists) {
|
||||
throw new LogicException('Cannot manage prop values for an unsaved tenant.');
|
||||
}
|
||||
}
|
||||
|
||||
protected function resolveProp(TenantProp|string $prop): TenantProp
|
||||
{
|
||||
if ($prop instanceof TenantProp) {
|
||||
return $prop;
|
||||
}
|
||||
|
||||
return TenantProp::query()
|
||||
->where('codigo', $prop)
|
||||
->firstOrFail();
|
||||
}
|
||||
}
|
||||
|
||||
42
app/Domains/Tenant/Models/TenantProp.php
Normal file
42
app/Domains/Tenant/Models/TenantProp.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Tenant\Models;
|
||||
|
||||
use App\Domains\Tenant\Support\TenantPropDataType;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable([
|
||||
'codigo',
|
||||
'nombre',
|
||||
'descripcion',
|
||||
'is_required',
|
||||
'data_type',
|
||||
])]
|
||||
class TenantProp extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'tenant_props';
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'is_required' => 'boolean',
|
||||
'data_type' => TenantPropDataType::class,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<TenantPropValue, $this>
|
||||
*/
|
||||
public function values(): HasMany
|
||||
{
|
||||
return $this->hasMany(TenantPropValue::class, 'tenant_prop_codigo', 'codigo');
|
||||
}
|
||||
}
|
||||
36
app/Domains/Tenant/Models/TenantPropValue.php
Normal file
36
app/Domains/Tenant/Models/TenantPropValue.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Tenant\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[Fillable([
|
||||
'tenant_codigo',
|
||||
'tenant_prop_codigo',
|
||||
'value',
|
||||
])]
|
||||
class TenantPropValue extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'tenant_prop_values';
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Tenant, $this>
|
||||
*/
|
||||
public function tenant(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Tenant::class, 'tenant_codigo', 'codigo');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<TenantProp, $this>
|
||||
*/
|
||||
public function prop(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(TenantProp::class, 'tenant_prop_codigo', 'codigo');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user