feat(menu): implement TenantMenuController, StoreTenantMenuRequest, and TenantMenuService for managing tenant-specific menus
This commit is contained in:
32
app/Domains/Menu/Controllers/TenantMenuController.php
Normal file
32
app/Domains/Menu/Controllers/TenantMenuController.php
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Menu\Controllers;
|
||||||
|
|
||||||
|
use App\Domains\Menu\Models\Menu;
|
||||||
|
use App\Domains\Menu\Requests\StoreTenantMenuRequest;
|
||||||
|
use App\Domains\Menu\Services\TenantMenuService;
|
||||||
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
|
||||||
|
class TenantMenuController extends Controller
|
||||||
|
{
|
||||||
|
public function __construct(private readonly TenantMenuService $tenantMenuService) {}
|
||||||
|
|
||||||
|
public function store(
|
||||||
|
StoreTenantMenuRequest $request,
|
||||||
|
string $tenantCode,
|
||||||
|
string $menuCode,
|
||||||
|
): JsonResponse {
|
||||||
|
$tenant = Tenant::query()->where('codigo', $tenantCode)->firstOrFail();
|
||||||
|
$menu = Menu::query()->where('code', $menuCode)->firstOrFail();
|
||||||
|
|
||||||
|
$tenantMenu = $this->tenantMenuService->configure(
|
||||||
|
$tenant,
|
||||||
|
$menu,
|
||||||
|
$request->validated('static_content'),
|
||||||
|
);
|
||||||
|
|
||||||
|
return response()->json($tenantMenu);
|
||||||
|
}
|
||||||
|
}
|
||||||
49
app/Domains/Menu/Requests/StoreTenantMenuRequest.php
Normal file
49
app/Domains/Menu/Requests/StoreTenantMenuRequest.php
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Menu\Requests;
|
||||||
|
|
||||||
|
use App\Domains\Menu\Models\Menu;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
|
|
||||||
|
class StoreTenantMenuRequest extends FormRequest
|
||||||
|
{
|
||||||
|
private ?Menu $menuModel = null;
|
||||||
|
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function prepareForValidation(): void
|
||||||
|
{
|
||||||
|
$this->menuModel = Menu::query()
|
||||||
|
->where('code', $this->route('menu_code'))
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if (! $this->menuModel) {
|
||||||
|
throw ValidationException::withMessages([
|
||||||
|
'menu_code' => 'El menú indicado no existe.',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
if ($this->menuModel?->content_type !== Menu::CONTENT_TYPE_STATIC) {
|
||||||
|
return [
|
||||||
|
'static_content' => ['nullable', 'array'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$rules = [
|
||||||
|
'static_content' => ['required', 'array'],
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($this->menuModel->static_content_schema as $field => $rule) {
|
||||||
|
$rules["static_content.{$field}"] = $rule;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $rules;
|
||||||
|
}
|
||||||
|
}
|
||||||
26
app/Domains/Menu/Services/TenantMenuService.php
Normal file
26
app/Domains/Menu/Services/TenantMenuService.php
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Menu\Services;
|
||||||
|
|
||||||
|
use App\Domains\Menu\Models\Menu;
|
||||||
|
use App\Domains\Menu\Models\TenantMenu;
|
||||||
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class TenantMenuService
|
||||||
|
{
|
||||||
|
public function configure(Tenant $tenant, Menu $menu, ?array $staticContent): TenantMenu
|
||||||
|
{
|
||||||
|
return DB::transaction(fn () => TenantMenu::query()->updateOrCreate(
|
||||||
|
[
|
||||||
|
'tenant_code' => $tenant->codigo,
|
||||||
|
'menu_code' => $menu->code,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'static_content' => $menu->content_type === Menu::CONTENT_TYPE_STATIC
|
||||||
|
? $staticContent
|
||||||
|
: null,
|
||||||
|
],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Domains\Menu\Controllers\MenuController;
|
use App\Domains\Menu\Controllers\MenuController;
|
||||||
|
use App\Domains\Menu\Controllers\TenantMenuController;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
Route::apiResource('menues', MenuController::class);
|
Route::apiResource('menues', MenuController::class);
|
||||||
|
|
||||||
|
Route::post(
|
||||||
|
'{tenant_code}/menues/{menu_code}',
|
||||||
|
[TenantMenuController::class, 'store']
|
||||||
|
);
|
||||||
|
|||||||
130
tests/Feature/Menu/TenantMenuControllerTest.php
Normal file
130
tests/Feature/Menu/TenantMenuControllerTest.php
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Menu;
|
||||||
|
|
||||||
|
use App\Domains\Attachable\Enums\AttachmentType;
|
||||||
|
use App\Domains\Attachable\Models\Attachment;
|
||||||
|
use App\Domains\Menu\Models\Menu;
|
||||||
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class TenantMenuControllerTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function test_it_validates_static_content_with_the_menu_schema(): void
|
||||||
|
{
|
||||||
|
$tenant = $this->createTenant();
|
||||||
|
$menu = Menu::query()->create([
|
||||||
|
'code' => 'about',
|
||||||
|
'content_type' => Menu::CONTENT_TYPE_STATIC,
|
||||||
|
'static_content_schema' => [
|
||||||
|
'title' => 'required|string|max:20',
|
||||||
|
'sections' => 'required|array|min:1',
|
||||||
|
'sections.*.body' => 'required|string',
|
||||||
|
],
|
||||||
|
'route' => '/about',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->postJson("/api/{$tenant->codigo}/menues/{$menu->code}", [
|
||||||
|
'static_content' => [
|
||||||
|
'title' => 123,
|
||||||
|
'sections' => [],
|
||||||
|
],
|
||||||
|
])
|
||||||
|
->assertUnprocessable()
|
||||||
|
->assertJsonValidationErrors([
|
||||||
|
'static_content.title',
|
||||||
|
'static_content.sections',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertDatabaseMissing('tenants_menues', [
|
||||||
|
'tenant_code' => $tenant->codigo,
|
||||||
|
'menu_code' => $menu->code,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_it_configures_a_static_menu_when_content_matches_the_schema(): void
|
||||||
|
{
|
||||||
|
$tenant = $this->createTenant();
|
||||||
|
$menu = Menu::query()->create([
|
||||||
|
'code' => 'about',
|
||||||
|
'content_type' => Menu::CONTENT_TYPE_STATIC,
|
||||||
|
'static_content_schema' => [
|
||||||
|
'title' => 'required|string|max:20',
|
||||||
|
'sections' => 'required|array|min:1',
|
||||||
|
'sections.*.body' => 'required|string',
|
||||||
|
],
|
||||||
|
'route' => '/about',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$content = [
|
||||||
|
'title' => 'Nosotros',
|
||||||
|
'sections' => [
|
||||||
|
['body' => 'Nuestra historia'],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->postJson("/api/{$tenant->codigo}/menues/{$menu->code}", [
|
||||||
|
'static_content' => $content,
|
||||||
|
])
|
||||||
|
->assertOk()
|
||||||
|
->assertJsonPath('static_content.title', 'Nosotros')
|
||||||
|
->assertJsonPath('static_content.sections.0.body', 'Nuestra historia');
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
$content,
|
||||||
|
$tenant->menues()->where('menues.code', $menu->code)->firstOrFail()->pivot->static_content
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_dynamic_menus_do_not_require_static_content(): void
|
||||||
|
{
|
||||||
|
$tenant = $this->createTenant();
|
||||||
|
$menu = Menu::query()->create([
|
||||||
|
'code' => 'catalog',
|
||||||
|
'route' => '/catalog',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->postJson("/api/{$tenant->codigo}/menues/{$menu->code}")
|
||||||
|
->assertOk()
|
||||||
|
->assertJsonPath('static_content', null);
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('tenants_menues', [
|
||||||
|
'tenant_code' => $tenant->codigo,
|
||||||
|
'menu_code' => $menu->code,
|
||||||
|
'static_content' => null,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createTenant(): Tenant
|
||||||
|
{
|
||||||
|
$headerLogo = $this->createAttachment('header.png');
|
||||||
|
$footerLogo = $this->createAttachment('footer.png');
|
||||||
|
|
||||||
|
return Tenant::query()->create([
|
||||||
|
'codigo' => 'acme',
|
||||||
|
'nombre' => 'Acme',
|
||||||
|
'dominio' => 'acme.com',
|
||||||
|
'primary_color' => '#111111',
|
||||||
|
'secondary_color' => '#222222',
|
||||||
|
'danger_color' => '#333333',
|
||||||
|
'success_color' => '#444444',
|
||||||
|
'header_bg_color' => '#ffffff',
|
||||||
|
'footer_bg_color' => '#ffffff',
|
||||||
|
'header_logo_id' => $headerLogo->id,
|
||||||
|
'footer_logo_id' => $footerLogo->id,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createAttachment(string $filename): Attachment
|
||||||
|
{
|
||||||
|
return Attachment::query()->create([
|
||||||
|
'path' => "test/{$filename}",
|
||||||
|
'filename' => $filename,
|
||||||
|
'type' => AttachmentType::Image,
|
||||||
|
'mime_type' => 'image/png',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user