feat(website-extras): add showExtra method and resource for individual website extras, update routes and tests
This commit is contained in:
@@ -5,6 +5,7 @@ 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;
|
||||||
@@ -25,6 +26,20 @@ 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
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Tenant\Resources\AdminApp;
|
||||||
|
|
||||||
|
use App\Domains\Attachable\Models\Attachment;
|
||||||
|
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->resolvedConfig(),
|
||||||
|
fn (Attachment $attachment): string => $attachment->getTemporaryUrl(1440)
|
||||||
|
),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,10 +6,8 @@ use Illuminate\Support\Facades\Route;
|
|||||||
Route::prefix('v1/adminapp/tenant')
|
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']);
|
||||||
->name('adminapp.tenant.website-extras.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']);
|
||||||
->name('adminapp.tenant.website-extras.update');
|
Route::patch('website-extras/{websiteExtraCode}/toggle', [WebsiteExtraController::class, 'toggle']);
|
||||||
Route::patch('website-extras/{websiteExtraCode}/toggle', [WebsiteExtraController::class, 'toggle'])
|
|
||||||
->name('adminapp.tenant.website-extras.toggle');
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -84,6 +84,36 @@ 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