refactor(tenant): remove single website extra endpoint
This commit is contained in:
@@ -5,7 +5,6 @@ namespace App\Domains\Tenant\Controllers\AdminApp;
|
|||||||
use App\Domains\Auth\Models\User;
|
use App\Domains\Auth\Models\User;
|
||||||
use App\Domains\Tenant\Models\Tenant;
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
use App\Domains\Tenant\Requests\AdminApp\UpdateWebsiteExtraRequest;
|
use App\Domains\Tenant\Requests\AdminApp\UpdateWebsiteExtraRequest;
|
||||||
use App\Domains\Tenant\Resources\AdminApp\WebsiteExtraResource;
|
|
||||||
use App\Domains\Tenant\Resources\AdminApp\WebsiteExtrasResource;
|
use App\Domains\Tenant\Resources\AdminApp\WebsiteExtrasResource;
|
||||||
use App\Domains\Tenant\Services\TenantInformationService;
|
use App\Domains\Tenant\Services\TenantInformationService;
|
||||||
use App\Domains\Tenant\Services\WebsiteExtraService;
|
use App\Domains\Tenant\Services\WebsiteExtraService;
|
||||||
@@ -26,20 +25,6 @@ class WebsiteExtraController extends Controller
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function showExtra(Request $request, string $websiteExtraCode): WebsiteExtraResource
|
|
||||||
{
|
|
||||||
$tenant = $this->loadTenant($request->user());
|
|
||||||
$definition = $this->websiteExtraService->definitionForTenant($tenant, $websiteExtraCode);
|
|
||||||
$websiteExtra = $tenant->websiteExtras
|
|
||||||
->firstWhere('website_type_extra_id', $definition->id);
|
|
||||||
|
|
||||||
if (! $websiteExtra) {
|
|
||||||
abort(404);
|
|
||||||
}
|
|
||||||
|
|
||||||
return WebsiteExtraResource::make($websiteExtra);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function update(
|
public function update(
|
||||||
UpdateWebsiteExtraRequest $request,
|
UpdateWebsiteExtraRequest $request,
|
||||||
string $websiteExtraCode
|
string $websiteExtraCode
|
||||||
|
|||||||
@@ -1,86 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Domains\Tenant\Resources\AdminApp;
|
|
||||||
|
|
||||||
use App\Domains\Attachable\Models\Attachment;
|
|
||||||
use App\Domains\Attachable\Models\AttachmentCrop;
|
|
||||||
use App\Domains\Tenant\Models\WebsiteExtra;
|
|
||||||
use Illuminate\Http\Request;
|
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @mixin WebsiteExtra
|
|
||||||
*/
|
|
||||||
class WebsiteExtraResource extends JsonResource
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @return array<string, mixed>
|
|
||||||
*/
|
|
||||||
public function toArray(Request $request): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'codigo' => $this->websiteTypeExtra->codigo,
|
|
||||||
'nombre' => $this->websiteTypeExtra->nombre,
|
|
||||||
'descripcion' => $this->websiteTypeExtra->descripcion,
|
|
||||||
'is_required' => $this->websiteTypeExtra->is_required,
|
|
||||||
'is_enabled' => $this->is_enabled,
|
|
||||||
'request_rules' => $this->websiteTypeExtra->config_schema['request_rules'] ?? [],
|
|
||||||
'config' => $this->formatConfig(
|
|
||||||
$this->resolvedConfig(),
|
|
||||||
fn (Attachment $attachment): string => $attachment->key
|
|
||||||
),
|
|
||||||
'resolved_config' => $this->formatConfig(
|
|
||||||
$this->resolvedAdminConfig(),
|
|
||||||
fn (Attachment $attachment): string => $attachment->getTemporaryUrl(1440)
|
|
||||||
),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
private function resolvedAdminConfig(): mixed
|
|
||||||
{
|
|
||||||
$config = $this->resolvedConfig();
|
|
||||||
|
|
||||||
if (
|
|
||||||
$this->websiteTypeExtra->codigo !== 'heroConfig'
|
|
||||||
|| ! is_array($config)
|
|
||||||
|| ! ($config['background_image_id'] ?? null) instanceof Attachment
|
|
||||||
) {
|
|
||||||
return $config;
|
|
||||||
}
|
|
||||||
|
|
||||||
$attachment = $config['background_image_id'];
|
|
||||||
$fullRange = ['start_percentage' => 0.0, 'end_percentage' => 100.0];
|
|
||||||
$crops = $attachment->cropVariants->keyBy('variant');
|
|
||||||
$config['background_image_id'] = [
|
|
||||||
'url' => $attachment->getTemporaryUrl(1440),
|
|
||||||
'crops' => collect(AttachmentCrop::VARIANTS)->mapWithKeys(
|
|
||||||
function (string $variant) use ($crops, $fullRange): array {
|
|
||||||
$crop = $crops->get($variant);
|
|
||||||
|
|
||||||
return [$variant => [
|
|
||||||
'crop_horizontal' => $crop?->crop_horizontal ?? $fullRange,
|
|
||||||
'crop_vertical' => $crop?->crop_vertical ?? $fullRange,
|
|
||||||
]];
|
|
||||||
}
|
|
||||||
)->all(),
|
|
||||||
];
|
|
||||||
|
|
||||||
return $config;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function formatConfig(mixed $value, callable $formatAttachment): mixed
|
|
||||||
{
|
|
||||||
if ($value instanceof Attachment) {
|
|
||||||
return $formatAttachment($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! is_array($value)) {
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return array_map(
|
|
||||||
fn (mixed $item): mixed => $this->formatConfig($item, $formatAttachment),
|
|
||||||
$value
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,7 +7,6 @@ Route::prefix('v1/adminapp/tenant')
|
|||||||
->middleware(['auth:sanctum', 'adminapp.tenant'])
|
->middleware(['auth:sanctum', 'adminapp.tenant'])
|
||||||
->group(function (): void {
|
->group(function (): void {
|
||||||
Route::get('website-extras', [WebsiteExtraController::class, 'show']);
|
Route::get('website-extras', [WebsiteExtraController::class, 'show']);
|
||||||
Route::get('website-extras/{websiteExtraCode}', [WebsiteExtraController::class, 'showExtra']);
|
|
||||||
Route::put('website-extras/{websiteExtraCode}', [WebsiteExtraController::class, 'update']);
|
Route::put('website-extras/{websiteExtraCode}', [WebsiteExtraController::class, 'update']);
|
||||||
Route::patch('website-extras/{websiteExtraCode}/toggle', [WebsiteExtraController::class, 'toggle']);
|
Route::patch('website-extras/{websiteExtraCode}/toggle', [WebsiteExtraController::class, 'toggle']);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -88,36 +88,6 @@ class AdminAppWebsiteExtraControllerTest extends TestCase
|
|||||||
->assertJsonPath('data.resolved_extras.contactConfig.phone', '+54 341 555 0101');
|
->assertJsonPath('data.resolved_extras.contactConfig.phone', '+54 341 555 0101');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_adminapp_user_can_read_one_website_extra(): void
|
|
||||||
{
|
|
||||||
$tenant = $this->createTenant('acme');
|
|
||||||
$definition = $this->websiteType->extras()->firstOrFail();
|
|
||||||
$tenant->websiteExtras()->create([
|
|
||||||
'website_type_extra_id' => $definition->id,
|
|
||||||
'config' => ['phone' => '+54 341 555 0101'],
|
|
||||||
'is_enabled' => true,
|
|
||||||
]);
|
|
||||||
|
|
||||||
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
|
||||||
|
|
||||||
$this->getJson('/api/v1/adminapp/tenant/website-extras/contactConfig')
|
|
||||||
->assertOk()
|
|
||||||
->assertJsonPath('data.codigo', 'contactConfig')
|
|
||||||
->assertJsonPath('data.nombre', 'Configuración de contacto')
|
|
||||||
->assertJsonPath('data.is_enabled', true)
|
|
||||||
->assertJsonPath('data.config.phone', '+54 341 555 0101')
|
|
||||||
->assertJsonPath('data.resolved_config.phone', '+54 341 555 0101');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function test_reading_an_unconfigured_website_extra_returns_not_found(): void
|
|
||||||
{
|
|
||||||
$tenant = $this->createTenant('acme');
|
|
||||||
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
|
||||||
|
|
||||||
$this->getJson('/api/v1/adminapp/tenant/website-extras/contactConfig')
|
|
||||||
->assertNotFound();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function test_adminapp_user_updates_one_extra_without_touching_other_tenants(): void
|
public function test_adminapp_user_updates_one_extra_without_touching_other_tenants(): void
|
||||||
{
|
{
|
||||||
$tenant = $this->createTenant('acme');
|
$tenant = $this->createTenant('acme');
|
||||||
|
|||||||
Reference in New Issue
Block a user