Files
shopit-back/app/Domains/Tenant/Resources/TenantResource.php
ncoronel 689d115a8f 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
2026-06-24 14:50:58 -03:00

50 lines
1.3 KiB
PHP

<?php
namespace App\Domains\Tenant\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* @mixin \App\Domains\Tenant\Models\Tenant
*/
class TenantResource extends JsonResource
{
/**
* @return array<string, mixed>
*/
protected function collapseProps(): array
{
return $this->resource->propValues()
->with('prop')
->get()
->mapWithKeys(fn ($propValue): array => [
$propValue->tenant_prop_codigo => $this->normalizePropValue($propValue->value, $propValue->prop?->data_type?->value),
])
->all();
}
protected function normalizePropValue(mixed $value, ?string $dataType): mixed
{
return match ($dataType) {
'boolean' => filter_var($value, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE) ?? $value,
'number' => is_numeric($value) ? $value + 0 : $value,
default => $value,
};
}
/**
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'codigo' => $this->codigo,
'nombre' => $this->nombre,
'dominio' => $this->dominio,
'props' => $this->collapseProps(),
];
}
}